Uniapp如何封裝網(wǎng)絡(luò)請(qǐng)求方法demo
引言
寫(xiě)文章的故事:給整宕機(jī)了
第一步:定義請(qǐng)求文件
request.js
import encrypt from '@/util/encrypt'
import {
settings
} from '@/settings.js'
const BASE_URL = settings.BASE_URL
const encryptKey = settings.ENCRYPT_KEY
const whiteList = settings.WHITE_LIST
export const request = (options) => {
const token = uni.getStorageSync('shopToken')
const head = {
'Authorization': '',
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
}
if (!options.header || !options.header['Content-Type']) {
options.header = head
}
if (whiteList.indexOf(options.url) === -1) {
if (!token) {
uni.showToast({
icon: "none",
title: "請(qǐng)先登錄"
})
uni.removeStorageSync("shopToken")
return Promise.reject("請(qǐng)先登錄")
} else {
options.header.Authorization = token
}
}
if (options.data) {
options.data['sign'] = encrypt.encryptMd5(options.data, encryptKey)
}
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + options.url,
method: options.method || 'GET',
data: options.data || {},
header: {
'Authorization': options.header.Authorization,
'Content-Type': options.header['Content-Type']
},
success: (res) => {
if (res.data.code !== 200) {
if (res.data.code === 1000 || res.datacode === 1001 || res.datacode ===
1002 || res.data.code === 1010 || res.data.code === 500) {
uni.showModal({
title: '提示',
content: '長(zhǎng)時(shí)間未操作請(qǐng)重新登錄',
showCancel: false,
success: function(d) {
uni.removeStorageSync('shopToken')
uni.navigateTo({
url: '/pages/login/register',
})
}
});
} else if (res.data.msg != 'token不匹配') {
} else if (res.data.msg == 'token不匹配') {
res.data.msg = "登錄狀態(tài)過(guò)期請(qǐng)重新登錄"
}
uni.showToast({ icon: "none", title: res.data.msg })
reject(res.data.msg)
} else {
resolve(res.data)
}
},
fail(err) {
reject(err)
}
})
})
}第二步:訪問(wèn)接口
settings.js
export const settings = {
// #ifdef MP-WEIXIN
BASE_URL: 'http://192.168.101.35', // 訪問(wèn)接口
// #endif
// #ifdef H5
BASE_URL: '', // 訪問(wèn)接口
// #endif
// BASE_URL: 'https://api.helicong.com:80', // 訪問(wèn)接口
ENCRYPT_KEY: 'jdp*#(@KFJ322!@#$jkl(#jdlmkdhc', // 前端加密key
WHITE_LIST: ["/api/lifeMerchant/baseInfo/selectSortByDistance", "/api/middle/sendSms", "/api/lifeMerchant/account/merchantRegisterLogin"] , // 接口過(guò)濾白名單 ,'/alipay/credit','/alipay/pay',"/alipay/tradeRefund","/alipay/unFreeze"
}以上就是Uniapp如何封裝網(wǎng)絡(luò)請(qǐng)求方法demo的詳細(xì)內(nèi)容,更多關(guān)于Uniapp封裝網(wǎng)絡(luò)請(qǐng)求的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
利用js實(shí)現(xiàn)遮罩以及彈出可移動(dòng)登錄窗口
本篇文章是對(duì)使用js實(shí)現(xiàn)遮罩以及彈出可移動(dòng)登錄窗口的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
js實(shí)現(xiàn)貪吃蛇游戲(簡(jiǎn)易版)
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)貪吃蛇游戲簡(jiǎn)易版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
JavaScript實(shí)現(xiàn)復(fù)制內(nèi)容到粘貼板代碼
最近做了一個(gè)前端項(xiàng)目,其中有需求:通過(guò)button直接把input或者textarea里的值復(fù)制到粘貼板里。下面小編給大家分享JavaScript實(shí)現(xiàn)復(fù)制內(nèi)容到粘貼板代碼,需要的朋友參考下2016-03-03
ajax前臺(tái)后臺(tái)跨域請(qǐng)求處理方式
本篇文章通過(guò)前臺(tái)跨域請(qǐng)求處理以及后臺(tái)跨域的數(shù)據(jù)處理方式介紹,詳細(xì)分析了ajax跨域的問(wèn)題,對(duì)此有需要的朋友學(xué)習(xí)下。2018-02-02
js簡(jiǎn)單實(shí)現(xiàn)自動(dòng)生成表格功能示例
這篇文章主要介紹了js簡(jiǎn)單實(shí)現(xiàn)自動(dòng)生成表格功能,結(jié)合實(shí)例形式分析了JavaScript針對(duì)數(shù)組元素遍歷與頁(yè)面元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-06-06
javaScript手機(jī)號(hào)碼校驗(yàn)工具類PhoneUtils詳解
這篇文章主要為大家詳細(xì)介紹了javaScript手機(jī)號(hào)碼校驗(yàn)工具類PhoneUtils,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
JavaScript調(diào)用瀏覽器打印功能實(shí)例分析
這篇文章主要介紹了JavaScript調(diào)用瀏覽器打印功能的方法,實(shí)例分析了針對(duì)各種常用瀏覽器調(diào)用打印功能的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

