当前位置:网站首页>uniapp拦截请求
uniapp拦截请求
2022-06-27 19:15:00 【小四是个处女座】
//把配置项单独处理
export function service(options = {}) {
// 获取租户id
if(!uni.getStorageSync(‘tenantId’)){
uni.request({
url:/api/blade-user/app/getTeanantId?domain=${'http://localhost'},
success: ((res) => {
uni.setStorageSync(‘tenantId’,res.data.data.tenantId);
})
})
}else{
return new Promise((resolved, rejected) => {
var header = ‘application/json’
if (options.headerType == 1) {
header = ‘application/x-www-form-urlencoded’
// options.data = JSON.stringify(options.data)
} else if (options.headerType == 2) {
header = ‘multipart/form-data’
}
var httpHeaders = {
‘content-type’: header,
‘Authorization’: “Basic c3dvcmQ6c3dvcmRfc2VjcmV0”,
}
var userInfo = uni.getStorageSync(‘userInfo’)
console.log(userInfo, ‘userInfo’)
if (userInfo) {
httpHeaders[‘Blade-Auth’] = userInfo.access_token
}
uni.request({
// url: baseUrl + options.url,
url: options.url,
data: options.data,
method: options.method,
header: httpHeaders,
tenantId: uni.getStorageSync(‘tenantId’),
success: ((res) => {
// data.msg
if (res.statusCode == 200) {
uni.hideLoading();
resolved(res);
} else if (res.statusCode == 401) {
uni.showToast({
icon: 'none',
title: '请求未授权',
duration: 1800
});
rejected(res)
setTimeout(function() {
uni.redirectTo({
url: '/pages/login/login'
});
}, 2000)
} else {
uni.showToast({
icon: 'none',
title: res.data.msg,
duration: 1800
});
rejected(res)
}
}),
fail: (res => {
console.log(res, '失败了')
})
})
});
}
}
// 上传图片接口拦截
export function serviceImg(data) {
console.log(“照片”, data)
return new Promise((resolve, reject) => {
uni.uploadFile({
// 微信小程序
// url: baseUrl + data.url,
// web
url: data.url,
filePath: data.tempFilePaths[0],
name: ‘file’,
header: {
‘Tenant-Id’: “000000”,
‘Blade-Auth’: ‘bearer’ + ’ ’ + uni.getStorageSync(‘token’),
‘Authorization’: ‘Basic c2FiZXI6c2FiZXJfc2VjcmV0’
},
success: (res) => {
resolve(res) //如果请求成功,调用resolve返回数据
},
})
})
}
边栏推荐
- Dictionary tree (review)
- MySQL usage notes 1
- Cerebral cortex: predicting children's mathematical skills from task state and resting state brain function connections
- Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
- GFS distributed file system
- 农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
- 爱数课实验 | 第八期-新加坡房价预测模型构建
- Use the storcli tool to configure raid. Just collect this article
- White whoring red team goby & POC, how do you call white whoring?
- Flask----应用案例
猜你喜欢

CEPH distributed storage

VMware vSphere esxi 7.0 installation tutorial

Galaxy Kirin system LAN file sharing tutorial

众昂矿业:新能源或成萤石最大应用领域

100 important knowledge points that SQL must master: filtering data

SQL Server for循环用法

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日

JPA踩坑系列之save方法

How to do a good job of gateway high availability protection in the big promotion scenario

让马化腾失望了!Web3.0,毫无希望
随机推荐
Leetcode 1381. Design a stack that supports incremental operations
Save method of JPA stepping pit series
Runmaide medical opened the offering: without the participation of cornerstone investors, the amount of loss doubled
通过CE修改器修改大型网络游戏
“好声音“连唱10年,星空华文如何唱响港交所?
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
白嫖红队goby&POC,叫你如何白嫖?
爱数课实验 | 第八期-新加坡房价预测模型构建
分享一次自己定位 + 解决问题的经历
Leetcode 821. Minimum distance of characters (simple) - sequel
Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
How to do a good job of gateway high availability protection in the big promotion scenario
一套系统,减轻人流集中地10倍的通行压力
Shell command used in actual work - sed
OpenSSL 编程 二:搭建 CA
Share how I take notes
OpenSSL 编程 一:基本概念
爱数课实验 | 第六期-金融反欺诈案例研究
KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
Safe and efficient, non-contact "hand brushing" identification helps epidemic prevention and control