index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <template>
  2. <view class="process_container">
  3. <uni-card spacing="0">
  4. <view class="main_container">
  5. <uni-forms label-position="left" :label-width="125" :border="true">
  6. <uni-forms-item v-for="(item, index) in formElements" :label="item.elementName" :key="index">
  7. <uni-easyinput v-if="'多少小时' == item.elementName" :disabled="true" placeholder=""
  8. v-model="diffHours"></uni-easyinput>
  9. <!-- 输入框 -->
  10. <uni-easyinput v-else-if="'0' == item.type" :disabled="'0' == item.canEdit"
  11. :placeholder="'0' == item.canEdit ? '' : '请输入内容'"
  12. v-model="item.defaultValue"></uni-easyinput>
  13. <!-- 富文本输入框 -->
  14. <uni-easyinput v-else-if="'1' == item.type" :disabled="'0' == item.canEdit"
  15. :placeholder="'0' == item.canEdit ? '' : '请输入内容'" v-model="item.defaultValue"
  16. type="textarea"></uni-easyinput>
  17. <!-- 下拉框 -->
  18. <picker class="picker_container" v-else-if="'2' == item.type"
  19. @change="bindPickerChange($event, item)" :value="item.defaultValue"
  20. :range="formatDict(item.typeDetail.enum)">
  21. <view class="uni-input input_text">
  22. <!-- 设置默认值为第一个选项 -->
  23. {{ item.defaultValue ? item.defaultValue : item.defaultValue =
  24. item.typeDetail.enum[0].enumVname }}
  25. </view>
  26. </picker>
  27. <!-- 开始时间选择器 -->
  28. <uni-datetime-picker :end="endTime" @change="setTimeRange(item)"
  29. v-else-if="timeFlag && '9' == item.type && '开始时间' == item.elementName"
  30. v-model="item.defaultValue" :clear-icon="false" type="datetime" />
  31. <!-- 结束时间选择器 -->
  32. <uni-datetime-picker :start="startTime" @change="setTimeRange(item)" :disabled="disableEndTime"
  33. :placeholder="disableEndTime ? '请先确认开始时间' : '确认结束时间'"
  34. v-else-if="timeFlag && '9' == item.type && '结束时间' == item.elementName"
  35. v-model="item.defaultValue" :clear-icon="false" type="datetime" />
  36. <!-- 其他时间选择器 -->
  37. <!-- 年月日 时分秒 -->
  38. <uni-datetime-picker v-else-if="'9' == item.type" v-model="item.defaultValue" type="datetime" />
  39. <!-- 年月日 -->
  40. <uni-datetime-picker v-else-if="'3' == item.type" v-model="item.defaultValue" type="date" />
  41. </uni-forms-item>
  42. </uni-forms>
  43. </view>
  44. </uni-card>
  45. <view v-if="repeatingForm.elementItem.length > 0" class="repeating_table">
  46. <uni-card v-for="(table, tableIndex) in repeatingForm.elements" spacing="0" :key="tableIndex">
  47. <uni-forms label-position="left" :label-width="125" :border="true">
  48. <uni-forms-item v-for="(item, itemIndex) in table"
  49. :label="repeatingForm.elementItem[itemIndex].elementName.slice(3)" :key="itemIndex">
  50. <uni-easyinput v-if="repeatingForm.elementItem[itemIndex].bddzText" :disabled="true"
  51. :placeholder="''" :value="computedrepeatingFormItemValue(item, table)"></uni-easyinput>
  52. <uni-easyinput v-else placeholder="请输入内容" v-model="item.value"></uni-easyinput>
  53. </uni-forms-item>
  54. </uni-forms>
  55. <view class="repeating_table_button_container">
  56. <uni-row>
  57. <uni-col :span="10" :offset="1">
  58. <button @click="addrepeatingFormItem(tableIndex)" type="primary">新增</button>
  59. </uni-col>
  60. <uni-col :span="10" :offset="2">
  61. <button @click="delrepeatingFormItem(tableIndex)"
  62. :disabled="repeatingForm.elements.length <= 1" type="warn">删除</button>
  63. </uni-col>
  64. </uni-row>
  65. </view>
  66. </uni-card>
  67. </view>
  68. <uni-card title="上传附件" :extra="`${fileList.length}/15`" spacing="0">
  69. <uni-file-picker ref="filePicker" v-model="fileList" :auto-upload="true" mode="list" limit="10"
  70. file-mediatype="all" @select="handleFileSelect" @progress="handleFileProgress"
  71. @success="handleFileSuccess" @fail="handleFileFail" @delete="handleFileDelete" />
  72. </uni-card>
  73. <uni-card spacing="0" padding="0">
  74. <button :loading="!button_state" type="primary" class="submit_button" @click="submitProcess">提交</button>
  75. </uni-card>
  76. <view style="height: 5px; margin-top: 10px;"></view>
  77. </view>
  78. </template>
  79. <script setup lang="ts">
  80. import { onMounted, reactive, ref, computed } from 'vue'
  81. import { onLoad } from '@dcloudio/uni-app'
  82. import { useUserStore } from '@/store/user.js'
  83. import $modal from '@/plugins/modal.js'
  84. import $tab from '@/plugins/tab.js'
  85. import { convertToChineseCurrency } from '@/utils/ygoa.js'
  86. import { getProcessInfo, getProcessForm, submitProcessForm, uploadFile } from '@/api/work.js'
  87. let processInfo = reactive({
  88. modelName: '流程申请',
  89. control: '',
  90. formId: '',
  91. modelId: '',
  92. tmodelId: '',
  93. isMoreIns: '',
  94. pathJudgeType: '',
  95. form: []
  96. })
  97. const userStore = useUserStore()
  98. const title = ref('')
  99. onLoad((options) => {
  100. const { modelName, modelId, control } = options
  101. processInfo.modelName = modelName
  102. processInfo.modelId = modelId
  103. processInfo.control = control
  104. title.value = userStore.user.name + '的' + processInfo.modelName
  105. // 设置导航栏标题
  106. uni.setNavigationBarTitle({
  107. title: title.value
  108. });
  109. })
  110. onMounted(() => {
  111. initProcessInfo().then(() => {
  112. initProcessForm()
  113. })
  114. })
  115. function initProcessInfo() {
  116. return new Promise<void>((resolve, reject) => {
  117. getProcessInfo(processInfo)
  118. .then(({ returnParams }) => {
  119. const { formId, tmodelId, isMoreIns, pathJudgeType } = returnParams.flow[0]
  120. processInfo.formId = formId
  121. processInfo.tmodelId = tmodelId
  122. processInfo.isMoreIns = isMoreIns
  123. processInfo.pathJudgeType = pathJudgeType
  124. resolve()
  125. })
  126. })
  127. }
  128. function initProcessForm() {
  129. getProcessForm(userStore.user, processInfo).then(({ returnParams }) => {
  130. formElements.value = returnParams.formElements
  131. repeatingForm.value = returnParams.repeatingForm
  132. // 生成第一个重复表数据
  133. if (repeatingForm.value) {
  134. addrepeatingFormItem(0)
  135. }
  136. startEleIndex.value = formElements.value.findIndex((item) => '开始时间' == item.elementName)
  137. endEleIndex.value = formElements.value.findIndex((item) => '结束时间' == item.elementName)
  138. // console.log('startEle', startEleIndex.value);
  139. // console.log('endEle', endEleIndex.value);
  140. if (startEleIndex.value != -1 && endEleIndex.value != -1) { // 判断是否需要计算时间差
  141. // console.log('if (startEleIndex && endEleIndex)');
  142. timeFlag.value = true
  143. disableEndTime.value = true // 计算时间差时默认禁用结束时间选择器
  144. formElements.value[startEleIndex.value].defaultValue = new Date()
  145. }
  146. })
  147. }
  148. const formElements = ref([])
  149. const repeatingForm = ref({
  150. elementItem: [],
  151. elements: []
  152. })
  153. const startEleIndex = ref(0) // 开始时间
  154. const endEleIndex = ref(0) // 结束时间
  155. function addrepeatingFormItem(index) {
  156. const table = repeatingForm.value.elementItem.map(({ tableField, bddzText }) => {
  157. const item = {
  158. name: tableField,
  159. value: ""
  160. }
  161. if (bddzText != '') {
  162. const mulItem = bddzText.split('*')
  163. item['bddzText'] = mulItem.map(item => {
  164. return repeatingForm.value.elementItem.findIndex(({ elementName }) => item == elementName)
  165. })
  166. }
  167. return item
  168. })
  169. repeatingForm.value.elements.splice(index + 1, 0, table)
  170. }
  171. function delrepeatingFormItem(index) {
  172. console.log("delrepeatingFormItem: " + index);
  173. $modal.confirm('', '确认删除该重复表数据')
  174. .then(() => {
  175. repeatingForm.value.elements.splice(index, 1)
  176. })
  177. .catch(err => {
  178. })
  179. }
  180. function computedrepeatingFormItemValue(item, table) {
  181. let res = 1
  182. item.bddzText.forEach(itemIndex => {
  183. res = res * table[itemIndex].value
  184. })
  185. item.value = res
  186. console.log('convertToChineseCurrency', convertToChineseCurrency(res));
  187. return res
  188. }
  189. // 下拉框
  190. function bindPickerChange(e, item) {
  191. const index = e.detail.value;
  192. item.defaultValue = item.typeDetail.enum[index].enumVname
  193. }
  194. function formatDict(dict) {
  195. return dict.map(({ enumVname }) => enumVname)
  196. }
  197. const startTime = ref(0) // 开始时间
  198. const endTime = ref(0) // 结束时间
  199. const diffHours = ref(' ') // 时间差
  200. const timeFlag = ref(false) // 是否计算时间差
  201. const disableEndTime = ref(false) // 禁用 结束时间选择器
  202. function setTimeRange(e) {
  203. if (timeFlag.value && '开始时间' == e.elementName) {
  204. startTime.value = e.defaultValue // 设置 开始时间
  205. disableEndTime.value = false // 解除 结束时间选择器 禁用
  206. }
  207. if (timeFlag.value && '结束时间' == e.elementName) {
  208. endTime.value = e.defaultValue // 设置 结束时间
  209. }
  210. if (timeFlag.value && startTime.value && endTime.value) {
  211. const start = new Date(startTime.value).getTime() // 获取 开始时间 时间戳
  212. const end = new Date(endTime.value).getTime() // 获取 结束时间 时间戳
  213. const diffSec = Math.abs(end - start) // 获取 时间戳 差值
  214. diffHours.value = (diffSec / 1000 / 60 / 60).toFixed(2) // 计算 时间差值 并格式化 为只有2位小数
  215. }
  216. }
  217. const fileList = ref([]) // 文件列表
  218. const fileSeqs = ref([]) // 文件上传返回 seq 列表
  219. const filePicker = ref(null)
  220. // TODO 中间数组缓存将要上传的文件列表 保存上传顺序 上传成功后保存seq后再添加入 实际文件列表渲染
  221. async function handleFileSelect(files) { // 新增文件
  222. console.log('handleFileSelect', files.tempFiles)
  223. files.tempFiles.forEach(file => {
  224. const data = {
  225. name: file.name,
  226. filePath: file.path,
  227. }
  228. uploadFile(data) // TODO 改为同步
  229. .then(res => {
  230. fileSeqs.value.push(res.returnParams)
  231. file.seq = res.returnParams
  232. fileList.value.push(file)
  233. // file.seq = res.returnParams
  234. })
  235. .catch(err => {
  236. $modal.msgError('文件' + data.name + '上传失败,请重新上传')
  237. switch (err) {
  238. case -201:
  239. console.log('文件上传失败 未找到该文件');
  240. break;
  241. case -20201:
  242. console.log('文件上传失败 返回值不是JSON字符串');
  243. break;
  244. }
  245. // files.tempFiles.filter(item => item.path == file.path)
  246. })
  247. })
  248. console.log('UploadFiles', files.tempFiles);
  249. }
  250. function handleFileProgress(file, progress) {
  251. // console.log('handleFileProgress', file, progress);
  252. }
  253. function handleFileSuccess(file, res) {
  254. // console.log('handleFileSuccess', file, res);
  255. }
  256. function handleFileFail(file, err) {
  257. // console.log('handleFileFail', file, err);
  258. }
  259. function handleFileDelete(file, index) { // 移除文件
  260. console.log('handleDelete', file, index)
  261. console.log('index', file)
  262. fileList.value.splice(fileList.value.indexOf(file.tempFiles), 1)
  263. fileSeqs.value.splice(index, 1)
  264. }
  265. const button_state = ref(true)
  266. function submitProcess() { // 提交表单
  267. // 禁用提交按钮
  268. button_state.value = false
  269. // 保存表单数据
  270. processInfo.form = formElements.value.map(({ tableField, defaultValue }) => {
  271. return { name: tableField, value: defaultValue }
  272. })
  273. repeatingForm.value.elements.forEach((item, index) => {
  274. const newItem = item.map(({ name, value }) => {
  275. return {
  276. name: name + '_' + (index + 1),
  277. value
  278. }
  279. })
  280. processInfo.form.push(...newItem)
  281. })
  282. button_state.value = true
  283. // return
  284. submitProcessForm(userStore.user, processInfo, fileSeqs.value).then(res => {
  285. console.log('submitProcessForm', res);
  286. if (res.returnMsg.includes('提交失败')) {
  287. // 启用提交按钮
  288. button_state.value = true
  289. $modal.msgError(res.returnMsg)
  290. } else {
  291. $modal.msgSuccess(res.returnMsg)
  292. // 通知列表刷新数据
  293. uni.$emit('ReloadProcessData', '测试$emit');
  294. setTimeout(() => {
  295. // 返回上一页
  296. $tab.navigateBack();
  297. }, 1000)
  298. }
  299. })
  300. }
  301. </script>
  302. <style lang="scss">
  303. .process_container {
  304. .main_container {
  305. min-height: 70vh;
  306. }
  307. .picker_container {
  308. border: 1px solid #e5e5e5;
  309. background-color: #fff;
  310. border-radius: 4px;
  311. // box-sizing: border-box;
  312. .input_text {
  313. color: #333;
  314. font-size: 14px;
  315. line-height: 35px;
  316. height: 35px;
  317. padding: 0 0 0 10px;
  318. }
  319. }
  320. .repeating_table {
  321. .uni-forms-item--border {
  322. padding: 5px 0 !important;
  323. }
  324. .repeating_table_button_container {
  325. margin-top: 5px;
  326. button {
  327. height: 36px;
  328. line-height: 36px;
  329. color: #fff;
  330. }
  331. .add {
  332. background-color: #1ca035;
  333. }
  334. .del {
  335. background-color: #e64340;
  336. }
  337. }
  338. }
  339. .is-disabled {
  340. color: #666 !important;
  341. }
  342. .submit_button {
  343. background-color: #007aff !important;
  344. color: #fff !important;
  345. }
  346. .submit_button::after {
  347. border: 1px solid rgba(0, 0, 0, .2);
  348. border-radius: 10px;
  349. box-sizing: border-box;
  350. content: " ";
  351. height: 200%;
  352. left: 0;
  353. position: absolute;
  354. top: 0;
  355. -webkit-transform: scale(.5);
  356. transform: scale(.5);
  357. -webkit-transform-origin: 0 0;
  358. transform-origin: 0 0;
  359. width: 200%;
  360. border-top-width: 1px;
  361. border-right-width: 1px;
  362. border-bottom-width: 1px;
  363. border-left-width: 1px;
  364. border-top-style: solid;
  365. border-right-style: solid;
  366. border-bottom-style: solid;
  367. border-left-style: solid;
  368. border-top-color: rgba(0, 0, 0, 0.2);
  369. border-right-color: rgba(0, 0, 0, 0.2);
  370. border-bottom-color: rgba(0, 0, 0, 0.2);
  371. border-left-color: rgba(0, 0, 0, 0.2);
  372. border-image-source: initial;
  373. border-image-slice: initial;
  374. border-image-width: initial;
  375. border-image-outset: initial;
  376. border-image-repeat: initial;
  377. border-top-left-radius: 10px;
  378. border-top-right-radius: 10px;
  379. border-bottom-right-radius: 10px;
  380. border-bottom-left-radius: 10px;
  381. }
  382. }
  383. </style>