2025-11-03 14:04:00 +08:00
|
|
|
|
<script lang="ts" setup>
|
|
|
|
|
|
import type { MpAccountApi } from '#/api/mp/account';
|
|
|
|
|
|
|
2025-11-04 18:07:04 +08:00
|
|
|
|
import { computed, onMounted, reactive, ref, watch } from 'vue';
|
2025-11-03 14:04:00 +08:00
|
|
|
|
import { useRouter } from 'vue-router';
|
|
|
|
|
|
|
2025-11-04 18:07:04 +08:00
|
|
|
|
import { useTabs } from '@vben/hooks';
|
|
|
|
|
|
|
2025-11-04 16:53:08 +08:00
|
|
|
|
import { message, Select, SelectOption } from 'ant-design-vue';
|
2025-11-03 14:04:00 +08:00
|
|
|
|
|
|
|
|
|
|
import { getSimpleAccountList } from '#/api/mp/account';
|
|
|
|
|
|
|
2025-11-09 17:30:37 +08:00
|
|
|
|
// TODO @hw:【可讨论】如果这个组件,有办法调整下,让接入的 yudao-ui-admin-vben-v5/apps/web-antd/src/views/mp/draft/index.vue 判断简单点,也可以。
|
|
|
|
|
|
|
2025-11-03 14:04:00 +08:00
|
|
|
|
defineOptions({ name: 'WxAccountSelect' });
|
|
|
|
|
|
|
2025-11-04 16:53:08 +08:00
|
|
|
|
const props = defineProps<{
|
|
|
|
|
|
modelValue?: number;
|
|
|
|
|
|
}>();
|
|
|
|
|
|
|
2025-11-03 14:04:00 +08:00
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
|
(e: 'change', id: number, name: string): void;
|
2025-11-04 16:53:08 +08:00
|
|
|
|
(e: 'update:modelValue', id: number): void;
|
2025-11-03 14:04:00 +08:00
|
|
|
|
}>();
|
|
|
|
|
|
|
2025-11-04 18:07:04 +08:00
|
|
|
|
const { closeCurrentTab } = useTabs(); // 视图操作
|
|
|
|
|
|
const { push } = useRouter();
|
2025-11-03 14:04:00 +08:00
|
|
|
|
|
|
|
|
|
|
const account: MpAccountApi.AccountSimple = reactive({
|
|
|
|
|
|
id: -1,
|
|
|
|
|
|
name: '',
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const accountList = ref<MpAccountApi.AccountSimple[]>([]);
|
|
|
|
|
|
|
2025-11-04 16:53:08 +08:00
|
|
|
|
// 计算当前选中的 ID,优先使用 modelValue(表单绑定),否则使用内部 account.id
|
|
|
|
|
|
const currentId = computed({
|
|
|
|
|
|
get: () => {
|
|
|
|
|
|
// 如果外部传入了 modelValue,优先使用外部的值
|
|
|
|
|
|
if (props.modelValue !== undefined && props.modelValue !== null) {
|
|
|
|
|
|
return props.modelValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
return account.id;
|
|
|
|
|
|
},
|
|
|
|
|
|
set: (value: number) => {
|
|
|
|
|
|
// 更新内部状态
|
|
|
|
|
|
account.id = value;
|
|
|
|
|
|
// 同步到外部(表单系统)
|
|
|
|
|
|
emit('update:modelValue', value);
|
|
|
|
|
|
// 触发 change 事件(保持向后兼容)
|
|
|
|
|
|
const found = accountList.value.find(
|
|
|
|
|
|
(v: MpAccountApi.AccountSimple) => v.id === value,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (found) {
|
|
|
|
|
|
account.name = found.name;
|
|
|
|
|
|
emit('change', value, found.name);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 监听外部 modelValue 变化,同步到内部状态
|
|
|
|
|
|
watch(
|
|
|
|
|
|
() => props.modelValue,
|
|
|
|
|
|
(newValue) => {
|
|
|
|
|
|
if (
|
|
|
|
|
|
newValue !== undefined &&
|
|
|
|
|
|
newValue !== null &&
|
|
|
|
|
|
newValue !== account.id
|
|
|
|
|
|
) {
|
|
|
|
|
|
account.id = newValue;
|
|
|
|
|
|
const found = accountList.value.find(
|
|
|
|
|
|
(v: MpAccountApi.AccountSimple) => v.id === newValue,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (found) {
|
|
|
|
|
|
account.name = found.name;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2025-11-03 14:04:00 +08:00
|
|
|
|
/** 查询公众号列表 */
|
|
|
|
|
|
async function handleQuery() {
|
|
|
|
|
|
accountList.value = await getSimpleAccountList();
|
|
|
|
|
|
if (accountList.value.length === 0) {
|
|
|
|
|
|
message.error('未配置公众号,请在【公众号管理 -> 账号管理】菜单,进行配置');
|
2025-11-04 18:07:04 +08:00
|
|
|
|
await closeCurrentTab();
|
2025-11-03 14:04:00 +08:00
|
|
|
|
await push({ name: 'MpAccount' });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-11-04 16:53:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果外部没有传入值(modelValue 为空),默认选中第一个
|
|
|
|
|
|
if (props.modelValue === undefined || props.modelValue === null) {
|
|
|
|
|
|
const firstAccount = accountList.value[0];
|
|
|
|
|
|
if (firstAccount) {
|
|
|
|
|
|
currentId.value = firstAccount.id;
|
2025-11-03 14:04:00 +08:00
|
|
|
|
account.name = firstAccount.name;
|
2025-11-04 16:53:08 +08:00
|
|
|
|
emit('change', firstAccount.id, firstAccount.name);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 如果外部有值,同步到内部状态
|
|
|
|
|
|
const found = accountList.value.find(
|
|
|
|
|
|
(v: MpAccountApi.AccountSimple) => v.id === props.modelValue,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (found) {
|
|
|
|
|
|
account.id = props.modelValue;
|
|
|
|
|
|
account.name = found.name;
|
2025-11-03 14:04:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 初始化 */
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
|
handleQuery();
|
|
|
|
|
|
});
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-11-07 17:43:13 +08:00
|
|
|
|
<Select v-model:value="currentId" placeholder="请选择公众号" class="w-full">
|
2025-11-04 16:53:08 +08:00
|
|
|
|
<SelectOption v-for="item in accountList" :key="item.id" :value="item.id">
|
2025-11-03 14:04:00 +08:00
|
|
|
|
{{ item.name }}
|
2025-11-04 16:53:08 +08:00
|
|
|
|
</SelectOption>
|
2025-11-03 14:04:00 +08:00
|
|
|
|
</Select>
|
|
|
|
|
|
</template>
|