123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- import axios from 'axios';
- import qs from 'qs';
- import { Message } from 'element-ui';
- import store from '@/store';
- import { getToken, setToken } from '@/utils/auth';
- axios.defaults.baseURL = process.env.VUE_APP_BASE_API;
- axios.defaults.timeout = process.env.VUE_APP_API_TIMEOUT;
- const service = axios.create({
- baseURL: process.env.VUE_APP_BASE_API,
-
- timeout: 5000
- });
- axios.interceptors.request.use(
- config => {
-
- if (store.getters.token) {
-
-
-
- config.headers['token'] = getToken();
- }
- return config;
- },
- error => {
-
- console.log(error);
- return Promise.reject(error);
- }
- );
- axios.interceptors.response.use(
-
- response => {
- const res = response.data;
- if (res.code === 401) {
- setToken();
-
- location.replace(`https://youxiangle.cn/yxl-admin/#/login`);
- Message({
- message: 'Token失效,请重新登陆',
- type: 'error',
- duration: 5 * 1000
- });
- return Promise.reject(new Error(''));
- }
- return res;
- },
- error => {
- console.log('错误 response', error);
- return Promise.reject(error);
- }
- );
- const pretreatConfig = _config => {
- if (_config && 'dataType' in _config) {
- if (_config.dataType === 'json') {
- Object.assign(_config, {
- headers: {
- 'Content-Type': 'application/json;charset=UTF-8'
- }
- });
- } else if (_config.dataType === 'formdata') {
- Object.assign(_config, {
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- }
- });
- }
- }
- if (
- process.env.NODE_ENV !== 'production' &&
- _config &&
- 'mock' in _config &&
- _config.mock
- ) {
- Object.assign(_config, {
- baseURL: process.env.VUE_APP_BASE_API_MOCK
- });
- }
- return _config;
- };
- const formatResponse = fetchResult =>
- fetchResult
- .then(response => formatStatus(response))
- .then(res => formatCode(res), handleNetworkError);
- const formatStatus = response => {
-
- if (response && response.code === 200) {
- response.success = true;
- return response;
-
- }
-
-
- let txt = '网络异常';
- if (!response) {
- txt = '通信错误';
- } else if (response.code === 500) {
- txt = response.msg || '服务器错误';
-
-
- }
- Message({
- message: txt || 'Error',
- type: 'error',
- duration: 5 * 1000
- });
-
- return {
- code: -404,
- msg: txt,
- request: response.request || {}
- };
- };
- const formatCode = ({ code, data, msg }) => {
-
- if (code === -404) {
- return {
- code: code,
- success: false,
- msg,
- data: {}
- };
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return {
- success: code === 200,
- msg,
- code,
- data
- };
- };
- const handleNetworkError = error => {
- if (error) {
- if (error.response && error.response.status === 500) {
- return {
- code: 500,
- success: false,
- msg: '通信错误',
- data: {}
- };
- } else if (error.response && error.response.status === 401) {
- store.dispatch('user/resetToken');
- setToken();
-
- }
- if (error.message.includes('timeout')) {
- return {
- code: 1,
- success: false,
- msg: '通信超时',
- data: {}
- };
-
-
-
-
-
-
-
- }
- }
- return {
- code: -1,
- success: false,
- msg: '',
- data: {}
- };
- };
- const get = (url, data = {}, config) => {
- config = pretreatConfig(config);
- return formatResponse(
- axios.get(url, {
- ...config,
- params: data,
- paramsSerializer(params) {
- return qs.stringify(params, {
- arrayFormat: 'brackets'
- });
- }
- })
- );
- };
- const del = (url, data = {}, config, type) => {
- config = pretreatConfig(config);
- return formatResponse(
- axios.delete(url, {
- ...config,
- params: data,
- paramsSerializer(params) {
- return qs.stringify(params, {
- arrayFormat: 'brackets'
- });
- }
- })
- );
- };
- const delData = (url, data = {}, params, config) => {
- config = pretreatConfig(config);
- return formatResponse(
- axios.delete(url, {
- ...config,
- data: data,
- params: params,
- paramsSerializer(params) {
- return qs.stringify(params, {
- arrayFormat: 'brackets'
- });
- }
- })
- );
- };
- const post = (url, data = {}, config) => {
- config = pretreatConfig(config);
- config = Object.assign(
- {
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8'
- }
- },
- config
- );
- return formatResponse(
- axios.post(
- url,
- {
- data,
- seq: config.seq,
- token: config.token,
- limit: data.limit,
- start: data.start
- },
- config
- )
- );
- };
- const postData = (url, data = {}, config) => {
- config = pretreatConfig(config);
- config = Object.assign(
- {
- headers: {
- 'Content-Type': 'multipart/form-data; charset=UTF-8'
- }
- },
- config
- );
- return formatResponse(axios.post(url, data, config));
- };
- const put = (url, data = {}, config) => {
- config = pretreatConfig(config);
- config = Object.assign(
- {
- headers: {
- 'Content-Type': 'application/json; charset=UTF-8'
- }
- },
- config
- );
- return formatResponse(
- axios.put(url, { data, seq: config.seq, token: config.token }, config)
- );
- };
- export default {
- get,
- post,
- postData,
- del,
- put,
- delData
- // download
- };
|