当前位置:网站首页>uniapp-第三方网络请求
uniapp-第三方网络请求
2022-06-30 20:20:00 【智江鹏】
背景
小程序项目中, 不支持 axios, 而且原生的 wx.request() API功能较为简单, 不支持拦截器等全局定制的功能. 因此,
建议在 uni-app 项目中使用 @escook/request-miniprogram 第三方包发起网络数据请求
安装
npm i @escook/request-miniprogram -S 下载网络请求包main.js
// 导入网络请求的包
import {
$http
} from '@escook/request-miniprogram'
uni.$http = $http
// 请求的根路径
$http.baseUrl = 'https://api-hmugo-web.itheima.net'
// 请求拦截器
$http.beforeRequest = function(options) {
uni.showLoading({
title: '数据加载中...'
})
// 判断请求的是否为有权限的 API 接口
if (options.url.indexOf('/my/') !== -1) {
// 为请求头添加身份认证字段
options.header = {
// 字段的值可以直接从 vuex 中进行获取
Authorization: store.state.m_user.token,
}
}
}
// 响应拦截器
$http.afterRequest = function() {
uni.hideLoading()
}使用
const {
data: res2
} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
order_number: orderNumber
})
// 2.2 预付订单生成失败
if (res2.meta.status !== 200) return uni.$showError('预付订单生成失败!')
// 2.3 得到订单支付相关的必要参数
const payInfo = res2.message.pay封装接口
1.在utils文件夹下新建config.js
var url;
if (process.env.NODE_ENV === 'development') {
console.log('开发环境')
url = 'https://www.uinav.com'
} else {
console.log('生产环境')
url = 'https://www.uinav.com'
}
export const baseUrl = url
2.在utils文件夹下新建request.js
import {
$http
} from '@escook/request-miniprogram'
import {
baseUrl
} from "@/utils/config.js"
const service = $http
// 请求的根路径
$http.baseUrl = baseUrl
// 请求拦截器
$http.beforeRequest = function(options) {
uni.showLoading({
title: '数据加载中...'
})
// 判断请求的是否为有权限的 API 接口
if (options.url.indexOf('/my/') !== -1) {
// 为请求头添加身份认证字段
options.header = {
// 字段的值可以直接从 vuex 中进行获取
Authorization: store.state.m_user.token,
}
}
}
// 响应拦截器
$http.afterRequest = function() {
uni.hideLoading()
}
export default service
3.在apis文件下新建login.js
// 登录
export function login(data) {
return request.post('/login', data)
}
// 获取轮播图
export function getswiperdata(data) {
return request.get('/api/public/v1/home/swiperdata', data)
}
4.在页面中使用接口
import {
login
} from "@/apis/login.js"
---------------------------------------
var data2 = {
username: this.phone,
password: this.password
}
const {
data
} = await login(data2);
console.log(data);
if (data.code !== 200){
uni.showToast({
title: data.msg,
});
} else{
uni.showToast({
title: "登录成功",
});
}边栏推荐
- unittest自动测试多个用例时,logging模块重复打印解决
- B_QuRT_User_Guide(32)
- PHP require/include 区别
- 第299场
- What bank card do you need to open an account online? In addition, is it safe to open an account online now?
- QT qstringlist usage
- Comparison between QT and other GUI Libraries
- mysql登录出现1045错误修改方法[通俗易懂]
- Document contains & conditional competition
- 请指教在线开户需要什么银行卡?另外想问,现在在线开户安全么?
猜你喜欢

为什么vscode用久了电脑速度变慢?

Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could

亚马逊在阿拉伯联合酋长国限制LGBTQ相关的搜索和产品销售

Study on PEGylation of lumiprobe and PEG linker - iodine-peg3-acid

Amazon restricts LGBTQ related search and product sales in the United Arab Emirates

NLP paper lead reading | what about the degradation of text generation model? Simctg tells you the answer

杰理之触摸按键识别流程【篇】

Basic concepts of tree

Huffman tree (I) basic concept and C language implementation

Lumiprobe染料 NHS 酯丨BDP FL NHS 酯研究
随机推荐
mysql登录出现1045错误修改方法[通俗易懂]
二叉查找树(一) - 概念与C语言实现
Web APIs 综合案例-Tab栏切换 丨黑马程序员
哈夫曼樹(一)基本概念與C語言實現
Scene 299
Lumiprobe nucleic acid quantitative qudye dsDNA br detection kit
断点续传和下载原理分析
Description of the latest RTSP address rules for Hikvision camera, NVR, streaming media server, playback and streaming [easy to understand]
Peking University ACM problems 1001:exposition
Study on lumiprobe modified triphosphate biotin-11-utp
Peking University ACM problems 1003:hangover
Comparison between QT and other GUI Libraries
Openfire在使用MySQL数据库后的中文乱码问题解决
好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与
Notes on modification of Jerry's test box pairing software [chapter]
杰理之触摸按键识别流程【篇】
Encoding type of Perl conversion file
Evolution of screen display technology
哈夫曼树(一)基本概念与C语言实现
Lvalue reference and lvalue reference