| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import axios from 'axios'
- import store from '@/store'
- import Cookies from 'cookies'
- import { MessageBox, Message, Loading } from 'element-ui'
- import { isAdmin, langFilter } from '@/utils/http.js'
- const service = axios.create({
- timeout: 5000
- })
- const lang = localStorage.getItem('lang') || 'cn'
- service.interceptors.request.use(
- config => {
- // if (config.isLoading) {
- // startLoading()
- // }
- if (localStorage.getItem('userToken')) {
- config.headers.ApiToken = localStorage.getItem('userToken')
- }
- return config
- },
- error => {
- return Promise.reject(error)
- }
- )
- service.interceptors.response.use(
- response => {
- // if (response.config.isLoading) {
- // endLoading();
- // }
- const res = response.data
- if (res.code === 0) {
- Message({
- message: res.msg || 'Error',
- type: 'error',
- duration: 5 * 1000
- })
- } else if (res.code === 401) {
- localStorage.clear()
- Message({
- message: 'Login information expired, please log in again',
- type: 'error',
- duration: 3 * 1000,
- onClose: () => {
- window.location.href = '/'
- }
- })
- } else if (res.code === 1) {
- if (isAdmin()) {
- return res
- } else {
- // 前台响应数据
- let { data } = res
- res.data = langFilter(data)
- return res
- }
- } else if (res.code === 402) {
- Message({
- message: 'error',
- type: 'error',
- duration: 3 * 1000,
- })
- }
- return res
- },
- error => {
- Message({
- message: 'error',
- type: 'error',
- duration: 3 * 1000
- })
- // endLoading();
- return Promise.reject(error)
- }
- )
- let loading
- function startLoading() {
- loading = Loading.service({
- lock: true,
- text: '加载中',
- background: 'rgba(0, 0, 0, 0.7)'
- })
- }
- function endLoading() {
- loading.close()
- }
- const request = (url, method = 'get', data, isLoading = false) => {
- const config = {}
- if (data) {
- if (method === 'get') {
- config.params = data
- } else {
- config.data = data
- }
- }
- return service({
- url,
- method,
- isLoading,
- ...config
- })
- }
- export default request
|