Files
iot-device-management-frontend/apps/web-antd/src/views/mp/modules/wx-account-select/main.vue

125 lines
3.3 KiB
Vue
Raw Normal View History

2025-11-03 14:04:00 +08:00
<script lang="ts" setup>
import type { MpAccountApi } from '#/api/mp/account';
2025-11-04 16:53:08 +08:00
import { computed, onMounted, reactive, ref, unref, watch } from 'vue';
2025-11-03 14:04:00 +08:00
import { useRouter } from 'vue-router';
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-04 16:53:08 +08:00
import { useTagsViewStore } from '#/store/tagsView';
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 16:53:08 +08:00
const { delView } = useTagsViewStore(); // 视图操作
2025-11-03 14:04:00 +08:00
const { push, currentRoute } = useRouter();
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 16:53:08 +08:00
delView(unref(currentRoute));
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>
<Select
2025-11-04 16:53:08 +08:00
v-model:value="currentId"
2025-11-03 14:04:00 +08:00
placeholder="请选择公众号"
2025-11-04 16:53:08 +08:00
style="width: 240px"
2025-11-03 14:04:00 +08:00
>
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>