12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import config from "@/config";
- import store from "../store";
- const logout = () => {
- uni.removeStorageSync('token');
- uni.removeStorageSync('chatToken');
- uni.removeStorageSync('vuex_state');
- store.dispatch('app/changeOrganization', {});
- store.dispatch('app/changeProject', {});
- store.dispatch('app/changeUser', {});
- store.dispatch('app/changeIdentity', {});
- uni.removeTabBarBadge({
- index: 1
- })
- uni.navigateTo({
- url: '/pages/login/login'
- })
- }
- 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');
- if (options.url === '/im/message/save') header['token'] = uni.getStorageSync('systemChatToken');
- 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) {
- resolve(res.data);
- if (res.data.code != 200) {
- if (res.data.code == 20005) {
- if (!uni.getStorageSync('token')) return;
- uni.showToast({
- title: '登录过期,请重新登录',
- icon: 'none'
- })
- logout();
- return;
- }
- uni.showToast({
- title: res.data.message,
- icon: 'none'
- })
- }
- } else {
- // 可以根据项目要求修改错误处理
- reject(res.data);
- uni.hideLoading();
- }
- },
- fail: (err) => {
- // 请求失败处理
- uni.showToast({
- title: '出错了,请联系管理员~',
- icon: 'none'
- })
- reject(err);
- uni.hideLoading();
- },
- });
- });
- };
|