zhangwencui
7 天以前 f35825c1922149df154e3913d6dfebee57ee8cee
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
70
71
<script lang="ts" setup>
import type { ImManagerChannelMaterialApi } from '#/api/im/manager/channel/material';
 
import { computed, ref, watch } from 'vue';
 
import { Select } from 'ant-design-vue';
 
import { getSimpleManagerChannelMaterialList } from '#/api/im/manager/channel/material';
 
defineOptions({ name: 'ImManagerMaterialSelect' });
 
const props = withDefaults(
  defineProps<{
    allowClear?: boolean
    channelId?: number
    modelValue?: number
    placeholder?: string
  }>(),
  {
    allowClear: true,
    channelId: undefined,
    modelValue: undefined,
    placeholder: '请选择素材',
  },
);
 
const emit = defineEmits<{
  'update:modelValue': [value: number | undefined];
}>();
 
const loading = ref(false);
const materialList = ref<ImManagerChannelMaterialApi.Material[]>([]);
 
const value = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val),
});
 
const options = computed(() =>
  materialList.value.map((item) => ({ label: item.title, value: item.id })),
);
 
/** 按频道加载素材 */
async function loadMaterialList(channelId?: number) {
  emit('update:modelValue', undefined);
  materialList.value = [];
  if (!channelId) {
    return;
  }
  loading.value = true;
  try {
    materialList.value = await getSimpleManagerChannelMaterialList(channelId);
  } finally {
    loading.value = false;
  }
}
 
watch(() => props.channelId, loadMaterialList, { immediate: true });
</script>
 
<template>
  <Select
    v-model:value="value"
    :allow-clear="allowClear"
    :disabled="!channelId"
    :loading="loading"
    :options="options"
    :placeholder="placeholder"
    class="w-full"
  />
</template>