2026-08-03 858df78951d5c29e8d4217cffcf81d2cc4fabcd4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<script lang="ts" setup>
import type { OaNoticeApi } from '#/api/bpm/oa/notice';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { createNotice, getNotice, updateNotice } from '#/api/bpm/oa/notice';
import { $t } from '#/locales';
import UserSelect from '#/views/system/user/components/select.vue';
 
import { SEND_SCOPE, useFormSchema } from '../data';
 
const emit = defineEmits(['success']);
const formData = ref<OaNoticeApi.NoticeVO>();
const selectedUserIds = ref<number[]>([]);
const currentSendScope = ref<number>(SEND_SCOPE.ALL);
 
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['通知公告'])
    : $t('ui.actionTitle.create', ['通知公告']);
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 80,
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
  handleValuesChange: (values) => {
    currentSendScope.value = (values.sendScope as number) ?? SEND_SCOPE.ALL;
  },
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    // 发送范围为指定人时校验
    if (currentSendScope.value === SEND_SCOPE.SPECIFIC && selectedUserIds.value.length === 0) {
      message.warning('请选择指定人员');
      return;
    }
    modalApi.lock();
    const data = (await formApi.getValues()) as OaNoticeApi.NoticeSaveReqVO;
    // 合并指定人员
    if (currentSendScope.value === SEND_SCOPE.SPECIFIC) {
      data.userIds = selectedUserIds.value;
    }
    try {
      await (formData.value?.id ? updateNotice(data) : createNotice(data));
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      selectedUserIds.value = [];
      currentSendScope.value = SEND_SCOPE.ALL;
      return;
    }
    const data = modalApi.getData<OaNoticeApi.NoticeVO>();
    if (!data || !data.id) {
      return;
    }
    modalApi.lock();
    try {
      formData.value = await getNotice(data.id);
      await formApi.setValues(formData.value);
      selectedUserIds.value = formData.value.userIds ?? [];
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal :title="getTitle" class="w-1/2">
    <Form class="mx-4" />
    <div
      v-if="currentSendScope === SEND_SCOPE.SPECIFIC"
      class="mx-4 mt-4"
    >
      <div class="mb-2 text-sm text-gray-700">指定人员</div>
      <UserSelect
        v-model:value="selectedUserIds"
        :multiple="true"
        placeholder="请选择指定人员"
      />
    </div>
  </Modal>
</template>