inspectionValue.vue 12 KB

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