| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658 |
- <template>
- <page-meta root-font-size="system" />
- <view class="process_container">
- <!-- 主表单 -->
- <uni-card spacing="0">
- <view class="main_container">
- <uni-forms ref="$mainForm" :modelValue="mainFormValue" :rules="$mainFormRules" label-position="left"
- :label-width="125" :border="true">
- <view v-for="(elem, index) in formElements" :key="index">
- <uni-forms-item v-if="'0' != elem.canEdit && '8' != elem.type" :name="elem.elementId" :label="elem.elementName">
- <!-- 关联变量输入框 -->
- <uni-easyinput v-if="undefined != elem.bindTimeRange" placeholder="" :value="calculateTimeDifference(elem, formElements)"></uni-easyinput>
- <!-- 金额转大写 -->
- <uni-easyinput v-else-if="elem.elementName.endsWith('大写')" placeholder="" :value="computedNumberToChineseCurrency(elem, formElements)"></uni-easyinput>
- <!-- 输入框 -->
- <uni-easyinput v-else-if="'0' == elem.type" :disabled="'0' == elem.canEdit" :type="fieldTypeDict[elem.fieldType] || 'text'"
- :placeholder="'0' == elem.canEdit ? '' : '请输入内容'"
- v-model="elem.defaultValue"></uni-easyinput>
- <!-- 富文本输入框 -->
- <uni-easyinput v-else-if="'1' == elem.type" :disabled="'0' == elem.canEdit" :placeholder="'0' == elem.canEdit ? '' : '请输入内容'" v-model="elem.defaultValue"
- type="textarea"></uni-easyinput>
- <!-- 下拉框 -->
- <picker class="picker_container" v-else-if="'2' == elem.type"
- @change="bindPickerChange($event, elem)" :value="elem.defaultValue" :range="formatDict(elem.typeDetail.enum)">
- <view class="uni-input input_text">
- <!-- 设置默认值为第一个选项 -->
- {{ elem.defaultValue ? elem.defaultValue : elem.defaultValue = elem.typeDetail.enum[0].enumVname }}
- </view>
- </picker>
- <!-- 开始时间选择器 -->
- <uni-datetime-picker :end="formElements[elem.endElemIndex].defaultValue" @change="setTimeRange(elem)"
- v-else-if="'9' == elem.type && undefined != elem.endElemIndex" v-model="elem.defaultValue" :clear-icon="false" type="datetime" />
- <!-- 结束时间选择器 -->
- <uni-datetime-picker :start="formElements[elem.startElemIndex].defaultValue" @change="setTimeRange(elem)"
- v-else-if="'9' == elem.type && undefined != elem.startElemIndex" v-model="elem.defaultValue" :clear-icon="false" type="datetime" />
- <!-- 其他时间选择器 -->
- <!-- 年月日 时分秒 -->
- <uni-datetime-picker v-else-if="'9' == elem.type" v-model="elem.defaultValue" type="datetime" />
- <!-- 年月日 -->
- <uni-datetime-picker v-else-if="'3' == elem.type" v-model="elem.defaultValue" type="date" />
- </uni-forms-item>
- </view>
- </uni-forms>
- </view>
- </uni-card>
- <!-- 重复表单 -->
- <view v-if="repeatingForm.elementItem.length > 0" class="repeating_table">
- <uni-card v-for="(table, tableIndex) in repeatingForm.elements" spacing="0" :key="tableIndex">
- <uni-forms :ref="(el) => $repeatingForms[tableIndex] = el" :modelValue="repeatingFormsValue[tableIndex]"
- :rules="$repFormRules" label-position="left" :label-width="125" :border="true">
- <uni-forms-item :name="elem.tableField" v-for="(elem, itemIndex) in table"
- :label="repeatingForm.elementItem[itemIndex].elementName.slice(3)" :key="itemIndex">
- <!-- 自定义关联变量 -->
- <uni-easyinput v-if="repeatingForm.elementItem[itemIndex].bddzText" :placeholder="''" :value="calculateRepeatingFormExpression(elem, table)"></uni-easyinput>
- <uni-easyinput v-else placeholder="请输入内容" v-model="elem.defaultValue"
- :type="fieldTypeDict[repeatingForm.elementItem[itemIndex].fieldType.value] || 'text'"></uni-easyinput>
- </uni-forms-item>
- </uni-forms>
- <view class="repeating_table_button_container">
- <uni-row>
- <uni-col :span="10" :offset="1">
- <button @click="addRepeatingFormItem(tableIndex)" type="primary">新增</button>
- </uni-col>
- <uni-col :span="10" :offset="2">
- <button @click="delRepeatingFormItem(tableIndex)"
- :disabled="repeatingForm.elements.length <= 1" type="warn">删除</button>
- </uni-col>
- </uni-row>
- </view>
- </uni-card>
- </view>
- <!-- 上传附件 -->
- <view class="file_picker_container">
- <uni-card title="上传附件" :extra="`${fileList.length}/15`" spacing="0">
- <uni-file-picker ref="filePicker" v-model="fileList" :auto-upload="true" mode="list" limit="10"
- file-mediatype="all" @select="handleFileSelect" @progress="handleFileProgress"
- @success="handleFileSuccess" @fail="handleFileFail" @delete="handleFileDelete" />
- </uni-card>
- </view>
- <view class="submit_button_container">
- <uni-card spacing="0" padding="0">
- <button :disabled="!button_state" :loading="!button_state" type="primary" class="submit_button"
- @click="submitProcess">提交</button>
- </uni-card>
- </view>
- </view>
- <view style="height: 5px; margin-top: 10px;"></view>
- </template>
- <script setup lang="ts">
- import { onMounted, reactive, ref, computed } from 'vue'
- import { onLoad } from '@dcloudio/uni-app'
- import { useUserStore } from '@/store/user.js'
- import $modal from '@/plugins/modal.js'
- import $tab from '@/plugins/tab.js'
- import { convertToChineseCurrency } from '@/utils/ygoa.js'
- import { calCommonExp } from '@/utils/rpn.js'
- import { getProcessInfo, getProcessForm, submitProcessForm, uploadFile } from '@/api/work.js'
- const fieldTypeDict = {
- '0': 'text',
- '1': 'number'
- }
- let processInfo = reactive({
- modelName: '流程申请',
- reqOffice: 0,
- control: '',
- formId: '',
- modelId: '',
- tmodelId: '',
- isMoreIns: '',
- pathJudgeType: '',
- form: []
- })
- const userStore = useUserStore()
- const title = ref('')
- onLoad((options) => {
- const { modelName, modelId, control } = options
- processInfo.modelName = modelName
- processInfo.modelId = modelId
- processInfo.control = control
- title.value = userStore.user.name + '的' + processInfo.modelName
- // 设置导航栏标题
- uni.setNavigationBarTitle({
- title: title.value
- });
- })
- onMounted(() => {
- initProcessInfo().then(() => {
- initProcessForm()
- })
- })
- function initProcessInfo() {
- return new Promise<void>((resolve, reject) => {
- getProcessInfo(processInfo)
- .then(({ returnParams }) => {
- const { formId, tmodelId, isMoreIns, pathJudgeType, reqOffice } = returnParams.flow[0]
- processInfo.formId = formId
- processInfo.tmodelId = tmodelId
- processInfo.isMoreIns = isMoreIns
- processInfo.pathJudgeType = pathJudgeType
- processInfo.reqOffice = reqOffice
- resolve()
- })
- })
- }
- const formElements = ref([])
- const repeatingForm = ref({
- elementItem: [],
- elements: []
- })
- function initProcessForm() {
- getProcessForm(userStore.user, processInfo).then(({ returnParams }) => {
- formElements.value = returnParams.formElements
- repeatingForm.value = returnParams.repeatingForm
- getValidateRules()
- // 生成第一个重复表数据
- if (repeatingForm.value) {
- addRepeatingFormItem(0)
- }
- bindTimeRangeData()
- })
- }
- const timeRangeItems = ref([
- ['开始时间', '结束时间', '多少小时'],
- ['出发时间', '预计返回时间'],
- ['出差时间', '返回时间'],
- ['出门时间', '预计返回时间'],
- ['开始时间', '结束时间', '合计小时'],
- ])
- // 关联时间变量
- function bindTimeRangeData() {
- return new Promise<void>((resolve) => {
- timeRangeItems.value.forEach((range) => {
- const [startName, endName, bindName] = range;
- // 找到 startName 和 endName 的索引
- const startIndex = formElements.value.findIndex((item) => item.elementName === startName);
- const endIndex = formElements.value.findIndex((item) => item.elementName === endName);
- // 只有找到 startName 和 endName 后,才检查 bindName
- if (startIndex !== -1 && endIndex !== -1) {
- if (bindName) {
- const bindIndex = formElements.value.findIndex((item) => item.elementName === bindName);
- if (bindIndex !== -1) {
- // 所有匹配项都存在,保存索引
- formElements.value[startIndex].endElemIndex = endIndex;
- formElements.value[endIndex].startElemIndex = startIndex;
- formElements.value[bindIndex].bindTimeRange = {
- startIndex,
- endIndex
- };
- }
- } else {
- // 没有 bindName,仅保存 start 和 end 的索引
- formElements.value[startIndex].endElemIndex = endIndex;
- formElements.value[endIndex].startElemIndex = startIndex;
- }
- // TODO 加班开始时间默认当天下班时间 请假时间默认当天上班时间
- }
- });
- resolve(); // 返回一个 resolved 状态的 Promise
- });
- }
- // 计算时间差
- function calculateTimeDifference(item, table) {
- // 获取 开始时间 和 结束时间
- const { startIndex, endIndex } = item.bindTimeRange;
- const startTime = new Date(table[startIndex].defaultValue);
- const endTime = new Date(table[endIndex].defaultValue);
- // 检查时间是否合法
- if (isNaN(startTime.getTime()) || isNaN(endTime.getTime())) {
- return item.defaultValue = 0
- }
- // 计算时间差
- const timeDifferenceInMs = endTime - startTime;
- let timeDifferenceInHours = (timeDifferenceInMs / (1000 * 60 * 60)).toFixed(1);
- // let timeDifferenceInHours = (timeDifferenceInMs / (1000 * 60 * 60))
- // // 计算小数部分
- // const decimalPart = timeDifferenceInHours - Math.floor(timeDifferenceInHours)
- // if (decimalPart >= 0.5) {
- // // 如果小数部分大于等于 0.5,向上取整到 0.5 的倍数
- // timeDifferenceInHours = Math.floor(timeDifferenceInHours) + 0.5;
- // } else {
- // // 如果小数部分小于 0.5,向下取整到 0.5 的倍数
- // timeDifferenceInHours = Math.floor(timeDifferenceInHours);
- // }
- // 保存到 defaultValue
- return item.defaultValue = timeDifferenceInHours;
- }
- function setTimeRange(e) {
- // console.log('setTimeRange', e)
- }
- // 生成人民币大写
- function computedNumberToChineseCurrency(item, table) {
- const elem = table.find(elem => elem.elementName == item.BddzText.slice(3))
- return item.defaultValue = convertToChineseCurrency(elem.defaultValue)
- }
- // 下拉框
- function bindPickerChange(e, item) {
- const index = e.detail.value;
- item.defaultValue = item.typeDetail.enum[index].enumVname
- }
- function formatDict(dict) {
- return dict.map(({ enumVname }) => enumVname)
- }
- // 新增重复表表单
- function addRepeatingFormItem(index) {
- const table = repeatingForm.value.elementItem.map(({ tableField, bddzText }) => {
- const item = {
- name: tableField,
- defaultValue: '',
- bddzText
- }
- return item
- })
- repeatingForm.value.elements.splice(index + 1, 0, table)
- }
- // 删除重复表表单
- function delRepeatingFormItem(index) {
- $modal.confirm('', '确认删除该重复表数据')
- .then(() => {
- repeatingForm.value.elements.splice(index, 1)
- $repeatingForms.value.splice(index, 1)
- })
- .catch(() => { })
- }
- // 计算重复表关联变量表达式结果值
- function calculateRepeatingFormExpression(item, table) {
- // 提取表达式中的变量名
- const variablePattern = /my:[\u4e00-\u9fa5]+/g;
- let match;
- let expression = item.bddzText
- // 替换表达式中的变量
- while ((match = variablePattern.exec(item.bddzText)) !== null) {
- const variableName = match[0]; // 完整变量名
- // 找到 重复表 中对应的索引
- const index = repeatingForm.value.elementItem.findIndex(item => item.elementName === variableName);
- if (index !== -1) {
- const value = table[index]?.defaultValue || 0;
- expression = expression.replace(match[0], value);
- } else {
- // 未匹配到的变量替换为 0
- expression = expression.replace(match[0], 0);
- }
- }
- if (/[^0-9\+\-\*\/\(\)\.]/.test(expression)) {
- console.error('错误的表达式:', expression);
- return expression;
- }
- return item.defaultValue = calCommonExp(expression); // 返回填充后的表达式
- }
- const fileList = ref([]) // 文件列表
- const fileSeqs = ref([])
- async function handleFileSelect(files) { // 新增文件
- // console.log('handleFileSelect', files.tempFiles)
- files.tempFiles.forEach(file => {
- const data = {
- name: file.name,
- filePath: file.path,
- }
- uploadFile(data)
- .then(res => {
- file.seq = res.returnParams
- fileSeqs.value.push({ 'seq': res.returnParams, 'path': file.path })
- fileList.value.push(file)
- // console.log('handleFileSelect fileList: ',fileList);
- // file.seq = res.returnParams
- })
- .catch(err => {
- $modal.msgError('文件' + data.name + '上传失败,请删除重新上传')
- switch (err) {
- case -201:
- console.log('文件上传失败 未找到该文件');
- break;
- case -20201:
- console.log('文件上传失败 返回值不是JSON字符串');
- break;
- }
- })
- })
- // console.log('UploadFiles', files.tempFiles);
- }
- function handleFileProgress(file, progress) {
- // console.log('handleFileProgress', file, progress);
- }
- function handleFileSuccess(file, res) {
- // console.log('handleFileSuccess', file, res);
- }
- function handleFileFail(file, err) {
- // console.log('handleFileFail', file, err);
- }
- function handleFileDelete(file) { // 移除文件
- // console.log('handleDelete', file)
- fileSeqs.value.splice(fileSeqs.value.findIndex(({ path }) => path == file.tempFilePath))
- }
- //表单校验
- const $mainForm = ref(null)
- const mainFormValue = ref({})
- const $repeatingForms = ref([])
- const repeatingFormsValue = ref([])
- const formatTypeDict = {
- '0': 'string',
- '1': 'number'
- }
- // 校验重复表
- function validateRepeatingForm() {
- // 设置重复表校验数据
- repeatingFormsValue.value = repeatingForm.value.elements.map((item, index) => {
- return computed(() => {
- const obj = {};
- item.forEach(({ name, defaultValue }) => {
- obj[name] = defaultValue;
- });
- return obj;
- }).value
- })
- // console.log('$repeatingForms: ',$repeatingForms.value);
- // console.log('repeatingFormsValue: ',repeatingFormsValue.value);
- // 重复表数据校验
- // BUG validate()校验无效
- $repeatingForms.value.forEach((form) => {
- // console.log('form: ',form);
- form.validate().then(res => {
- console.log('验证成功:', res);
- }).catch(err => {
- console.log('验证失败:', err);
- });
- })
- }
- function validateRepeatingForm2() {
- // 设置重复表校验数据
- repeatingFormsValue.value = repeatingForm.value.elements.map((item, index) => {
- return computed(() => {
- const obj = {};
- item.forEach(({ name, defaultValue }) => {
- obj[name] = defaultValue;
- });
- return obj;
- }).value
- })
- let flag = false
- repeatingFormsValue.value.forEach((item, index) => {
- for (const elem in item) {
- if (item[elem] == '') {
- $modal.msgError(`重复表 ${index + 1} 数据填写错误`)
- flag = false
- return
- }
- }
- flag = true
- })
- return flag
- }
- // 设置表单校验规则
- const $mainFormRules = ref({})
- const $repFormRules = ref({})
- function getValidateRules() {
- $repFormRules.value = computed(() => {
- const obj = {};
- repeatingForm.value.elementItem.forEach(elem => {
- obj[elem.tableField] = {
- rules: [
- {
- required: true,
- },
- {
- formatTypeDict: formatTypeDict[elem.fieldType.value] || 'string',
- }
- ],
- label: elem.elementName.slice(3)
- };
- });
- return obj;
- }).value
- $mainFormRules.value = computed(() => {
- const obj = {};
- formElements.value.forEach(elem => {
- if (!('0' != elem.canEdit && '8' != elem.type)) return
- obj[elem.elementId] = {
- rules: [
- {
- required: '1' == elem.noNull,
- },
- {
- format: formatTypeDict[elem.fieldType] || 'string',
- }
- ],
- label: elem.elementName
- };
- });
- return obj;
- }).value
- }
- const button_state = ref(true)
- function submitProcess() { // 提交表单
- // 禁用提交按钮
- button_state.value = false
- // validateMainForm()
- // 设置主表校验数据
- mainFormValue.value = computed(() => {
- const obj = {};
- formElements.value.forEach(elem => {
- if (!('0' != elem.canEdit && '8' != elem.type)) return
- obj[elem.elementId] = elem.defaultValue;
- });
- return obj;
- }).value
- // 主表数据校验
- $mainForm.value.validate().then(res => {
- // 重复表数据校验
- // validateRepeatingForm()
- if (!validateRepeatingForm2()) {
- button_state.value = true
- return
- }
- // 保存表单数据
- processInfo.form = formElements.value.map(({ tableField, defaultValue }) => {
- return { name: tableField, value: defaultValue }
- })
- repeatingForm.value.elements.forEach((item, index) => {
- const newItem = item.map(({ name, defaultValue }) => {
- return {
- name: name + '_' + (index + 1),
- value: defaultValue
- }
- })
- processInfo.form.push(...newItem)
- })
- // 保存附件ID
- const seqs = fileSeqs.value.map(({ seq }) => seq)
- if (processInfo.reqOffice == 1 && seqs.length == 0) {
- button_state.value = true
- $modal.msgError('请上传附件')
- return
- }
- submitProcessForm(userStore.user, processInfo, seqs).then(res => {
- if (res.returnMsg.includes('提交失败')) {
- // 启用提交按钮
- button_state.value = true
- $modal.msgError(res.returnMsg)
- } else {
- $modal.msgSuccess(res.returnMsg)
- // 通知列表刷新数据
- uni.$emit('ReloadProcessData');
- setTimeout(() => {
- // 返回上一页
- $tab.navigateBack();
- }, 1000)
- }
- })
- }).catch(err => {
- button_state.value = true
- $modal.msgError('表单填写错误')
- });
- }
- </script>
- <style lang="scss" scoped>
- .process_container {
- position: relative;
- ::v-deep .uni-forms {
- .uni-forms-item__label {
- font-size: calc(1rem + 0px) !important;
- line-height: calc(1rem + 0px) !important;
- font-weight: 600;
- }
- .uni-forms-item__content {
- .uni-easyinput__content-textarea {
- font-size: calc(14px + .5*(1rem - 16px)) !important;
- }
- .uni-easyinput__content-input {
- font-size: calc(14px + .5*(1rem - 16px)) !important;
- color: #333;
- }
- .uni-date {
- .uni-icons {
- font-size: calc(22px + .5*(1rem - 16px)) !important;
- }
- .uni-date__x-input {
- font-size: calc(14px + .5*(1rem - 16px)) !important;
- }
- }
- }
- }
- ::v-deep button {
- font-size: calc(18px + .5*(1rem - 16px));
- }
- .main_container {
- min-height: 30vh;
- }
- .picker_container {
- border: 1px solid #e5e5e5;
- background-color: #fff;
- border-radius: 4px;
- // box-sizing: border-box;
- .input_text {
- color: #333;
- font-size: calc(14px + .5*(1rem - 16px));
- line-height: 35px;
- height: 35px;
- padding: 0 0 0 10px;
- }
- }
- .repeating_table {
- ::v-deep .uni-forms-item--border {
- padding: 5px 0 !important;
- }
- .repeating_table_button_container {
- margin-top: 5px;
- button {
- height: 36px;
- line-height: 36px;
- color: #fff;
- }
- .add {
- background-color: #1ca035;
- }
- .del {
- background-color: #e64340;
- }
- }
- }
- .file_picker_container {
- ::v-deep .uni-card {
- .uni-card__header-content-title {
- font-size: calc(15px + .5*(1rem - 16px));
- }
- .uni-card__header-extra-text {
- font-size: calc(15px + .5*(1rem - 16px));
- }
- }
- }
- .is-disabled {
- color: #666 !important;
- }
- .submit_button_container {
- position: sticky;
- z-index: 10;
- width: 100%;
- bottom: 10px;
- .submit_button {
- background-color: #007aff !important;
- color: #fff !important;
- }
- .submit_button::after {
- border: 1px solid rgba(0, 0, 0, .2);
- border-radius: 10px;
- box-sizing: border-box;
- content: " ";
- height: 200%;
- left: 0;
- position: absolute;
- top: 0;
- -webkit-transform: scale(.5);
- transform: scale(.5);
- -webkit-transform-origin: 0 0;
- transform-origin: 0 0;
- width: 200%;
- border-top-width: 1px;
- border-right-width: 1px;
- border-bottom-width: 1px;
- border-left-width: 1px;
- border-top-style: solid;
- border-right-style: solid;
- border-bottom-style: solid;
- border-left-style: solid;
- border-top-color: rgba(0, 0, 0, 0.2);
- border-right-color: rgba(0, 0, 0, 0.2);
- border-bottom-color: rgba(0, 0, 0, 0.2);
- border-left-color: rgba(0, 0, 0, 0.2);
- border-image-source: initial;
- border-image-slice: initial;
- border-image-width: initial;
- border-image-outset: initial;
- border-image-repeat: initial;
- border-top-left-radius: 10px;
- border-top-right-radius: 10px;
- border-bottom-right-radius: 10px;
- border-bottom-left-radius: 10px;
- }
- }
- }
- </style>
|