ProblemReportModal.vue 15 KB

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