From 316877c1d9e5b6d92334f30b03d97d7e833cd934 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期二, 02 二月 2021 16:01:57 +0800
Subject: [PATCH] 2021-02-02

---
 src/tabviews/subtable/index.jsx |  333 ++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 256 insertions(+), 77 deletions(-)

diff --git a/src/tabviews/subtable/index.jsx b/src/tabviews/subtable/index.jsx
index 6e798b6..07dbc4c 100644
--- a/src/tabviews/subtable/index.jsx
+++ b/src/tabviews/subtable/index.jsx
@@ -8,8 +8,9 @@
 import zhCN from '@/locales/zh-CN/main.js'
 import enUS from '@/locales/en-US/main.js'
 import Utils from '@/utils/utils.js'
+import options from '@/store/options.js'
 import UtilsDM from '@/utils/utils-datamanage.js'
-import UtilsUpdate from '@/utils/utils-update.js'
+import { updateSubTable } from '@/utils/utils-update.js'
 import { modifyTabview } from '@/store/action'
 import asyncComponent from '@/utils/asyncComponent'
 import asyncSpinComponent from '@/utils/asyncSpinComponent'
@@ -61,29 +62,27 @@
     statFields: [],       // 鍚堣瀛楁
     statFValue: [],       // 鍚堣鍊�
     absFields: [],        // 缁濆鍊煎瓧娈�
