gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
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
<script lang="ts" setup>
import type { SelectValue } from 'ant-design-vue/es/select';
 
import type { MpAccountApi } from '#/api/mp/account';
 
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
 
import { message, Select } from 'ant-design-vue';
 
import { getSimpleAccountList } from '#/api/mp/account';
 
defineOptions({ name: 'WxAccountSelect' });
 
const emit = defineEmits<{
  (e: 'change', id: number, name: string): void;
}>();
 
const { push } = useRouter();
 
const account = ref<MpAccountApi.Account>({
  id: -1,
  name: '',
}); // 当前选中的公众号
const accountList = ref<MpAccountApi.Account[]>([]); // 公众号列表
 
/** 查询公众号列表 */
async function handleQuery() {
  accountList.value = await getSimpleAccountList();
  if (accountList.value.length === 0) {
    message.error('未配置公众号,请在【公众号管理 -> 账号管理】菜单,进行配置');
    await push({ name: 'MpAccount' });
    return;
  }
 
  // 默认选中第一个,如无数据则不执行
  const first = accountList.value[0];
  if (first) {
    account.value.id = first.id;
    account.value.name = first.name;
    emit('change', account.value.id, account.value.name);
  }
}
 
/** 切换选中公众号 */
function onChanged(id: SelectValue) {
  const found = accountList.value.find((v) => v.id === id);
  if (found) {
    account.value.name = found.name;
    emit('change', account.value.id, account.value.name);
  }
}
 
/** 初始化 */
onMounted(handleQuery);
</script>
 
<template>
  <Select
    v-model:value="account.id"
    placeholder="请选择公众号"
    class="!w-full"
    @change="onChanged"
  >
    <Select.Option v-for="item in accountList" :key="item.id" :value="item.id">
      {{ item.name }}
    </Select.Option>
  </Select>
</template>