import { HTTP_REQUEST_URL_THREE, HEADER, TOKENNAME, } from '@/config/app'; import { Toast } from '../libs/uniApi'; // import { checkLogin } from '../libs/login'; import store from '../store'; function toLogin() { uni.showToast({ title: '请先登录', icon: 'none', duration: 1000 }); } function baseRequestTwo(url, method, data, { noAuth = false, noVerify = false, onReLogin = false }) { let Url = HTTP_REQUEST_URL_THREE, header = HEADER; if (!noAuth) { // 已经未登录了,禁止请求 if (!store.state.config.request) return Promise.reject({ msg: '未登录' }); //登录过期自动登录 if (!store.state.app.token) { toLogin(); store.commit("SET_REQUEST", false); return Promise.reject({ msg: '未登录' }); } } // if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token; if (store.state.app.token) header[TOKENNAME] = store.state.app.token; // header[TOKENNAME] = 'Bearer sdjflidshjgfkbdasgjmasbgvhauuiavhkesvndkaesbvkjsdbv'; return new Promise((reslove, reject) => { // uni.showLoading({ // title: '加载中' // }) uni.request({ // url: Url + '/api/v1' + url, url: Url + '/api' + url, method: method || 'GET', header: { ...header }, data: method != 'GET' ? data || {} : {}, params: method == 'GET' ? data : {}, success: (res) => { if (noVerify) reslove(res.data); else if (res.data.code == -1) { if(onReLogin) { store.commit('LOGOUT'); return reject(); } // 如果登录超时,自动重新登录并且继续发送请求 store.dispatch("RE_LOGIN", { url: url, method: method, data: data, opt: { noAuth, noVerify } }).then((e)=>{ reslove(e); }).catch((err)=>{ reject(res.data); }) // store.commit("SET_REQUEST", false); } else if (res.data.code == 1) { if (res.data.msg != '无登录信息') { uni.showToast({ title: res.data.msg || '请检查网络', icon: 'none', }) } reject(res.data); } else if (res.data.code == 0) { store.commit("SET_REQUEST"); reslove(res.data); } else if (res.data.code == 200) { store.commit("SET_REQUEST"); reslove(res.data.data); } else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) { toLogin(); reject(res.data); } else if (res.data.code == 501) { reject(res.data); } else { uni.showToast({ title: res.data.msg || '请检查网络', icon: 'none' }) reject(res.data.msg || '请检查网络'); } }, fail: (message) => { // uni.hideLoading() uni.showToast({ title: '网络错误', icon: 'none' }) reject('请求失败'); } }) }); } const oahttp = {}; ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => { oahttp[method] = (api, data, opt) => baseRequestTwo(api, method, data, opt || {}) }); export default oahttp;