+    loadCustomApi: true,  // 鍔犺浇澶栭儴璧勬簮
+    hasReqFields: false
   }
 
   /**
    * @description 涓婄骇鑿滃崟id鍙樺寲鏃讹紝鍒锋柊鏁版嵁
    */
   UNSAFE_componentWillReceiveProps(nextProps) {
-    if (this.state.config && this.props.Tab.supMenu && !is(fromJS(this.props.BID), fromJS(nextProps.BID))) {
+    const { config, setting } = this.state
+
+    if (config && setting && this.props.Tab.supMenu && !is(fromJS(this.props.BID), fromJS(nextProps.BID))) {
       MKEmitter.emit('resetTable', this.props.MenuID + this.props.Tab.uuid) // 鍒楄〃閲嶇疆
       this.setState({
         pageIndex: 1
       }, () => {
-        if (this.state.setting) {
-          this.loadmaindata(nextProps.BID, 'refresh')
-          this.getStatFieldsValue(nextProps.BID, 'refresh')
-        }
+        this.loadData()
       })
-    } else if (!this.props.Tab.supMenu && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
-      if (this.state.setting) {
-        this.setState({}, () => {
-          this.loadmaindata()
-          this.getStatFieldsValue()
-        })
-      }
+    } else if (setting && !this.props.Tab.supMenu && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
+      this.setState({}, () => {
+        this.loadData()
+      })
     }
   }
 
@@ -103,6 +102,7 @@
 
       try { // 閰嶇疆淇℃伅瑙f瀽
         config = JSON.parse(window.decodeURIComponent(window.atob(result.LongParam)))
+        config.setting.MenuName = Tab.label
       } catch (e) {
         console.warn('Parse Failure')
         config = ''
@@ -135,7 +135,7 @@
       let absFields = []     // 缁濆鍊煎瓧娈�
 
       // 鐗堟湰鍏煎
-      config = UtilsUpdate.updateSubTable(config)
+      config = updateSubTable(config)
 
       // 鏉冮檺杩囨护
       if (this.props.menuType !== 'HS') {
@@ -262,8 +262,11 @@
       }
 
       let valid = true // 鎼滅储鏉′欢蹇呭~楠岃瘉
+      let hasReqFields = false
       config.search.forEach(field => {
-        if (field.required === 'true' && !field.initval) {
+        if (field.required !== 'true') return
+        hasReqFields = true
+        if (!field.initval) {
           valid = false
         }
       })
@@ -274,7 +277,7 @@
       config.setting.execute = config.setting.default !== 'false'     // 榛樿sql鏄惁鎵ц锛岃浆涓篵oolean 缁熶竴鏍煎紡
       config.setting.customScript = ''                                // 鑷畾涔夎剼鏈�
 
-      if (config.setting.interType === 'system') {
+      if (config.setting.interType === 'system' || (config.setting.interType === 'custom' && config.setting.requestMode === 'system')) {
         if (config.setting.scripts && config.setting.scripts.length > 0) {
           let _customScript = ''
           config.setting.scripts.forEach(item => {
@@ -317,11 +320,11 @@
         actions: _actions,
         columns: _columns,
         arr_field: _arrField.join(','),
-        search: Utils.initMainSearch(config.search) // 鎼滅储鏉′欢鍒濆鍖栵紙鍚湁鏃堕棿鏍煎紡锛岄渶瑕佽浆鍖栵級
+        search: Utils.initMainSearch(config.search), // 鎼滅储鏉′欢鍒濆鍖栵紙鍚湁鏃堕棿鏍煎紡锛岄渶瑕佽浆鍖栵級
+        hasReqFields
       }, () => {
         if (config.setting.onload !== 'false' && (!Tab.supMenu || BID || Tab.isTreeNode) && valid) { // 鍒濆鍖栧彲鍔犺浇
-          this.loadmaindata()
-          this.getStatFieldsValue()
+          this.loadData()
         }
       })
     } else {
@@ -338,24 +341,23 @@
       })
     }
   }
+  
+  loadData = () => {
+    const { mainSearch, BID } = this.props
+    const { setting, search, loadCustomApi, hasReqFields } = this.state
 
-  /**
-   * @description 瀛愯〃鏁版嵁鍔犺浇
-   */
-  async loadmaindata (bid, type) {
-    const { mainSearch } = this.props
-    const { setting, arr_field, search, orderBy, pageIndex, pageSize, absFields } = this.state
-
-    let _BID = this.props.BID
     let searches = fromJS(search).toJS()
     if (mainSearch && mainSearch.length > 0) { // 涓昏〃鎼滅储鏉′欢
       searches = [...mainSearch, ...searches]
     }
 
-    let requireFields = search.filter(item => item.required && (!item.value || item.value.length === 0))
-    let prex = this.props.Tab && this.props.Tab.label ? this.props.Tab.label + '-' : ''
-
+    let requireFields = []
+    if (hasReqFields) {
+      requireFields = searches.filter(item => item.required && (!item.value || item.value.length === 0))
+    }
+    
     if (requireFields.length > 0) {
+      let prex = this.props.Tab && this.props.Tab.label ? this.props.Tab.label + '-' : ''
       let labels = requireFields.map(item => item.label)
       labels = Array.from(new Set(labels))
 
@@ -365,34 +367,235 @@
         duration: 3
       })
       return
+    } else if (this.props.Tab.supMenu && !BID) { // 涓昏〃ID涓嶅瓨鍦ㄦ椂锛屼笉鏌ヨ瀛愯〃
+      this.setState({
+        data: [],
+        selectedData: [],
+        statFValue: [],
+        total: 0
+      })
+      this.handleTableId()
+      return
+    } else if (window.GLOB.systemType === 'production' && setting.interType === 'custom' && !setting.proInterface) {
+      notification.warning({
+        top: 92,
+        message: '鏈缃寮忕郴缁熷湴鍧�!',
+        duration: 3
+      })
+      return
     }
 
-    if (type === 'refresh') {
-      if (!bid) { // 涓昏〃ID涓嶅瓨鍦ㄦ椂锛屼笉鏌ヨ瀛愯〃
-        this.setState({
-          data: [],
-          total: 0
-        })
-        return
+    this.setState({
+      selectedData: []
+    })
+    this.handleTableId()
+
+    if (setting.interType === 'custom' && loadCustomApi) {
+      if (setting.execTime === 'once') {
+        this.setState({loadCustomApi: false})
+      }
+
+      this.loadOutResource(searches)
+      if (setting.execType === 'async') {
+        this.loadmaindata()
+      }
+    } else {
+      this.loadmaindata()
+    }
+  }
+
+  loadOutResource = (searches) => {
+    const { BID } = this.props
+    const { setting } = this.state
+
+    let param = UtilsDM.getPrevQueryParams(setting, searches, BID, this.props.menuType)
+
+    if (setting.execType === 'sync') {
+      this.setState({
+        loading: true
+      })
+    }
+
+    Api.genericInterface(param).then(res => {
+      if (res.status) {
+        if (res.mk_ex_invoke === 'false') {
+          this.loadmaindata()
+        } else {
+          this.customOuterRequest(res)
+        }
       } else {
-        _BID = bid
+        this.setState({
+          loading: false
+        })
+        notification.error({
+          top: 92,
+          message: res.message,
+          duration: 10
+        })
+      }
+    }, () => {
+      this.setState({
+        loading: false
+      })
+    })
+  }
+
+  customOuterRequest = (result) => {
+    const { setting } = this.state
+    let url = ''
+
+    if (window.GLOB.systemType === 'production') {
+      url = setting.proInterface
+    } else {
+      url = setting.interface
+    }
+
+    let mkey = result.mk_api_key || ''
+
+    delete result.mk_ex_invoke
+    delete result.status
+    delete result.message
+    delete result.ErrCode
+    delete result.ErrMesg
+    delete result.mk_api_key
+
+    let param = {}
+
+    Object.keys(result).forEach(key => {
+      key = key.replace(/^mk_/ig, '')
+      param[key] = result[key]
+    })
+
+    Api.directRequest(url, setting.method, param).then(res => {
+      if (typeof(res) !== 'object' || Array.isArray(res)) {
+        let error = '鏈煡鐨勮繑鍥炵粨鏋滐紒'
+
+        if (typeof(res) === 'string') {
+          error = res.replace(/'/ig, '"')
+        }
+
+        let _result = {
+          mk_api_key: mkey,
+          $ErrCode: 'E',
+          $ErrMesg: error
+        }
+
+        this.customCallbackRequest(_result)
+      } else {
+        res.mk_api_key = mkey
+        this.customCallbackRequest(res)
+      }
+    }, (e) => {
+      let _result = {
+        mk_api_key: mkey,
+        $ErrCode: 'E',
+        $ErrMesg: e && e.statusText ? e.statusText : ''
+      }
+
+      this.customCallbackRequest(_result)
+    })
+  }
+
+  customCallbackRequest = (result) => {
+    const { setting } = this.state
+    let errSql = ''
+    if (result.$ErrCode === 'E') {
+      errSql = `
+        set @ErrorCode='E'
+        set @retmsg='${result.$ErrMesg}'
+      `
+      delete result.$ErrCode
+      delete result.$ErrMesg
+    }
+
+    let lines = UtilsDM.getCallBackSql(setting, result)
+    let param = {}
+
+    if (setting.callbackType === 'script') { // 浣跨敤鑷畾涔夎剼鏈�
+      let sql = lines.map(item => (`
+        ${item.insert}
+        ${item.selects.join(` union all
+        `)}
+      `))
+      sql = sql.join('')
+      
+      param = UtilsDM.getCallBackQueryParams(setting, sql, errSql)
+
+      if (this.props.BID) {
+        param.BID = this.props.BID
+      }
+
+      if (this.props.menuType === 'HS') { // 鍑芥暟 sPC_TableData_InUpDe 浜戠楠岃瘉
+        param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
+      }
+    } else {
+      param.func = 's_ex_result_back'
+      param.s_ex_result = lines.map((item, index) => ({
+        MenuID: this.props.MenuID,
+        MenuName: this.props.Tab.label,
+        TableName: item.table,
+        LongText: window.btoa(window.encodeURIComponent(`${item.insert}  ${item.selects.join(` union all `)}`)),
+        Sort: index + 1
+      }))
+
+      if ((window.GLOB.systemType !== 'production' && options.sysType !== 'cloud') || window.debugger === true) {
+        let sql = lines.map(item => (`
+          ${item.insert}
+          ${item.selects.join(` union all
+          `)}
+        `))
+        sql = sql.join('')
+        console.info(sql.replace(/\n\s{10}/ig, '\n'))
       }
     }
+
+    Api.genericInterface(param).then(res => {
+      if (res.status) {
+        this.loadmaindata()
+      } else {
+        this.setState({
+          loading: false
+        })
+        notification.error({
+          top: 92,
+          message: res.message,
+          duration: 10
+        })
+      }
+    }, () => {
+      this.setState({
+        loading: false
+      })
+    })
+  }
+
+  /**
+   * @description 瀛愯〃鏁版嵁鍔犺浇
+   */
+  async loadmaindata () {
+    const { mainSearch, BID } = this.props
+    const { setting, arr_field, search, orderBy, pageIndex, pageSize, absFields } = this.state
+
+    let searches = fromJS(search).toJS()
+    if (mainSearch && mainSearch.length > 0) { // 涓昏〃鎼滅储鏉′欢
+      searches = [...mainSearch, ...searches]
+    }
+
     this.setState({
-      selectedData: [],
       loading: true
     })
 
     let _orderBy = orderBy || setting.order
-    let param = UtilsDM.getQueryDataParams(setting, arr_field, searches, _orderBy, pageIndex, pageSize, _BID, this.props.menuType)
-
-    this.handleTableId()
+    let param = UtilsDM.getQueryDataParams(setting, arr_field, searches, _orderBy, pageIndex, pageSize, BID, this.props.menuType)
 
     if (param.func === 'sPC_Get_TableData') {
       param.menuname = this.props.Tab.label || ''
     }
 
     let result = await Api.genericInterface(param)
+
+    this.getStatFieldsValue(searches)
+
     if (result.status) {
       this.setState({
         data: result.data.map((item, index) => {
@@ -406,7 +609,7 @@
           }
           item.key = index
           item.$$uuid = item[setting.primaryKey] || ''
-          item.$$BID = _BID || ''
+          item.$$BID = BID || ''
 
           return item
         }),
@@ -415,6 +618,7 @@
         loading: false
       })
     } else {
+      let prex = this.props.Tab && this.props.Tab.label ? this.props.Tab.label + '-' : ''
       this.setState({
         loading: false
       })
@@ -501,36 +705,14 @@
   /**
    * @description 鑾峰彇鍚堣瀛楁鍊�
    */
-  getStatFieldsValue = (bid, type) => {
-    const { mainSearch } = this.props
-    const { setting, search, orderBy, statFields } = this.state
+  getStatFieldsValue = (searches) => {
+    const { BID } = this.props
+    const { setting, orderBy, statFields } = this.state
 
-    let _BID = this.props.BID
-    let searches = fromJS(search).toJS()
-    if (mainSearch && mainSearch.length > 0) { // 涓昏〃鎼滅储鏉′欢
-      searches = [...mainSearch, ...searches]
-    }
-
-    if (statFields.length === 0 || setting.interType !== 'system' || !setting.dataresource) return
-
-    let requireFields = search.filter(item => item.required && (!item.value || item.value.length === 0))
-    if (requireFields.length > 0) {
-      return
-    }
-
-    if (type === 'refresh') {
-      if (!bid) { // 涓昏〃ID涓嶅瓨鍦ㄦ椂锛屼笉鏌ヨ瀛愯〃鍚堣鍊�
-        this.setState({
-          statFValue: []
-        })
-        return
-      } else {
-        _BID = bid
-      }
-    }
+    if (statFields.length === 0 || !(setting.interType === 'system' || (setting.interType === 'custom' && setting.requestMode === 'system')) || !setting.dataresource) return
 
     let _orderBy = orderBy || setting.order
-    let param = UtilsDM.getStatQueryDataParams(setting, statFields, searches, _orderBy, _BID, this.props.menuType)
+    let param = UtilsDM.getStatQueryDataParams(setting, statFields, searches, _orderBy, BID, this.props.menuType)
 
     if (param.func === 'sPC_Get_TableData') {
       param.menuname = this.props.Tab.label || ''
@@ -579,8 +761,7 @@
       pageIndex: 1,
       search: searches,
     }, () => {
-      this.loadmaindata()
-      this.getStatFieldsValue()
+      this.loadData()
     })
   }
 
@@ -601,7 +782,7 @@
       pageSize: pagination.pageSize,
       orderBy: (sorter.field && sorter.order) ? `${sorter.field} ${sorter.order}` : ''
     }, () => {
-      this.loadmaindata()
+      this.loadData()
     })
   }
 
@@ -614,13 +795,11 @@
       this.setState({
         pageIndex: 1
       }, () => {
-        this.loadmaindata()
-        this.getStatFieldsValue()
+        this.loadData()
       })
     } else {
       MKEmitter.emit('resetTable', this.props.MenuID + this.props.Tab.uuid, 'false') // 鍒楄〃閲嶇疆
-      this.loadmaindata()
-      this.getStatFieldsValue()
+      this.loadData()
     }
   }
 

--
Gitblit v1.8.0