main.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <template>
  2. <el-select v-model="account.id" placeholder="请选择公众号" class="!w-240px" @change="onChanged">
  3. <el-option v-for="item in accountList" :key="item.id" :label="item.name" :value="item.id" />
  4. </el-select>
  5. </template>
  6. <script lang="ts" setup name="WxAccountSelect">
  7. import * as MpAccountApi from '@/api/mp/account'
  8. const account: MpAccountApi.AccountVO = reactive({
  9. id: -1,
  10. name: ''
  11. })
  12. const accountList = ref<MpAccountApi.AccountVO[]>([])
  13. const emit = defineEmits<{
  14. (e: 'change', id: number, name: string)
  15. }>()
  16. const handleQuery = async () => {
  17. accountList.value = await MpAccountApi.getSimpleAccountList()
  18. // 默认选中第一个
  19. if (accountList.value.length > 0) {
  20. account.id = accountList.value[0].id
  21. if (account.id) {
  22. account.name = accountList.value[0].name
  23. emit('change', account.id, account.name)
  24. }
  25. }
  26. }
  27. const onChanged = (id?: number) => {
  28. const found = accountList.value.find((v) => v.id === id)
  29. if (account.id) {
  30. account.name = found ? found.name : ''
  31. emit('change', account.id, account.name)
  32. }
  33. }
  34. /** 初始化 */
  35. onMounted(() => {
  36. handleQuery()
  37. })
  38. </script>