king
2025-05-08 400fee62fb40006a9839f1c3a8244b82566b5057
src/views/design/header/transmenu/index.jsx
@@ -1,21 +1,28 @@
import React, { Component } from 'react'
import { is, fromJS } from 'immutable'
import { Modal, notification, Button, Spin, Cascader } from 'antd'
// import { ClockCircleOutlined, SyncOutlined, WarningOutlined, CheckCircleOutlined } from '@ant-design/icons'
import { Modal, Radio, notification, Button, Spin, Select, Cascader } from 'antd'
import moment from 'moment'
import md5 from 'md5'
import Api from '@/api'
import Utils from '@/utils/utils.js'
import MenuUtils, { setLangTrans } from '@/utils/utils-custom.js'
import './index.scss'
const { confirm } = Modal
class TransMenu extends Component {
  state = {
    visible: false,
    loading: false,
    lang: '',
    saveing: false,
    type: '',
    dicts: [],
    menus: [],
    values: []
    billMenus: [],
    tabMenus: [],
    values: '',
    images: '',
  }
  shouldComponentUpdate (nextProps, nextState) {
@@ -23,18 +30,17 @@
  }
  trigger = () => {
    let langs = sessionStorage.getItem('subLangList')
    langs = JSON.parse(langs)
    this.setState({
      lang: langs[0],
      loading: true,
      visible: true,
      values: []
      saveing: false,
      type: 'menu',
      images: '',
      values: ''
    })
    
    this.getDicts()
    this.getMenus(langs[0])
    this.getMenus()
  }
  
