/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2022-08-02 14:50:38
 * @LastEditors: wenjie 1454560336@qq.com
 * @LastEditTime: 2024-11-01 11:54:14
 * @FilePath: \admin-manage\src\utils\request.js
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
 */
import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken,setToken,getREToken,removeToken } from '@/utils/auth'
import { refreshToken } from '@/api/auth'
// create an axios instance
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // baseURL: "http://nei.pgtgame.com/api/apimock-v2/9066e4ae45cff724b020fe29478b3506", // url = base url + request url
  baseURL: "/api", // url = base url + request url
  
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 30000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent

    // if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['Accesstoken'] = getToken()
      config.headers['code'] = 1
      config.headers['epid'] = localStorage.getItem('epid')
      // config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    // }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    console.log(response);
    const res = response.data
    // 如果code不是 0, 这被判定为一个错误。
    if (res.state !== "Success" && res.status != 0) {
      Message({
        message: (!Array.isArray[res.content]?res.content:false)|| res.msg||res.exception.message|| 'Error',
        type: 'error',
        duration: 5 * 1000
      })

      try {
        // 10001:令牌过期
        if (res.code === 'AuthenticationCredentialsNotFoundException'||res.exception.type === 'AuthenticationCredentialsNotFoundException'||res.exception.type ==='AccessDeniedException') {
          removeToken()
          location.reload()
        }
      } catch (error) {
        return res
        
      }
        return res
      // return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
