武田基因病B开发环境
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.6 KiB

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