123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- // http封装库
- import axios from 'axios';
- import { getEnvConfig, removeStorage, storageKey, getMid, getUserInfo, appVersionCode, appType } from '../utils/help';
- // 测试数据(需手动开启关闭)
- // import '../mockjs/index';
- // axios config
- const { host } = getEnvConfig();
- const instance = axios.create({
- baseURL: host,
- timeout: 240000,
- headers: {
- 'content-type': 'application/json',
- Accept: 'application/json',
- },
- });
- // 响应拦截器
- instance.interceptors.response.use(
- (res) => {
- if (res.data.code === -107) {
- // token失效
- removeStorage(storageKey.userInfo);
- location.reload();
- } else {
- return res.data;
- }
- },
- function (err) {
- return Promise.reject(err);
- }
- );
- export const postRequest = (url, params = {}, config = null) => {
- const myConfig = {};
- const userInfo = getUserInfo();
- if (config) {
- Object.assign(myConfig, config);
- }
- params = Object.assign(
- {
- baseInfo: {
- mid: getMid(),
- machineCode: getMid(),
- loginUid: (userInfo && userInfo.uid) || '',
- token: (userInfo && userInfo.accessToken) || '',
- appType,
- appVersionCode,
- },
- },
- params
- );
- return instance
- .post(url, params, myConfig)
- .then((res) => {
- return res;
- })
- .catch((err) => {
- return err;
- });
- };
- export const getRequest = (url, params = {}, config = null) => {
- const myConfig = Object.assign({}, { params: Object.assign({}, params) });
- if (config) {
- Object.assign(myConfig, config);
- }
- return instance
- .get(url, myConfig)
- .then((res) => {
- return res;
- })
- .catch((err) => {
- return err;
- });
- };
|