|
|
@@ -28,39 +28,73 @@ function showErrToast(e) {
|
|
|
|
|
|
function getPromise(url, data, method) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- wx.request({
|
|
|
- url: `${baseUrl}${url}`,
|
|
|
- header: getHeader(),
|
|
|
- method: method,
|
|
|
- data: data,
|
|
|
- success: function (res) {
|
|
|
- if (res.data.code === 200) {
|
|
|
- resolve(res.data.data);
|
|
|
- } else {
|
|
|
- if (res.statusCode === 401) {
|
|
|
- reject(401);
|
|
|
+ const accessToken = wx.getStorageSync("accessToken");
|
|
|
+
|
|
|
+ if (!accessToken) {
|
|
|
+ // Cache the current API call
|
|
|
+ const cachedCall = () => {
|
|
|
+ wx.request({
|
|
|
+ url: `${baseUrl}${url}`,
|
|
|
+ header: getHeader(),
|
|
|
+ method: method,
|
|
|
+ data: data,
|
|
|
+ success: function (res) {
|
|
|
+ if (res.data.code === 200) {
|
|
|
+ resolve(res.data.data);
|
|
|
+ } else {
|
|
|
+ if (res.statusCode === 401) {
|
|
|
+ reject(401);
|
|
|
+ } else {
|
|
|
+ reject(res.data.message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: function (err) {
|
|
|
+ reject(err);
|
|
|
+ },
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ // Call App.getMUser to fetch accessToken
|
|
|
+ App.getMUser()
|
|
|
+ .then(() => {
|
|
|
+ // Retry the cached API call after getting the token
|
|
|
+ cachedCall();
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ console.error("Failed to fetch accessToken", err);
|
|
|
+ showErrToast("Failed to fetch accessToken");
|
|
|
+ reject(err);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // Proceed with the API call if accessToken exists
|
|
|
+ wx.request({
|
|
|
+ url: `${baseUrl}${url}`,
|
|
|
+ header: getHeader(),
|
|
|
+ method: method,
|
|
|
+ data: data,
|
|
|
+ success: function (res) {
|
|
|
+ if (res.data.code === 200) {
|
|
|
+ resolve(res.data.data);
|
|
|
} else {
|
|
|
- reject(res.data.message);
|
|
|
+ if (res.statusCode === 401) {
|
|
|
+ reject(401);
|
|
|
+ } else {
|
|
|
+ reject(res.data.message);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- },
|
|
|
- fail: function (err) {
|
|
|
- reject(err);
|
|
|
- },
|
|
|
- });
|
|
|
+ },
|
|
|
+ fail: function (err) {
|
|
|
+ reject(err);
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
}).catch((e) => {
|
|
|
- // if (401 === e) {
|
|
|
- // redirect to login page
|
|
|
- // wx.navigateTo({
|
|
|
- // url: "/pages/login/login",
|
|
|
- // });
|
|
|
- // } else {
|
|
|
- console.error(`in promise error, url: ${baseUrl}${url}`);
|
|
|
- console.error(e);
|
|
|
- if (e) {
|
|
|
- showErrToast(e);
|
|
|
- }
|
|
|
- // }
|
|
|
+ console.error(`in promise error, url: ${baseUrl}${url}`);
|
|
|
+ console.error(e);
|
|
|
+ if (e) {
|
|
|
+ showErrToast(e);
|
|
|
+ }
|
|
|
});
|
|
|
}
|
|
|
|