inspectionValue.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <template>
  2. <view class="inspectionValue">
  3. <scroll-view scroll-y class="body">
  4. <uni-forms ref="baseForm" :model="formValues" :rules="rules" class="form" label-position="top">
  5. <template v-for="(item, index) of baseFormData" :key="index">
  6. <!-- 下拉 -->
  7. <uni-forms-item v-if="item.type === '1'" class="formItem" :required="item.required" :name="item.key">
  8. <template v-slot:label>
  9. <view class="customLabelWrap">
  10. <text :class="{'is-required': item.required}">*</text><text class="customLabel">{{item.name}}</text><text v-if="item.isException" class="customLabel red">&nbsp;异</text>
  11. </view>
  12. </template>
  13. <uni-data-picker :placeholder="'请选择' + item.name" :popup-title="'请选择' + item.name" :localdata="item.list" v-model="formValues[item.key]" @change="changeForm()">
  14. </uni-data-picker>
  15. </uni-forms-item>
  16. <!-- 单选 -->
  17. <uni-forms-item v-if="item.type === '2'" class="formItem" :required="item.required" :name="item.key">
  18. <template v-slot:label>
  19. <view class="customLabelWrap">
  20. <text :class="{'is-required': item.required}">*</text><text class="customLabel">{{item.name}}</text><text v-if="item.isException" class="customLabel red">&nbsp;异</text>
  21. </view>
  22. </template>
  23. <uni-data-checkbox v-model="formValues[item.key]" @change="changeForm()" :localdata="item.list" />
  24. </uni-forms-item>
  25. <!-- 多选 -->
  26. <uni-forms-item v-if="item.type === '3'" class="formItem" :required="item.required" :name="item.key">
  27. <template v-slot:label>
  28. <view class="customLabelWrap">
  29. <text :class="{'is-required': item.required}">*</text><text class="customLabel">{{item.name}}</text><text v-if="item.isException" class="customLabel red">&nbsp;异</text>
  30. </view>
  31. </template>
  32. <uni-data-checkbox v-model="formValues[item.key]" @change="changeForm()" multiple :localdata="item.list" />
  33. </uni-forms-item>
  34. <!-- 数字 -->
  35. <uni-forms-item v-if="item.type === '4'" class="formItem" :required="item.required" :name="item.key">
  36. <template v-slot:label>
  37. <view class="customLabelWrap">
  38. <text :class="{'is-required': item.required}">*</text><text class="customLabel">{{item.name}}</text><text v-if="item.isException" class="customLabel red">&nbsp;异</text>
  39. </view>
  40. </template>
  41. <uni-number-box v-model="formValues[item.key]" @change="changeForm()" :min="-9999" :max="9999" :placeholder="'请输入' + item.name"></uni-number-box>
  42. </uni-forms-item>
  43. <!-- 单行 -->
  44. <uni-forms-item v-if="item.type === '5' && item.isException" class="formItem" :label="item.name" :required="item.required" :name="item.key">
  45. <uni-easyinput style="padding-top: 8px;" v-model="formValues[item.key]" :placeholder="'请输入' + item.name" />
  46. </uni-forms-item>
  47. <!-- 多行 -->
  48. <uni-forms-item v-if="item.type === '6' && item.isException" class="formItem" :label="item.name" :required="item.required" :name="item.key">
  49. <uni-easyinput type="textarea" v-model="formValues[item.key]" :placeholder="'请输入' + item.name" />
  50. </uni-forms-item>
  51. <!-- 图片上传 -->
  52. <uni-forms-item v-if="item.type === '7' && item.isException" class="formItem" :label="item.name" :required="item.required" :name="item.key">
  53. <DsFilePicker :extra="item.id" :inspectionExecuteId="inspectionExecuteId" :ref="(el) => itemRefs[item.id] = el"></DsFilePicker>
  54. </uni-forms-item>
  55. <!-- 分割线 -->
  56. <view class="detail_head" v-if="item.type === '8'">
  57. <text class="title">{{ item.name }}</text>
  58. </view>
  59. </template>
  60. </uni-forms>
  61. </scroll-view>
  62. <view class="foot_common_btns">
  63. <button @click="goBack" type="default" class="cancelButton btn">返回</button>
  64. <button @click="submit" type="default" class="primaryButton btn">保存</button>
  65. </view>
  66. </view>
  67. </template>
  68. <script setup>
  69. import fromPairs from 'lodash-es/fromPairs'
  70. import keyBy from 'lodash-es/keyBy'
  71. import DsFilePicker from '@/components/DsFilePicker.vue';
  72. import { ref, reactive, nextTick } from 'vue'
  73. import { onLoad } from '@dcloudio/uni-app'
  74. import { api_addModel } from "@/http/api.js"
  75. import { defaultColor } from '@/static/js/theme.js'
  76. import { useSetTitle } from '@/share/useSetTitle.js'
  77. import { useMakePhoneCall } from '@/share/useMakePhoneCall.js'
  78. import { useGoBack } from '@/share/useGoBack.js'
  79. import { useLoginUserStore } from '@/stores/loginUser'
  80. import { useInspectionValueStore } from '@/stores/inspectionValue'
  81. import { forIn } from 'lodash-es';
  82. useSetTitle();
  83. const loginUserStore = useLoginUserStore();
  84. const { goBack } = useGoBack();
  85. const inspectionValueStore = useInspectionValueStore();
  86. // 主题颜色
  87. const primaryColor = ref(defaultColor)
  88. // 所有页码数据
  89. const formPageList = reactive([]);
  90. // 表单
  91. const baseForm = ref()
  92. // 图片上传ref数组
  93. const itemRefs = ref({});
  94. const inspectionExecuteId = ref()
  95. // 数据-原始
  96. const dataInfo = reactive({})
  97. // 数据-检验
  98. const rules = reactive({})
  99. // 数据-填写值
  100. const formValues = reactive({})
  101. // 表单数据-渲染
  102. // 下拉框 1
  103. // 单选 2
  104. // 多选 3
  105. // 数值 4
  106. // 单行文本 5
  107. // 多行文本 6
  108. // 照片上传 7
  109. // 分割线 8
  110. const baseFormData = reactive([
  111. // { type: 1, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  112. // { type: 2, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  113. // { type: 3, name: '', value: [], list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  114. // { type: 4, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  115. // { type: 5, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  116. // { type: 6, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  117. // { type: 7, name: '', value: [], list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  118. ])
  119. // 修改
  120. function changeForm(){
  121. handleData(true);
  122. }
  123. // 保存
  124. function submit(){
  125. console.log(itemRefs.value);
  126. console.log(formValues);
  127. console.log(baseFormData);
  128. // 处理图片上传检验问题
  129. Object.values(itemRefs.value).forEach(v => {
  130. if(v && v.baseFormData.handlerImgList.length){
  131. baseFormData.forEach(vv => {
  132. if(vv.id == v.extra){
  133. formValues[vv.key] = v.inspectionExecuteId;
  134. vv.value = v.inspectionExecuteId;
  135. }
  136. })
  137. }
  138. })
  139. nextTick(() => {
  140. baseForm.value.validate().then(res => {
  141. console.log('success', res);
  142. uni.showLoading({
  143. title: "加载中",
  144. mask: true,
  145. });
  146. console.log(formValues);
  147. let postData = {
  148. account: loginUserStore.loginUser.user.account,
  149. valuesList: [],
  150. };
  151. // baseFormData
  152. for(let key in formValues){
  153. let obj = baseFormData.find(v => v.key === key);
  154. if(!((obj.type === '5' && !obj.isException) || (obj.type === '6' && !obj.isException) || (obj.type === '7' && !obj.isException))){
  155. let name = obj.name;
  156. let itemId = obj.id;
  157. let _formPageList = dataInfo.inspectionFormDTO?.formPageList || [];
  158. _formPageList = _formPageList.map(v => v.formItemList).flat();
  159. let formItem = _formPageList.find(v => v.id === itemId);
  160. postData.valuesList.push({
  161. taskId: inspectionExecuteId.value,
  162. nodeId: dataInfo.id,
  163. formId: dataInfo.inspectionFormDTO.id,
  164. itemId: formItem.id,
  165. pageId: formItem.pageId,
  166. orders: formItem.orders,
  167. name,
  168. valuex: (formItem.type.value === '1' || formItem.type.value === '2' || formItem.type.value === '3') ? '' : formValues[key].toString(),
  169. configIds: (formItem.type.value === '1' || formItem.type.value === '2' || formItem.type.value === '3') ? formValues[key].toString() : undefined,
  170. hosId: dataInfo.inspectionFormDTO.hosId,
  171. })
  172. }
  173. }
  174. console.log(postData);
  175. console.log(itemRefs.value);
  176. // return;
  177. api_addModel(postData).then((res) => {
  178. uni.hideLoading();
  179. if (res.status == 200) {
  180. Object.values(itemRefs.value).forEach(v => {
  181. v && v.uploadFn();
  182. })
  183. if(res.exception){
  184. // 异常
  185. if(true){
  186. uni.showToast({
  187. icon: 'none',
  188. title: "巡检提交成功,有异常",
  189. duration: 2000,
  190. mask: true,
  191. })
  192. setTimeout(() => {
  193. inspectionValueStore.clearInspectionValueData();
  194. uni.reLaunch({
  195. url: `/pages/inspection/inspectionExecute/inspectionExecute`
  196. })
  197. }, 2000)
  198. }
  199. }else{
  200. // 无异常
  201. uni.showToast({
  202. icon: 'none',
  203. title: "巡检提交成功,无异常",
  204. duration: 2000,
  205. mask: true,
  206. })
  207. setTimeout(() => {
  208. inspectionValueStore.clearInspectionValueData();
  209. uni.reLaunch({
  210. url: `/pages/inspection/inspectionExecute/inspectionExecute`
  211. })
  212. }, 2000)
  213. }
  214. } else {
  215. uni.showToast({
  216. icon: 'none',
  217. title: res.msg || '请求数据失败!'
  218. });
  219. }
  220. });
  221. }).catch(err => {
  222. console.log('err', err);
  223. })
  224. })
  225. }
  226. // 处理数据
  227. function handleData(isException = false){
  228. // 目前只取第一页
  229. let firstPage = formPageList[0]?.formItemList || [];
  230. firstPage = firstPage.filter( v => v.display === 1);
  231. firstPage = firstPage.map((v, i) => {
  232. let value = '';
  233. v.formItemConfigList = v.formItemConfigList || [];
  234. // value
  235. if(v.type.value === '3'){
  236. // 多选
  237. let arr = v.formItemConfigList.filter(v => v.checkDefault === 1);
  238. value = arr.length ? arr.map(v => v.id) : [];
  239. console.log('多选', value)
  240. } else if(v.type.value === '7'){
  241. // 图片
  242. value = [];
  243. } else if(v.type.value === '1' || v.type.value === '2') {
  244. // 单选或下拉
  245. let defaultValue = v.formItemConfigList.find(v => v.checkDefault === 1);
  246. value = defaultValue ? defaultValue.id : '';
  247. console.log('单选或下拉', value)
  248. } else{
  249. // 其他
  250. value = v.defaultValue;
  251. }
  252. return {
  253. id: v.id,
  254. key: `field${i}`,
  255. type: v.type.value,
  256. name: v.name,
  257. value: isException ? formValues[`field${i}`] : value,
  258. list: v.formItemConfigList ? v.formItemConfigList.map(v => ({text: v.name, value: v.id, checkException: v.checkException, uncheckException: v.uncheckException})) : [],
  259. required: v.required === 1,
  260. checkType: v.checkType,
  261. showError: v.showError,
  262. orders: v.orders,
  263. valueMin: v.checkType === 2 ? v.valueLow : ( v.checkType === 1 ? (v.valuex - v.valueGap) : undefined),
  264. valueMax: v.checkType === 2 ? v.valueUp : ( v.checkType === 1 ? (v.valuex + v.valueGap) : undefined),
  265. }
  266. });
  267. let firstPageBottom = firstPage.filter(v => v.type === '5' || v.type === '6' || v.type === '7');
  268. firstPage = firstPage.filter(v => v.type !== '5' && v.type !== '6' && v.type !== '7');
  269. // 其他项
  270. if(!isException){
  271. firstPage.forEach(v => {
  272. // 下拉,单选
  273. if(v.type === '1' || v.type === '2'){
  274. let obj = v.list.find(vv => formValues[v.key] === vv.value);
  275. v.isException = obj ? obj.checkException === 1 : false;
  276. } else if(v.type === '3'){
  277. // 多选
  278. v.isException = v.list.some(vv => {
  279. if(formValues[v.key].includes(vv.value)){
  280. return vv.checkException === 1;
  281. }else{
  282. return false;
  283. }
  284. })
  285. } else if(v.type === '4'){
  286. v.isException = (v.value < v.valueMin) || (v.value > v.valueMax);
  287. }
  288. })
  289. }else{
  290. firstPage.forEach(v => {
  291. if(v.type === '1' || v.type === '2'){
  292. // 下拉,单选
  293. let obj = v.list.find(vv => v.value === vv.value);
  294. v.isException = obj ? obj.checkException === 1 : false;
  295. } else if(v.type === '3'){
  296. // 多选
  297. v.isException = v.list.some(vv => {
  298. if(v.value.includes(vv.value)){
  299. return vv.checkException === 1;
  300. }else{
  301. return false;
  302. }
  303. })
  304. } else if(v.type === '4'){
  305. // 数值
  306. v.isException = (formValues[v.key] < v.valueMin) || (formValues[v.key] > v.valueMax);
  307. }
  308. })
  309. }
  310. // 单行|多行|图片上传
  311. let someIsException = firstPage.filter(v => v.type !== '8').some(v => v.isException);
  312. console.log('someIsException=>', someIsException)
  313. firstPageBottom.forEach(v => {
  314. console.log('v.showError=>', v.showError)
  315. v.isException = (v.showError === 1 && someIsException) || v.showError === 0;
  316. })
  317. firstPage = firstPage.concat(firstPageBottom).sort((a, b) => a.orders - b.orders);
  318. console.log('firstPage=>', firstPage);
  319. Object.assign(baseFormData, firstPage);
  320. console.log('baseFormData=>', baseFormData);
  321. Object.assign(formValues, fromPairs(firstPage.filter(v => v.type !== '8').map(v => ([v.key, v.value]))));
  322. console.log('formValues=>', formValues);
  323. let rulesObj = keyBy(firstPage.filter(v => v.type !== '8'), 'key');
  324. console.log(rulesObj)
  325. for(let key in rulesObj){
  326. rulesObj[key] = {
  327. rules: [
  328. { required: rulesObj[key].required, errorMessage: `${rulesObj[key].name}不能为空` }
  329. ]
  330. }
  331. }
  332. Object.assign(rules, rulesObj);
  333. console.log('rules=>', rules);
  334. }
  335. onLoad((option) => {
  336. inspectionExecuteId.value = +option.inspectionExecuteId;
  337. // 巡检项
  338. if(inspectionValueStore.inspectionValue.data){
  339. Object.assign(dataInfo, inspectionValueStore.inspectionValue.data);
  340. Object.assign(formPageList, dataInfo.inspectionFormDTO?.formPageList || []);
  341. handleData()
  342. }
  343. })
  344. </script>
  345. <style lang="scss" scoped>
  346. .customLabelWrap{
  347. .is-required{
  348. color: #dd524d;
  349. font-weight: bold;
  350. margin-top: 8rpx;
  351. }
  352. }
  353. .inspectionValue{
  354. height: 100%;
  355. display: flex;
  356. flex-direction: column;
  357. justify-content: space-between;
  358. .body{
  359. box-sizing: border-box;
  360. flex: 1;
  361. min-height: 0;
  362. padding-bottom: 24rpx;
  363. .formItem{
  364. padding: 32rpx 24rpx 0;
  365. margin-bottom: 0;
  366. }
  367. ::v-deep .uni-forms-item__label{
  368. width: auto!important;
  369. padding: 0!important;
  370. height: auto!important;
  371. align-items: flex-start!important;
  372. }
  373. .detail_head{
  374. padding: 24rpx;
  375. border-top: 1rpx solid #D2D2D2;
  376. border-bottom: 1rpx solid #D2D2D2;
  377. display: flex;
  378. justify-content: space-between;
  379. align-items: center;
  380. margin-top: 24rpx;
  381. &:first-of-type{
  382. border-top: none;
  383. margin-top: 0;
  384. }
  385. .title{
  386. font-size: 26rpx;
  387. color: $uni-primary;
  388. padding-left: 18rpx;
  389. position: relative;
  390. &:before{
  391. content: '';
  392. width: 8rpx;
  393. height: 25rpx;
  394. background-color: $uni-primary;
  395. position: absolute;
  396. left: 0;
  397. top: 50%;
  398. transform: translateY(-50%);
  399. }
  400. }
  401. }
  402. }
  403. }
  404. </style>