inspectionValue.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 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. inspectionValueStore.clearInspectionValueData();
  184. uni.reLaunch({
  185. url: `/pages/inspection/inspectionExecute/inspectionExecute`
  186. })
  187. } else {
  188. uni.showToast({
  189. icon: 'none',
  190. title: res.msg || '请求数据失败!'
  191. });
  192. }
  193. });
  194. }).catch(err => {
  195. console.log('err', err);
  196. })
  197. })
  198. }
  199. // 处理数据
  200. function handleData(isException = false){
  201. // 目前只取第一页
  202. let firstPage = formPageList[0]?.formItemList || [];
  203. firstPage = firstPage.filter( v => v.display === 1);
  204. firstPage = firstPage.map((v, i) => {
  205. let value = '';
  206. v.formItemConfigList = v.formItemConfigList || [];
  207. // value
  208. if(v.type.value === '3'){
  209. // 多选
  210. let arr = v.formItemConfigList.filter(v => v.checkDefault === 1);
  211. value = arr.length ? arr.map(v => v.id) : [];
  212. console.log('多选', value)
  213. } else if(v.type.value === '7'){
  214. // 图片
  215. value = [];
  216. } else if(v.type.value === '1' || v.type.value === '2') {
  217. // 单选或下拉
  218. let defaultValue = v.formItemConfigList.find(v => v.checkDefault === 1);
  219. value = defaultValue ? defaultValue.id : '';
  220. console.log('单选或下拉', value)
  221. } else{
  222. // 其他
  223. value = v.defaultValue;
  224. }
  225. return {
  226. id: v.id,
  227. key: `field${i}`,
  228. type: v.type.value,
  229. name: v.name,
  230. value: isException ? formValues[`field${i}`] : value,
  231. list: v.formItemConfigList ? v.formItemConfigList.map(v => ({text: v.name, value: v.id, checkException: v.checkException, uncheckException: v.uncheckException})) : [],
  232. required: v.required === 1,
  233. checkType: v.checkType,
  234. showError: v.showError,
  235. orders: v.orders,
  236. valueMin: v.checkType === 2 ? v.valueLow : ( v.checkType === 1 ? (v.valuex - v.valueGap) : undefined),
  237. valueMax: v.checkType === 2 ? v.valueUp : ( v.checkType === 1 ? (v.valuex + v.valueGap) : undefined),
  238. }
  239. });
  240. let firstPageBottom = firstPage.filter(v => v.type === '5' || v.type === '6' || v.type === '7');
  241. firstPage = firstPage.filter(v => v.type !== '5' && v.type !== '6' && v.type !== '7');
  242. // 其他项
  243. if(!isException){
  244. firstPage.forEach(v => {
  245. // 下拉,单选
  246. if(v.type === '1' || v.type === '2'){
  247. let obj = v.list.find(vv => formValues[v.key] === vv.value);
  248. v.isException = obj ? obj.checkException === 1 : false;
  249. } else if(v.type === '3'){
  250. // 多选
  251. v.isException = v.list.some(vv => {
  252. if(formValues[v.key].includes(vv.value)){
  253. return vv.checkException === 1;
  254. }else{
  255. return false;
  256. }
  257. })
  258. } else if(v.type === '4'){
  259. v.isException = (v.value < v.valueMin) || (v.value > v.valueMax);
  260. }
  261. })
  262. }else{
  263. firstPage.forEach(v => {
  264. if(v.type === '1' || v.type === '2'){
  265. // 下拉,单选
  266. let obj = v.list.find(vv => v.value === vv.value);
  267. v.isException = obj ? obj.checkException === 1 : false;
  268. } else if(v.type === '3'){
  269. // 多选
  270. v.isException = v.list.some(vv => {
  271. if(v.value.includes(vv.value)){
  272. return vv.checkException === 1;
  273. }else{
  274. return false;
  275. }
  276. })
  277. } else if(v.type === '4'){
  278. // 数值
  279. v.isException = (formValues[v.key] < v.valueMin) || (formValues[v.key] > v.valueMax);
  280. }
  281. })
  282. }
  283. // 单行|多行|图片上传
  284. let someIsException = firstPage.filter(v => v.type !== '8').some(v => v.isException);
  285. console.log('someIsException=>', someIsException)
  286. firstPageBottom.forEach(v => {
  287. console.log('v.showError=>', v.showError)
  288. v.isException = (v.showError === 1 && someIsException) || v.showError === 0;
  289. })
  290. firstPage = firstPage.concat(firstPageBottom).sort((a, b) => a.orders - b.orders);
  291. console.log('firstPage=>', firstPage);
  292. Object.assign(baseFormData, firstPage);
  293. console.log('baseFormData=>', baseFormData);
  294. Object.assign(formValues, fromPairs(firstPage.filter(v => v.type !== '8').map(v => ([v.key, v.value]))));
  295. console.log('formValues=>', formValues);
  296. let rulesObj = keyBy(firstPage.filter(v => v.type !== '8'), 'key');
  297. console.log(rulesObj)
  298. for(let key in rulesObj){
  299. rulesObj[key] = {
  300. rules: [
  301. { required: rulesObj[key].required, errorMessage: `${rulesObj[key].name}不能为空` }
  302. ]
  303. }
  304. }
  305. Object.assign(rules, rulesObj);
  306. console.log('rules=>', rules);
  307. }
  308. onLoad((option) => {
  309. inspectionExecuteId.value = +option.inspectionExecuteId;
  310. // 巡检项
  311. if(inspectionValueStore.inspectionValue.data){
  312. Object.assign(dataInfo, inspectionValueStore.inspectionValue.data);
  313. Object.assign(formPageList, dataInfo.inspectionFormDTO?.formPageList || []);
  314. handleData()
  315. }
  316. })
  317. </script>
  318. <style lang="scss" scoped>
  319. .customLabelWrap{
  320. .is-required{
  321. color: #dd524d;
  322. font-weight: bold;
  323. margin-top: 8rpx;
  324. }
  325. }
  326. .inspectionValue{
  327. height: 100%;
  328. display: flex;
  329. flex-direction: column;
  330. justify-content: space-between;
  331. .body{
  332. box-sizing: border-box;
  333. flex: 1;
  334. min-height: 0;
  335. padding-bottom: 24rpx;
  336. .formItem{
  337. padding: 24rpx 24rpx 0;
  338. margin-bottom: 0;
  339. }
  340. ::v-deep .uni-forms-item__label{
  341. width: auto!important;
  342. }
  343. .detail_head{
  344. padding: 24rpx;
  345. border-top: 1rpx solid #D2D2D2;
  346. border-bottom: 1rpx solid #D2D2D2;
  347. display: flex;
  348. justify-content: space-between;
  349. align-items: center;
  350. margin-top: 24rpx;
  351. &:first-of-type{
  352. border-top: none;
  353. margin-top: 0;
  354. }
  355. .title{
  356. font-size: 26rpx;
  357. color: $uni-primary;
  358. padding-left: 18rpx;
  359. position: relative;
  360. &:before{
  361. content: '';
  362. width: 8rpx;
  363. height: 25rpx;
  364. background-color: $uni-primary;
  365. position: absolute;
  366. left: 0;
  367. top: 50%;
  368. transform: translateY(-50%);
  369. }
  370. }
  371. }
  372. }
  373. }
  374. </style>