import {baseUrl} from './config.js' import store from '@/store/index.js' // 请求错误消息提示 const showError=error=>{ let errMsg='' switch(error.code){ case 400: errMsg=error.msg; break; default: errMsg=error.msg; break; } uni.showToast({ title:errMsg, icon:'none', duration:1000 }) } // 请求数 let LOADINGCOUNT = 0; const http=(url,params,method)=>{ // 设置loading if (LOADINGCOUNT === 0) { uni.showLoading({ title:'加载中...' }) } LOADINGCOUNT++; return new Promise((resolve,reject)=>{ uni.request({ url:baseUrl+url, data:params, method:method, header:{ Authorization:store.state.token, }, success(res) { if(res.data.code!==200){ setTimeout(()=>{showError(res.data)},0)//解决 hideloading 冲突问题 } //401 代表token异常,用户需要重新静默授权,获取最新的token //403 用户需要进行绑定操作,需要跳转到输入账号密码绑定页面用户需要进行绑定操作 if(res.data.code===401||res.data.code===403){ uni.reLaunch({ url:"/pages/login/login" }) } resolve(res.data) }, fail(error) { console.log(error); setTimeout(() => { uni.showToast({ title:'网络异常', icon:'none' }) }, 0); }, complete() { // 关闭loading LOADINGCOUNT--; if (LOADINGCOUNT === 0) { uni.hideLoading() } } }) }) } // get 请求 export const httpGet=(url,params)=>{ return http(url,params,'GET') } // post 请求 export const httpPost=(url,params)=>{ return http(url,params,'POST') }