EditEquirement.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <script setup lang="ts">
  2. import { VxeUI, type VxeFormProps, type VxeFormListeners } from 'vxe-pc-ui';
  3. import { useRequest } from 'alova/client';
  4. import { addDeviceRegisterMethod, getDeviceRegisterDetailMethod } from '@/request/api/device.api';
  5. import { branchMethod } from '@/request/api/system.api';
  6. import { notification } from 'ant-design-vue';
  7. import { getDictionaryMethod } from '@/request/api/dictionary.api';
  8. import type { EquirementModel } from '@/model/device.model';
  9. type FollowModel = Partial<EquirementModel>;
  10. const defaultModel = {
  11. deviceIds: [''],
  12. };
  13. const props = defineProps<{ data: FollowModel }>();
  14. const emits = defineEmits<{
  15. submit: [data?: EquirementModel];
  16. }>();
  17. const model = ref<EquirementModel>({ ...defaultModel });
  18. watchEffect(() => {
  19. if (props.data) {
  20. model.value = { ...defaultModel, ...props.data };
  21. }
  22. });
  23. // 获取详情
  24. const getDetail = async () => {
  25. const res = await getDeviceRegisterDetailMethod(props.data);
  26. if (res) {
  27. model.value = { ...defaultModel, ...res };
  28. model.value.deviceIds = [model.value.deviceCode ?? ''];
  29. }
  30. };
  31. onMounted(() => {
  32. if (props.data && props.data.id) {
  33. getDetail();
  34. }
  35. });
  36. const branch = ref<any[]>([]);
  37. const { loading: branchLoading } = useRequest(branchMethod(0, 1, 1)).onSuccess(({ data }) => {
  38. const to = (data?: any[]): any[] => {
  39. return Array.isArray(data)
  40. ? data.map((item) => {
  41. return {
  42. ...item,
  43. value: item.id,
  44. key: item.id.toString(),
  45. children: to(item.children),
  46. };
  47. })
  48. : [];
  49. };
  50. branch.value = to(data);
  51. });
  52. const { loading: submitting, send: submit } = useRequest(addDeviceRegisterMethod, { immediate: false }).onSuccess(() => {
  53. emits('submit');
  54. });
  55. // 获取设备类型
  56. const deviceTypes = ref<{ id: string; name: string }[]>([]);
  57. const deviceTypesLoading = ref(false);
  58. // 获取设备类型
  59. async function getDeviceType() {
  60. deviceTypesLoading.value = true;
  61. const res = await getDictionaryMethod('fdhb_device_type');
  62. if (res && res.length > 0) {
  63. console.log(res, 'res==>');
  64. deviceTypes.value = res.map((item: any) => ({
  65. id: item.value,
  66. name: item.label,
  67. }));
  68. }
  69. console.log(deviceTypes.value, 'deviceTypes==>');
  70. // 设备类型加载后,若当前值是名称则映射成对应的id
  71. normalizeDeviceTypeToId();
  72. deviceTypesLoading.value = false;
  73. }
  74. const showDept = ref(false);
  75. const insArr = ref<any[]>([]);
  76. const insLoading = ref(false);
  77. async function getInstitution(orgId: string | number) {
  78. insLoading.value = true;
  79. const res = await branchMethod(1, 0, Number(orgId));
  80. if (res && res.length > 0) {
  81. insArr.value = res;
  82. }
  83. insLoading.value = false;
  84. }
  85. watch(
  86. () => model.value.orgId,
  87. async (newVal, oldVal) => {
  88. showDept.value = !!newVal;
  89. if (showDept.value) {
  90. // 加载机构列表,确保编辑时机构名称可显示
  91. await getInstitution(newVal ?? '');
  92. } else {
  93. insArr.value = [];
  94. }
  95. // 仅当组织实际发生切换时清空机构
  96. if (oldVal !== undefined && newVal !== oldVal) {
  97. model.value.institutionId = '';
  98. }
  99. },
  100. { immediate: true }
  101. );
  102. // 始终以数组形式暴露设备ID,避免模板类型告警
  103. const deviceIdsSafe = computed<string[]>({
  104. get() {
  105. if (!Array.isArray(model.value.deviceIds)) {
  106. model.value.deviceIds = [''];
  107. }
  108. return model.value.deviceIds as string[];
  109. },
  110. set(newVal: string[]) {
  111. model.value.deviceIds = newVal;
  112. },
  113. });
  114. const formItems = computed(() => {
  115. const baseItems: any[] = [
  116. {
  117. field: 'deviceType',
  118. title: '设备名称',
  119. span: 13,
  120. itemRender: {
  121. name: 'VxeSelect',
  122. props: {
  123. placeholder: '请选择',
  124. loading: deviceTypesLoading.value,
  125. options: computed(() => deviceTypes.value),
  126. optionProps: { value: 'id', label: 'name' },
  127. // optionGroupProps: { options: 'groups' },
  128. clearable: true,
  129. filterable: true,
  130. },
  131. },
  132. },
  133. {
  134. field: 'id',
  135. title: '设备ID',
  136. span: 24,
  137. slots: {
  138. title: 'deviceIdTitleSlot',
  139. default: 'deviceIdSlot',
  140. },
  141. },
  142. {
  143. field: 'orgId',
  144. title: '组织名称',
  145. span: 13,
  146. itemRender: {
  147. name: 'VxeSelect',
  148. props: {
  149. placeholder: '请选择',
  150. loading: computed(() => branchLoading.value),
  151. options: computed(() => branch.value),
  152. optionProps: {
  153. value: 'value',
  154. label: 'label',
  155. },
  156. clearable: true,
  157. },
  158. },
  159. },
  160. {
  161. field: 'remark',
  162. title: '备注',
  163. span: 24,
  164. slots: {
  165. default: 'remarksSlot',
  166. },
  167. },
  168. { align: 'center', span: 24, slots: { default: 'active' } },
  169. ];
  170. // 如果 showDept 为 true,在 institutionId 后面插入 institutionId
  171. if (showDept.value) {
  172. baseItems.splice(3, 0, {
  173. field: 'institutionId',
  174. title: '机构名称',
  175. span: 13,
  176. itemRender: {
  177. name: 'VxeTreeSelect',
  178. props: {
  179. placeholder: '请选择',
  180. loading: computed(() => insLoading.value),
  181. options: computed(() => insArr.value),
  182. optionProps: { value: 'id', label: 'label' },
  183. clearable: true,
  184. },
  185. },
  186. });
  187. }
  188. return baseItems;
  189. });
  190. const formProps = reactive<VxeFormProps>({
  191. titleWidth: 100,
  192. titleAlign: 'right',
  193. titleColon: true,
  194. data: computed(() => model.value),
  195. items: [] as any, // 临时设置为空数组,我们将在模板中使用动态items
  196. rules: {
  197. deviceType: [{ required: true, message: '请选择设备名称' }],
  198. deviceIds: [{ required: true, message: '请输入设备ID' }],
  199. orgId: [{ required: true, message: '请选择组织名称' }],
  200. institutionId: [{ required: true, message: '请选择机构名称' }],
  201. },
  202. });
  203. const formEmits: VxeFormListeners = {
  204. submit({ data }) {
  205. // 提交前确保 deviceType 存为 id
  206. normalizeDeviceTypeToId();
  207. // 验证必填字段
  208. if (!data.deviceType) {
  209. notification.error({
  210. message: '请选择设备名称',
  211. });
  212. return false; // 阻止表单提交
  213. }
  214. if (!data.deviceIds || data.deviceIds.length === 0 || data.deviceIds.every((id: string) => !id.trim())) {
  215. notification.error({
  216. message: '请输入设备ID',
  217. });
  218. return false; // 阻止表单提交
  219. }
  220. if (!data.orgId) {
  221. notification.error({
  222. message: '请选择组织名称',
  223. });
  224. return false; // 阻止表单提交
  225. }
  226. if (!data.institutionId) {
  227. notification.error({
  228. message: '请选择机构名称',
  229. });
  230. return false; // 阻止表单提交
  231. }
  232. if (data.id) {
  233. data.deviceCode = data.deviceIds[0];
  234. }
  235. data.remark ??= '';
  236. console.log(data, 'data==>');
  237. submit(data).then(() => {
  238. notification.success({
  239. message: '操作成功',
  240. });
  241. VxeUI.modal.close('equipment-modal');
  242. });
  243. },
  244. };
  245. function cancel() {
  246. VxeUI.modal.close('equirement-modal');
  247. }
  248. function addDeviceId() {
  249. if (!model.value.deviceIds) {
  250. model.value.deviceIds = [''];
  251. }
  252. model.value.deviceIds.push('');
  253. }
  254. function removeDeviceId(index: number) {
  255. if (model.value.deviceIds && model.value.deviceIds.length > 1) {
  256. model.value.deviceIds.splice(index, 1);
  257. }
  258. }
  259. onBeforeMount(async () => {
  260. if (props.data) {
  261. model.value = { ...defaultModel, ...props.data };
  262. }
  263. // 获取设备名称·
  264. getDeviceType();
  265. });
  266. // 若 deviceType 当前为名称,按照选项映射成对应的 id
  267. function normalizeDeviceTypeToId() {
  268. const current = model.value.deviceType;
  269. if (!current) return;
  270. const options = deviceTypes.value || [];
  271. // 已经是有效 id 则不处理
  272. const existsById = options.some((opt) => String(opt.id) === String(current));
  273. if (existsById) return;
  274. // 如果值是名称,回填为对应的 id
  275. const byName = options.find((opt) => String(opt.name) === String(current));
  276. if (byName) {
  277. model.value.deviceType = String(byName.id);
  278. }
  279. }
  280. </script>
  281. <template>
  282. <div class="form-container">
  283. <vxe-form
  284. :title-width="formProps.titleWidth"
  285. :title-align="formProps.titleAlign"
  286. :title-colon="formProps.titleColon"
  287. :data="formProps.data"
  288. :items="formItems"
  289. :rules="formProps.rules"
  290. v-on="formEmits"
  291. :loading="submitting"
  292. >
  293. <template #deviceIdTitleSlot> <span style="color: #f56c6c; font-size: 20px">*</span> 设备ID </template>
  294. <template #deviceIdSlot>
  295. <div class="device-ids-container">
  296. <div v-for="(deviceId, index) in deviceIdsSafe" :key="index" class="device-id-item">
  297. <vxe-input v-model="deviceIdsSafe[index]" placeholder="请输入" style="width: 200px" />
  298. <vxe-button v-if="(model.deviceIds || []).length > 1" type="text" style="color: #ff4d4f; margin-left: 8px" @click="removeDeviceId(index)">
  299. <template #default>×</template>
  300. </vxe-button>
  301. </div>
  302. <vxe-button type="text" style="border: 1px dashed #d9d9d9; width: 32px; height: 32px; margin-bottom: 8px" @click="addDeviceId" v-if="!model?.id">
  303. <template #default>+</template>
  304. </vxe-button>
  305. </div>
  306. </template>
  307. <template #remarksSlot>
  308. <div class="section-container">
  309. <textarea
  310. v-model="model.remark"
  311. placeholder="请输入"
  312. rows="3"
  313. style="width: 100%; padding: 8px; border: 1px solid #d9d9d9; border-radius: 4px; resize: vertical; font-family: inherit"
  314. />
  315. </div>
  316. </template>
  317. <template #active>
  318. <vxe-button type="reset" content="取消" :disabled="submitting" @click="cancel"></vxe-button>
  319. <vxe-button type="submit" status="warning" content="确定" :loading="submitting"></vxe-button>
  320. </template>
  321. </vxe-form>
  322. </div>
  323. </template>
  324. <style scoped lang="scss">
  325. .form-container {
  326. padding: 20px;
  327. }
  328. .device-ids-container {
  329. display: flex;
  330. flex-direction: row;
  331. flex-wrap: wrap;
  332. gap: 8px;
  333. align-items: flex-start;
  334. .device-id-item {
  335. display: flex;
  336. align-items: center;
  337. gap: 8px;
  338. margin-bottom: 8px;
  339. }
  340. }
  341. .section-container {
  342. margin-bottom: 20px;
  343. .section-title {
  344. font-size: 16px;
  345. font-weight: bold;
  346. margin-bottom: 10px;
  347. color: #333;
  348. }
  349. .section-divider {
  350. height: 1px;
  351. background-color: #d9d9d9;
  352. margin-bottom: 15px;
  353. }
  354. }
  355. .process-config {
  356. .config-item {
  357. display: flex;
  358. align-items: center;
  359. margin-bottom: 16px;
  360. flex-wrap: wrap;
  361. gap: 8px;
  362. .config-label {
  363. font-weight: bold;
  364. min-width: 80px;
  365. }
  366. .report-label {
  367. margin-left: 40px;
  368. color: #666;
  369. margin-right: 10px;
  370. }
  371. .radio-group {
  372. display: flex;
  373. align-items: center;
  374. gap: 30px;
  375. .radio-item {
  376. display: flex;
  377. align-items: center;
  378. gap: 4px;
  379. cursor: pointer;
  380. input[type='radio'] {
  381. margin: 0;
  382. cursor: pointer;
  383. }
  384. span {
  385. font-size: 14px;
  386. }
  387. }
  388. .badge {
  389. background-color: #faad14;
  390. color: white;
  391. border-radius: 12px;
  392. padding: 2px 8px;
  393. font-size: 12px;
  394. min-width: 20px;
  395. text-align: center;
  396. }
  397. }
  398. }
  399. }
  400. :deep(.vxe-form--item) {
  401. .vxe-form--item-wrapper {
  402. .vxe-form--item-content {
  403. .vxe-input {
  404. width: 100%;
  405. }
  406. }
  407. }
  408. }
  409. .required-field {
  410. :deep(.vxe-form--item-title) {
  411. position: relative;
  412. &::before {
  413. content: '*';
  414. color: #ff4d4f;
  415. position: absolute;
  416. left: -8px;
  417. top: 0;
  418. }
  419. }
  420. }
  421. </style>