gaoluyang
2 天以前 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
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
72
73
74
75
76
77
78
79
80
81
82
<script lang="ts" setup>
import type { ImManagerChannelApi } from '#/api/im/manager/channel'
 
import { computed, ref } from 'vue'
 
import { useVbenModal } from '#/packages/effects/common-ui/src'
 
import { message } from 'ant-design-vue'
 
import { useVbenForm } from '#/adapter/form'
import {
  createManagerChannel,
  getManagerChannel,
  updateManagerChannel
} from '#/api/im/manager/channel'
import { $t } from '#/locales'
 
import { useFormSchema } from '../data'
 
const emit = defineEmits(['success'])
const formData = ref<ImManagerChannelApi.Channel>()
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['频道'])
    : $t('ui.actionTitle.create', ['频道'])
})
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full'
    },
    formItemClass: 'col-span-2',
    labelWidth: 100
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false
})
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate()
    if (!valid) {
      return
    }
    modalApi.lock()
    const data = (await formApi.getValues()) as ImManagerChannelApi.Channel
    try {
      await (formData.value?.id ? updateManagerChannel(data) : createManagerChannel(data))
      await modalApi.close()
      emit('success')
      message.success($t('ui.actionMessage.operationSuccess'))
    } finally {
      modalApi.unlock()
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined
      return
    }
    const data = modalApi.getData<ImManagerChannelApi.Channel>()
    if (!data || !data.id) {
      return
    }
    modalApi.lock()
    try {
      formData.value = await getManagerChannel(data.id)
      await formApi.setValues(formData.value)
    } finally {
      modalApi.unlock()
    }
  }
})
</script>
 
<template>
  <Modal :title="getTitle" class="w-1/3">
    <Form class="mx-4" />
  </Modal>
</template>