From cc87fc060c30597ec2efe38d0c3efe62f4957fd5 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期五, 14 七月 2023 11:20:02 +0800
Subject: [PATCH] 2023-07-14

---
 src/api/index.js |  213 +++++++++++++++++++++++-----------------------------
 1 files changed, 95 insertions(+), 118 deletions(-)

diff --git a/src/api/index.js b/src/api/index.js
index a4dd5b9..d2d5304 100644
--- a/src/api/index.js
+++ b/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,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.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 +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)
     })
   }
 
@@ -288,7 +265,7 @@
     return axios({
       url,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -343,7 +320,7 @@
     return axios({
       url,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -499,7 +476,7 @@
     return axios({
       url,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -523,7 +500,7 @@
     return axios({
       url: `${url}/${param.func}`,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -555,7 +532,7 @@
     return axios({
       url: `${url}${param.func ? '/' + param.func : ''}`,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -567,7 +544,7 @@
     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') { // 缂栬緫鐘舵�侊紝鍗曠偣鐧诲綍鏈嶅姟鍣ㄤ负浜戠
@@ -595,7 +572,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 +598,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 +697,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 +728,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,7 +761,7 @@
       axios({
         url: token.interface,
         method: 'post',
-        data: param
+        data: JSON.stringify(param)
       }).then(res => {
         _resolve(res)
       })
@@ -813,7 +790,7 @@
       axios({
         url,
         method: 'post',
-        data: _param
+        data: JSON.stringify(_param)
       }).then(result => {
         if (result.status) {
           window.GLOB.OuterToken[token.interface] = {
@@ -831,7 +808,7 @@
           axios({
             url: token.interface,
             method: 'post',
-            data: param
+            data: JSON.stringify(param)
           }).then(res => {
             _resolve(res)
           })
@@ -854,7 +831,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 +880,7 @@
         return axios({
           url: `${url}${param.func ? '/' + param.func : ''}`,
           method: 'post',
-          data: param
+          data: JSON.stringify(param)
         })
       }
 
@@ -914,7 +891,7 @@
             axios({
               url: `${url}${param.func ? '/' + param.func : ''}`,
               method: 'post',
-              data: param
+              data: JSON.stringify(param)
             }).then(result => {
               resolve(result)
             })
@@ -927,7 +904,7 @@
       return axios({
         url: `${url}${param.func ? '/' + param.func : ''}`,
         method: 'post',
-        data: param
+        data: JSON.stringify(param)
       })
     }
   }
@@ -968,7 +945,7 @@
     return axios({
       url,
       method: 'post',
-      data: param
+      data: JSON.stringify(param)
     })
   }
 
@@ -979,6 +956,7 @@
     return axios({
       url: '/webapi/doupload',
       method: 'post',
+      headers: { 'Content-Type': 'multipart/form-data' },
       data: param
     })
   }
@@ -990,6 +968,7 @@
     return axios({
       url: '/webapi/dopreload',
       method: 'post',
+      headers: { 'Content-Type': 'multipart/form-data' },
       data: param
     })
   }
@@ -1017,6 +996,7 @@
     return axios({
       url: _url,
       method: 'post',
+      headers: { 'Content-Type': 'multipart/form-data' },
       data: param
     })
   }
@@ -1026,14 +1006,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 +1018,7 @@
     return axios({
       url: 'http://127.0.0.1:888/postek/print',
       method: 'post',
-      data: data
+      data: qs.stringify(data)
     })
   }
 }

--
Gitblit v1.8.0