// 接口方法及axios封装
import qs from "qs"
import axios from "axios"
import { URL_ADD } from "./api"
import state from '@/store/state'

//请求成功后端返回状态码
export const REQUEST_SUCCESS = '1'

//用户无权限
export const USER_NO_JURISDICTION = '1006'

// 测试使用用户id
export const USER_ID = '881'

// 数据字典表中的常量
export const ORDER_TYPE = 'order_type'

export const ORDER_TEMP_TYPE = 'order_temp_type'

export const ORDER_STATUS = 'order_status'

export const OREDER_SUB_TYPE = 'order_sub_type'

export const OREDER_STATE = 'order_state'


// 请求超时时间
if (!state.isH5Milieu) {
  axios.defaults.timeout = 2 * 50 * 1000  // 在原来的超时时间上扩大2倍
}

// post请求头
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// 请求拦截器
axios.interceptors.request.use(config => {
  //发送请求之前处理
  config.headers['X-Authorization-Key'] = 'D0K4-66J9-9143-4043-24K4'
  config.headers["X-Authorization-Ip"] = "183.224.8.165"

  //河南环境下 切割token
  let Authorization_Token = sessionStorage.getItem('Authorization-Token')
  let Authorization_fromSystem = sessionStorage.getItem('Authorization-fromSystem')
  config.headers["X-Authorization-Token"] = (process.env.NODE_ENV === 'hebei' ? (Authorization_Token ? Authorization_Token : '') : '')
  config.headers["X-Authorization-fromSystem"] = (process.env.NODE_ENV === 'hebei' ? (Authorization_fromSystem ? Authorization_fromSystem : '') : '')
  return config
}, error => {
  //错误后抛出
  return Promise.reject(error)
})

// 响应拦截器s
axios.interceptors.response.use(response => {
  // if (response.data.code == REQUEST_SUCCESS) {
  //   return response.data.data
  // }
  //有的请求不规范_________________________
  return response
}, error => {
  return Promise.reject(error)
})



/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
 */
export function _get (url, data) {
  return axios.get(URL_ADD + '/' + url, { params: data })
    .then(res => {
      return res.data
    })
    .catch(error => {
      return error
    })
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
 * @param {Boolean} isSerialize [是否序列化]
 */
export function _post (url, data, isSerialize) {
  if (isSerialize) data = qs.stringify(data)
  return axios.post(URL_ADD + '/' + url, data)
    .then(res => {
      return res.data
    })
    .catch(err => {
      return err
    })
}

export function _service ({ method, url, data }) {
  const params = method === 'get' ? { params: data } : data
  return axios[method](url, params)
    .then(res => res.data)
    .catch(error => error)
}
