import Taro from '@tarojs/taro'; interface Param { method?: string, url: string, params?: object, contentType?: string, hiddenToast?: boolean, } export default ({ method = 'GET', url = '', params = {}, contentType = '', hiddenToast = false }: Param) => { if (!hiddenToast) Taro.showLoading({ title: '加载中', mask: true }); const targeturl = url; const token = Taro.getStorageSync('stoken'); const head = token ? { stoken: token } : {}; const args: any = { url: targeturl, data: { ...params }, header: { 'Content-Type': contentType || 'application/x-www-form-urlencoded', ...head }, method: method.toUpperCase(), }; const promise = Taro.request(args); promise.then((res) => { if (!hiddenToast) Taro.hideLoading(); const { data } = res; if (!data) return promise; if (Number(data.errno) !== 0) { // 302000 - 登录状态过期;403000 - 手机号不在系统账号体系中 if (Number(data.errno) === 302000 || Number(data.errno) === 403000) { Taro.clearStorage(); } if (!hiddenToast) { Taro.showToast({ title: data.errmsg, icon: 'none', mask: true, duration: 2000, }); } } // 接口正常返回promise处理1 return promise; }); };