// 讀取本地儲(chǔ)存
getUserInfo: function () {
return new Promise((resolve, reject) => {
wx.getStorage({
key: 'user',
success: function (res) {
resolve(res.data)
},
fail: function (res) {
reject('err')
}
})
})
},
//getUrlImg 解決帶域名或不帶域名的圖片地址
getUrlImg: function ($string) {
if (!$string) {
return this.imgUrl() + '/uploadfiles/UserFace/no-image.jpg';
}
if ($string.indexOf('https://') < 0) { // 不是http開頭的
return this.imgUrl() + $string;
}
return $string;
},
imgUrl: function () {
return "這里放置url"
},
// 封裝post請(qǐng)求
post: function (url, data) {
return new Promise((resolve, reject) => {
//網(wǎng)絡(luò)請(qǐng)求
wx.request({
url: url,
data: data,
method: 'POST',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) { //服務(wù)器返回?cái)?shù)據(jù)
console.log(data)
if (res.statusCode == 200) {
if (res.data.code == "NO_LOGIN") {
wx.showToast({
title: res.data.msg,
icon: 'none',
})
wx.removeStorage({
key: 'user',
success: function (res) { }
})
setTimeout(function () {
wx.redirectTo({
url: "/pages/login/login",
})
}, 1000) //延遲時(shí)間 這里是1秒
} else {
resolve(res);
}
} else { //返回錯(cuò)誤提示信息
reject(res.data);
}
},
error: function (e) {
reject('網(wǎng)絡(luò)出錯(cuò)');
}
})
});
},
// 封裝get請(qǐng)求
get: function (url, data) {
return new Promise((resolve, reject) => {
//網(wǎng)絡(luò)請(qǐng)求
wx.request({
url: url,
data: data,
method: 'GET',
header: {
'content-type': 'application/json',
},
success: function (res) { //服務(wù)器返回?cái)?shù)據(jù)
if (res.statusCode == 200) {
if (res.data.code == "NO_LOGIN") {
wx.showToast({
title: res.data.msg,
icon: 'none',
})
wx.removeStorage({
key: 'user',
success: function (res) { }
})
setTimeout(function () {
wx.redirectTo({
url: "/pages/login/login",
})
}, 1000) //延遲時(shí)間 這里是1秒
} else {
resolve(res);
}
} else { //返回錯(cuò)誤提示信息
reject(res.data);
}
},
complete: function () {
reject('網(wǎng)絡(luò)出錯(cuò)');
},
error: function (e) {
reject('網(wǎng)絡(luò)出錯(cuò)');
}
})
});
},