Complete core mini program flows
This commit is contained in:
+18
-2
@@ -38,7 +38,7 @@ async function secure(path, method, data) {
|
||||
function toast(error) {
|
||||
wx.showToast({ title: error.message || "操作失败", icon: "none" });
|
||||
}
|
||||
function upload(path) {
|
||||
function uploadOnce(path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = getApp().globalData.token;
|
||||
wx.uploadFile({
|
||||
@@ -50,7 +50,11 @@ function upload(path) {
|
||||
try {
|
||||
const body = JSON.parse(res.data);
|
||||
if (body.code === 0) resolve(body.data);
|
||||
else reject(new Error(body.message));
|
||||
else {
|
||||
const error = new Error(body.message || "上传失败");
|
||||
error.status = res.statusCode;
|
||||
reject(error);
|
||||
}
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
@@ -59,4 +63,16 @@ function upload(path) {
|
||||
});
|
||||
});
|
||||
}
|
||||
async function upload(path) {
|
||||
await getApp().login();
|
||||
try {
|
||||
return await uploadOnce(path);
|
||||
} catch (error) {
|
||||
if (error.status !== 401) throw error;
|
||||
wx.removeStorageSync("pet_token");
|
||||
getApp().globalData.token = "";
|
||||
await getApp().login();
|
||||
return uploadOnce(path);
|
||||
}
|
||||
}
|
||||
module.exports = { request, secure, toast, upload };
|
||||
|
||||
Reference in New Issue
Block a user