广丰卷烟厂数采质量分析系统
zhuguifei
2026-04-03 08114b7451615854cb01556a7c477e32e17f520e
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<script setup lang="tsx">
import { computed, ref, watch } from 'vue';
import { NTag } from 'naive-ui';
import { jsonClone } from '@sa/utils';
import { fetchCreateDictData, fetchUpdateDictData } from '@/service/api/system/dict-data';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { useDict } from '@/hooks/business/dict';
import { $t } from '@/locales';
 
defineOptions({
  name: 'DictDataOperateDrawer'
});
useDict('sys_yes_no');
interface Props {
  /** the type of operation */
  operateType: NaiveUI.TableOperateType;
  /** the edit row data */
  rowData?: Api.System.DictData | null;
  dictType: string;
}
 
const props = defineProps<Props>();
 
interface Emits {
  (e: 'submitted'): void;
}
 
const emit = defineEmits<Emits>();
 
const visible = defineModel<boolean>('visible', {
  default: false
});
 
const { formRef, validate, restoreValidation } = useNaiveForm();
const { createRequiredRule } = useFormRules();
 
const title = computed(() => {
  const titles: Record<NaiveUI.TableOperateType, string> = {
    add: $t('page.system.dict.addDictData'),
    edit: $t('page.system.dict.editDictData')
  };
  return titles[props.operateType];
});
 
type Model = Api.System.DictDataOperateParams;
 
const model = ref<Model>(createDefaultModel());
 
const listClassOptions: Record<string, string>[] = [
  { label: 'Text', value: 'text' },
  { label: 'Default', value: 'default' },
  { label: 'Tertiary', value: 'tertiary' },
  { label: 'Primary', value: 'primary' },
  { label: 'Info', value: 'info' },
  { label: 'Success', value: 'success' },
  { label: 'Warning', value: 'warning' },
  { label: 'Error', value: 'error' }
];
 
function createDefaultModel(): Model {
  return {
    dictSort: 0,
    dictLabel: '',
    dictValue: '',
    dictType: props.dictType,
    cssClass: '',
    listClass: null,
    remark: '',
    isDefault: 'N'
  };
}
 
type RuleKey = Extract<keyof Model, 'dictCode' | 'dictLabel' | 'dictValue'>;
 
const rules: Record<RuleKey, App.Global.FormRule> = {
  dictCode: createRequiredRule($t('page.system.dict.form.dictCode.invalid')),
  dictLabel: createRequiredRule($t('page.system.dict.form.dictLabel.invalid')),
  dictValue: createRequiredRule($t('page.system.dict.form.dictValue.invalid'))
};
 
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() {
  await validate();
 
  const { dictCode, dictSort, dictLabel, dictValue, dictType, cssClass, listClass, isDefault, remark } = model.value;
 
  // request
  if (props.operateType === 'add') {
    const { error } = await fetchCreateDictData({
      dictSort,
      dictLabel,
      dictValue,
      dictType,
      cssClass,
      listClass,
      isDefault,
      remark
    });
    if (error) return;
  }
 
  if (props.operateType === 'edit') {
    const { error } = await fetchUpdateDictData({
      dictCode,
      dictSort,
      dictLabel,
      dictValue,
      dictType,
      cssClass,
      listClass,
      isDefault,
      remark
    });
    if (error) return;
  }
 
  window.$message?.success($t('common.updateSuccess'));
  closeDrawer();
  emit('submitted');
}
 
watch(visible, () => {
  if (visible.value) {
    handleUpdateModelWhenEdit();
    restoreValidation();
  }
});
 
function renderTagLabel(option: { label: string; value: string }) {
  if (option.value === 'text') {
    return option.label;
  }
  return (
    <NTag size="small" type={option.value as any}>
      {option.label}
    </NTag>
  );
}
</script>
 
<template>
  <NDrawer v-model:show="visible" :title="title" display-directive="show" :width="800" class="max-w-90%">
    <NDrawerContent :title="title" :native-scrollbar="false" closable>
      <NForm ref="formRef" :model="model" :rules="rules">
        <NFormItem :label="$t('page.system.dict.dictType')" path="dictType">
          <NInput
            v-model:value="model.dictType"
            disabled
            :placeholder="$t('page.system.dict.form.dictType.required')"
          />
        </NFormItem>
        <NFormItem :label="$t('page.system.dict.data.listClass')" path="listClass">
          <NSelect
            v-model:value="model.listClass"
            clearable
            :options="listClassOptions"
            :placeholder="$t('page.system.dict.form.listClass.required')"
            :render-label="renderTagLabel"
          />
        </NFormItem>
        <NFormItem :label="$t('page.system.dict.data.label')" path="dictLabel">
          <NInput v-model:value="model.dictLabel" :placeholder="$t('page.system.dict.form.dictLabel.required')" />
        </NFormItem>
        <NFormItem :label="$t('page.system.dict.data.value')" path="dictValue">
          <NInput v-model:value="model.dictValue" :placeholder="$t('page.system.dict.form.dictValue.required')" />
        </NFormItem>
        <NFormItem :label="$t('page.system.dict.data.cssClass')" path="cssClass">
          <NInput v-model:value="model.cssClass" :placeholder="$t('page.system.dict.form.cssClass.required')" />
        </NFormItem>
        <NFormItem :label="$t('page.system.dict.data.dictSort')" path="dictSort">
          <NInputNumber v-model:value="model.dictSort" :placeholder="$t('page.system.dict.form.dictSort.required')" />
        </NFormItem>
        <NFormItem :label="$t('page.system.dict.data.isDefault')" path="isDefault">
          <DictRadio v-model:value="model.isDefault" dict-code="sys_yes_no" />
        </NFormItem>
        <NFormItem :label="$t('page.system.dict.data.remark')" path="remark">
          <NInput
            v-model:value="model.remark"
            :rows="3"
            type="textarea"
            :placeholder="$t('page.system.dict.form.remark.required')"
          />
        </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>