  getDicts = () => {
@@ -67,12 +73,17 @@
    })
  }
  getMenus = (lang) => {
  getMenus = () => {
    if (this.state.menus.length > 0) {
      this.setState({loading: false})
      return
    }
    let _param = {
      func: 's_get_pc_menus',
      systemType: window.GLOB.sysType,
      pro_sys: '',
      lang: lang,
      lang: 'zh-CN',
      debug: 'Y'
    }
@@ -135,25 +146,374 @@
        menus.push(fstItem)
      })
      menus.push({
        value: 'home_page_id',
        label: '首页',
      })
      this.setState({menus: menus, loading: false})
    })
  }
  submit = () => {
    const { menulist } = this.props
    const { values } = this.state
    const { values, type } = this.state
    if (!values || values.length !== 3) {
    if (!values) {
      notification.warning({
        top: 92,
        message: '请选择三级菜单!',
        message: type === 'menu' ? '请选择菜单!' : '请选择模板!',
        duration: 5
      })
      return
    }
    console.log(values)
    console.log(menulist)
    if (type === 'bill') {
      this.billSubmit()
      return
    } else if (type === 'tab') {
      this.tabSubmit()
      return
    }
    if (values.length !== 3 && values[0] !== 'home_page_id') {
      notification.warning({
        top: 92,
        message: '请选择菜单!',
        duration: 5
      })
      return
    }
    this.setState({saveing: true})
    if (values[0] === 'home_page_id') {
      this.transThdMenu({
        value: 'home_page_id',
        MenuID: 'home_page_id',
        label: '首页'
      })
    } else {
      this.transFirstMenu(values[0], values[1], values[2])
    }
  }
  transFirstMenu = (firstId, secId, thdId) => {
    const { menulist } = this.props
    const { menus, dicts } = this.state
    let tail = md5(window.GLOB.appkey + sessionStorage.getItem('lang')).toLowerCase()
    tail = tail.slice(-8)
    let oriFirstMenu = menus.filter(item => item.value === firstId)[0]
    let oriSecMenu = oriFirstMenu.children.filter(item => item.value === secId)[0]
    let oriThdMenu = oriSecMenu.children.filter(item => item.value === thdId)[0]
    oriThdMenu = fromJS(oriThdMenu).toJS()
    let curFirstId = firstId
    if (curFirstId.length <= 24) {
      curFirstId = md5(window.GLOB.appkey + curFirstId + sessionStorage.getItem('lang')).toLowerCase()
    } else {
      curFirstId = curFirstId.slice(0, 24) + tail
    }
    let curSectId = secId
    if (curSectId.length <= 24) {
      curSectId = md5(window.GLOB.appkey + curSectId + sessionStorage.getItem('lang')).toLowerCase()
    } else {
      curSectId = curSectId.slice(0, 24) + tail
    }
    let curThdId = thdId
    if (curThdId.length <= 24) {
      curThdId = md5(window.GLOB.appkey + curThdId + sessionStorage.getItem('lang')).toLowerCase()
    } else {
      curThdId = curThdId.slice(0, 24) + tail
    }
    let firstName = oriFirstMenu.label
    let secName = oriSecMenu.label
    dicts.forEach(item => {
      if (item.type !== 'menu') return
      if (item.reg === firstName) {
        firstName = item.value
      }
      if (item.reg === secName) {
        secName = item.value
      }
      if (item.reg === oriThdMenu.label) {
        oriThdMenu.label = item.value
      }
    })
    let firstParam = {
      func: 'sPC_MainMenu_Add',
      MenuID: curFirstId,
      MenuName: firstName,
      PageParam: JSON.stringify({
        OpenType: 'menu',
        linkUrl: ''
      })
    }
    let secParam = {
      func: 'sPC_SndMenu_Add',
      ParentID: curFirstId,
      MenuID: curSectId,
      MenuName: secName,
      PageParam: JSON.stringify({
        Icon: ''
      })
    }
    oriThdMenu.fstMenuId = curFirstId
    oriThdMenu.parentId = curSectId
    oriThdMenu.MenuID = curThdId
    if (menulist.findIndex(item => item.MenuID === firstParam.MenuID) === -1) {
      Api.getCloudConfig(firstParam).then(res => {
        if (!res.status) {
          notification.warning({
            top: 92,
            message: res.message,
            duration: 5
          })
          this.setState({saveing: false})
          return
        }
        this.transSecondMenu(secParam, oriThdMenu, true)
      })
    } else {
      this.transSecondMenu(secParam, oriThdMenu)
    }
  }
  transSecondMenu = (secParam, oriThdMenu, reload) => {
    const { menulist } = this.props
    let creat = true
    menulist.forEach(item => {
      if (item.children.findIndex(cell => cell.MenuID === secParam.MenuID) > -1) {
        creat = false
      }
    })
    if (creat) {
      Api.getCloudConfig(secParam).then(res => {
        if (!res.status) {
          notification.warning({
            top: 92,
            message: res.message,
            duration: 5
          })
          this.setState({saveing: false})
          return
        }
        this.transThdMenu(oriThdMenu, true)
      })
    } else {
      this.transThdMenu(oriThdMenu, reload)
    }
  }
  transThdMenu = (menu, reload) => {
    const { menulist } = this.props
    const { dicts } = this.state
    let _param = {
      func: 'sPC_Get_LongParam',
      MenuID: menu.value,
      lang: 'zh-CN'
    }
    let creat = true
    if (menu.MenuID === 'home_page_id') {
      creat = false
    } else {
      menulist.forEach(item => {
        item.children.forEach(cell => {
          if (cell.children.findIndex(n => n.MenuID === menu.MenuID) > -1) {
            creat = false
          }
        })
      })
    }
    Api.getCloudConfig(_param).then(res => {
      if (!res.status) {
        notification.warning({
          top: 92,
          message: res.message,
          duration: 5
        })
        this.setState({saveing: false})
        return
      }
      let config = null
      if (res.LongParam) {
        try {
          config = JSON.parse(window.decodeURIComponent(window.atob(res.LongParam)))
        } catch (e) {
          console.warn('Parse Failure')
          config = ''
        }
      }
      if (!config) {
        notification.warning({
          top: 92,
          message: '未获取到菜单配置信息',
          duration: 5
        })
        this.setState({saveing: false})
        if (reload) {
          this.props.reload()
        }
        return
      }
      let btnDict = {}
      let titDict = {}
      let lisDict = {}
      let regs = []
      dicts.forEach(item => {
        if (item.type === 'button') {
          btnDict[item.reg] = item.value
        } else if (item.type === 'title') {
          titDict[item.reg] = item.value
        } else if (item.type === 'list') {
          lisDict[item.reg] = item.value
        } else if (item.type === 'text') {
          regs.push({reg: new RegExp(item.reg, 'g'), value: item.value, sort: item.reg.length})
        }
      })
      regs.sort((a, b) => b.sort - a.sort)
      let tail = md5(window.GLOB.appkey + sessionStorage.getItem('lang')).toLowerCase()
      tail = tail.slice(-8)
      setLangTrans(config, btnDict, titDict, lisDict, {}, regs, tail)
      if (config.components) {
        let commonId = Utils.getuuid()
        if (config.interfaces && config.interfaces.length > 0) {
          config.interfaces = config.interfaces.map(inter => {
            inter.uuid = md5(commonId + inter.uuid)
            return inter
          })
        }
        config.components = MenuUtils.resetConfig(config.components, commonId, false)
        config.tables = config.tables || []
        config.style = config.style || {}
      }
      delete config.tbkey
      config.enabled = false
      config.MenuName = menu.label
      config.fstMenuId = menu.fstMenuId || ''
      config.parentId = menu.parentId || ''
      config.uuid = menu.MenuID || ''
      config.open_edition = ''
      if (config.MenuNo) {
        config.MenuNo = config.MenuNo + '_' + sessionStorage.getItem('lang')
      }
      let param = {
        func: 'sPC_TrdMenu_AddUpt',
        FstID: config.fstMenuId,
        SndID: config.parentId,
        ParentID: config.parentId,
        MenuID: config.uuid,
        MenuNo: config.MenuNo || '',
        EasyCode: config.easyCode || '',
        Template: config.Template,
        MenuName: config.MenuName || '',
        PageParam: JSON.stringify({Template: config.Template, OpenType: config.OpenType || 'newtab'})
      }
      if (creat) {
        param.open_edition = config.open_edition
        param.LongParam = window.btoa(window.encodeURIComponent(JSON.stringify(config)))
        this.setMenu(param, true)
      } else {
        Api.getCloudConfig({
          func: 'sPC_Get_LongParam',
          MenuID: menu.MenuID
        }).then(res => {
          if (!res.status) {
            notification.warning({
              top: 92,
              message: res.message,
              duration: 5
            })
            this.setState({saveing: false})
            return
          }
          config.open_edition = res.open_edition || ''
          param.open_edition = config.open_edition
          param.LongParam = window.btoa(window.encodeURIComponent(JSON.stringify(config)))
          if (res.LongParam) {
            const that = this
            confirm({
              title: '菜单已存在,确定重新生成吗?',
              content: '',
              onOk() {
                that.setMenu(param, true)
              },
              onCancel() {}
            })
          } else {
            this.setMenu(param, true)
          }
        })
      }
    })
  }
  setMenu = (param, reload) => {
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    param.secretkey = Utils.encrypt('', param.timestamp)
    Api.getCloudConfig(param).then(res => {
      if (!res.status) {
        notification.warning({
          top: 92,
          message: res.message,
          duration: 5
        })
        this.setState({saveing: false})
        return
      }
      notification.success({
        top: 92,
        message: '添加成功。',
        duration: 5
      })
      this.setState({
        loading: false,
        visible: false
      })
      if (reload) {
        this.props.reload()
      }
    })
  }
  cancel = () => {
@@ -163,27 +523,357 @@
    })
  }
  onChange = (e) => {
    this.setState({
      type: e.target.value,
      images: '',
      values: ''
    })
    if (e.target.value === 'bill') {
      this.getBillMenus()
    } else if (e.target.value === 'tab') {
      this.getTabMenus()
    }
  }
  getBillMenus = () => {
    const { billMenus } = this.state
    if (billMenus.length) return
    let _param = {
      func: 's_get_sPrintTemplate_web',
      OrderCol: 'ID desc',
      lang_s: sessionStorage.getItem('lang'),
      dataM: 'Y',
      PageIndex: 1,
      PageSize: 1000
    }
    Api.getCloudConfig(_param).then(res => {
      if (!res.status) {
        notification.warning({
          top: 92,
          message: res.message,
          duration: 5
        })
        return
      }
      this.setState({billMenus: res.data || []})
    })
  }
  getTabMenus = () => {
    const { tabMenus } = this.state
    if (tabMenus.length) return
    let _param = {
      func: 's_get_sPrintTemplate',
      OrderCol: 'ID desc',
      lang_s: sessionStorage.getItem('lang'),
      dataM: 'Y',
      PageIndex: 1,
      PageSize: 1000
    }
    Api.getCloudConfig(_param).then(res => {
      if (!res.status) {
        notification.warning({
          top: 92,
          message: res.message,
          duration: 5
        })
        return
      }
      this.setState({tabMenus: res.data || []})
    })
  }
  billSubmit = () => {
    const { dicts, values, billMenus } = this.state
    let _param = {
      func: 'sPC_Get_LongParam',
      MenuID: values
    }
    this.setState({saveing: true})
    let temp = billMenus.filter(item => item.ID === values)[0]
    Api.getCloudConfig(_param).then(res => {
      if (!res.status) {
        notification.warning({
          top: 92,
          message: res.message,
          duration: 5
        })
        this.setState({saveing: false})
        return
      }
      let config = null
      if (res.LongParam) {
        try {
          config = JSON.parse(window.decodeURIComponent(window.atob(res.LongParam)))
        } catch (e) {
          console.warn('Parse Failure')
          config = ''
        }
      }
      if (!config) {
        notification.warning({
          top: 92,
          message: '未获取到模板配置信息',
          duration: 5
        })
        this.setState({saveing: false})
        return
      }
      let btnDict = {}
      let titDict = {}
      let lisDict = {}
      let regs = []
      dicts.forEach(item => {
        if (item.type === 'button') {
          btnDict[item.reg] = item.value
        } else if (item.type === 'title') {
          titDict[item.reg] = item.value
        } else if (item.type === 'list') {
          lisDict[item.reg] = item.value
        } else if (item.type === 'text') {
          regs.push({reg: new RegExp(item.reg, 'g'), value: item.value, sort: item.reg.length})
        }
      })
      regs.sort((a, b) => b.sort - a.sort)
      let tail = md5(window.GLOB.appkey + sessionStorage.getItem('lang')).toLowerCase()
      tail = tail.slice(-8)
      setLangTrans(config, btnDict, titDict, lisDict, {}, regs, tail)
      if (config.components) {
        let commonId = Utils.getuuid()
        if (config.interfaces && config.interfaces.length > 0) {
          config.interfaces = config.interfaces.map(inter => {
            inter.uuid = md5(commonId + inter.uuid)
            return inter
          })
        }
        config.components = MenuUtils.resetConfig(config.components, commonId, false)
        config.tables = config.tables || []
        config.style = config.style || {}
      }
      delete config.tbkey
      config.enabled = false
      config.MenuNo = temp.PrintTempNO || ''
      config.MenuName = temp.PrintTempName || ''
      config.fstMenuId = 'BillPrintTemp'
      config.parentId = 'BillPrintTemp'
      config.uuid = values
      config.MenuID = config.uuid
      config.open_edition = res.open_edition || ''
      let param = {
        func: 'sPC_TrdMenu_AddUpt',
        FstID: config.fstMenuId,
        SndID: config.parentId,
        ParentID: config.parentId,
        MenuID: config.uuid,
        MenuNo: config.MenuNo || '',
        EasyCode: config.easyCode || '',
        Template: config.Template,
        MenuName: config.MenuName || '',
        open_edition: config.open_edition,
        PageParam: JSON.stringify({Template: config.Template, OpenType: config.OpenType || 'newtab'})
      }
      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      param.secretkey = Utils.encrypt('', param.timestamp)
      Api.getCloudConfig(param).then(res => {
        if (!res.status) {
          notification.warning({
            top: 92,
            message: res.message,
            duration: 5
          })
          this.setState({saveing: false})
          return
        }
        notification.success({
          top: 92,
          message: '转换成功。',
          duration: 5
        })
        this.setState({
          saveing: false,
          visible: false
        })
      })
    })
  }
  tabSubmit = () => {
    const { values, dicts, tabMenus } = this.state
    let param = {
      func: 's_PrintTemplateMGetData',
      Type: 'Y',
      ID: values
    }
    this.setState({saveing: true})
    let temp = tabMenus.filter(item => item.ID === values)[0]
    Api.getCloudConfig(param).then(result => {
      if (!result.status) {
        notification.warning({
          top: 92,
          message: result.message,
          duration: 5
        })
        this.setState({saveing: false})
      } else if (!result.ConfigParam) {
        notification.warning({
          top: 92,
          message: '未获取到打印模板信息!',
          duration: 5
        })
        this.setState({saveing: false})
      } else {
        let config = ''
        try {
          config = JSON.parse(window.decodeURIComponent(window.atob(result.ConfigParam)))
        } catch (e) {
          config = ''
        }
        if (!config) {
          notification.warning({
            top: 92,
            message: '未获取到打印模板信息!',
            duration: 5
          })
          this.setState({saveing: false})
          return
        }
        let titDict = {}
        dicts.forEach(item => {
          if (item.type === 'title') {
            titDict[item.reg] = item.value
          }
        })
        config.elements.forEach(item => {
          if (item.type === 'text' && item.value && titDict[item.value]) {
            item.value = titDict[item.value]
          }
        })
        config.name = temp.PrintTempName || ''
        config.remark = temp.Remark || ''
        config.PrintTempNO = temp.PrintTempNO || ''
        let _param = {
          func: 's_PrintTemplateMSub',
          ID: values,
          ConfigParam: window.btoa(window.encodeURIComponent(JSON.stringify(config))),
          Images: temp.Images || '',
          PrintTempName: config.name,
          Remark: config.remark,
          PrintTempNO: config.PrintTempNO
        }
        Api.getCloudConfig(_param).then(result => {
          if (!result.status) {
            notification.warning({
              top: 92,
              message: result.message,
              duration: 5
            })
            this.setState({saveing: false})
            return
          }
          notification.success({
            top: 92,
            message: '转换成功。',
            duration: 5
          })
          this.setState({
            saveing: false,
            visible: false
          })
        })
      }
    })
  }
  render () {
    const { visible, loading, menus } = this.state
    const { visible, loading, saveing, menus, type, billMenus, tabMenus, images } = this.state
    return (
      <>
        <Button type="primary" onClick={this.trigger}>
          添加
          转换
        </Button>
        <Modal
          wrapClassName="sync-menu-modal"
          title="添加菜单"
          title="菜单转换"
          visible={visible}
          width={800}
          width={600}
          closable={false}
          maskClosable={false}
          onOk={this.submit}
          onCancel={this.cancel}
          confirmLoading={saveing}
          destroyOnClose
        >
          {loading ? <Spin /> : <div>
            <Cascader onChange={(val) => this.setState({values: val})} options={menus} expandTrigger="hover" placeholder="请选择模板菜单"/>
            <Radio.Group defaultValue="menu" onChange={this.onChange}>
              <Radio.Button value="menu">菜单</Radio.Button>
              <Radio.Button value="bill">单据打印模板</Radio.Button>
              <Radio.Button value="tab">标签打印模板</Radio.Button>
            </Radio.Group>
            {type === 'menu' ? <Cascader popupClassName="sync-cascader" onChange={(val) => this.setState({values: val})} options={menus} expandTrigger="hover" placeholder="请选择菜单"/> : null}
            {type === 'bill' ? <Select
              showSearch
              placeholder="请选择单据模板"
              dropdownMatchSelectWidth={false}
              onChange={(val, option) => this.setState({values: val, images: option.props.images || ''})}
              filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
            >
              {billMenus.map((item, i) => (<Select.Option key={i} images={item.Images || ''} value={item.ID}>{`${item.PrintTempName}(${item.PrintTempNO})`}</Select.Option>))}
            </Select> : null}
            {type === 'tab' ? <Select
              showSearch
              placeholder="请选择标签模板"
              dropdownMatchSelectWidth={false}
              onChange={(val, option) => this.setState({values: val, images: option.props.images || ''})}
              filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
            >
              {tabMenus.map((item, i) => (<Select.Option key={i} images={item.Images || ''} value={item.ID}>{`${item.PrintTempName}(${item.PrintTempNO})`}</Select.Option>))}
            </Select> : null}
            {images ? <img src={images} alt=""/> : null}
          </div>}
        </Modal>
      </>