Browse Source

uas手机版
生成通用表单工具类
iconfont

RaoMeng 5 years ago
parent
commit
4e9dd465fc

+ 1 - 0
uas-office-web/uas-mobile/package.json

@@ -3,6 +3,7 @@
   "version": "0.1.0",
   "private": true,
   "dependencies": {
+    "@ant-design/icons": "^4.2.2",
     "@antv/data-set": "^0.11.1",
     "antd": "^4.7.2",
     "antd-mobile": "^2.3.4",

+ 11 - 0
uas-office-web/uas-mobile/src/configs/iconfont.conig.js

@@ -0,0 +1,11 @@
+import { createFromIconfontCN } from '@ant-design/icons'
+
+/**
+ * Created by RaoMeng on 2020/11/5
+ * Desc: iconfont图标库
+ */
+const UasIcon = createFromIconfontCN({
+  scriptUrl: '//at.alicdn.com/t/font_1650368_rupnaa7p26o.js',
+})
+
+export default UasIcon

+ 43 - 1
uas-office-web/uas-mobile/src/utils/fetchRequest.js

@@ -1,6 +1,13 @@
 import { getStrValue, isObjNull } from './common'
 
-export function fetchPost (url, params, header) {
+/**
+ * 参数为FormData
+ * @param url
+ * @param params
+ * @param header
+ * @returns {Promise<never>|*}
+ */
+export function fetchPostForm (url, params, header) {
   if (window.navigator.onLine == false) {
     return Promise.reject('网络连接失败,请检查网络连接')
   }
@@ -37,6 +44,13 @@ export function fetchPost (url, params, header) {
   return fetchResult(request)
 }
 
+/**
+ * 参数为json对象
+ * @param url
+ * @param params
+ * @param header
+ * @returns {Promise<never>|*}
+ */
 export function fetchPostObj (url, params, header) {
   if (window.navigator.onLine == false) {
     return Promise.reject('网络连接失败,请检查网络连接')
@@ -65,6 +79,13 @@ export function fetchPostObj (url, params, header) {
   return fetchResult(request)
 }
 
+/**
+ * get请求
+ * @param url
+ * @param params
+ * @param header
+ * @returns {Promise<never>|*}
+ */
 export function fetchGet (url, params, header) {
   if (window.navigator.onLine == false) {
     return Promise.reject('网络连接失败,请检查网络连接')
@@ -148,6 +169,27 @@ function fetchResult (request) {
           throw result
         }
       }
+    }).then(result => {
+      if (result.hasOwnProperty('success') && result.success === false) {
+        if (result.exceptionInfo) {
+          if (result.exceptionInfo.length > 80) {
+            throw '接口请求异常'
+          } else {
+            throw result.exceptionInfo
+          }
+        } else if (result.message) {
+          if (result.message.length > 80) {
+            throw '接口请求异常'
+          } else {
+            throw result.message
+          }
+        } else {
+          throw result
+        }
+      } else {
+        return result
+
+      }
     })
   } catch (e) {
     return Promise.reject('请求异常')

+ 396 - 0
uas-office-web/uas-mobile/src/utils/form.util.js

@@ -0,0 +1,396 @@
+import React from 'react'
+import {
+  isObjEmpty,
+  isObjNull,
+} from '../utils/common'
+import FormInput from '../components/common/formNew/FormInput'
+import FormAdd from '../components/common/formNew/FormAdd'
+import { message } from 'antd'
+import BillModel, {
+  TYPE_ADD,
+  TYPE_TAB,
+  TYPE_TITLE,
+} from '../model/common/BillModel'
+import FormTitle from '../components/common/formNew/FormTitle'
+import BillGroupModel from '../model/common/BillGroupModel'
+
+/**
+ * Created by RaoMeng on 2020/11/6
+ * Desc: 通用表单生成组件工具类
+ */
+
+let mBaseUrl = window.location.origin
+  + (process.env.REACT_APP_ROUTER_BASE_NAME || '')
+
+/**
+ * 通过json数据获取表单组件列表
+ * @param billGroupModelList
+ * @returns {[]}
+ */
+export function getFormItems (billGroupModelList) {
+  let formItems = []
+  if (!isObjEmpty(billGroupModelList)) {
+    //从表的起始Groupindex
+    let gridStartIndex = -1
+    for (let g = 0; g < billGroupModelList.length; g++) {
+      let billGroup = billGroupModelList[g]
+      if (!isObjNull(billGroup)) {
+        let showBillFields = billGroup.showBillFields
+        if (!isObjNull(showBillFields) &&
+          showBillFields.length > 0) {
+          //添加组标题
+          gridStartIndex = addGroupTitle(billGroupModelList,
+            g, billGroup, gridStartIndex, formItems)
+          //添加显示字段
+          for (let i = 0; i < showBillFields.length; i++) {
+            let billModel = showBillFields[i]
+            if (!isObjNull(billModel)) {
+              let itemViewType = getItemViewType(billModel.type)
+              if (billModel.renderer == 'detailAttach') {
+                itemViewType = 2
+              }
+              switch (itemViewType) {
+                case 1:
+                  formItems.push(
+                    <FormInput billModel={billModel} groupIndex={g}
+                               childIndex={i}
+                               baseUrl={mBaseUrl}
+                               onTextChange={onTextChange.bind(this,
+                                 billGroupModelList)}
+                               onInputClick={onInputClick.bind(this,
+                                 billGroupModelList)}
+                    />,
+                  )
+                  break
+                case 2:
+                  //附件
+                  /*formItems.push(
+                    <FormEnclosure billModel={billModel} groupIndex={g}
+                                   baseUrl={mBaseUrl}
+                                   childIndex={i}/>,
+                  )*/
+                  break
+                default:
+                  break
+              }
+            }
+          }
+          if (billGroup.isForm == false && billGroup.lastInType == true) {
+            //如果是最后一个从表,则添加【新增】按钮
+            formItems.push(
+              <FormAdd billModel={getAddModel(g)} groupIndex={g}
+                       onAddClick={onAddClick.bind(this, billGroupModelList)}
+              />,
+            )
+          }
+        }
+      }
+    }
+  }
+  return formItems
+}
+
+/**
+ * 添加明细组
+ * @param groupIndex
+ */
+function onAddClick (billGroupModelList, groupIndex) {
+  billGroupModelList[groupIndex].lastInType = false
+
+  let newBillGroup = newGridBillGroup(groupIndex,
+    billGroupModelList[groupIndex])
+  billGroupModelList.push(newBillGroup)
+}
+
+function newGridBillGroup (groupIndex, oldBillGroup) {
+  let oldGridIndex = oldBillGroup.gridIndex
+  let isForm = oldBillGroup.isForm
+
+  let newBillGroup = new BillGroupModel()
+  newBillGroup.isForm = isForm
+  if (isForm == true) {
+    newBillGroup.group = oldBillGroup.group
+  } else {
+    newBillGroup.group = `明细${oldGridIndex + 1}`
+  }
+  newBillGroup.billCaller = oldBillGroup.billCaller
+  newBillGroup.gridIndex = oldGridIndex + 1
+  newBillGroup.isDeleteAble = true
+  newBillGroup.lastInType = true
+  newBillGroup.groupIndex = groupIndex + 1
+
+  let showBillFields = oldBillGroup.showBillFields
+  let hideBillFields = oldBillGroup.hideBillFields
+
+  if (!isObjEmpty(showBillFields)) {
+    for (let i = 0; i < showBillFields.length; i++) {
+      let billModel = showBillFields[i]
+      newBillGroup.addShow(new BillModel(billModel))
+    }
+  }
+  if (!isObjEmpty(hideBillFields)) {
+    for (let i = 0; i < hideBillFields.length; i++) {
+      let billModel = hideBillFields[i]
+      newBillGroup.addHide(new BillModel(billModel))
+    }
+  }
+  return newBillGroup
+}
+
+function getAddModel (index) {
+  let addBillModel = new BillModel()
+  addBillModel.groupIndex = index
+  addBillModel.type = TYPE_ADD
+  addBillModel.caption = '添加单据'
+  return addBillModel
+}
+
+function getItemViewType (dfType) {
+  if (isObjEmpty(dfType)) {
+    return -1
+  }
+  switch (dfType.toUpperCase()) {
+    case TYPE_TITLE:
+      return 0
+    case TYPE_ADD:
+      return 110
+    case TYPE_TAB:
+      return 111
+    case 'C':
+    case 'SF':
+    case 'DF':
+    case 'S':
+    case 'SS':
+      return 1
+    case 'FF':
+      return 2
+    default:
+      return 1
+  }
+}
+
+/**
+ * 添加分组头部
+ * @param g
+ * @param billGroup
+ * @param gridStartIndex
+ * @param formItems
+ */
+function addGroupTitle (
+  billGroupModelList, g, billGroup, gridStartIndex, formItems) {
+  let titleBillModel = new BillModel()
+  titleBillModel.groupIndex = g
+  titleBillModel.type = TYPE_TITLE
+  if (billGroup.isForm == true) {
+    titleBillModel.caption = billGroup.group
+    titleBillModel.allowBlank = 'F'
+
+  } else {
+    if (gridStartIndex === -1) {
+      gridStartIndex = g
+    }
+    titleBillModel.caption = `明细${g - gridStartIndex + 1}`
+    titleBillModel.allowBlank = billGroup.isDeleteAble == true
+      ? 'T'
+      : 'F'
+  }
+  if (!isObjEmpty(titleBillModel.caption)) {
+    formItems.push(
+      <FormTitle billModel={titleBillModel} groupIndex={g}
+                 onDeleteClick={onDeleteClick.bind(this, billGroupModelList)}
+      />,
+    )
+  }
+  return gridStartIndex
+}
+
+function onDeleteClick (billGroupModelList, groupIndex) {
+  this.deleteAlert = alert('提示', '确认删除该明细?', [
+    {
+      text: '取消',
+      onPress: () => {},
+      style: 'default',
+    },
+    {
+      text: '确定', onPress: () => {
+        deleteGroup(billGroupModelList, groupIndex)
+      },
+    },
+  ])
+}
+
+/**
+ * 删除分组
+ * @param groupIndex
+ */
+function deleteGroup (billGroupModelList, groupIndex) {
+  let deleteGroup = billGroupModelList[groupIndex]
+  let isLastGroup = deleteGroup.lastInType
+  if (isLastGroup == true && groupIndex - 1 >= 0) {
+    let billGroup = billGroupModelList[groupIndex - 1]
+    if (deleteGroup.isForm == false && (billGroup.isForm == true)) {
+      //明细删除光了,要默认添加一个空明细表
+      deleteGroup.groupIndex = groupIndex - 1
+      deleteGroup.gridIndex = 0
+      let newGridGroup = newGridBillGroup(groupIndex - 1, deleteGroup)
+      newGridGroup.isDeleteAble = false
+      billGroupModelList.splice(groupIndex, 1)
+      billGroupModelList.push(newGridGroup)
+    } else {
+      billGroup.lastInType = true
+      billGroupModelList.splice(groupIndex, 1)
+    }
+  } else {
+    billGroupModelList.splice(groupIndex, 1)
+  }
+}
+
+function onTextChange (
+  billGroupModelList, groupIndex, childIndex, value, display) {
+  if (!isObjNull(billGroupModelList) &&
+    !isObjNull(billGroupModelList[groupIndex])) {
+    let billGroup = billGroupModelList[groupIndex]
+    if (!isObjNull(billGroup.showBillFields) &&
+      !isObjNull(billGroup.showBillFields[childIndex])) {
+      billGroup.showBillFields[childIndex].value = value
+      if (!isObjNull(display)) {
+        billGroup.showBillFields[childIndex].display = display
+      }
+    }
+  }
+}
+
+function onInputClick (billGroupModelList, groupIndex, childIndex) {
+  /*if (!isObjNull(billGroupModelList) &&
+    !isObjNull(billGroupModelList[groupIndex])) {
+    let billGroup = billGroupModelList[groupIndex]
+    if (!isObjNull(billGroup.showBillFields) &&
+      !isObjNull(billGroup.showBillFields[childIndex])) {
+      let billModel = billGroup.showBillFields[childIndex]
+      if (isObjNull(billModel)) {
+        return
+      }
+      billModel.childIndex = childIndex
+      this.setState({
+        selectModel: billModel,
+      })
+      Toast.loading('数据请求中', 0)
+      let type = billModel.type
+      if (type === 'C') {
+        //单项选择
+        this.getComboValue(billModel)
+      } else if (type === 'SF' || type === 'DF') {
+        //DBFind选择
+        this.getDbfindList(billModel, billGroup)
+      } else if (type === 'MF') {
+        //多项选择
+
+      }
+    }
+  }*/
+}
+
+export function getFormAndGrid (billGroupModelList) {
+  if (!isObjEmpty(billGroupModelList)) {
+    let formFields = [], gridGroupFields = []
+    for (let i = 0; i < billGroupModelList.length; i++) {
+      let billGroup = billGroupModelList[i]
+      if (isObjNull(billGroup)) {
+        continue
+      }
+      if (billGroup.isForm) {
+        if (!isObjEmpty(billGroup.showBillFields)) {
+          formFields = formFields.concat(billGroup.showBillFields)
+        }
+        if (!isObjEmpty(billGroup.hideBillFields)) {
+          formFields = formFields.concat(billGroup.hideBillFields)
+        }
+      } else {
+        let gridFields = []
+        if (!isObjEmpty(billGroup.showBillFields)) {
+          gridFields = gridFields.concat(billGroup.showBillFields)
+        }
+        if (!isObjEmpty(billGroup.hideBillFields)) {
+          gridFields = gridFields.concat(billGroup.hideBillFields)
+        }
+        gridGroupFields.push(gridFields)
+      }
+    }
+    let formStore = analysisForm(formFields)
+    let gridStoreList = analysisGrid(gridGroupFields)
+
+    if (isObjNull(formStore) || isObjNull(gridStoreList)) {
+      return null
+    } else {
+      return {
+        form: formStore,
+        grid: gridStoreList,
+      }
+    }
+  }
+}
+
+/**
+ * 判断主表字段必填情况,拼接主表字段
+ * @param formFields
+ * @returns {null}
+ */
+function analysisForm (formFields) {
+  if (!isObjNull(formFields)) {
+    let formStore = {}
+    for (let i = 0; i < formFields.length; i++) {
+      let billModel = formFields[i]
+      if (isObjNull(billModel)) {
+        continue
+      }
+      if (isObjEmpty(getValue(billModel)) &&
+        (billModel.allowBlank === 'necessaryField'
+          || billModel.allowBlank === 'F')) {
+        message.error(`${billModel.caption}为必填项`)
+        return null
+      }
+      //Todo 附件上传
+
+      formStore[billModel.field] = getDisplay(billModel)
+    }
+    return formStore
+  } else {
+    return null
+  }
+}
+
+function analysisGrid (gridBillMap) {
+  let gridStoreList = []
+  if (!isObjNull(gridBillMap)) {
+    for (let i = 0; i < gridBillMap.length; i++) {
+      let gridFields = gridBillMap[i]
+      if (!isObjEmpty(gridFields)) {
+        let gridStore = {}
+        for (let j = 0; j < gridFields.length; j++) {
+          let billModel = gridFields[j]
+          if (isObjEmpty(getValue(billModel))
+            && (billModel.allowBlank === 'necessaryField'
+              || billModel.allowBlank === 'F')) {
+            message.error(`${billModel.caption}为必填项`)
+            return null
+          }
+          //Todo 附件上传
+
+          gridStore[billModel.field] = getDisplay(billModel)
+        }
+        gridStoreList.push(gridStore)
+      }
+    }
+  }
+  return gridStoreList
+}
+
+function getValue (billModel) {
+  return billModel.value || billModel.defValue || ''
+}
+
+function getDisplay (billModel) {
+  return billModel.display || getValue(billModel)
+}
+
+

+ 41 - 0
uas-office-web/uas-mobile/src/utils/regexp.util.js

@@ -0,0 +1,41 @@
+/**
+ * Created by RaoMeng on 2020/11/6
+ * Desc: 常用的正则规则
+ */
+
+export const regExpConfig = {
+  money: /(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,2}$)/,
+  IDcard: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/, // 身份证
+  mobile: /^1([3|4|5|6|7|8|9|])\d{9}$/, // 手机号码
+  telephone: /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/, // 固定电话
+  num: /^[0-9]*$/, // 数字
+  phoneNo: /(^1([3|4|5|6|7|8|9|])\d{9}$)|(^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$)/, // 电话或者手机
+  policeNo: /^[0-9A-Za-z]{4,10}$/, // 账号4-10位数字或字母组成
+  pwd: /^[0-9A-Za-z]{6,16}$/, // 密码由6-16位数字或者字母组成
+  isNumAlpha: /^[0-9A-Za-z]*$/, // 字母或数字
+  isAlpha: /^[a-zA-Z]*$/, // 是否字母
+  isNumAlphaCn: /^[0-9a-zA-Z\u4E00-\uFA29]*$/, // 是否数字或字母或汉字
+  isPostCode: /^[\d-]*$/i, // 是否邮编
+  isNumAlphaUline: /^[0-9a-zA-Z_]*$/, // 是否数字、字母或下划线
+  isNumAndThanZero: /^([1-9]\d*(\.\d+)?|0)$/, // 是否为整数且大于0/^[1-9]\d*(\.\d+)?$/
+  isNormalEncode: /^(\w||[\u4e00-\u9fa5]){0,}$/, // 是否为非特殊字符(包括数字字母下划线中文)
+  isTableName: /^[a-zA-Z][A-Za-z0-9#$_-]{0,29}$/, // 表名
+  isInt: /^-?\d+$/, // 整数
+  isTableOtherName: /^[\u4e00-\u9fa5]{0,20}$/, // 别名
+  // isText_30: /^(\W|\w{1,2}){0,15}$/, // 正则
+  // isText_20: /^(\W|\w{1,2}){0,10}$/, // 正则
+  isText_30: /^(\W|\w{1}){0,30}$/, // 匹配30个字符,字符可以使字母、数字、下划线、非字母,一个汉字算1个字符
+  isText_50: /^(\W|\w{1}){0,50}$/, // 匹配50个字符,字符可以使字母、数字、下划线、非字母,一个汉字算1个字符
+  isText_20: /^(\W|\w{1}){0,20}$/, // 匹配20个字符,字符可以使字母、数字、下划线、非字母,一个汉字算1个字符
+  isText_100: /^(\W|\w{1}){0,100}$/, // 匹配100个字符,字符可以使字母、数字、下划线、非字母,一个汉字算1个字符
+  isText_250: /^(\W|\w{1}){0,250}$/, // 匹配250个字符,字符可以使字母、数字、下划线、非字母,一个汉字算1个字符
+  isNotChina: /^[^\u4e00-\u9fa5]{0,}$/, // 不为中文  IDcard: /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/, // 身份证
+  IDcardAndAdmin: /^(([1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X))|(admin))$/, // 身份证或者是admin账号
+  IDcardTrim: /^\s*(([1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3})|([1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X))|(admin))\s*$/, // 身份证
+  num1: /^[1-9]*$/, // 数字
+  companyNO: /^qqb_[0-9a-zA-Z_]{1,}$/, // 公司人员账号
+  imgType: /image\/(png|jpg|jpeg|gif)$/, // 上传图片类型
+  isChina: /^[\u4e00-\u9fa5]{2,8}$/,
+  isNozeroNumber: /^\+?[1-9]\d*$/, // 大于零的正整数
+  float: /^\d+(\.?|(\.\d+)?)$/, // 匹配正整数或者小数 或者0.这个特殊值
+}