Files
bsmall/api/api.js
2021-03-09 17:19:12 +08:00

71 lines
2.2 KiB
JavaScript
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let apiUrl = "https://www.ysd-bs.com/api/",
timeout = 30000
let request = (obj) => {
return new Promise((resolve, reject) => {
my.request({
url : apiUrl + obj.url || '',
headers : obj.header || { 'content-type': 'application/json' },
method : obj.method || 'GET',
data : obj.data || {},
timeout : timeout,
success : (res) => {
if (res.data.status_code == '200') {
resolve(res.data)
} else if (res.data.status_code == '401') {
my.showToast({
type : "fail",
content : "接口请求失败未登录err" + res.data.message
});
}else {
reject(res);
}
},
fail : (err) => {
if(err.status == 401){
// 清理缓存
my.clearStorageSync()
// 清理全局数据
getApp().globalData.userInfo = {}
getApp().globalData.isUser = false
getApp().globalData.token = ""
// 提示信息
my.alert({
title : '提示',
content : '登录状态已过期,请重新登录',
success : res => {
my.redirectTo({
url: '../login/login'
});
}
});
}else{
reject(err)
}
}
});
})
}
let userinfo = (obj) => {
return request({
url : "users/info",
header : {
"Authorization": obj
},
method : "POST"
}).then(res=>{
getApp().globalData.userInfo = res.data
return res.data
}).catch(err=>{
my.showToast({
type : "fail",
content: "未登录,获取信息失败"
})
})
}
export default {
request,
userinfo
}