form.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <script setup>
  2. import { ref, computed, watch, nextTick, reactive } from 'vue';
  3. import { onLoad } from '@dcloudio/uni-app';
  4. import { getRuiHenReportDetail } from '@/api/ruihen';
  5. import { useDataDictStore } from '@/store/modules/dataDict';
  6. const props = defineProps({
  7. type: {
  8. type: String,
  9. default: 'edit',
  10. },
  11. });
  12. const FORM_KEYS = [
  13. 'id',
  14. 'deptId',
  15. 'projectId',
  16. 'taskId',
  17. 'deptName',
  18. 'contractName',
  19. 'taskName',
  20. 'dailyGasInjection',
  21. 'dailyWaterInjection',
  22. 'dailyInjectGasTime',
  23. 'dailyInjectWaterTime',
  24. 'nonProductionTime',
  25. 'nptReason',
  26. 'productionStatus',
  27. 'remark',
  28. 'relocationDays',
  29. 'capacity',
  30. 'createTime',
  31. 'opinion',
  32. 'status',
  33. 'auditStatus',
  34. ];
  35. const formType = ref('edit');
  36. const initFormData = () => ({
  37. dailyGasInjection: 0,
  38. dailyWaterInjection: 0,
  39. dailyInjectGasTime: 0,
  40. dailyInjectWaterTime: 0,
  41. nonProductionTime: 0,
  42. relocationDays: 0,
  43. capacity: 0,
  44. });
  45. const form = ref(initFormData());
  46. async function loadDetail(id) {
  47. try {
  48. const { data } = await getRuiHenReportDetail({ id });
  49. FORM_KEYS.forEach(key => {
  50. form.value[key] = data[key] ?? form.value[key];
  51. });
  52. form.value.id = id;
  53. if (props.type.includes('approval') && data.auditStatus !== 10) {
  54. formType.value = 'readonly';
  55. }
  56. if (props.type.includes('edit') && data.status !== 0) {
  57. formType.value = 'readonly';
  58. }
  59. if (props.type.includes('detail')) {
  60. formType.value = 'readonly';
  61. }
  62. if (!form.value.capacity) {
  63. uni.showToast({ title: '请维护增压机产能' });
  64. }
  65. } finally {
  66. }
  67. }
  68. const dictStore = useDataDictStore();
  69. const nptReasonOptions = ref([]);
  70. const loadOptions = () => {
  71. nptReasonOptions.value = dictStore.getStrDictOptions('nptReason').map(v => ({
  72. text: v.label,
  73. value: v.value,
  74. }));
  75. };
  76. onLoad(options => {
  77. if (dictStore.dataDict.length <= 0) {
  78. dictStore.loadDataDictList().then(() => {
  79. loadOptions();
  80. });
  81. } else loadOptions();
  82. loadDetail(options.id);
  83. });
  84. const defaultProps = computed(() => ({
  85. inputBorder: false,
  86. clearable: false,
  87. placeholder: '请输入',
  88. style: {
  89. 'text-align': 'right',
  90. },
  91. styles: {
  92. disableColor: '#fff',
  93. },
  94. }));
  95. const disabled = computed(() => field => {
  96. if (field === 'edit')
  97. return formType.value === 'readonly' || props.type.includes('approval') || props.type.includes('detail');
  98. else return formType.value === 'readonly';
  99. });
  100. const transitTime = computed(() => {
  101. const cap = form.value.capacity;
  102. const gas = form.value.dailyGasInjection ?? 0;
  103. if (!cap) return { original: 0, value: '0%' };
  104. const original = gas / cap;
  105. return { original, value: (original * 100).toFixed(2) + '%' };
  106. });
  107. const formRef = ref(null);
  108. // 辅助函数:计算总时间
  109. const sumTimes = () => {
  110. // 注意:uni-app input 出来可能是字符串,需转数字
  111. const gas = Number(form.value.dailyInjectGasTime) || 0;
  112. const water = Number(form.value.dailyInjectWaterTime) || 0;
  113. const npt = Number(form.value.nonProductionTime) || 0;
  114. return parseFloat((gas + water + npt).toFixed(2));
  115. };
  116. // 校验函数:总时间必须为 24
  117. const validateTotalTime = (rule, value, data, callback) => {
  118. console.log('value :>> ', value);
  119. const total = sumTimes();
  120. if (total !== 24) {
  121. callback(`当前合计 ${total} 小时,三项之和须为 24`);
  122. }
  123. return true; // uni-forms 如果没调用 callback error,需返回 true 代表通过
  124. };
  125. // // 校验函数:非生产时间原因
  126. // const validateNptReason = (rule, value, data, callback) => {
  127. // const npt = Number(form.value.nonProductionTime) || 0;
  128. // console.log('npt :>> ', npt);
  129. // console.log('value :>> ', value);
  130. // if (npt > 0 && !value) {
  131. // callback('非生产时间大于 0 时,必须选择原因');
  132. // }
  133. // return true;
  134. // };
  135. // 复用的时间规则
  136. const timeRuleItem = {
  137. rules: [
  138. { required: true, errorMessage: '请输入时间' },
  139. { validateFunction: validateTotalTime }, // 关联自定义校验
  140. ],
  141. };
  142. // uni-forms 规则定义
  143. const rules = reactive({
  144. dailyGasInjection: {
  145. rules: [{ required: true, errorMessage: '请输入当日注气量' }],
  146. },
  147. dailyWaterInjection: {
  148. rules: [{ required: true, errorMessage: '请输入当日注水量' }],
  149. },
  150. productionStatus: {
  151. rules: [{ required: true, errorMessage: '请输入生产动态' }],
  152. },
  153. // 时间字段应用复用规则
  154. dailyInjectGasTime: timeRuleItem,
  155. dailyInjectWaterTime: timeRuleItem,
  156. nonProductionTime: timeRuleItem,
  157. // nptReason: {
  158. // rules: [{ validateFunction: validateNptReason }],
  159. // },
  160. });
  161. watch(
  162. [() => form.value.dailyInjectGasTime, () => form.value.dailyInjectWaterTime, () => form.value.nonProductionTime],
  163. () => {
  164. nextTick(() => {
  165. formRef.value?.validateField(['nptReason']).catch(() => {});
  166. if (sumTimes() === 24) {
  167. formRef.value?.clearValidate(['dailyInjectGasTime', 'dailyInjectWaterTime', 'nonProductionTime']);
  168. }
  169. });
  170. }
  171. );
  172. defineExpose({ formRef, form, loadDetail });
  173. </script>
  174. <template>
  175. <view class="content">
  176. <view class="tip">
  177. <view class="item">
  178. <view class="left">
  179. <span>运行时效:</span>
  180. 当日注气量 / 产能
  181. </view>
  182. <span class="right red"> >120% 红色预警 </span>
  183. </view>
  184. <view class="item">
  185. <view class="left">
  186. <span>时间平衡:</span>
  187. 注气 + 注水 + 非生产 = 24H
  188. </view>
  189. <span class="right orange"> ≠24H 橙色预警 </span>
  190. </view>
  191. </view>
  192. <view v-if="!type.includes('approval') && form.opinion" class="opinion">
  193. <span class="left">审批意见:</span>
  194. <span class="right"> {{ form.opinion }} </span>
  195. </view>
  196. <uni-forms
  197. ref="formRef"
  198. labelWidth="auto"
  199. :model="form"
  200. :rules="rules"
  201. validateTrigger="blur"
  202. err-show-type="toast">
  203. <uni-forms-item label="施工队伍" name="deptName">
  204. <span class="readOnly">{{ form.deptName }}</span>
  205. </uni-forms-item>
  206. <uni-forms-item label="项目" name="contractName">
  207. <span class="readOnly">{{ form.contractName }}</span>
  208. </uni-forms-item>
  209. <uni-forms-item label="任务" name="taskName">
  210. <span class="readOnly">{{ form.taskName }}</span>
  211. </uni-forms-item>
  212. <uni-forms-item label="搬迁安装天数(D)" name="relocationDays">
  213. <span class="readOnly">{{ form.relocationDays }}</span>
  214. </uni-forms-item>
  215. <uni-forms-item label="运行时效" name="transitTime">
  216. <span class="readOnly" :class="{ 'red-text': transitTime.original > 1.2 }">{{ transitTime.value }}</span>
  217. </uni-forms-item>
  218. <uni-forms-item label="当日注气量(方)" name="dailyGasInjection" required>
  219. <uni-easyinput
  220. type="number"
  221. v-bind="defaultProps"
  222. :disabled="disabled('edit')"
  223. v-model="form.dailyGasInjection" />
  224. </uni-forms-item>
  225. <uni-forms-item label="当日注水量(方)" name="dailyWaterInjection" required>
  226. <uni-easyinput
  227. type="number"
  228. v-bind="defaultProps"
  229. :disabled="disabled('edit')"
  230. v-model="form.dailyWaterInjection" />
  231. </uni-forms-item>
  232. <uni-forms-item label="当日注气时间(H)" name="dailyInjectGasTime" required>
  233. <uni-easyinput
  234. type="number"
  235. v-bind="defaultProps"
  236. :class="{ 'orange-text': sumTimes() !== 24 }"
  237. :disabled="disabled('edit')"
  238. v-model="form.dailyInjectGasTime" />
  239. </uni-forms-item>
  240. <uni-forms-item label="当日注水时间(H)" name="dailyInjectWaterTime" required>
  241. <uni-easyinput
  242. type="number"
  243. v-bind="defaultProps"
  244. :class="{ 'orange-text': sumTimes() !== 24 }"
  245. :disabled="disabled('edit')"
  246. v-model="form.dailyInjectWaterTime" />
  247. </uni-forms-item>
  248. <uni-forms-item label="非生产时间(H)" name="nonProductionTime" required>
  249. <uni-easyinput
  250. type="number"
  251. v-bind="defaultProps"
  252. :class="{ 'orange-text': sumTimes() !== 24 }"
  253. :disabled="disabled('edit')"
  254. v-model="form.nonProductionTime" />
  255. </uni-forms-item>
  256. <uni-forms-item label="非生产时间原因" name="nptReason">
  257. <uni-data-select
  258. :clear="true"
  259. align="right"
  260. placeholder="请选择"
  261. :localdata="nptReasonOptions"
  262. placement="top"
  263. :disabled="disabled('edit')"
  264. v-model="form.nptReason" />
  265. </uni-forms-item>
  266. <uni-forms-item label="生产动态" name="productionStatus" required>
  267. <uni-easyinput
  268. type="textarea"
  269. autoHeight
  270. v-bind="defaultProps"
  271. v-model="form.productionStatus"
  272. :disabled="disabled('edit')"
  273. :maxlength="1000" />
  274. </uni-forms-item>
  275. <uni-forms-item label="备注" name="remark">
  276. <uni-easyinput
  277. type="textarea"
  278. autoHeight
  279. v-bind="defaultProps"
  280. :disabled="disabled('edit')"
  281. v-model="form.remark"
  282. :maxlength="1000" />
  283. </uni-forms-item>
  284. <uni-forms-item v-if="type.includes('approval')" label="审批意见" name="opinion">
  285. <uni-easyinput
  286. type="textarea"
  287. autoHeight
  288. v-bind="defaultProps"
  289. :disabled="disabled('approval')"
  290. v-model="form.opinion"
  291. :maxlength="1000" />
  292. </uni-forms-item>
  293. </uni-forms>
  294. </view>
  295. </template>
  296. <style lang="scss" scoped>
  297. .content {
  298. background-color: white;
  299. padding: 16px 16px;
  300. border-radius: 8px;
  301. box-sizing: border-box;
  302. }
  303. .uni-forms {
  304. margin-top: 10px;
  305. height: 100%;
  306. .uni-form {
  307. height: 100%;
  308. }
  309. .uni-forms-item {
  310. display: flex;
  311. align-items: center;
  312. flex: 1;
  313. margin-bottom: 6px;
  314. border-bottom: 1px dashed #cacccf;
  315. }
  316. :deep(.uni-forms-item__content) {
  317. text-align: right;
  318. .readOnly {
  319. padding-right: 10px;
  320. }
  321. }
  322. :deep(.uni-forms-item__label) {
  323. height: 44px;
  324. font-weight: 500;
  325. font-size: 14px;
  326. color: #333333 !important;
  327. width: max-content !important;
  328. }
  329. :deep(.uni-select) {
  330. border: none;
  331. text-align: right;
  332. padding-right: 0;
  333. .uniui-bottom:before {
  334. content: '\e6b5' !important;
  335. font-size: 16px !important;
  336. }
  337. }
  338. :deep(.uni-easyinput__content-textarea) {
  339. min-height: inherit;
  340. margin: 10px;
  341. }
  342. :deep(.is-disabled) {
  343. color: #333333 !important;
  344. }
  345. :deep(.red-text > .is-disabled) {
  346. color: rgb(220 38 38 / 0.8) !important;
  347. }
  348. :deep(.orange-text > .is-disabled) {
  349. color: rgb(234 88 12 / 0.8) !important;
  350. }
  351. :deep(.uni-select--disabled) {
  352. background-color: #fff;
  353. }
  354. }
  355. .red-text {
  356. color: rgb(220 38 38 / 0.8) !important;
  357. }
  358. .orange-text {
  359. color: rgb(234 88 12 / 0.8) !important;
  360. }
  361. .red {
  362. border: 1px solid rgb(254 226 226);
  363. color: rgb(220 38 38 / 0.8);
  364. background-color: rgb(254 226 226);
  365. }
  366. .orange {
  367. border: 1px solid rgb(254 215 170);
  368. color: rgb(234 88 12 / 0.8);
  369. background-color: rgb(254 215 170);
  370. }
  371. .tip {
  372. border-radius: 8px;
  373. border: 1px solid #e5e5e5;
  374. background-color: rgba(239, 246, 255, 0.8);
  375. box-sizing: border-box;
  376. padding: 10px;
  377. display: flex;
  378. flex-direction: column;
  379. gap: 6px;
  380. .item {
  381. display: flex;
  382. align-items: center;
  383. justify-content: space-between;
  384. font-size: 12px;
  385. .left {
  386. color: rgb(75 85 99);
  387. span {
  388. color: rgb(31 41 55);
  389. font-weight: 600;
  390. }
  391. }
  392. .right {
  393. display: inline-flex;
  394. align-items: center;
  395. border-radius: 4px;
  396. padding: 2px 4px;
  397. font-weight: 500;
  398. }
  399. }
  400. }
  401. .opinion {
  402. border-radius: 8px;
  403. border: 1px solid rgb(254 240 138);
  404. background-color: rgb(254 252 232);
  405. box-sizing: border-box;
  406. padding: 10px;
  407. display: flex;
  408. align-items: center;
  409. justify-content: space-between;
  410. font-size: 12px;
  411. margin-top: 10px;
  412. .left {
  413. font-weight: 600;
  414. color: rgb(133 77 14);
  415. }
  416. .right {
  417. font-weight: 500;
  418. color: rgb(75 85 99);
  419. }
  420. }
  421. </style>