import axios from '@/libs/base/api.request'
import iView from 'view-design'
import store from '@/store'
import router from '@/router'
import {localRead} from "@/libs/base/util";

export const request = (url, data, method, token, primaryKey) => {
  //拼接业务服务编码
  if (store.state.user.svcCode) {
    url = url.replace('mdm-server', 'mdm-server' + '-' + store.state.user.svcCode.replace('_', '-'))
  }

  // if (data && !data.dealerId && store.state.user.loginType == 1 && url !== 'file/get_qr_code_url'){
  //   data.dealerId = store.state.user.dealerId
  // }
  // if (data && typeof data == 'object' && store.state.user.cpId) {
  //   data.cpId = store.state.user.cpId
  // }
  return new Promise((resolve, reject) => {
    iView.LoadingBar.start()
    let header = {}
    // console.log('store.state.user', store.state.user)
    if (store.state.user.token){
      header.Authorization = "Bearer "+store.state.user.token
    }
    if (token){
      header.Authorization = "Bearer "+token
    }
    // 语言
    header.i18n = localRead('local_lan')
    axios.request({
      url: url,
      data:data,
      method: method || 'post',
      headers: header,
    }).then(res=>{
      iView.LoadingBar.finish()
      if (res.data.code == 1002) {
        //token过期
        store.dispatch('handleLogOut')
        router.push({
          name: 'login'
        })
      }
      if (res.headers.authorization) {
        //刷新token
        store.dispatch('refreshToken', res.headers.authorization)
        sessionStorage.setItem('token', res.headers.authorization)
      }
      return res.data
    },err => {
      if(err.response.data&&err.response.data.code&&(err.response.data.code===1000||err.response.data.code===1001||err.response.data.code===1002)){
        //token过期
        store.dispatch('handleLogOut')
        router.push({
          name: store.state.user.loginType == 1 ? 'login_hegii' : 'login',
          params:{message:err.response.data.message}
        })
      }
      reject(err)
    }).then((res) => {
      resolve(res)
    })
  })
}

export const requestUpload = (url,data) => {
  return new Promise((resolve, reject) => {
    iView.LoadingBar.start()
    // console.log('store',store)
    axios.request({
      url: url,
      data:data,
      method: 'post',
      headers: {
        'Authorization': "Bearer " + store.state.user.token,
        'Content-Type': 'multipart/form-data',
        // Accept: '*/*'
      },
    }).then(res=>{
      iView.LoadingBar.finish()
      return res.data
    },err => {
      reject(err)
    }).then((res) => {
      resolve(res)
    })
  })
}

export const requestKJL = (url, data, method) => {
  // console.log('requestKJL', url, data, method)
  return new Promise((resolve, reject) => {
    iView.LoadingBar.start()
    axios.request({
      url: 'https://openapi.kujiale.com' + url,
      data:data,
      method: method || 'post',
      headers: {'Content-Type': 'application/json;charset=utf-8'}
    }).then(res=>{
      iView.LoadingBar.finish()
      return res.data
    },err => {
      reject(err)
    }).then((res) => {
      resolve(res)
    })
  })
}



