广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-02 80ff784bf60637cd348ae665fc907f7b1e527dd8
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
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { jsonClone } from '@sa/utils';
import type { UmoEditor } from '@umoteam/editor';
import { fetchCreateNotice, fetchUpdateNotice } from '@/service/api/system/notice';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { $t } from '@/locales';
 
defineOptions({
  name: 'NoticeOperateDrawer'
});
 
interface Props {
  /** the type of operation */
  operateType: NaiveUI.TableOperateType;
  /** the edit row data */
  rowData?: Api.System.Notice | null;
}
 
const props = defineProps<Props>();
 
interface Emits {
  (e: 'submitted'): void;
}
 
const emit = defineEmits<Emits>();
 
const visible = defineModel<boolean>('visible', {
  default: false
});
 
const umoEditorRef = ref<InstanceType<typeof UmoEditor>>();
const { formRef, validate, restoreValidation } = useNaiveForm();
const { createRequiredRule } = useFormRules();
 
const title = computed(() => {
  const titles: Record<NaiveUI.TableOperateType, string> = {
    add: '新增通知公告',
    edit: '编辑通知公告'
  };
  return titles[props.operateType];
});
 
type Model = Api.System.NoticeOperateParams;
 
const model = ref<Model>(createDefaultModel());
 
function createDefaultModel(): Model {
  return {
    noticeTitle: '',
    noticeType: '1',
    noticeContent: '',
    status: '0'
  };
}
 
type RuleKey = Extract<keyof Model, 'noticeId' | 'noticeTitle' | 'noticeType' | 'noticeContent' | 'status'>;
 
const rules: Record<RuleKey, App.Global.FormRule> = {
  noticeId: createRequiredRule('公告ID不能为空'),
  noticeTitle: createRequiredRule('公告标题不能为空'),
  noticeType: createRequiredRule('公告类型不能为空'),
  noticeContent: createRequiredRule('公告内容不能为空'),
  status: createRequiredRule('公告状态不能为空')
};
 
function handleUpdateModelWhenEdit() {
  model.value = createDefaultModel();
 
  if (props.operateType === 'edit' && props.rowData) {
    Object.assign(model.value, jsonClone(props.rowData));
  }
}
 
function closeDrawer() {
  visible.value = false;
}
 
async function handleSubmit() {
  umoEditorRef.value?.saveContent();
  await validate();
 
  const { noticeId, noticeTitle, noticeType, noticeContent, status } = model.value;
 
  // request
  if (props.operateType === 'add') {
    const { error } = await fetchCreateNotice({ noticeTitle, noticeType, noticeContent, status });
    if (error) return;
  }
 
  if (props.operateType === 'edit') {
    const { error } = await fetchUpdateNotice({ noticeId, noticeTitle, noticeType, noticeContent, status });
    if (error) return;
  }
 
  window.$message?.success($t('common.updateSuccess'));
  closeDrawer();
  emit('submitted');
}
 
watch(visible, () => {
  if (visible.value) {
    handleUpdateModelWhenEdit();
    restoreValidation();
  }
});
</script>
 
<template>
  <NDrawer
    v-model:show="visible"
    :trap-focus="false"
    :title="title"
    display-directive="show"
    :width="1000"
    class="max-w-90%"
  >
    <NDrawerContent :title="title" :native-scrollbar="false" closable>
      <NForm ref="formRef" :model="model" :rules="rules">
        <div class="grid grid-cols-1 gap-16px md:grid-cols-4">
          <NFormItem class="col-span-2" label="公告标题" path="noticeTitle">
            <NInput v-model:value="model.noticeTitle" placeholder="请输入公告标题" />
          </NFormItem>
          <NFormItem class="col-span-1" label="公告类型" path="noticeType">
            <DictRadio v-model:value="model.noticeType" dict-code="sys_notice_type" />
          </NFormItem>
          <NFormItem class="col-span-1" label="公告状态" path="status">
            <DictRadio v-model:value="model.status" dict-code="sys_normal_disable" />
          </NFormItem>
        </div>
        <NFormItem :show-label="false" path="noticeContent">
          <UmoDocEditor ref="umoEditorRef" v-model:value="model.noticeContent!" />
        </NFormItem>
      </NForm>
      <template #footer>
        <NSpace :size="16">
          <NButton @click="closeDrawer">{{ $t('common.cancel') }}</NButton>
          <NButton type="primary" @click="handleSubmit">{{ $t('common.confirm') }}</NButton>
        </NSpace>
      </template>
    </NDrawerContent>
  </NDrawer>
</template>
 
<style scoped></style>