inspectionValue.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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, api_getDictionary } 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. import { useIncidentBuildStore } from '@/stores/incidentBuild'
  83. useSetTitle();
  84. const loginUserStore = useLoginUserStore();
  85. const { goBack } = useGoBack();
  86. const inspectionValueStore = useInspectionValueStore();
  87. const incidentBuildStore = useIncidentBuildStore();
  88. // 主题颜色
  89. const primaryColor = ref(defaultColor)
  90. // 所有页码数据
  91. const formPageList = reactive([]);
  92. // 故障来源列表
  93. const defaultSourceValue = ref();
  94. // 表单
  95. const baseForm = ref()
  96. // 图片上传ref数组
  97. const itemRefs = ref({});
  98. const inspectionExecuteId = ref()
  99. // 数据-原始
  100. const dataInfo = reactive({})
  101. // 数据-检验
  102. const rules = reactive({})
  103. // 数据-填写值
  104. const formValues = reactive({})
  105. // 表单数据-渲染
  106. // 下拉框 1
  107. // 单选 2
  108. // 多选 3
  109. // 数值 4
  110. // 单行文本 5
  111. // 多行文本 6
  112. // 照片上传 7
  113. // 分割线 8
  114. const baseFormData = reactive([
  115. // { type: 1, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  116. // { type: 2, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  117. // { type: 3, name: '', value: [], list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  118. // { type: 4, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  119. // { type: 5, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  120. // { type: 6, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  121. // { type: 7, name: '', value: [], list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  122. ])
  123. // 修改
  124. function changeForm(){
  125. handleData(true);
  126. }
  127. // 跳转巡检列表
  128. function toInspectionExecute(){
  129. inspectionValueStore.clearInspectionValueData();
  130. uni.reLaunch({
  131. url: `/pages/inspection/inspectionExecute/inspectionExecute`
  132. })
  133. }
  134. // 获取故障来源列表
  135. function getSources(){
  136. let postData = {
  137. "key": 'incident_source',
  138. "type": "list",
  139. };
  140. api_getDictionary(postData).then(res => {
  141. res = res || [];
  142. let discover = res.find(v => v.value === 'inspection');
  143. if(discover){
  144. defaultSourceValue.value = discover.id;
  145. }
  146. })
  147. }
  148. // 跳转新建事件
  149. function toBuildIncident(res){
  150. let imgList = Object.values(itemRefs.value);
  151. let repairImgList = [];
  152. if(imgList.length){
  153. repairImgList = imgList[0].baseFormData.handlerImgList;
  154. }
  155. let incidentData = {
  156. place: res.inspectionNode.floorDTO,
  157. houseNumber: res.inspectionNode.address,
  158. source: defaultSourceValue.value,
  159. description: res.inspectionValuesList.filter(v => v.exception == 1).map(v => `${v.name}填写值为${v.valuex ? v.valuex : (v.formItemConfigList ? v.formItemConfigList.map(vv => v.name).join(',') : '')}`).join(';'),
  160. requester: loginUserStore.loginUser.user,
  161. branch: loginUserStore.loginUser.user.currentHospital.parent ? loginUserStore.loginUser.user.currentHospital.parent.id : loginUserStore.loginUser.user.currentHospital.id,
  162. contacts: loginUserStore.loginUser.user.name,
  163. contactsInformation: loginUserStore.loginUser.user.phone,
  164. category: res.inspectionForm.categoryDTO,
  165. department: res.inspectionForm.repairDeptDTO,
  166. priority: res.inspectionForm.priorityDTO,
  167. repairImgList,
  168. inspectionTaskId: res.id,
  169. }
  170. console.log(repairImgList, 'repairImgList')
  171. console.log(incidentData, 'incidentData')
  172. incidentBuildStore.setIncidentBuildData(incidentData, 'buildIncident');
  173. inspectionValueStore.clearInspectionValueData();
  174. uni.navigateTo({
  175. url: `/pages/buildIncident/buildIncident?type=inspection`
  176. })
  177. }
  178. // 保存
  179. function submit(){
  180. console.log(itemRefs.value);
  181. console.log(formValues);
  182. console.log(baseFormData);
  183. // 处理图片上传检验问题
  184. Object.values(itemRefs.value).forEach(v => {
  185. if(v && v.baseFormData.handlerImgList.length){
  186. baseFormData.forEach(vv => {
  187. if(vv.id == v.extra){
  188. formValues[vv.key] = v.inspectionExecuteId;
  189. vv.value = v.inspectionExecuteId;
  190. }
  191. })
  192. }
  193. })
  194. nextTick(() => {
  195. baseForm.value.validate().then(res => {
  196. console.log('success', res);
  197. uni.showLoading({
  198. title: "加载中",
  199. mask: true,
  200. });
  201. console.log(formValues);
  202. let postData = {
  203. account: loginUserStore.loginUser.user.account,
  204. valuesList: [],
  205. };
  206. // baseFormData
  207. for(let key in formValues){
  208. let obj = baseFormData.find(v => v.key === key);
  209. if(!((obj.type === '5' && !obj.isException) || (obj.type === '6' && !obj.isException) || (obj.type === '7' && !obj.isException))){
  210. let name = obj.name;
  211. let itemId = obj.id;
  212. let _formPageList = dataInfo.inspectionFormDTO?.formPageList || [];
  213. _formPageList = _formPageList.map(v => v.formItemList).flat();
  214. let formItem = _formPageList.find(v => v.id === itemId);
  215. postData.valuesList.push({
  216. taskId: inspectionExecuteId.value,
  217. nodeId: dataInfo.id,
  218. formId: dataInfo.inspectionFormDTO.id,
  219. itemId: formItem.id,
  220. pageId: formItem.pageId,
  221. orders: formItem.orders,
  222. name,
  223. valuex: (formItem.type.value === '1' || formItem.type.value === '2' || formItem.type.value === '3') ? '' : formValues[key].toString(),
  224. configIds: (formItem.type.value === '1' || formItem.type.value === '2' || formItem.type.value === '3') ? formValues[key].toString() : undefined,
  225. hosId: dataInfo.inspectionFormDTO.hosId,
  226. })
  227. }
  228. }
  229. console.log(postData);
  230. console.log(itemRefs.value);
  231. // return;
  232. api_addModel(postData).then((res) => {
  233. uni.hideLoading();
  234. if (res.status == 200) {
  235. Object.values(itemRefs.value).forEach(v => {
  236. v && v.uploadFn();
  237. })
  238. if(res.exception){
  239. // 异常
  240. if(res.inspectionForm.createOrder == 1){
  241. // 是否自动生成维修单
  242. uni.showModal({
  243. title: '提示',
  244. content: `巡检提交成功,您巡检有产生异常;系统自动生成工单:${res.incidentSign}`,
  245. showCancel: false,
  246. confirmText: '返回巡检列表',
  247. success: (res) => {
  248. if (res.confirm) {
  249. toInspectionExecute();
  250. }
  251. }
  252. });
  253. } else if(res.inspectionForm.showOrder == 1){
  254. // 是否显示生成维修单
  255. uni.showModal({
  256. title: '提示',
  257. content: '巡检提交成功,您巡检有产生异常,是否生成维修单',
  258. confirmText: '生成维修单',
  259. cancelText: '返回巡检列表',
  260. success: (result) => {
  261. if (result.confirm) {
  262. toBuildIncident(res);
  263. } else if (result.cancel) {
  264. toInspectionExecute();
  265. }
  266. }
  267. });
  268. } else {
  269. uni.showToast({
  270. icon: 'none',
  271. title: "巡检提交成功,有异常",
  272. duration: 2000,
  273. mask: true,
  274. })
  275. setTimeout(() => {
  276. toInspectionExecute();
  277. }, 2000)
  278. }
  279. }else{
  280. // 无异常
  281. uni.showToast({
  282. icon: 'none',
  283. title: "巡检提交成功,无异常",
  284. duration: 2000,
  285. mask: true,
  286. })
  287. setTimeout(() => {
  288. toInspectionExecute();
  289. }, 2000)
  290. }
  291. } else if(res.status == 555){
  292. // 自动生成维修单失败
  293. uni.showModal({
  294. title: '提示',
  295. content: '巡检提交成功,您巡检有产生异常;系统自动生成工单失败,请手动建单',
  296. confirmText: '生成维修单',
  297. cancelText: '返回巡检列表',
  298. success: (result) => {
  299. if (result.confirm) {
  300. toBuildIncident(res);
  301. } else if (result.cancel) {
  302. toInspectionExecute();
  303. }
  304. }
  305. });
  306. } else {
  307. uni.showToast({
  308. icon: 'none',
  309. title: res.msg || '请求数据失败!'
  310. });
  311. }
  312. });
  313. }).catch(err => {
  314. console.log('err', err);
  315. })
  316. })
  317. }
  318. // 处理数据
  319. function handleData(isException = false){
  320. // 目前只取第一页
  321. let firstPage = formPageList[0]?.formItemList || [];
  322. firstPage = firstPage.filter( v => v.display === 1);
  323. firstPage = firstPage.map((v, i) => {
  324. let value = '';
  325. v.formItemConfigList = v.formItemConfigList || [];
  326. // value
  327. if(v.type.value === '3'){
  328. // 多选
  329. let arr = v.formItemConfigList.filter(v => v.checkDefault === 1);
  330. value = arr.length ? arr.map(v => v.id) : [];
  331. console.log('多选', value)
  332. } else if(v.type.value === '7'){
  333. // 图片
  334. value = [];
  335. } else if(v.type.value === '1' || v.type.value === '2') {
  336. // 单选或下拉
  337. let defaultValue = v.formItemConfigList.find(v => v.checkDefault === 1);
  338. value = defaultValue ? defaultValue.id : '';
  339. console.log('单选或下拉', value)
  340. } else{
  341. // 其他
  342. value = v.defaultValue;
  343. }
  344. return {
  345. id: v.id,
  346. key: `field${i}`,
  347. type: v.type.value,
  348. name: v.name,
  349. value: isException ? formValues[`field${i}`] : value,
  350. list: v.formItemConfigList ? v.formItemConfigList.map(v => ({text: v.name, value: v.id, checkException: v.checkException, uncheckException: v.uncheckException})) : [],
  351. required: v.required === 1,
  352. checkType: v.checkType,
  353. showError: v.showError,
  354. orders: v.orders,
  355. valueMin: v.checkType === 2 ? v.valueLow : ( v.checkType === 1 ? (v.valuex - v.valueGap) : undefined),
  356. valueMax: v.checkType === 2 ? v.valueUp : ( v.checkType === 1 ? (v.valuex + v.valueGap) : undefined),
  357. }
  358. });
  359. let firstPageBottom = firstPage.filter(v => v.type === '5' || v.type === '6' || v.type === '7');
  360. firstPage = firstPage.filter(v => v.type !== '5' && v.type !== '6' && v.type !== '7');
  361. // 其他项
  362. if(!isException){
  363. console.log(firstPage, 'firstPage')
  364. console.log(formValues, 'formValues')
  365. console.log(isException, 'isException')
  366. firstPage.forEach(v => {
  367. // 下拉,单选
  368. if(v.type === '1' || v.type === '2'){
  369. let obj = v.list.find(vv => v.value === vv.value);
  370. v.isException = obj ? obj.checkException === 1 : false;
  371. } else if(v.type === '3'){
  372. // 多选
  373. v.isException = v.list.some(vv => {
  374. if(v.value.includes(vv.value)){
  375. return vv.checkException === 1;
  376. }else{
  377. return false;
  378. }
  379. })
  380. } else if(v.type === '4'){
  381. v.isException = (v.value < v.valueMin) || (v.value > v.valueMax);
  382. }
  383. })
  384. }else{
  385. firstPage.forEach(v => {
  386. if(v.type === '1' || v.type === '2'){
  387. // 下拉,单选
  388. let obj = v.list.find(vv => v.value === vv.value);
  389. v.isException = obj ? obj.checkException === 1 : false;
  390. } else if(v.type === '3'){
  391. // 多选
  392. v.isException = v.list.some(vv => {
  393. if(v.value.includes(vv.value)){
  394. return vv.checkException === 1;
  395. }else{
  396. return false;
  397. }
  398. })
  399. } else if(v.type === '4'){
  400. // 数值
  401. v.isException = (formValues[v.key] < v.valueMin) || (formValues[v.key] > v.valueMax);
  402. }
  403. })
  404. }
  405. // 单行|多行|图片上传
  406. let someIsException = firstPage.filter(v => v.type !== '8').some(v => v.isException);
  407. console.log('someIsException=>', someIsException)
  408. firstPageBottom.forEach(v => {
  409. console.log('v.showError=>', v.showError)
  410. v.isException = (v.showError === 1 && someIsException) || v.showError === 0;
  411. })
  412. firstPage = firstPage.concat(firstPageBottom).sort((a, b) => a.orders - b.orders);
  413. console.log('firstPage=>', firstPage);
  414. Object.assign(baseFormData, firstPage);
  415. console.log('baseFormData=>', baseFormData);
  416. Object.assign(formValues, fromPairs(firstPage.filter(v => v.type !== '8').map(v => ([v.key, v.value]))));
  417. console.log('formValues=>', formValues);
  418. let rulesObj = keyBy(firstPage.filter(v => v.type !== '8'), 'key');
  419. console.log(rulesObj)
  420. for(let key in rulesObj){
  421. rulesObj[key] = {
  422. rules: [
  423. { required: rulesObj[key].required, errorMessage: `${rulesObj[key].name}不能为空` }
  424. ]
  425. }
  426. }
  427. Object.assign(rules, rulesObj);
  428. console.log('rules=>', rules);
  429. }
  430. onLoad((option) => {
  431. getSources();
  432. inspectionExecuteId.value = +option.inspectionExecuteId;
  433. // 巡检项
  434. if(inspectionValueStore.inspectionValue.data){
  435. Object.assign(dataInfo, inspectionValueStore.inspectionValue.data);
  436. Object.assign(formPageList, dataInfo.inspectionFormDTO?.formPageList || []);
  437. handleData()
  438. }
  439. })
  440. </script>
  441. <style lang="scss" scoped>
  442. .customLabelWrap{
  443. .is-required{
  444. color: #dd524d;
  445. font-weight: bold;
  446. margin-top: 8rpx;
  447. }
  448. }
  449. ::v-deep .uni-modal__btn_primary{
  450. color: $uni-primary!important;
  451. }
  452. .inspectionValue{
  453. height: 100%;
  454. display: flex;
  455. flex-direction: column;
  456. justify-content: space-between;
  457. .body{
  458. box-sizing: border-box;
  459. flex: 1;
  460. min-height: 0;
  461. padding-bottom: 24rpx;
  462. .formItem{
  463. padding: 32rpx 24rpx 0;
  464. margin-bottom: 0;
  465. }
  466. ::v-deep .uni-forms-item__label{
  467. width: auto!important;
  468. padding: 0!important;
  469. height: auto!important;
  470. align-items: flex-start!important;
  471. }
  472. .detail_head{
  473. padding: 24rpx;
  474. border-top: 1rpx solid #D2D2D2;
  475. border-bottom: 1rpx solid #D2D2D2;
  476. display: flex;
  477. justify-content: space-between;
  478. align-items: center;
  479. margin-top: 24rpx;
  480. &:first-of-type{
  481. border-top: none;
  482. margin-top: 0;
  483. }
  484. .title{
  485. font-size: 26rpx;
  486. color: $uni-primary;
  487. padding-left: 18rpx;
  488. position: relative;
  489. &:before{
  490. content: '';
  491. width: 8rpx;
  492. height: 25rpx;
  493. background-color: $uni-primary;
  494. position: absolute;
  495. left: 0;
  496. top: 50%;
  497. transform: translateY(-50%);
  498. }
  499. }
  500. }
  501. }
  502. }
  503. </style>