ProblemReportModal.vue 15 KB

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