当前位置:网站首页>Uniapp third party network request
Uniapp third party network request
2022-06-30 21:56:00 【Zhijiangpeng】
background
In applet project , I won't support it axios, And the original wx.request() API The function is relatively simple , Global customization functions such as interceptors are not supported . therefore ,
It is suggested that uni-app Project use @escook/request-miniprogram Third party packet initiates network data request
install
npm i @escook/request-miniprogram -S Download network request package
main.js
// Import the package requested by the network
import {
$http
} from '@escook/request-miniprogram'
uni.$http = $http
// The root path of the request
$http.baseUrl = 'https://api-hmugo-web.itheima.net'
// Request interceptor
$http.beforeRequest = function(options) {
uni.showLoading({
title: ' Data loading ...'
})
// Judge whether the requested is authorized API Interface
if (options.url.indexOf('/my/') !== -1) {
// Add an authentication field to the request header
options.header = {
// The value of the field can be directly from vuex Intermediate acquisition
Authorization: store.state.m_user.token,
}
}
}
// Response interceptors
$http.afterRequest = function() {
uni.hideLoading()
}
Use
const {
data: res2
} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
order_number: orderNumber
})
// 2.2 Prepaid order generation failed
if (res2.meta.status !== 200) return uni.$showError(' Prepaid order generation failed !')
// 2.3 Get the necessary parameters related to order payment
const payInfo = res2.message.pay
Encapsulation interface
1. stay utils New under folder config.js
var url;
if (process.env.NODE_ENV === 'development') {
console.log(' development environment ')
url = 'https://www.uinav.com'
} else {
console.log(' Production environment ')
url = 'https://www.uinav.com'
}
export const baseUrl = url
2. stay utils New under folder request.js
import {
$http
} from '@escook/request-miniprogram'
import {
baseUrl
} from "@/utils/config.js"
const service = $http
// The root path of the request
$http.baseUrl = baseUrl
// Request interceptor
$http.beforeRequest = function(options) {
uni.showLoading({
title: ' Data loading ...'
})
// Judge whether the requested is authorized API Interface
if (options.url.indexOf('/my/') !== -1) {
// Add an authentication field to the request header
options.header = {
// The value of the field can be directly from vuex Intermediate acquisition
Authorization: store.state.m_user.token,
}
}
}
// Response interceptors
$http.afterRequest = function() {
uni.hideLoading()
}
export default service
3. stay apis New under file login.js
// Sign in
export function login(data) {
return request.post('/login', data)
}
// Get the carousel
export function getswiperdata(data) {
return request.get('/api/public/v1/home/swiperdata', data)
}
4. Using interfaces in pages
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: " Login successful ",
});
}
边栏推荐
- Jupyter notebook/lab switch CONDA environment
- Analysis of doctor Aifen's incident
- ssh 默认端口不是22时的一些问题
- 微服務鏈路風險分析
- Is Wu Enda's machine learning suitable for entry?
- 1-15 nodemon
- 京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
- Sqlserver gets the data of numbers, Chinese and characters in the string
- It is urgent for enterprises to protect API security
- 興奮神經遞質——穀氨酸與大腦健康
猜你喜欢
Reading notes of Clickhouse principle analysis and Application Practice (3)
Error reporting: internal error XFS_ WANT_ CORRUPTED_ GOTO at line 1635 of file fs/xfs/libxfs/xfs_ alloc. c.
Development techniques - import files using easyexcel (simple example)
Go Web 编程入门: 一探优秀测试库 GoConvey
吴恩达的机器学习适合入门吗?
AKK菌——下一代有益菌
USBCAN分析仪的配套CAN和CANFD综合测试软件LKMaster软件解决工程师CAN总线测试难题
Usbcan analyzer's supporting can and canfd comprehensive test software lkmaster software solves engineers' can bus test problems
【MySQL入门】第一话 · 初入“数据库”大陆
Look at the top 10 capabilities of alicloud cipu
随机推荐
Dm8: generate DM AWR Report
1-18 create the most basic express server & API module for creating routes
Gartner focuses on low code development in China how UNIPRO practices "differentiation"
Ten security measures against unauthorized access attacks
牛逼|珍藏多年的工具让我实现了带薪摸鱼自由
程序员女友给我做了一个疲劳驾驶检测
【回溯】全排列 leetcode46
The programmer's girlfriend gave me a fatigue driving test
布隆过滤器
介绍一款|用于多组学整合和网络可视化分析的在线平台
[backtracking] full arrangement leetcode46
Arcmap|assign values to different categories of IDS with the field calculator
Troubleshooting the problem of pytorch geometric torch scatter and torch spark installation errors
jenkins下载插件下载不了,解决办法
Reading notes of Clickhouse principle analysis and Application Practice (3)
Jupyter notebook/lab switch CONDA environment
[untitled] first time to participate in CSDN activities
Qsort function and Simulation Implementation of qsort function
1-2 安装并配置MySQL相关的软件
Zhoushaojian, rare