import config from "@/config"; import store from "../store"; import chat from '@/uitls/chat.js' const logout = () => { chat.disConnect(); uni.removeStorageSync('token'); uni.removeStorageSync('chatToken'); uni.removeStorageSync('vuex_state'); store.dispatch('app/changeOrganization', {}); store.dispatch('app/changeUser', {}); uni.removeTabBarBadge({ index: 3 }) } /* 消息提示 */ const tip = (msg) => { let types = 'warning'; if (msg == 'RET_INVALID_PASSWORD') msg = '账号密码有误' if (msg == 'RET_INVALID_CODE') msg = '短信验证码有误' if (msg == 'RET_AUTH_FAILED') { logout(); let pageList = getCurrentPages(); if (pageList[pageList.length - 1].route !== 'pages/login/login') { setTimeout(() => { uni.navigateTo({ url: '/pages/login/login' }); }, 400) } msg = '登录已过期,请重新登录' } uni.showToast({ title: msg, icon: 'none' }) } const errorCallBack = (resolve) => { // 请求失败处理 tip('出错了,请联系系统管理员~'); resolve({ state: false, data: null, msg: 'error' }); uni.hideLoading(); } export function request(options) { return new Promise((resolve, reject) => { let header = options.header || { 'Content-Type': 'application/json' } if (uni.getStorageSync('token')) header['token'] = uni.getStorageSync('token'); uni.request({ url: config.baseUrl + options.url, // 服务器url method: options.method || 'GET', // 请求方法,默认为GET data: options.data || {}, // 请求参数 header: header, // 设置请求的 header success: (res) => { // 请求成功 if (res.statusCode === 200) { let data = res.data; if (data.code != '200') { tip(data.message || data.msg); uni.hideLoading(); } resolve({ state: data.code == '200', data: data.data, msg: data.code == '200' ? 'success' : 'error' }); } else { errorCallBack(resolve); } }, fail: () => errorCallBack(resolve) }); }); };