当前位置:网站首页>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: "登录成功",
});
}边栏推荐
- Lvalue reference and lvalue reference
- 北京大学ACM Problems 1005:I Think I Need a Houseboat
- On inline function
- 【微服务~Nacos】Nacos之配置中心
- Lumiprobe protein quantitation - qudye Protein Quantitation Kit
- Lumiprobe蛋白质定量丨QuDye 蛋白定量试剂盒
- Common questions and answering skills of project manager interview
- Is the project manager a leader? Can you criticize and blame members?
- Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
- Jerry's touch key recognition process [chapter]
猜你喜欢

Big God explains open source buff gain strategy live broadcast

Huffman Tree (1) Basic Concept and C - language Implementation

SQL优化

Study on lumiprobe modified triphosphate biotin-11-utp

NLP skill tree learning route - (I) route overview

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

Lumiprobe dye hydrazide - BDP FL hydrazide solution

Great God detailed open source Buff gain Introduction 丨 Live

maya房子建模

Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
随机推荐
Summary of PHP file upload (garbled code, move failure, permission, display picture)
All the important spark summit features were released here last night (with ultra clear video attached)
Lumiprobe 聚乙二醇化和 PEG 接头丨碘-PEG3-酸研究
Dynamic style binding --style and class
jfinal中如何使用过滤器监控Druid监听SQL执行?
Go 语言标识符、包名规范
C file pointer
杰理之触摸按键识别流程【篇】
【微服务~Nacos】Nacos之配置中心
Qt和其它GUI库的对比
Peking University ACM problems 1001:exposition
B_QuRT_User_Guide(32)
居家办公没有“血泪史”| 社区征文
杰理之关于长按复位【篇】
Halcon knowledge: check the measurement objects [1]
PHP获取Opcode及C源码
北京大学ACM Problems 1006:Biorhythms
杰理之关于长按开机检测抬起问题【篇】
Jerry's determination of detection sensitivity level [chapter]
Jerry's touch key recognition process [chapter]