// +---------------------------------------------------------------------- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ] // +---------------------------------------------------------------------- // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved. // +---------------------------------------------------------------------- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权 // +---------------------------------------------------------------------- // | Author: CRMEB Team // +---------------------------------------------------------------------- import { HTTP_REQUEST_URL, HEADER, TOKENNAME } from '@/config/app'; import { toLogin, checkLogin } from '../libs/login'; import store from '../store'; import i18n from './lang.js'; /** * 发送请求 */ function baseRequest(url, method, data, { noAuth = false, noVerify = false }) { let Url = HTTP_REQUEST_URL, header = HEADER; if (!noAuth) { //登录过期自动登录 if (!store.state.app.token && !checkLogin()) { toLogin(); return Promise.reject({ msg: i18n.t(`未登录`) }); } } if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token; return new Promise((reslove, reject) => { if (uni.getStorageSync('locale')) { header['Cb-lang'] = uni.getStorageSync('locale') } uni.request({ url: Url + '/api/' + url, method: method || 'GET', header: header, data: data || {}, success: (res) => { if (noVerify) reslove(res.data, res); else if (res.data.status == 200) reslove(res.data, res); else if ([110002, 110003, 110004].indexOf(res.data.status) !== -1) { toLogin(); reject(res.data); } else if (res.data.status == 100103) { uni.showModal({ title: i18n.t(`提示`), content: res.data.msg, showCancel: false, confirmText: i18n.t(`我知道了`) }); } else // reject(res.data || i18n.t(`系统错误`)); reject(res || i18n.t(`系统错误`)); }, fail: (msg) => { let data = { mag: i18n.t(`请求失败`), status: 1 //1没网 } // #ifdef APP-PLUS reject(data); // #endif // #ifndef APP-PLUS reject(i18n.t(`请求失败`)); // #endif } }) }); } const request = {}; ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => { request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {}) }); export default request;