import store from '@/store' import { getToken } from '@/utils/auth' import {server_url} from '@/utils/config.js' import Vue from 'vue' import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载,也可按需进行加载 let timeout = 30000 function service(options = {}) { options.timeout = options.timeout || timeout, options.url = `${server_url}${options.url}`; options.data = options.params || options.data; options.dataType = 'json'; options.header = { 'Authorization': 'Bearer ' + getToken() }; let method = options.method.toUpperCase() if(method == 'PUT') { options.header['content-type'] = 'application/x-www-form-urlencoded' } return new Promise((resolved, rejected) => { options.success = (res) => { // console.log(res,'res') let code = Number(res.data.code); // Do something with response data switch (code) { case 0: resolved(res.data); break; case 401: if(Vue.prototype.hasHostPlat(["dingH5"]) && Vue.prototype.hasPlat(["enterprises"]) && dd.env.platform != "notInDingTalk") { let corpId = uni.getStorageSync('corpId') if(corpId) { // console.log('test111') store.dispatch('LogOut').then(res => { getApp().handleDingLogin({corpId}) }) } } else if(Vue.prototype.hasHostPlat(["dingH5Ins"]) && Vue.prototype.hasPlat(["enterprises"]) && dd.env.platform != "notInDingTalk") { let corpId = uni.getStorageSync('corpId') if(corpId) { // console.log('test111') store.dispatch('LogOut').then(res => { getApp().handleDingInsLogin({corpId}) }) } } else { uni.showModal({ title: '提示', content: '登录状态已过期,您可以继续留在该页面,或者重新登录?', cancelText: '取消', confirmText: '确定', success: function(res) { if (res.confirm) { store.dispatch('LogOut').then(res => { uni.reLaunch({ url: '/pages/login/login' }) }) } } }) } rejected(res.data.msg); break; default: uni.showToast({ icon: 'none', title: res.data.msg }) rejected(res.data.msg); break; } } options.fail = (error) => { //Do something with request error let { errMsg } = error console.log(error,'errorerrorerror') if (errMsg === 'Network Error') { errMsg = '后端接口连接异常' } else if (errMsg.includes('timeout')) { errMsg = '系统接口请求超时' } else if (errMsg.includes('Request failed with status code')) { errMsg = '系统接口' + errMsg.substr(errMsg.length - 3) + '异常' } uni.showToast({ icon: 'none', title: errMsg, }) rejected(error) } uni.request(options); }); } export default service;