index.js 4.33 KB
Newer Older
1 2
import React, { useState, useEffect, useMemo, useContext } from 'react'
import { Input, message, ConfigProvider } from 'antd'
田翔's avatar
田翔 committed
3
import Icon, { PlusOutlined } from '@ant-design/icons'
田翔's avatar
田翔 committed
4
import * as icons from '@ant-design/icons'
5
import { GetFieldValueFromTable, getUserName, getGroupName } from '../../../../apis/process'
田翔's avatar
田翔 committed
6
import styles from './index.less'
田翔's avatar
田翔 committed
7

田翔's avatar
田翔 committed
8
const iconList = Object.keys(icons).filter((item) => typeof icons[item] === 'object')
9 10 11 12 13 14
const initUserInfo = {
  fullName: '【本人姓名】',
  Phone: '【本人电话】',
  depart: { name: '【本人部门】' }
}
const loaclPaths = ['【本人姓名】', '【本人电话】', '【本人部门】']
田翔's avatar
田翔 committed
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29
const debounce = (fn) => {
  let t = null
  return function (e) {
    const context = this
    const args = arguments
    if (t) {
      clearTimeout(t)
    }
    t = setTimeout(function () {
      fn.call(context, args)
    }, 200)
  }
}

田翔's avatar
田翔 committed
30
const TextInput = (props) => {
田翔's avatar
田翔 committed
31

32
  const preview = sessionStorage.getItem('FormRender')
33
  const userInfo = preview === 'preview' ? initUserInfo : window?.globalConfig?.userInfo || initUserInfo
田翔's avatar
田翔 committed
34
  const { value, onChange, schema, addons } = props
田翔's avatar
田翔 committed
35
  const [showValue, setShowValue] = useState(value)
36
  const { title, disabled, placeholder, presetValue, addonBefore, addonAfter, maxLength, rules, uniqueVerify, tableName, fieldName, isStoreID, textDefalut } = schema
田翔's avatar
田翔 committed
37 38

  const handleChange = (e) => {
田翔's avatar
田翔 committed
39 40
    if (addons) {
      onChange(e.target.value)
41
      setShowValue(e.target.value)
田翔's avatar
田翔 committed
42
    }
田翔's avatar
田翔 committed
43 44
  }

45 46 47 48 49
  //仅仅支持本人姓名
  const getUserInfo = async (userID) => {
    const { code, data } = await getUserName(userID)
    if (code === 0) {
      if (Array.isArray(data) && data.length > 0) {
田翔's avatar
田翔 committed
50
        setShowValue(data[0])
田翔's avatar
田翔 committed
51 52
      }
    }
53
  }
田翔's avatar
田翔 committed
54

55 56 57 58
  const getDeptInfo = async (groupIds) => {
    const { code, data } = await getGroupName(groupIds)
    if (code === 0) {
      if (Array.isArray(data) && data.length > 0) {
田翔's avatar
田翔 committed
59
        setShowValue(data[0])
60 61 62 63
      }
    }
  }

64
  useEffect(() => {
65
    let valueNext = presetValue
田翔's avatar
田翔 committed
66
    if (preview !== 'preview') {
67
      if (!presetValue || loaclPaths.includes(presetValue)) {
68
        if (textDefalut === '【本人姓名】') {
田翔's avatar
田翔 committed
69
          if (isStoreID) {
70
            valueNext = userInfo.OID + ''
田翔's avatar
田翔 committed
71
          } else {
72
            valueNext = userInfo.fullName
田翔's avatar
田翔 committed
73
          }
74
        } else if (textDefalut === '【本人部门】') {
田翔's avatar
田翔 committed
75
          if (isStoreID) {
76
            valueNext = userInfo.depart.OID + ''
田翔's avatar
田翔 committed
77
          } else {
78
            valueNext = userInfo.depart.name
田翔's avatar
田翔 committed
79
          }
80
        } else if (textDefalut === '【本人电话】') {
81
          valueNext = userInfo.Phone
田翔's avatar
田翔 committed
82
        }
田翔's avatar
田翔 committed
83 84
      }
    }
85
    if (addons) {
86
      addons.setValue(addons.dataPath, valueNext || '')
87
    } else {
88
      onChange(valueNext || '')
89
    }
田翔's avatar
田翔 committed
90
    setShowValue(valueNext)
91 92 93 94 95 96
    if (isStoreID && valueNext) {
      if (textDefalut === '【本人姓名】') {
        getUserInfo(valueNext)
      } else if (textDefalut === '【本人部门】') {
        getDeptInfo(valueNext)
      }
97 98
    }
  }, [])
田翔's avatar
田翔 committed
99

100 101 102 103 104 105
  useEffect(() => {
    if (uniqueVerify === '表名/字段名' && tableName && fieldName) {
      if (addons) {
        let getValue = async (params) => {
          const [rule, value, callback] = params
          const { code, data, msg } = await GetFieldValueFromTable(tableName, fieldName, `${fieldName}=${value}`)
田翔's avatar
田翔 committed
106 107
          if (Array.isArray(data) && data.length) {
            callback(new Error(`${title}已重复,请重新输入`));
108 109 110 111 112 113 114 115 116 117 118 119 120
          } else {
            callback()
          }
        }
        let uniqueRules = [
          {
            validator: debounce(getValue)
          }
        ]
        addons.setSchemaByPath(addons.dataPath, { ...schema, rules: uniqueRules })
      }
    }
  }, [uniqueVerify, tableName, fieldName])
田翔's avatar
田翔 committed
121

田翔's avatar
田翔 committed
122
  return (
田翔's avatar
田翔 committed
123
    <div className={styles.textInput} isdisabled={JSON.stringify(disabled)}>
124
      <Input
125
        style={{ color: loaclPaths.includes(presetValue) ? 'blue' : '' }}
126
        disabled={disabled}
田翔's avatar
田翔 committed
127
        value={showValue}
128
        maxLength={maxLength}
田翔's avatar
田翔 committed
129
        placeholder={disabled ? (placeholder || '') : (placeholder || '请输入内容')}
130 131 132 133 134
        onChange={handleChange}
        addonBefore={iconList.includes(addonBefore) ? <Icon style={{ color: 'pink' }} component={icons[addonBefore]} /> : addonBefore}
        addonAfter={iconList.includes(addonAfter) ? <Icon component={icons[addonAfter]} /> : addonAfter}
      />
    </div>
田翔's avatar
田翔 committed
135 136 137 138 139
  )

}

export default TextInput