inspectionValue.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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="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="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="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="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 v-model="formValues[item.key]"></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 } 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. const inspectionExecuteId = ref()
  93. // 数据-原始
  94. const dataInfo = reactive({})
  95. // 数据-检验
  96. const rules = reactive({})
  97. // 数据-填写值
  98. const formValues = reactive({})
  99. // 表单数据-渲染
  100. // 下拉框 1
  101. // 单选 2
  102. // 多选 3
  103. // 数值 4
  104. // 单行文本 5
  105. // 多行文本 6
  106. // 照片上传 7
  107. // 分割线 8
  108. const baseFormData = reactive([
  109. // { type: 1, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  110. // { type: 2, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  111. // { type: 3, name: '', value: [], list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  112. // { type: 4, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  113. // { type: 5, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  114. // { type: 6, name: '', value: '', list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  115. // { type: 7, name: '', value: [], list: [], required: false, checkType: 0, valueMin: '', valueMax: '', },
  116. ])
  117. // 修改
  118. function changeForm(){
  119. handleData(true);
  120. }
  121. // 保存
  122. function submit(){
  123. baseForm.value.validate().then(res => {
  124. console.log('success', res);
  125. uni.showLoading({
  126. title: "加载中",
  127. mask: true,
  128. });
  129. console.log(formValues);
  130. let postData = {
  131. account: loginUserStore.loginUser.user.account,
  132. valuesList: [],
  133. };
  134. // baseFormData
  135. for(let key in formValues){
  136. let obj = baseFormData.find(v => v.key === key);
  137. if(!((obj.type === '5' && !obj.isException) || (obj.type === '6' && !obj.isException) || (obj.type === '7' && !obj.isException))){
  138. let name = obj.name;
  139. let itemId = obj.id;
  140. let _formPageList = dataInfo.inspectionFormDTO?.formPageList || [];
  141. _formPageList = _formPageList.map(v => v.formItemList).flat();
  142. let formItem = _formPageList.find(v => v.id === itemId);
  143. postData.valuesList.push({
  144. taskId: inspectionExecuteId.value,
  145. nodeId: dataInfo.id,
  146. formId: dataInfo.inspectionFormDTO.id,
  147. itemId: formItem.id,
  148. pageId: formItem.pageId,
  149. orders: formItem.orders,
  150. name,
  151. valuex: (formItem.type.value === '1' || formItem.type.value === '2' || formItem.type.value === '3') ? '' : formValues[key].toString(),
  152. configIds: (formItem.type.value === '1' || formItem.type.value === '2' || formItem.type.value === '3') ? formValues[key].toString() : undefined,
  153. hosId: dataInfo.inspectionFormDTO.hosId,
  154. })
  155. }
  156. }
  157. console.log(postData);
  158. api_addModel(postData).then((res) => {
  159. uni.hideLoading();
  160. if (res.status == 200) {
  161. inspectionValueStore.clearInspectionValueData();
  162. uni.reLaunch({
  163. url: `/pages/inspection/inspectionExecute/inspectionExecute`
  164. })
  165. } else {
  166. uni.showToast({
  167. icon: 'none',
  168. title: res.msg || '请求数据失败!'
  169. });
  170. }
  171. });
  172. }).catch(err => {
  173. console.log('err', err);
  174. })
  175. }
  176. // 处理数据
  177. function handleData(isException = false){
  178. // 目前只取第一页
  179. let firstPage = formPageList[0]?.formItemList || [];
  180. firstPage = firstPage.filter( v => v.display === 1);
  181. firstPage = firstPage.map((v, i) => {
  182. let value = '';
  183. v.formItemConfigList = v.formItemConfigList || [];
  184. // value
  185. if(v.type.value === '3'){
  186. // 多选
  187. let arr = v.formItemConfigList.filter(v => v.checkDefault === 1);
  188. value = arr.length ? arr.map(v => v.id) : [];
  189. console.log('多选', value)
  190. } else if(v.type.value === '7'){
  191. // 图片
  192. value = [];
  193. } else if(v.type.value === '1' || v.type.value === '2') {
  194. // 单选或下拉
  195. let defaultValue = v.formItemConfigList.find(v => v.checkDefault === 1);
  196. value = defaultValue ? defaultValue.id : '';
  197. console.log('单选或下拉', value)
  198. } else{
  199. // 其他
  200. value = v.defaultValue;
  201. }
  202. return {
  203. id: v.id,
  204. key: `field${i}`,
  205. type: v.type.value,
  206. name: v.name,
  207. value: isException ? formValues[`field${i}`] : value,
  208. list: v.formItemConfigList ? v.formItemConfigList.map(v => ({text: v.name, value: v.id, checkException: v.checkException, uncheckException: v.uncheckException})) : [],
  209. required: v.required === 1,
  210. checkType: v.checkType,
  211. showError: v.showError,
  212. orders: v.orders,
  213. valueMin: v.checkType === 2 ? v.valueLow : ( v.checkType === 1 ? (v.valuex - v.valueGap) : undefined),
  214. valueMax: v.checkType === 2 ? v.valueUp : ( v.checkType === 1 ? (v.valuex + v.valueGap) : undefined),
  215. }
  216. });
  217. let firstPageBottom = firstPage.filter(v => v.type === '5' || v.type === '6');
  218. firstPage = firstPage.filter(v => v.type !== '5' && v.type !== '6' && v.type !== '7');
  219. // 其他项
  220. if(isException){
  221. firstPage.forEach(v => {
  222. // 下拉,单选
  223. if(v.type === '1' || v.type === '2'){
  224. let obj = v.list.find(vv => formValues[v.key] === vv.value);
  225. v.isException = obj ? obj.checkException === 1 : false;
  226. } else if(v.type === '3'){
  227. // 多选
  228. v.isException = v.list.some(vv => {
  229. if(formValues[v.key].includes(vv.value)){
  230. return vv.checkException === 1;
  231. }else{
  232. return false;
  233. }
  234. })
  235. } else if(v.type === '4'){
  236. v.isException = (v.value < v.valueMin) || (v.value > v.valueMax);
  237. }
  238. })
  239. }else{
  240. firstPage.forEach(v => {
  241. if(v.type === '1' || v.type === '2'){
  242. // 下拉,单选
  243. let obj = v.list.find(vv => v.value === vv.value);
  244. v.isException = obj ? obj.checkException === 1 : false;
  245. } else if(v.type === '3'){
  246. // 多选
  247. v.isException = v.list.some(vv => {
  248. if(v.value.includes(vv.value)){
  249. return vv.checkException === 1;
  250. }else{
  251. return false;
  252. }
  253. })
  254. } else if(v.type === '4'){
  255. // 数值
  256. v.isException = (formValues[v.key] < v.valueMin) || (formValues[v.key] > v.valueMax);
  257. }
  258. })
  259. }
  260. // 单行|多行|图片上传
  261. let someIsException = firstPage.filter(v => v.type !== '8').some(v => v.isException);
  262. console.log('someIsException=>', someIsException)
  263. firstPageBottom.forEach(v => {
  264. console.log('v.showError=>', v.showError)
  265. v.isException = (v.showError === 1 && someIsException) || v.showError === 0;
  266. })
  267. firstPage = firstPage.concat(firstPageBottom).sort((a, b) => a.orders - b.orders);
  268. console.log('firstPage=>', firstPage);
  269. Object.assign(baseFormData, firstPage);
  270. console.log('baseFormData=>', baseFormData);
  271. Object.assign(formValues, fromPairs(firstPage.filter(v => v.type !== '8').map(v => ([v.key, v.value]))));
  272. console.log('formValues=>', formValues);
  273. let rulesObj = keyBy(firstPage.filter(v => v.type !== '8'), 'key');
  274. console.log(rulesObj)
  275. for(let key in rulesObj){
  276. rulesObj[key] = {
  277. rules: [
  278. { required: rulesObj[key].required, errorMessage: `${rulesObj[key].name}不能为空` }
  279. ]
  280. }
  281. }
  282. Object.assign(rules, rulesObj);
  283. console.log('rules=>', rules);
  284. }
  285. onLoad((option) => {
  286. inspectionExecuteId.value = +option.inspectionExecuteId;
  287. // 巡检项
  288. if(inspectionValueStore.inspectionValue.data){
  289. Object.assign(dataInfo, inspectionValueStore.inspectionValue.data);
  290. Object.assign(formPageList, dataInfo.inspectionFormDTO?.formPageList || []);
  291. handleData()
  292. }
  293. })
  294. </script>
  295. <style lang="scss" scoped>
  296. .inspectionValue{
  297. height: 100%;
  298. display: flex;
  299. flex-direction: column;
  300. justify-content: space-between;
  301. .body{
  302. box-sizing: border-box;
  303. flex: 1;
  304. min-height: 0;
  305. padding-bottom: 24rpx;
  306. .formItem{
  307. padding: 24rpx 24rpx 0;
  308. margin-bottom: 0;
  309. }
  310. ::v-deep .uni-forms-item__label{
  311. width: auto!important;
  312. }
  313. .detail_head{
  314. padding: 24rpx;
  315. border-top: 1rpx solid #D2D2D2;
  316. border-bottom: 1rpx solid #D2D2D2;
  317. display: flex;
  318. justify-content: space-between;
  319. align-items: center;
  320. margin-top: 24rpx;
  321. &:first-of-type{
  322. border-top: none;
  323. margin-top: 0;
  324. }
  325. .title{
  326. font-size: 26rpx;
  327. color: $uni-primary;
  328. padding-left: 18rpx;
  329. position: relative;
  330. &:before{
  331. content: '';
  332. width: 8rpx;
  333. height: 25rpx;
  334. background-color: $uni-primary;
  335. position: absolute;
  336. left: 0;
  337. top: 50%;
  338. transform: translateY(-50%);
  339. }
  340. }
  341. }
  342. }
  343. }
  344. </style>