康鲁杰
2025-03-31 9cbbd2531ed5e950fe51a652c1994eb17da8cc45
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
<template>
  <el-select v-model="account.id" placeholder="请选择公众号" class="!w-240px" @change="onChanged">
    <el-option v-for="item in accountList" :key="item.id" :label="item.name" :value="item.id" />
  </el-select>
</template>
 
<script lang="ts" setup>
import * as MpAccountApi from '@/api/mp/account'
import { useTagsViewStore } from '@/store/modules/tagsView'
 
const message = useMessage() // 消息弹窗
const { delView } = useTagsViewStore() // 视图操作
const { push, currentRoute } = useRouter() // 路由
 
defineOptions({ name: 'WxAccountSelect' })
 
const account: MpAccountApi.AccountVO = reactive({
  id: -1,
  name: ''
})
 
const accountList = ref<MpAccountApi.AccountVO[]>([])
 
const emit = defineEmits<{
  (e: 'change', id: number, name: string)
}>()
 
const handleQuery = async () => {
  accountList.value = await MpAccountApi.getSimpleAccountList()
  if (accountList.value.length == 0) {
    message.error('未配置公众号,请在【公众号管理 -> 账号管理】菜单,进行配置')
    delView(unref(currentRoute))
    await push({ name: 'MpAccount' })
    return
  }
  // 默认选中第一个
  if (accountList.value.length > 0) {
    account.id = accountList.value[0].id
    if (account.id) {
      account.name = accountList.value[0].name
      emit('change', account.id, account.name)
    }
  }
}
 
const onChanged = (id?: number) => {
  const found = accountList.value.find((v) => v.id === id)
  if (account.id) {
    account.name = found ? found.name : ''
    emit('change', account.id, account.name)
  }
}
 
/** 初始化 */
onMounted(() => {
  handleQuery()
})
</script>