本时支付宝小程序
This commit is contained in:
71
api/api.js
Executable file
71
api/api.js
Executable file
@@ -0,0 +1,71 @@
|
||||
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(err)
|
||||
}
|
||||
},
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user