当前位置:网站首页>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: "登录成功",
});
}边栏推荐
猜你喜欢

NLP skill tree learning route - (I) route overview

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

MFC界面库BCGControlBar v33.0 - 桌面警报窗口、网格控件升级等
![翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]](/img/a8/6472e2051a295f5e42a88d64199517.png)
翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]

RP原型资源分享-购物类App

Evolution of screen display technology

Lumiprobe nucleic acid quantitative qudye dsDNA br detection kit

毕业设计

【数字IC应届生职业规划】Chap.1 IC行业产业链概述及代表企业大厂汇总

在线教育项目用户登录和注册
随机推荐
《大厂面试》之JVM篇21问与答
北京大学ACM Problems 1006:Biorhythms
北京大学ACM Problems 1000:A+B Problem
杰理之触摸按键识别流程【篇】
No "history of blood and tears" in home office | community essay solicitation
北京大学ACM Problems 1005:I Think I Need a Houseboat
北京大学ACM Problems 1002:487-3279
第81场双周赛
左值引用和右值引用
What are database OLAP and OLTP? Same and different? Applicable scenarios
Document contains & conditional competition
jfinal中如何使用过滤器监控Druid监听SQL执行?
1045 error occurred in MySQL login. Modification method [easy to understand]
PHP obtains opcode and C source code
Label Contrastive Coding based Graph Neural Network for Graph Classification
Lumiprobe cell biology - dia, instructions for lipophilic tracer
19.04 分配器
Huffman tree (I) basic concept and C language implementation
Jerry's touch key recognition process [chapter]
杰理之用测试盒配对软件修改注意点【篇】