ProblemReportModal.vue 15 KB

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