EditEquirement.vue 11 KB

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