liu
3 天以前 fe2d6627b8f1002af7a31b08f6f43bec034d04ee
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<script lang="ts" setup>
import type { WxCpNoticeApi } from '#/api/wxcp/notice';
 
import type { NoticeFormType } from '../data';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { Button, message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createNotice,
  getNotice,
  updateNotice,
} from '#/api/wxcp/notice';
 
import { NOTICE_TYPE, useNoticeFormSchema } from '../data';
 
defineOptions({ name: 'WxCpNoticeForm' });
 
const emit = defineEmits<{ success: [] }>();
 
const formType = ref<NoticeFormType>('create');
const id = ref<number>();
const submitting = ref(false);
 
const isEdit = computed(() => formType.value !== 'detail');
const title = computed(() => {
  if (formType.value === 'detail') return '查看通知';
  if (id.value) return '编辑通知';
  return '新建通知';
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: { class: 'w-full' },
    formItemClass: 'col-span-1',
    labelWidth: 90,
  },
  layout: 'horizontal',
  schema: [],
  showDefaultActions: false,
  wrapperClass: 'grid-cols-1',
});
 
const [Modal, modalApi] = useVbenModal({
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      id.value = undefined;
      return;
    }
    const data = modalApi.getData<{ formType: NoticeFormType; id?: number }>();
    formType.value = data.formType;
    id.value = data.id;
    formApi.setState({ schema: useNoticeFormSchema(formType.value) });
    formApi.setDisabled(formType.value === 'detail');
    if (data.id) {
      modalApi.lock();
      try {
        const detail = await getNotice(data.id);
        const values: Record<string, unknown> = { ...detail };
        // 文件通知:把 blobId 还原成上传组件可识别的文件对象
        if (detail.noticeType === NOTICE_TYPE.FILE && detail.fileBlobId) {
          values.fileBlobId = [
            {
              uid: String(detail.fileBlobId),
              name: detail.fileName || '',
              status: 'done',
              id: detail.fileBlobId,
            },
          ];
        }
        await formApi.setValues(values);
      } finally {
        modalApi.unlock();
      }
    } else {
      await formApi.resetForm();
      await formApi.setValues({ noticeType: NOTICE_TYPE.TEXT }, false);
    }
  },
});
 
async function handleSubmit() {
  const { valid } = await formApi.validate();
  if (!valid) {
    return;
  }
  submitting.value = true;
  try {
    const data = (await formApi.getValues()) as WxCpNoticeApi.Notice;
    // 保存时只做文件归一化;具体模板必填项在推送时按所选模板校验
    const noticeType = data.noticeType ?? NOTICE_TYPE.TEXT;
    if (noticeType === NOTICE_TYPE.FILE) {
      const raw = data.fileBlobId as unknown;
      let blobId: number | undefined;
      if (Array.isArray(raw)) {
        const first = raw[0];
        blobId =
          typeof first === 'number'
            ? first
            : (first as { id?: number } | undefined)?.id;
      } else if (typeof raw === 'number') {
        blobId = raw;
      } else if (raw && typeof raw === 'object') {
        blobId = (raw as { id?: number }).id;
      }
      data.fileBlobId = blobId;
    } else {
      data.fileBlobId = undefined;
    }
    if (id.value) {
      await updateNotice({ ...data, id: id.value });
      message.success('修改成功');
    } else {
      await createNotice(data);
      message.success('创建成功,请在列表中发布后再推送');
    }
    await modalApi.close();
    emit('success');
  } catch (error: unknown) {
    message.error((error as Error)?.message || '提交失败');
  } finally {
    submitting.value = false;
  }
}
</script>
 
<template>
  <Modal :title="title" class="w-3/5">
    <Form class="mx-4" />
 
    <template #footer>
      <div class="flex justify-end gap-2">
        <Button @click="modalApi.close()">取消</Button>
        <Button
          v-if="isEdit"
          type="primary"
          :loading="submitting"
          @click="handleSubmit"
        >
          确认
        </Button>
        <Button v-else type="primary" @click="modalApi.close()">关闭</Button>
      </div>
    </template>
  </Modal>
</template>