king
2023-06-30 0519dcda8b6fb2914987f8d4d58e1d14bcf98dc3
src/api/index.js
@@ -1,6 +1,6 @@
import axios from 'axios'
import qs from 'qs'
import { notification } from 'antd'
import { notification, Modal } from 'antd'
import md5 from 'md5'
import CryptoJS from 'crypto-js'
import jsSHA from 'jssha'
@@ -11,6 +11,7 @@
window.GLOB.IndexDB = null
window.GLOB.OuterToken = {}
window.GLOB.$error = false
let service = window.GLOB.service ? '-' + window.GLOB.service.replace('/', '') : ''
let db = `mkdb${service}`
@@ -23,57 +24,53 @@
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.withCredentials = false
axios.interceptors.request.use((config) => {
  if (/LoginAndRedirect|getjsonresult|wxNativePay|postek/ig.test(config.url)) {
    config.data = qs.stringify(config.data)
  } else if (/\/doupload|\/dopreload|\/upload/.test(config.url)) {
    config.headers = { 'Content-Type': 'multipart/form-data' }
  } else if (config.method === 'post' && config.data) {
    config.data = JSON.stringify(config.data)
  }
  return config
}, (error) => {
  return Promise.reject(error)
})
const setCurrentUrl = (res) => {
  if (!!(window.history && window.history.pushState)) {
    sessionStorage.clear()
    sessionStorage.setItem('loginError', JSON.stringify({url: res.config ? res.config.url : '', request: res.config ? res.config.data : '', response: JSON.stringify(res.data)}))
    window.history.replaceState(null, null, window.location.href.split('#')[0] + '#/login')
    window.location.reload()
  }
}
axios.interceptors.response.use((response) => {
  if (response.data.ErrCode === 'LoginError') {
    if (window.debugger === true) {
    if (window.GLOB.developing) {
      sessionStorage.setItem('devError', 'true')
      response.data.ErrCode = 'E'
      return Promise.resolve(response.data)
    } else if (!sessionStorage.getItem('loginError')) {
      setCurrentUrl(response)
    } else if (response.config.url.indexOf('https://sso.mk9h.cn/webapi/dostars') > -1) {
      return Promise.reject(response.data)
    } else if (response.config.url.indexOf('https://epc.mk9h.cn/webapi/dostars') > -1) {
      return Promise.reject(response.data)
    }
    if (!window.GLOB.$error) {
      window.GLOB.$error = true
      Modal.destroyAll()
      Modal.error({
        title: response.data.message,
        onOk: () => {
          window.GLOB.$error = false
          sessionStorage.clear()
          if (!!(window.history && window.history.pushState)) {
            window.history.replaceState(null, null, window.location.href.split('#')[0] + '#/login')
            window.location.reload()
          }
        }
      })
    }
    return Promise.reject(response.data)
  } else {
    return Promise.resolve(response.data)
  }
}, (error) => {
  let response = error.response
  let response = error.response || ''
  if (response) {
    if (!response.data || !response.data.errors) { // 过滤旷视报错信息
      notification.error({
        className: 'notification-custom-error',
        bottom: 0,
        message: '状态码-' + response.status + ',请联系管理员',
        placement: 'bottomRight',
        duration: 15
      })
    }
    return Promise.reject(response)
  } else {
    return Promise.reject()
  if (response && (!response.data || !response.data.errors)) { // 过滤旷视报错信息
    notification.error({
      className: 'notification-custom-error',
      bottom: 0,
      message: '状态码-' + response.status + ',请联系管理员',
      placement: 'bottomRight',
      duration: 15
    })
  }
  return Promise.reject(response)
})
class Api {
@@ -91,24 +88,25 @@
    return axios({
      url: `/webapi/dostar${param.func ? '/' + param.func : ''}`,
      method: 'post',
      data: param
      data: JSON.stringify(param)
    })
  }
  /**
   * @description 微信业务请求
   */
  wxAccessToken () {
  wxAccessToken (domain = '') {
    let _url = window.GLOB.baseurl + 'wxpay/getaccesstoken'
    if (process.env.NODE_ENV !== 'production') {
      _url = document.location.origin + '/wxpay/getaccesstoken'
    if (domain) {
      _url = domain + 'wxpay/getaccesstoken'
    }
    return new Promise(resolve => {
      if (window.GLOB.accessToken.accessTime && (parseInt(new Date().getTime() / 1000) - window.GLOB.accessToken.accessTime < 30)) {
      if (window.GLOB.accessToken.domain === domain && window.GLOB.accessToken.accessTime && (parseInt(new Date().getTime() / 1000) - window.GLOB.accessToken.accessTime < 30)) {
        resolve(window.GLOB.accessToken)
      } else {
        window.GLOB.accessToken = {}
        window.GLOB.accessToken = {domain}
        axios({
          url: _url,
          method: 'get'
@@ -128,19 +126,23 @@
   * @description 微信业务请求
   */
  wxNginxRequest (url, method, param) {
    let _url = window.GLOB.location + '/' + url
    if (process.env.NODE_ENV === 'production') {
      _url = document.location.origin + '/' + url
    }
    if (/^http:\/\/(qingqiumarket.cn|cloud.mk9h.cn|sso.mk9h.cn)/.test(_url)) {
      _url = window.GLOB.location + ':8080/' + url
    let _url = url
    if (!/^http/.test(url)) {
      _url = window.GLOB.location + '/' + url
      if (process.env.NODE_ENV === 'production') {
        _url = document.location.origin + ':8080/' + url
        _url = document.location.origin + '/' + url
      }
    } else if (/^https:\/\/(qingqiumarket.cn|cloud.mk9h.cn|sso.mk9h.cn)/.test(_url)) {
      _url = window.GLOB.location + ':8443/' + url
      if (process.env.NODE_ENV === 'production') {
        _url = document.location.origin + ':8443/' + url
      if (/^http:\/\/(qingqiumarket.cn|cloud.mk9h.cn|sso.mk9h.cn)/.test(_url)) {
        _url = window.GLOB.location + ':8080/' + url
        if (process.env.NODE_ENV === 'production') {
          _url = document.location.origin + ':8080/' + url
        }
      } else if (/^https:\/\/(qingqiumarket.cn|cloud.mk9h.cn|sso.mk9h.cn)/.test(_url)) {
        _url = window.GLOB.location + ':8443/' + url
        if (process.env.NODE_ENV === 'production') {
          _url = document.location.origin + ':8443/' + url
        }
      }
    }
    
@@ -160,38 +162,8 @@
  /**
   * @description 直接请求
   * @param {Object} param 查询及提交参数
   */
  directRequest (url, method = 'post', param, cross) {
    if (cross === 'true' && param) {
      return axios({
        url,
        method,
        data: param
      })
    } else if (cross === 'true') {
      return axios({
        url,
        method
      })
    }
    let params = { method: 'post' }
    let _url = url
    if (method === 'get' && param) {
      let keys = Object.keys(param).map(key => `${key}=${param[key]}`)
      keys = keys.join('&')
      if (keys) {
        _url = _url + '?' + keys
      }
    } else if (method === 'post' && param) {
      params.data = param
    }
    _url = _url.replace(/&/ig, '%26')
    params.url = '/trans/redirect?rd=' + _url + '&method=' + method
  directRequest (params) {
    return axios(params)
  }
@@ -226,16 +198,20 @@
      param.thd_party_openid = openid
      param.thd_party_appid = appid
      param.id = scanId
    } else if (binding_type === 'login_check') { // appid 此时为目标
    } else if (binding_type === 'login_check') { // appid 此时为目标地址
      param.v_type = 'login_check'
      param.LoginUID = sessionStorage.getItem('LoginUID') || ''
      url = appid.replace(/\/webapi(.*)/, '/webapi/dologon/s_visitor_login')
      if (!param.rduri) {
        param.rduri = window.GLOB.baseurl + 'webapi/dologon/s_visitor_login'
      }
      param.linkurl = appid.replace(/\/webapi(.*)/, '/index.html')
      if (appid) {
        url = appid.replace(/\/webapi(.*)/, '/webapi/dologon/s_visitor_login')
        param.linkurl = appid.replace(/\/webapi(.*)/, '/index.html')
      } else {
        param.linkurl = window.GLOB.linkurl
      }
    }
    
    param.LText = md5(window.btoa(_SessionUid + param.timestamp + (param.linkurl || '')))
@@ -249,7 +225,7 @@
    return axios({
      url: url,
      method: 'post',
      data: param
      data: JSON.stringify(param)
    })
  }
@@ -288,7 +264,7 @@
    return axios({
      url,
      method: 'post',
      data: param
      data: JSON.stringify(param)
    })
  }
@@ -343,7 +319,7 @@
    return axios({
      url,
      method: 'post',
      data: param
      data: JSON.stringify(param)
    })
  }
@@ -499,7 +475,7 @@
    return axios({
      url,
      method: 'post',
      data: param
      data: JSON.stringify(param)
    })
  }
@@ -523,7 +499,7 @@
    return axios({
      url: `${url}/${param.func}`,
      method: 'post',
      data: param
      data: JSON.stringify(param)
    })
  }
@@ -555,7 +531,7 @@
    return axios({
      url: `${url}${param.func ? '/' + param.func : ''}`,
      method: 'post',
      data: param
      data: JSON.stringify(param)
    })
  }
@@ -595,7 +571,7 @@
          axios({
            url: `${url}${param.func ? '/' + param.func : ''}`,
            method: 'post',
            data: param
            data: JSON.stringify(param)
          }).then(res => {
            if (res.status && window.GLOB.IndexDB) {
              let msg = {
@@ -621,7 +597,7 @@
        axios({
          url: `${url}${param.func ? '/' + param.func : ''}`,
          method: 'post',
          data: param
          data: JSON.stringify(param)
        }).then(res => {
          if (res.status) {
            window.GLOB.CacheMap.set(key, res)
@@ -720,7 +696,7 @@
        axios({
          url: `${url}${param.func ? '/' + param.func : ''}`,
          method: 'post',
          data: param
          data: JSON.stringify(param)
        }).then(res => {
          if (res.status) {
            window.GLOB.CacheMap.set(_param, res)
@@ -751,7 +727,7 @@
      token = JSON.parse(window.decodeURIComponent(window.atob(token)))
    } catch (e) {
      token = null
      _resolve({status: false, ErrCode: 'E', message: '接口信息解析失败!'})
      _resolve({status: false, ErrCode: 'E', message: '接口信息解析失败!', ErrMesg: 'token_error'})
    }
    if (!token) return
@@ -784,7 +760,7 @@
      axios({
        url: token.interface,
        method: 'post',
        data: param
        data: JSON.stringify(param)
      }).then(res => {
        _resolve(res)
      })
@@ -813,7 +789,7 @@
      axios({
        url,
        method: 'post',
        data: _param
        data: JSON.stringify(_param)
      }).then(result => {
        if (result.status) {
          window.GLOB.OuterToken[token.interface] = {
@@ -831,7 +807,7 @@
          axios({
            url: token.interface,
            method: 'post',
            data: param
            data: JSON.stringify(param)
          }).then(res => {
            _resolve(res)
          })
@@ -854,7 +830,7 @@
    param.appkey = window.GLOB.appkey || ''
    if (param.$token === '') {
      return Promise.resolve({status: false, ErrCode: 'E', message: '接口地址尚未设置!'})
      return Promise.resolve({status: false, ErrCode: 'E', message: '接口地址尚未设置!', ErrMesg: 'token_error'})
    } else if (param.$token) {
      return new Promise(resolve => this.visitOuterSystem(param, resolve))
    }
@@ -903,7 +879,7 @@
        return axios({
          url: `${url}${param.func ? '/' + param.func : ''}`,
          method: 'post',
          data: param
          data: JSON.stringify(param)
        })
      }
@@ -914,7 +890,7 @@
            axios({
              url: `${url}${param.func ? '/' + param.func : ''}`,
              method: 'post',
              data: param
              data: JSON.stringify(param)
            }).then(result => {
              resolve(result)
            })
@@ -927,7 +903,7 @@
      return axios({
        url: `${url}${param.func ? '/' + param.func : ''}`,
        method: 'post',
        data: param
        data: JSON.stringify(param)
      })
    }
  }
@@ -968,7 +944,7 @@
    return axios({
      url,
      method: 'post',
      data: param
      data: JSON.stringify(param)
    })
  }
@@ -979,6 +955,7 @@
    return axios({
      url: '/webapi/doupload',
      method: 'post',
      headers: { 'Content-Type': 'multipart/form-data' },
      data: param
    })
  }
@@ -990,6 +967,7 @@
    return axios({
      url: '/webapi/dopreload',
      method: 'post',
      headers: { 'Content-Type': 'multipart/form-data' },
      data: param
    })
  }
@@ -1017,6 +995,7 @@
    return axios({
      url: _url,
      method: 'post',
      headers: { 'Content-Type': 'multipart/form-data' },
      data: param
    })
  }
@@ -1026,14 +1005,11 @@
   */
  getWxNativePay (param) {
    let _url = window.GLOB.baseurl + 'wxpay/wxNativePay'
    if (process.env.NODE_ENV !== 'production') {
      _url = document.location.origin + '/wxpay/wxNativePay'
    }
    return axios({
      url: _url,
      method: 'post',
      data: param
      data: qs.stringify(param)
    })
  }
@@ -1041,7 +1017,7 @@
    return axios({
      url: 'http://127.0.0.1:888/postek/print',
      method: 'post',
      data: data
      data: qs.stringify(data)
    })
  }
}