import { APP_API, ADD_API, APP_HOST } from '@/site.config.js'; import { isDef, storage, showToast, calcHeader, formatReturn } from '@/utils/utils.js'; let whiteListLogin = ['/account/manage/getOwnerAccountBase.do','/sysInfoSend/getStudentAllInfoCount'] //401也不跳转到登录页面 s let whiteListCodeNegative = ['/apply/manage/queryTrainingApplyByOwner.do','/account/manage/getOwnerAccountBase.do','/order/manage/getPrepaidResult.do','/order/manage/getKwzxPrepaidResult.do'] //code==-也不弹出提示 function notLogin() { storage.remove('userInfo'); storage.remove('Authorization'); storage.remove(); try{ let currentRoute = getCurrentPages().pop(); const {options,route} = currentRoute const optionsKeys = Object.keys(options) let params = '' if(optionsKeys.length !=0) { params = optionsKeys.reduce((pre,current)=>{ return pre + current + '=' + options[current] + '&' }, '?').slice(0,-1) } uni.setStorageSync('currentRoute',route + params) console.log(uni.getStorageSync("currentRoute")) console.log('uni.getStorageSync("currentRoute")') }catch(e){ console.log(e) } uni.showToast({ title:"请先登入", icon:"error", mask:true }) let timer = setTimeout(()=>{ // 跳转登录页 uni.navigateTo({ url: '/pages/user/login/login' }) clearTimeout(timer) },1500) } function invalidLogin(url, datas = {}, method = 'GET') { return new Promise((reslove, reject) => { setTimeout(async () => { const [err, data] = await http(url, (datas = {}), (method = 'GET')); if (err) return formatReturn('网络异常,请稍后再试~'); if (data.code === 401) { showToast('登录失效'); storage.remove('userInfo'); storage.remove('Authorization'); uni.redirectTo({ url: '/pages/user/login/login' }); return reject(formatReturn('登录失效~')); } return reslove(formatReturn(null, data)); }, 5000); }); } /** * 对内接口请求 * @param {*} api - api * @param {*} data - 参数/数据 * @param {*} method - 请求的方法,默认GET * @param {*} headers - 请求的header * @param {*} contentType - 传输类型, 默认为0 */ async function http(api, datas = {}, method = 'GET', headers = {}, contentType = 0, timeout = 6000) { const _url = APP_HOST + APP_API + api; const _header = calcHeader(headers, contentType); const [err, res] = await uni.request({ url: _url, data: datas, method: method.toUpperCase(), header: _header, sslVerify: false, timeout: timeout, }); // console.log('_url') // console.log(_url) // console.log('请求结果') // console.log(res) // console.log(err) if (err) return formatReturn('网络异常,请稍后再试~'); const { data, header, statusCode } = res; const Authorization = header.token || header.token || ''; if (isDef(Authorization)) storage.set('Authorization', Authorization); if (statusCode >= 200 && statusCode < 300) { const code = data.code; if (code === 401&&whiteListLogin.indexOf(api) == -1) return notLogin(); if (code === 500) return await invalidLogin(api, datas, method); if (code == -1 && whiteListCodeNegative.indexOf(api) == -1) return showToast(data.msg || data.message); return formatReturn(null, data); } } async function Ajax1(api, datas = {}, method = 'GET', headers = {}, contentType = 1, timeout = 60000) { const datasSrc = getPostUrl(datas) let _url = APP_HOST + APP_API + api if (datasSrc) _url = _url + '?' + datasSrc; const _header = calcHeader(headers, contentType); const [err, res] = await uni.request({ url: _url, params: datas, method: method.toUpperCase(), header: _header, sslVerify: false, timeout: timeout, enableCookie: true }); // console.log('_url') // console.log(_url) // console.log(res) if (err) return formatReturn('网络异常,请稍后再试~'); const { data, header, statusCode } = res; const Authorization = header.token || header.token || ''; if (isDef(Authorization)) storage.set('Authorization', Authorization); if (statusCode >= 200 && statusCode < 300) { const code = data.code; if (code === 401) return notLogin(); if (code === 500) return await invalidLogin(api, datas, method); if (code == -1 && whiteListCodeNegative.indexOf(api) == -1) return showToast(data.msg || data.message); return formatReturn(null, data); } } /** * 对外接口请求 * @param {*} url - 请求路径/参数 * @param {*} method - 请求的方法,默认GET */ async function Ajax1Add(api, datas = {}, method = 'GET', headers = {}, contentType = 1, timeout = 60000) { const _url = ADD_API + api; const _header = calcHeader(headers, contentType); const [err, res] = await uni.request({ url: _url, data: datas, method: method.toUpperCase(), header: { 'Authorization': _header.token }, sslVerify: false, timeout: timeout, }); console.log('_url') console.log(_url) console.log('请求广告列表结果') console.log(res) console.log(_header) if (err) return formatReturn('网络异常,请稍后再试~'); const { data, header, statusCode } = res; const Authorization = header.token || header.token || ''; if (isDef(Authorization)) storage.set('Authorization', Authorization); if (statusCode >= 200 && statusCode < 300) { const code = data.code; if (code === 401&&whiteListLogin.indexOf(api) == -1) return notLogin(); if (code === 500) return await invalidLogin(api, datas, method); if (code == -1 && whiteListCodeNegative.indexOf(api) == -1) return showToast(data.msg || data.message); return formatReturn(null, data); } } function getPostUrl(data) { let str = Object.keys(data).map(key => `${key}=${data[key]}`).join('&') str.split(0, -1) return str } // 导出请求 export { Ajax1Add, Ajax1 }; // 外部接口请求 export default http; // 内部接口请求