ProblemReportModal.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. @register="registerModal"
  5. @ok="handleSubmit"
  6. :title="getTitle"
  7. width="900px"
  8. :min-height="600"
  9. :max-height="1000"
  10. scrollable
  11. centered
  12. destroyOnClose
  13. :bodyStyle="{ padding: '20px' }"
  14. >
  15. <a-form ref="formRef" :model="formData" :rules="formRules" layout="horizontal" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
  16. <!-- 查看模式 -->
  17. <div class="que-container" v-if="mode === 'view'">
  18. <div class="que-status">
  19. <div>矿井状态:</div>
  20. <div>
  21. <span :class="getStatusClass(currentRecord?.mineLinkStatus)" class="status-dot">
  22. {{ getStatusText(currentRecord?.mineLinkStatus) }}
  23. </span>
  24. </div>
  25. </div>
  26. <div class="que-item" v-for="(item, index) in queList" :key="index">
  27. <div class="que-details">
  28. <div class="que-field">
  29. <span class="que-value que-goafName">{{ item.queTitle || '-' }}</span>
  30. </div>
  31. <div class="que-field time-field">
  32. <span class="que-label">时间:</span>
  33. <span class="que-value">{{ formatDate(item.startTime) || '-' }}</span>
  34. <span class="time-separator">-----</span>
  35. <span class="que-value">{{ formatDate(item.endTime) || '-' }}</span>
  36. </div>
  37. <div class="que-field">
  38. <span class="que-label">问题描述:</span>
  39. <span class="que-value">{{ item.queCon || '-' }}</span>
  40. </div>
  41. <!-- <div class="que-field">
  42. <span class="que-label">参数:</span>
  43. <span class="que-value">{{ (item.param || '-').replace(/,/g, ' ') }}</span>
  44. </div> -->
  45. </div>
  46. </div>
  47. </div>
  48. <!-- 编辑/新增模式 -->
  49. <div v-else class="edit-container">
  50. <div class="mine-base-info" v-if="mode === 'add'">
  51. <a-form-item name="deptId" label="煤矿名称">
  52. <MineCascader
  53. v-model:value="innerValue"
  54. :sync-to-store="false"
  55. :init-from-store="false"
  56. :change-on-select="false"
  57. :allow-clear="false"
  58. @change="changeCascader"
  59. />
  60. </a-form-item>
  61. <a-form-item name="goafId" label="密闭名称">
  62. <a-select v-model:value="goafId" :options="goafOptions" placeholder="请选择" />
  63. </a-form-item>
  64. </div>
  65. <!-- 问题项编辑区 -->
  66. <div class="que-item" v-for="(item, index) in queList" :key="item.orderNum || index">
  67. <div class="que-index">问题 {{ index + 1 }}</div>
  68. <div class="edit-fields">
  69. <a-form-item
  70. v-for="schema in formSchema"
  71. :key="`${schema.field}-${index}`"
  72. :name="[`queList`, index, schema.field]"
  73. :label="schema.label"
  74. :rules="schema.rules"
  75. >
  76. <component
  77. :is="getComponent(schema.component)"
  78. v-model:value="item[schema.field]"
  79. v-bind="schema.componentProps"
  80. :placeholder="`请输入${schema.label}`"
  81. style="width: 100%"
  82. />
  83. </a-form-item>
  84. <div class="form-actions">
  85. <a-button type="text" danger @click="removeItem(index)" :disabled="queList.length <= 1"> 删除 </a-button>
  86. </div>
  87. </div>
  88. </div>
  89. <a-button type="dashed" class="add-btn" @click="addNewItem"> <plus-outlined /> 新增问题 </a-button>
  90. </div>
  91. </a-form>
  92. </BasicModal>
  93. </template>
  94. <script setup lang="ts">
  95. import { ref, computed, reactive, watch } from 'vue';
  96. import { BasicModal, useModalInner } from '/@/components/Modal';
  97. import { formSchema } from '../problemReport.data';
  98. import { Select, Input, DatePicker, message } from 'ant-design-vue';
  99. import { PlusOutlined } from '@ant-design/icons-vue';
  100. import dayjs, { Dayjs } from 'dayjs';
  101. import MineCascader from '/@/components/Form/src/jeecg/components/MineCascader/MineCascader.vue';
  102. import type { FormInstance, RuleObject } from 'ant-design-vue/es/form';
  103. import { JEditor, JImageUpload } from '/@/components/Form';
  104. import { useInitForm } from '/@/views/analysis/warningAnalysis/connectAnalysis/hooks/form';
  105. import { useMineDepartmentStore } from '/@/store/modules/mine';
  106. // 组件映射表
  107. const componentMap = {
  108. Input,
  109. DatePicker,
  110. Select,
  111. InputTextArea: Input.TextArea,
  112. MineCascader,
  113. JEditor,
  114. JImageUpload,
  115. };
  116. // 定义事件发射
  117. const emit = defineEmits(['success']);
  118. const mineStore = useMineDepartmentStore();
  119. const { goafOptions, goafId, innerValue, initGoafOptions } = useInitForm();
  120. // 模态框模式:查看/编辑/新增
  121. const mode = ref<'view' | 'edit' | 'add'>('view');
  122. // 当前记录数据
  123. const currentRecord = ref<any>({
  124. mineCode: '',
  125. mineLinkStatus: '0',
  126. queJson: [],
  127. isOk: false,
  128. updateTime: '',
  129. createTime: '',
  130. });
  131. // 问题列表
  132. const queList = ref<any[]>([]);
  133. // 表单实例(用于校验)
  134. const formRef = ref<FormInstance>();
  135. // 表单数据聚合(适配Form组件的model)
  136. const formData = reactive({
  137. mineCode: '',
  138. queList: queList.value,
  139. });
  140. // 合并表单规则(从schema中提取)
  141. const formRules = reactive({
  142. queList: {
  143. type: 'array',
  144. required: true,
  145. validator: (rule: RuleObject, value: any[], callback: Function) => {
  146. // 校验每个问题项
  147. if (value.length === 0) {
  148. callback(new Error('至少需要填写一条问题信息'));
  149. return;
  150. }
  151. // 逐个校验问题项的必填字段
  152. for (let i = 0; i < value.length; i++) {
  153. const item = value[i];
  154. // 校验工作面名称
  155. if (!item.goafName) {
  156. callback(new Error(`第${i + 1}条问题:工作面名称不能为空`));
  157. return;
  158. }
  159. // 校验问题描述
  160. if (!item.queCon) {
  161. callback(new Error(`第${i + 1}条问题:问题描述不能为空`));
  162. return;
  163. }
  164. // 校验开始时间
  165. if (!item.startTime) {
  166. callback(new Error(`第${i + 1}条问题:开始时间不能为空`));
  167. return;
  168. }
  169. // 校验结束时间
  170. if (!item.endTime) {
  171. callback(new Error(`第${i + 1}条问题:结束时间不能为空`));
  172. return;
  173. }
  174. // 校验结束时间不能早于开始时间
  175. if (dayjs(item.endTime).isBefore(dayjs(item.startTime))) {
  176. callback(new Error(`第${i + 1}条问题:结束时间不能早于开始时间`));
  177. return;
  178. }
  179. // 校验参数
  180. if (!item.param) {
  181. callback(new Error(`第${i + 1}条问题:参数不能为空`));
  182. return;
  183. }
  184. }
  185. callback();
  186. },
  187. },
  188. });
  189. // 监听queList变化,同步到formData
  190. watch(
  191. queList,
  192. (newVal) => {
  193. formData.queList = JSON.parse(JSON.stringify(newVal));
  194. },
  195. { deep: true, immediate: true }
  196. );
  197. // 监听currentRecord变化,同步mineCode到formData
  198. watch(
  199. () => currentRecord.value.mineCode,
  200. (newVal) => {
  201. formData.mineCode = newVal;
  202. },
  203. { immediate: true }
  204. );
  205. // 根据组件名获取对应组件
  206. const getComponent = (componentName: string) => {
  207. return componentMap[componentName as keyof typeof componentMap];
  208. };
  209. // 日期格式化方法
  210. const formatDate = (date: string | Dayjs | null) => {
  211. if (!date) return '';
  212. return dayjs(date).format('YYYY-MM-DD HH:mm:ss');
  213. };
  214. // 根据状态值获取显示文本(在线/离线)
  215. const getStatusText = (status: string | number) => {
  216. return status === '1' || status === 1 ? '在线' : '离线';
  217. };
  218. // 根据状态值获取样式类
  219. const getStatusClass = (status: string | number) => {
  220. return status === '1' || status === 1 ? 'status-online' : 'status-offline';
  221. };
  222. // 注册模态框并初始化数据
  223. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  224. setModalProps({ confirmLoading: false });
  225. // 接收模式参数
  226. mode.value = data?.mode || 'view';
  227. // 初始化当前记录
  228. currentRecord.value = data?.record
  229. ? JSON.parse(JSON.stringify(data.record))
  230. : {
  231. mineCode: '',
  232. mineLinkStatus: '0',
  233. queJson: [],
  234. isOk: false,
  235. updateTime: '',
  236. createTime: '',
  237. };
  238. // 同步mineCode到formData
  239. formData.mineCode = currentRecord.value.mineCode;
  240. // 初始化问题列表
  241. if (mode.value === 'add') {
  242. // 新增模式:初始化一条空数据
  243. queList.value = [
  244. {
  245. orderNum: '1',
  246. goafName: '',
  247. queCon: '',
  248. startTime: null,
  249. endTime: null,
  250. param: '',
  251. },
  252. ];
  253. formData.queList = JSON.parse(JSON.stringify(queList.value));
  254. if (formRef.value) {
  255. formRef.value.resetFields();
  256. }
  257. } else {
  258. // 编辑/查看模式:解析已有问题数据
  259. try {
  260. const rawData = currentRecord.value?.queJson
  261. ? typeof currentRecord.value.queJson === 'string'
  262. ? currentRecord.value.queJson.trim()
  263. ? JSON.parse(currentRecord.value.queJson)
  264. : [] // 兼容空字符串
  265. : currentRecord.value.queJson
  266. : [];
  267. // 兼容日期格式,避免转换失败
  268. queList.value = rawData.map((item: any) => ({
  269. ...item,
  270. startTime: item.startTime ? dayjs(item.startTime, ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD']) : null,
  271. endTime: item.endTime ? dayjs(item.endTime, ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD']) : null,
  272. }));
  273. // 深拷贝同步到formData,切断引用
  274. formData.queList = JSON.parse(JSON.stringify(queList.value));
  275. } catch (error) {
  276. console.error('解析问题数据失败:', error);
  277. message.warning('问题数据格式异常,将显示原始数据');
  278. // 保留原始queJson,而非重置为空数据
  279. queList.value = currentRecord.value?.queJson ? (typeof currentRecord.value.queJson === 'string' ? [] : currentRecord.value.queJson) : [];
  280. formData.queList = JSON.parse(JSON.stringify(queList.value));
  281. }
  282. }
  283. // 重置表单校验状态
  284. if (formRef.value) {
  285. formRef.value.resetFields();
  286. }
  287. });
  288. // 模态框标题计算属性
  289. const getTitle = computed(() => {
  290. const titleMap = {
  291. view: '密闭墙问题详情',
  292. edit: '编辑密闭墙问题',
  293. add: '新增密闭墙问题',
  294. };
  295. return titleMap[mode.value];
  296. });
  297. // 新增问题项
  298. const addNewItem = () => {
  299. const newItem = {
  300. orderNum: (queList.value.length + 1).toString(),
  301. goafName: '',
  302. queCon: '',
  303. startTime: null,
  304. endTime: null,
  305. param: '',
  306. };
  307. queList.value.push(newItem);
  308. };
  309. // 删除问题项
  310. const removeItem = (index: number) => {
  311. queList.value.splice(index, 1);
  312. queList.value.forEach((item, i) => {
  313. item.orderNum = (i + 1).toString();
  314. });
  315. };
  316. // 提交表单处理
  317. async function handleSubmit() {
  318. try {
  319. // 查看模式直接提交
  320. if (mode.value === 'view') {
  321. closeModal();
  322. return;
  323. }
  324. // 1. 表单校验
  325. if (!formRef.value) return;
  326. const validateResult = await formRef.value.validate();
  327. if (!validateResult) return;
  328. setModalProps({ confirmLoading: true });
  329. // 2. 处理问题列表数据(空日期字段置空,避免空字符串)
  330. const submitQueList = queList.value.map((item) => ({
  331. ...item,
  332. startTime: item.startTime ? item.startTime.format('YYYY-MM-DD HH:mm:ss') : null,
  333. endTime: item.endTime ? item.endTime.format('YYYY-MM-DD HH:mm:ss') : null,
  334. }));
  335. // 3. 构造完整提交数据
  336. const now = dayjs().format('YYYY-MM-DD HH:mm:ss');
  337. const result = {
  338. ...currentRecord.value,
  339. deptId: innerValue.value,
  340. goafId: goafId.value,
  341. mineCode: mode.value === 'add' ? mineStore.findDepartById(innerValue.value)?.fax : formData.mineCode,
  342. queJson: JSON.stringify(submitQueList),
  343. createTime: mode.value === 'add' ? now : currentRecord.value.createTime || now,
  344. updateTime: now,
  345. isOk: mode.value === 'add' ? false : currentRecord.value.isOk,
  346. };
  347. // console.log('最终提交数据:', result);
  348. emit('success', result);
  349. closeModal();
  350. } catch (error: any) {
  351. console.error('提交失败:', error);
  352. // 显示校验错误提示
  353. message.error(error.message || '表单校验失败,请检查必填项');
  354. } finally {
  355. setModalProps({ confirmLoading: false });
  356. }
  357. }
  358. function changeCascader(val) {
  359. innerValue.value = val;
  360. initGoafOptions(val);
  361. }
  362. </script>
  363. <style scoped>
  364. .que-container {
  365. display: flex;
  366. flex-direction: column;
  367. gap: 16px;
  368. }
  369. .que-status {
  370. display: flex;
  371. width: 100%;
  372. background-color: #f8f9fc;
  373. padding: 8px 16px;
  374. align-items: center;
  375. border: 1px solid #cad2e0;
  376. border-radius: 5px;
  377. }
  378. .mine-info {
  379. padding: 12px 16px;
  380. border: 1px solid #cad2e0;
  381. border-radius: 5px;
  382. background-color: #f8f9fc;
  383. }
  384. .mine-field {
  385. display: flex;
  386. align-items: center;
  387. gap: 8px;
  388. margin-bottom: 8px;
  389. }
  390. .status-dot {
  391. position: relative;
  392. padding-left: 12px;
  393. font-weight: 500;
  394. }
  395. .status-dot::before {
  396. content: '';
  397. position: absolute;
  398. left: 0;
  399. top: 50%;
  400. transform: translateY(-50%);
  401. width: 6px;
  402. height: 6px;
  403. border-radius: 50%;
  404. background-color: inherit;
  405. }
  406. .status-online {
  407. color: #10952c;
  408. font-weight: 500;
  409. }
  410. .status-offline {
  411. color: #f5222d;
  412. font-weight: 500;
  413. }
  414. .status-online.status-dot::before {
  415. background-color: #10952c;
  416. }
  417. .status-offline.status-dot::before {
  418. background-color: #f5222d;
  419. }
  420. .que-item {
  421. padding: 8px 16px;
  422. border: 1px solid #cad2e0;
  423. border-radius: 8px;
  424. background-color: #f8f9fc;
  425. }
  426. .que-index {
  427. font-size: 16px;
  428. font-weight: 600;
  429. color: #1890ff;
  430. margin-bottom: 12px;
  431. padding-bottom: 8px;
  432. border-bottom: 1px dashed #e8e8e8;
  433. }
  434. .que-details {
  435. width: 100%;
  436. }
  437. .que-field {
  438. display: flex;
  439. align-items: center;
  440. gap: 8px;
  441. min-width: 200px;
  442. margin-bottom: 10px;
  443. }
  444. .que-label {
  445. font-size: 16px;
  446. color: #868789;
  447. white-space: nowrap;
  448. }
  449. .que-value {
  450. font-size: 16px;
  451. color: #838486;
  452. word-break: break-all;
  453. }
  454. .que-goafName {
  455. color: #4c4c4e;
  456. font-size: 20px;
  457. }
  458. .time-field {
  459. flex: 1;
  460. min-width: 420px;
  461. }
  462. .time-separator {
  463. color: #999;
  464. margin: 0 8px;
  465. }
  466. .edit-container {
  467. display: flex;
  468. flex-direction: column;
  469. gap: 16px;
  470. }
  471. .mine-base-info {
  472. padding: 12px 16px;
  473. border: 1px solid #cad2e0;
  474. border-radius: 5px;
  475. background-color: #f8f9fc;
  476. }
  477. .edit-fields {
  478. width: 100%;
  479. }
  480. .form-item {
  481. display: flex;
  482. margin-bottom: 10px;
  483. align-items: center;
  484. }
  485. .form-label {
  486. width: 20%;
  487. color: #666;
  488. font-size: 14px;
  489. }
  490. .add-btn {
  491. margin-top: 8px;
  492. width: 100%;
  493. }
  494. .form-actions {
  495. display: flex;
  496. justify-content: end;
  497. margin-bottom: 8px;
  498. }
  499. </style>