From 4adb8b8868aeed1f5f3b89ae269a7724c6b451ad Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期五, 11 八月 2023 16:58:31 +0800
Subject: [PATCH] 2023-08-11

---
 src/api/index.js |  295 ++++++++++++++++++++++++++---------------------------------
 1 files changed, 130 insertions(+), 165 deletions(-)

diff --git a/src/api/index.js b/src/api/index.js
index a4dd5b9..eb138b3 100644
--- a/src/api/index.js
+++ b/src/api/index.js
@@ -1,16 +1,17 @@
 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'
 import moment from 'moment'
+
 import Utils from '@/utils/utils.js'
 import CacheUtils from './cacheutils'
-import options from '@/store/options.js'
 
 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,54 @@
 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,
+        okText: '鐭ラ亾浜�',
+        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.status) {
+    notification.error({
+      className: 'notification-custom-error',
+      bottom: 0,
+      message: '鐘舵�佺爜-' + response.status + '锛岃鑱旂郴绠$悊鍛�',
+      placement: 'bottomRight',
+      duration: 15
+    })
   }
+
+  return Promise.reject(response)
 })
 
 class Api {
@@ -91,24 +89,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 +127,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 +163,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 +199,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 +226,7 @@
     return axios({
       url: url,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -264,7 +241,7 @@
       Password: '',
       check_code: checkcode,
       way_no: 'sms_vcode',
-      systemType: options.sysType,
+      systemType: window.GLOB.sysType,
       login_city: sessionStorage.getItem('city') || '',
       login_id_address: sessionStorage.getItem('ipAddress') || '',
       kei_id: window.btoa(window.encodeURIComponent(window.GLOB.host)),
@@ -275,11 +252,11 @@
     let url = '/webapi/dologon'
     if (isCloud) {
       param.debug = 'Y'
-      if (options.cloudServiceApi) {
-        param.rduri = options.cloudServiceApi.replace(/\/webapi(.*)/, '/webapi/dologon')
+      if (window.GLOB.cloudServiceApi) {
+        param.rduri = window.GLOB.cloudServiceApi.replace(/\/webapi(.*)/, '/webapi/dologon')
       }
     } else if (window.GLOB.mainSystemApi) {
-      if (options.sysType !== 'cloud' && window.GLOB.systemType !== 'production') {
+      if (window.GLOB.sysType !== 'cloud' && window.GLOB.systemType !== 'production') {
         param.linkurl = window.GLOB.linkurl
       }
       param.rduri = window.GLOB.mainSystemApi.replace(/\/webapi(.*)/, '/webapi/dologon')
@@ -288,7 +265,7 @@
     return axios({
       url,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -300,7 +277,7 @@
     let param = {
       // func: 'webapi_login',
       UserName: username,
-      systemType: options.sysType,
+      systemType: window.GLOB.sysType,
       Type: 'S',
       login_city: sessionStorage.getItem('city') || '',
       login_id_address: sessionStorage.getItem('ipAddress') || '',
@@ -330,11 +307,11 @@
 
     if (isCloud) {
       param.debug = 'Y'
-      if (options.cloudServiceApi) {
-        param.rduri = options.cloudServiceApi.replace(/\/webapi(.*)/, '/webapi/dologon')
+      if (window.GLOB.cloudServiceApi) {
+        param.rduri = window.GLOB.cloudServiceApi.replace(/\/webapi(.*)/, '/webapi/dologon')
       }
     } else if (window.GLOB.mainSystemApi) {
-      if (options.sysType !== 'cloud' && window.GLOB.systemType !== 'production') {
+      if (window.GLOB.sysType !== 'cloud' && window.GLOB.systemType !== 'production') {
         param.linkurl = window.GLOB.linkurl
       }
       param.rduri = window.GLOB.mainSystemApi.replace(/\/webapi(.*)/, '/webapi/dologon')
@@ -343,41 +320,33 @@
     return axios({
       url,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
   delCacheConfig (type = '') {
-    let date = moment().subtract(7, 'days').format('YYYY-MM-DD')
+    let date = moment().subtract(7, 'days').format('YYYY-MM-DD HH:mm:ss')
     CacheUtils.delIndexDBCacheConfig(date, type)
   }
 
   writeCacheConfig (menuid, data) {
     if (!menuid) return
-    let date = moment().format('YYYY-MM-DD')
+    let date = moment().format('YYYY-MM-DD HH:mm:ss')
     let _data = data ? JSON.stringify(data) : ''
 
     CacheUtils.writeCacheInIndexDB({menuid, CreateDate: date, LongParam: _data})
   }
 
-  getLCacheConfig (menuid) {
+  getLCacheConfig (menuid, minutes) {
     return new Promise((resolve, reject) => {
-      if (window.GLOB.IndexDB) {
-        CacheUtils.getIndexDBCacheConfig(menuid).then(res => {
-          if (res && res.LongParam) {
-            let _data = JSON.parse(res.LongParam)
-            if (_data.length === 0) {
-              resolve()
-            } else {
-              resolve(_data)
-            }
-          } else {
-            resolve()
-          }
-        })
-      } else {
-        resolve()
-      }
+      let limit = minutes ? moment().subtract(minutes, 'minutes').format('YYYY-MM-DD HH:mm:ss') : ''
+      CacheUtils.getIndexDBCacheConfig(menuid).then(res => {
+        if (res && res.LongParam) {
+          resolve({data: JSON.parse(res.LongParam), valid: !limit || res.CreateDate > limit})
+        } else {
+          resolve({data: null, valid: false})
+        }
+      })
     })
   }
 
@@ -440,7 +409,7 @@
           if (list.length > 0) {
             let clear = false
             let _appkey = window.GLOB.appkey.substr(-10)
-            let reg = new RegExp(_appkey + '$', 'ig')
+            let reg = new RegExp(_appkey + '$', 'i')
 
             list.forEach(mid => {
               if (reg.test(mid)) {
@@ -451,6 +420,7 @@
               list = ''
             }
             CacheUtils.delIndexDBConfig(list)
+            this.delCacheConfig('all')
           }
 
           CacheUtils.updateIndexDBversion({version: res.app_version || '1.00', createDate: curTime})
@@ -484,14 +454,14 @@
    */
   getCloudConfig (param) {
     param.lang = param.lang || sessionStorage.getItem('lang') || ''
-    param.appkey = window.GLOB.appkey || ''
+    param.appkey = param.appkey || window.GLOB.appkey || ''
     param.SessionUid = localStorage.getItem('SessionUid') || ''
-    param.userid = sessionStorage.getItem('CloudUserID') || ''
-    param.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
+    param.userid = param.userid || sessionStorage.getItem('CloudUserID') || ''
+    param.LoginUID = param.LoginUID || sessionStorage.getItem('CloudLoginUID') || ''
 
     param = this.encryptParam(param)
 
-    let url = options.cloudServiceApi ? options.cloudServiceApi : '/webapi/dostars'
+    let url = window.GLOB.cloudServiceApi ? window.GLOB.cloudServiceApi : '/webapi/dostars'
     if (param.func) {
       url = url + '/' + param.func
     }
@@ -499,7 +469,7 @@
     return axios({
       url,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -523,7 +493,7 @@
     return axios({
       url: `${url}/${param.func}`,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -538,8 +508,8 @@
     param.appkey = param.appkey || window.GLOB.appkey
 
     let url = '/webapi/dostars'
-    if (sessionStorage.getItem('isEditState') === 'true' && options.cloudServiceApi) { // 缂栬緫鐘舵�侊紝涓斿瓨鍦ㄤ簯绔湴鍧�
-      url = options.cloudServiceApi
+    if (window.GLOB.mkHS && window.GLOB.cloudServiceApi) { // HS涓嬶紝涓斿瓨鍦ㄤ簯绔湴鍧�
+      url = window.GLOB.cloudServiceApi
       param.userid = sessionStorage.getItem('CloudUserID') || ''
       param.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
     } else if (window.GLOB.mainSystemApi) {
@@ -555,7 +525,7 @@
     return axios({
       url: `${url}${param.func ? '/' + param.func : ''}`,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -567,12 +537,12 @@
     param.lang = param.lang || sessionStorage.getItem('lang') || ''
     param.SessionUid = localStorage.getItem('SessionUid') || ''
     param.LoginUID = sessionStorage.getItem('LoginUID') || ''
-    param.appkey = window.GLOB.appkey || ''
+    param.appkey = param.appkey || window.GLOB.appkey || ''
 
     let url = '/webapi/dostars'
-    if (sessionStorage.getItem('isEditState') === 'true') { // 缂栬緫鐘舵�侊紝鍗曠偣鐧诲綍鏈嶅姟鍣ㄤ负浜戠
-      if (options.cloudServiceApi) { // 瀛樺湪浜戠鍦板潃鏃讹紝浣跨敤浜戠绯荤粺鍙傛暟
-        url = options.cloudServiceApi
+    if (window.GLOB.mkHS) {          // HS涓嬶紝鍗曠偣鐧诲綍鏈嶅姟鍣ㄤ负浜戠
+      if (window.GLOB.cloudServiceApi) { // 瀛樺湪浜戠鍦板潃鏃讹紝浣跨敤浜戠绯荤粺鍙傛暟
+        url = window.GLOB.cloudServiceApi
         param.userid = sessionStorage.getItem('CloudUserID') || ''
         param.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
       }
@@ -595,7 +565,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 +591,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 +690,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 +721,7 @@
       token = JSON.parse(window.decodeURIComponent(window.atob(token)))
     } catch (e) {
       token = null
-      _resolve({status: false, ErrCode: 'E', message: '鎺ュ彛淇℃伅瑙f瀽澶辫触锛�'})
+      _resolve({status: false, ErrCode: 'E', message: '鎺ュ彛淇℃伅瑙f瀽澶辫触锛�', ErrMesg: 'token_error'})
     }
 
     if (!token) return
@@ -784,14 +754,14 @@
       axios({
         url: token.interface,
         method: 'post',
-        data: param
+        data: JSON.stringify(param)
       }).then(res => {
         _resolve(res)
       })
     } else {
       let _param = {
         UserName: token.username,
-        systemType: options.sysType,
+        systemType: window.GLOB.sysType,
         login_city: sessionStorage.getItem('city') || '',
         device_id: token.appkey || '',
         timestamp: moment().format('YYYY-MM-DD HH:mm:ss'),
@@ -813,7 +783,7 @@
       axios({
         url,
         method: 'post',
-        data: _param
+        data: JSON.stringify(_param)
       }).then(result => {
         if (result.status) {
           window.GLOB.OuterToken[token.interface] = {
@@ -831,7 +801,7 @@
           axios({
             url: token.interface,
             method: 'post',
-            data: param
+            data: JSON.stringify(param)
           }).then(res => {
             _resolve(res)
           })
@@ -854,19 +824,14 @@
     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))
     }
 
-    if (['sPC_Get_TableData', 'sPC_Get_TableData_debug', 'sPC_TableData_InUpDe', 'sPC_TableData_InUpDe_debug', 'sPC_Get_structured_data'].includes(param.func)) {
-      if (sessionStorage.getItem('isEditState') === 'true') {
-        param.username = sessionStorage.getItem('CloudUserName') || ''
-        param.fullname = sessionStorage.getItem('CloudFullName') || ''
-      } else {
-        param.username = sessionStorage.getItem('User_Name') || ''
-        param.fullname = sessionStorage.getItem('Full_Name') || ''
-      }
+    if (['sPC_TableData_InUpDe', 'sPC_TableData_InUpDe_debug'].includes(param.func)) {
+      param.username = sessionStorage.getItem('User_Name') || ''
+      param.fullname = sessionStorage.getItem('Full_Name') || ''
     }
 
     let login = false
@@ -903,7 +868,7 @@
         return axios({
           url: `${url}${param.func ? '/' + param.func : ''}`,
           method: 'post',
-          data: param
+          data: JSON.stringify(param)
         })
       }
 
@@ -914,7 +879,7 @@
             axios({
               url: `${url}${param.func ? '/' + param.func : ''}`,
               method: 'post',
-              data: param
+              data: JSON.stringify(param)
             }).then(result => {
               resolve(result)
             })
@@ -927,7 +892,7 @@
       return axios({
         url: `${url}${param.func ? '/' + param.func : ''}`,
         method: 'post',
-        data: param
+        data: JSON.stringify(param)
       })
     }
   }
@@ -968,7 +933,7 @@
     return axios({
       url,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -979,6 +944,7 @@
     return axios({
       url: '/webapi/doupload',
       method: 'post',
+      headers: { 'Content-Type': 'multipart/form-data' },
       data: param
     })
   }
@@ -990,6 +956,7 @@
     return axios({
       url: '/webapi/dopreload',
       method: 'post',
+      headers: { 'Content-Type': 'multipart/form-data' },
       data: param
     })
   }
@@ -1017,6 +984,7 @@
     return axios({
       url: _url,
       method: 'post',
+      headers: { 'Content-Type': 'multipart/form-data' },
       data: param
     })
   }
@@ -1026,14 +994,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 +1006,7 @@
     return axios({
       url: 'http://127.0.0.1:888/postek/print',
       method: 'post',
-      data: data
+      data: qs.stringify(data)
     })
   }
 }

--
Gitblit v1.8.0