shuziren/job-seeker/miniprogram-2/utils/service.js

49 lines
1.7 KiB
JavaScript
Raw 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 ajaxTimes = 0; // 同时发送异步代码的次数
export const myhttp = (parmas) => {
// 当有地方调用请求方法的时候,就增加全局变量,用于判断有几个请求了
ajaxTimes++;
wx.showLoading({ // 显示加载中loading效果
title: "加载中",
mask: true //开启蒙版遮罩
});
// 返回一个promise对象
return new Promise((resolve, reject) => {
wx.request({
url: parmas.url,
method: parmas.method,
data: parmas.data,
header: parmas.method === 'POST' ?{
'content-type': 'application/x-www-form-urlencoded',
"authorization": wx.getStorageSync('token') || ""
} : {
'content-type': 'application/json',
"authorization": wx.getStorageSync('token') || ""
},
success: (res)=> {
resolve(res.data)
},
fail: (err) => {
reject(err)
},
// 不管请求成功还是失败,都会触发
complete: () => {
/**
* !loading效果同时被多个请求触发是可以显示一个的但是关闭loading一旦被第一个请求完成后关闭后面的请求触发的loading效果就没了
* !所以需要通过全局设置一个变量来监听同时触发了几个请求当最后一个请求完成后再关闭loading
* ?每次结束请求后就减少全局变量当为0时就表示这是最后一个请求了
*/
ajaxTimes--;
// 此时就可以关闭loading效果了
if (ajaxTimes === 0) {
// 关闭正在等待loading效果
wx.hideLoading();
}
}
})
})
}