当前位置:网站首页>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.payEncapsulation 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 ",
});
}边栏推荐
- SQL server extracts pure numbers from strings
- Development techniques - import files using easyexcel (simple example)
- 【回溯】全排列 leetcode46
- 京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
- ssh 默认端口不是22时的一些问题
- Encryption and decryption and the application of OpenSSL
- Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
- Ml & DL: introduction to hyperparametric optimization in machine learning and deep learning, evaluation index, over fitting phenomenon, and detailed introduction to commonly used parameter adjustment
- 1-17 express Middleware
- The programmer's girlfriend gave me a fatigue driving test
猜你喜欢

漫谈Clickhouse Join

周少剑,很少见

Excitatory neurotransmitter glutamate and brain health

介绍一款|用于多组学整合和网络可视化分析的在线平台

Introduction and example of template method mode

Deployment and use of Nacos

clickhouse原生監控項,系統錶描述

Modify the name of the launched applet

Neurotransmetteurs excitateurs - glutamate et santé cérébrale

Error filesystemexception: /data/nodes/0/indices/gttxk-hntgkhacm-8n60jw/1/index/ es_ temp_ File: structure needs cleaning
随机推荐
周少剑,很少见
How to use data sets in machine learning?
1-18 create the most basic express server & API module for creating routes
阿婆做的臭豆腐
1-14 express托管静态资源
1-1 basic concepts of database
Akk bacteria - the next generation of beneficial bacteria
全面认识痛风:症状、风险因素、发病机理及管理
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
1-1 数据库的基本概念
吴恩达的机器学习适合入门吗?
Usbcan analyzer's supporting can and canfd comprehensive test software lkmaster software solves engineers' can bus test problems
CA I ah, several times Oh, ah, a sentence IU home Oh
PyTorch量化实践(1)
【MySQL入门】第一话 · 初入“数据库”大陆
Deployment and use of Nacos
Reading notes of Clickhouse principle analysis and Application Practice (2)
The Three Musketeers: One for All!
牛逼|珍藏多年的工具让我实现了带薪摸鱼自由
Document Layout Analysis: A Comprehensive Survey 2019论文学习总结