gaoluyang
2026-06-24 bfdc0e0e6d5e47aa501f9b6fadd143d9c97cf00a
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
<script lang="ts" setup>
import type { SelectValue } from 'ant-design-vue/es/select';
 
import type { SystemMailTemplateApi } from '#/api/system/mail/template';
 
import { computed, onMounted, ref } from 'vue';
 
import { Select } from 'ant-design-vue';
 
import { getSimpleMailTemplateList } from '#/api/system/mail/template';
 
defineOptions({ name: 'MailTemplateSelect', inheritAttrs: false });
 
const props = withDefaults(
  defineProps<{
    allowClear?: boolean;
    disabled?: boolean;
    modelValue?: string;
    placeholder?: string;
  }>(),
  {
    allowClear: true,
    disabled: false,
    modelValue: undefined,
    placeholder: '请选择邮件模板',
  },
);
 
const emit = defineEmits<{
  change: [template: SystemMailTemplateApi.MailTemplateSimple | undefined];
  'update:modelValue': [value: string | undefined];
}>();
 
const loading = ref(false);
const templateList = ref<SystemMailTemplateApi.MailTemplateSimple[]>([]);
const options = computed(() =>
  templateList.value.map((template) => ({
    label: `${template.name}(${template.code})`,
    value: template.code,
  })),
);
 
/** 选中变化 */
function handleChange(value: SelectValue) {
  const templateCode = typeof value === 'string' ? value : undefined;
  emit('update:modelValue', templateCode);
  emit(
    'change',
    templateList.value.find((template) => template.code === templateCode),
  );
}
 
/** 查询邮件模板精简列表 */
async function getList() {
  try {
    loading.value = true;
    templateList.value = await getSimpleMailTemplateList();
  } finally {
    loading.value = false;
  }
}
 
onMounted(() => {
  getList();
});
</script>
 
<template>
  <Select
    v-bind="$attrs"
    :allow-clear="allowClear"
    :disabled="disabled"
    :loading="loading"
    :options="options"
    :placeholder="placeholder"
    :value="props.modelValue"
    class="w-full"
    option-filter-prop="label"
    show-search
    @change="handleChange"
  />
</template>