123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- import {
- HTTP_REQUEST_URL,
- HEADER,
- TOKENNAME
- } from '@/config/app';
- import {
- toLogin,
- checkLogin
- } from '../libs/login';
- import store from '../store';
- function toLoginMp(){
- uni.showToast({
- title: '请登录',
- icon: 'none',
- duration: 1000
- });
- }
- /**
- * 发送请求
- */
- function baseRequest(url, method, data, {
- noAuth = false,
- noVerify = false
- }) {
- let Url = HTTP_REQUEST_URL,
- header = HEADER;
- if (!noAuth) {
- //登录过期自动登录
- if (!store.state.app.token && !checkLogin()) {
- // #ifndef MP
- toLogin();
- // #endif
- // #ifdef MP
- toLoginMp();
- // #endif
- return Promise.reject({
- msg: '未登录'
- });
- }
- }
- if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
- return new Promise((reslove, reject) => {
- console.log('请求接口',Url + '/api/' + url);
- 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 ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
- // #ifndef MP
- toLogin();
- // #endif
- // #ifdef MP
- toLoginMp();
- // #endif
- reject(res.data);
- } else if (res.data.status == 410010) {
- uni.showModal({
- title: '提示',
- content: res.data.msg,
- showCancel: false,
- confirmText: '我知道了'
- });
- } else if(res.data.status == 410020){
- // reject(res.data.msg);
- uni.showModal({
- title: '提示',
- content: res.data.msg,
- showCancel: false,
- confirmText: '我知道了'
- });
- uni.setStorageSync('authIng', true)
- } else if(res.data.status == 403){
- reject(res.data);
- } else
- reject(res.data.msg || '系统错误');
- },
- fail: (msg) => {
- let data = {
- mag: '请求失败',
- status: 1 //1没网
- }
- // #ifdef APP-PLUS
- reject(data);
- // #endif
- // #ifndef APP-PLUS
- reject('请求失败');
- // #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;
|