1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import store from '@/store'
- import { msg, getAuthToken } from './util'
- const BASE_URL = 'http://127.0.0.1:28080/api/';
- export const request = (options) => {
- return new Promise((resolve, reject) => {
-
- const authToken = getAuthToken();
- uni.request({
- url: BASE_URL + options.url,
- method: options.method || 'GET',
- data: options.data || {},
- header: {
- ...options.header,
- 'Authorization': authToken ? `Bearer ${authToken}` : ''
- }
- }).then(res => {
- res = res[1];
- const statusCode = res.statusCode;
- if (statusCode !== 200) {
- msg('请求失败,请重试');
- return;
- }
-
- const code = res.data.code;
- const message = res.data.msg;
-
- if (code === 401) {
- msg('登录信息已过期,请重新登录');
- store.commit('logout');
-
- return;
- }
-
- if (code === 500) {
- msg('系统异常,请稍后重试');
- reject(new Error(message));
- return;
- }
-
- if (code > 0) {
- msg(message);
-
-
-
- reject({
- 'code': code,
- 'msg': message
- });
- return;
- }
-
- resolve(res.data.data);
- }).catch((err) => {
- reject(err);
- })
- })
- }
|