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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<script lang="ts" setup>
import type { ImManagerFacePackItemApi } from '#/api/im/manager/face/item';
 
import { computed, ref, watch } from 'vue';
 
import { useVbenModal } from '#/packages/effects/common-ui/src';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createManagerFacePackItem,
  getManagerFacePackItem,
  updateManagerFacePackItem,
} from '#/api/im/manager/face/item';
import { $t } from '#/locales';
import { probeImageSize } from '#/views/im/manager/utils/format';
 
import { useItemFormSchema } from '../data';
 
const emit = defineEmits(['success']);
const formData = ref<ImManagerFacePackItemApi.FacePackItem>();
const packId = ref(0);
const lastUrl = ref('');
const getTitle = computed(() => {
  return formData.value?.id ? '修改表情' : '新增表情';
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 80,
  },
  layout: 'horizontal',
  schema: useItemFormSchema(),
  showDefaultActions: false,
});
 
/** 回填图片尺寸 */
async function applyImageSize(url?: string) {
  if (!url) {
    lastUrl.value = '';
    await formApi.setFieldValue('width', undefined);
    await formApi.setFieldValue('height', undefined);
    return;
  }
  if (url === lastUrl.value) {
    return;
  }
  lastUrl.value = url;
  try {
    const size = await probeImageSize(url);
    await formApi.setFieldValue('width', size.width);
    await formApi.setFieldValue('height', size.height);
  } catch {
    message.warning('图片尺寸探测失败,请手动填写宽高');
  }
}
 
watch(
  () => formApi.form.values?.url,
  (url) => {
    void applyImageSize(url as string | undefined);
  },
);
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    const data = (await formApi.getValues()) as ImManagerFacePackItemApi.FacePackItem;
    try {
      data.packId = data.packId || packId.value;
      await (formData.value?.id
        ? updateManagerFacePackItem(data)
        : createManagerFacePackItem(data));
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      lastUrl.value = '';
      await formApi.resetForm();
      return;
    }
    const data = modalApi.getData<{ id?: number; packId: number }>();
    packId.value = data?.packId || 0;
    await formApi.setValues({ packId: packId.value, sort: 0 });
    if (!data?.id) {
      return;
    }
    modalApi.lock();
    try {
      formData.value = await getManagerFacePackItem(data.id);
      lastUrl.value = formData.value.url;
      await formApi.setValues(formData.value);
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal :title="getTitle" class="w-1/3">
    <Form class="mx-4" />
  </Modal>
</template>