微信小程序请求Promise方法封装

// stime,ctime
// app.js 
// 登录之后存储的信息,仅供参考
this.setStore('stime', user.data.stime)
this.setStore('ctime', new Date().getTime())

// Promise封装
const wxRequest = async (url, params = {}) => {
    let ctime = wx.getStorageSync('ctime')
    let stime = wx.getStorageSync('stime')
    let ntime = new Date().getTime()
    let time = stime + ntime - ctime
    let moredata = parseInt((time + parseInt(time / 128) + 128) / 2) % 1024 + 20
    let header = {
        'Content-Type': 'application/json',
        'miniType': 0,
        'client_version': ENV.version
    }
    if (params.method == 'POST') {
        header = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'miniType': 0
        }
    }
    if (params.headers) {
        header = Object.assign(header, params.headers)
    }
    // 所有的请求,header默认携带token
    Object.assign(header, {
        token: wx.getStorageSync('token') || '',
        time: time,
        moredata: moredata
    })
    let data = params.data || {}
    let method = params.method || 'GET'
    // hideLoading可以控制是否显示加载状态

    if (!params.hideLoading) {
        wx.showLoading({
            title: '加载中...',
        })
    }
    // 判断显示异常toast
    var hideToast = params.hideToast
    let res = await new Promise((resolve, reject) => {
        wx.request({
            url: url,
            method: method,
            data: data,
            header: header,
            success: (res) => {
                // resolve(res.data)
                if (res && res.statusCode == 200) {
                    if (res.data.flag == 0 || res.data.flag == 1) {
                        resolve(res.data)
                    } else {
                        if (!hideToast) {
                            setTimeout(() => {
                                wx.showToast({
                                    title: res.data.msg,
                                    icon: "none",
                                    duration: 2000
                                })
                            }, 0);
                        }
                        reject(res);
                    }
                } else {
                    wx.showLoading({
                        title: '请求出错'
                    })
                    reject(res)
                }
            },
            fail: (err) => {
                wx.getNetworkType({
                    success: function (res) {
                        if (res.networkType == 'none') { // 判断无网络环境
                            wx.showToast({
                                title: '网络连接异常',
                                icon: 'none',
                            })
                            wx.navigateTo({
                                url: '/pages/404/404'
                            })
                        }
                    }
                })
                reject(err)
            },
            complete: (e) => {
                if (!params.hideLoading) {
                    wx.hideLoading()
                }
            }
        })
    })
    return res
}

export {
    wxRequest
}

promise本身为宏任务,直接同步执行。.then() .catch()为微任务

await:await后面的语句会立即执行,然后async方法本次事件循环直接结束,下面的代码直接被扔到下一次事件循环的宏任务中

定时器:js运行读取到定时器时,会将定时器放入执行队列。当js运行栈执行完事件循环中所有的宏任务和微任务时,才将定时器放入执行栈开始执行,无论事件循环几次 定时器永远都是最后执行。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。