interface IGlobalParams { gUrl: string version: string } export const request = function ( { gUrl, version }: IGlobalParams, { url, method, data, header, showMsg = true, loading = false, loadingText = '加载中...', isJSON = false, ...options }: IAgaxParams, ): Promise { return new Promise((resolve, reject) => { if (loading) { wx.showLoading({ title: loadingText, mask: true, }) } wx.request({ header: { loginState: getApp().globalData.loginState, ...header, }, url: gUrl + url, method, data: { loginState: getApp().globalData.loginState, ...(data as object), }, ...options, success(res: any) { const { code, data } = res.data if (isJSON) { resolve(res.data) } else if (code === 0) { resolve(data) } else if (showMsg) { const msg = errPicker(res.data) if (loading) { setTimeout(() => { wx.showToast({ title: msg, icon: 'none', }) }, 30) } else { wx.showToast({ title: msg, icon: 'none', }) reject(res) } } else { reject(res) } }, fail(err) { reject(err) }, complete() { if (loading) { wx.hideLoading() } }, }) }) } function errPicker(err) { if (typeof err === 'string') { return err } return err.data || err.msg || err.errMsg || (err.detail && err.detail.errMsg) || '未知错误' }