gaoluyang
2026-06-24 bfdc0e0e6d5e47aa501f9b6fadd143d9c97cf00a
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<script lang="ts" setup>
import type { MallCommentApi } from '#/api/mall/product/comment';
import type { MallSpuApi } from '#/api/mall/product/spu';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '../../../../../packages/effects/common-ui/src';
 
import { Button, message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { createComment, getComment } from '#/api/mall/product/comment';
import { getSpu } from '#/api/mall/product/spu';
import { $t } from '#/locales';
import {
  SkuTableSelect,
  SpuShowcase,
} from '#/views/mall/product/spu/components';
 
import { useFormSchema } from '../data';
 
const emit = defineEmits(['success']);
 
const formData = ref<Partial<MallCommentApi.Comment>>({
  descriptionScores: 5,
  benefitScores: 5,
});
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: 120,
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
});
 
const skuTableSelectRef = ref<InstanceType<typeof SkuTableSelect>>();
const selectedSku = ref<MallSpuApi.Sku>();
 
/** 处理商品的选择变化 */
async function handleSpuChange(spu?: MallSpuApi.Spu | null) {
  // 处理商品选择:如果 spu 为 null 或 id 为 0,表示清空选择
  const spuId = spu && spu.id ? spu.id : undefined;
  formData.value.spuId = spuId;
  await formApi.setFieldValue('spuId', spuId);
  // 清空已选规格
  selectedSku.value = undefined;
  formData.value.skuId = undefined;
  await formApi.setFieldValue('skuId', undefined);
}
 
/** 打开商品规格的选择弹框 */
async function openSkuSelect() {
  const currentValues =
    (await formApi.getValues()) as Partial<MallCommentApi.Comment>;
  const currentSpuId = currentValues.spuId ?? formData.value?.spuId;
  if (!currentSpuId) {
    message.warning('请先选择商品');
    return;
  }
  skuTableSelectRef.value?.open({ spuId: currentSpuId });
}
 
/** 处理商品规格的选择 */
async function handleSkuSelected(sku: MallSpuApi.Sku) {
  selectedSku.value = sku;
  formData.value.skuId = sku.id;
  await formApi.setFieldValue('skuId', sku.id);
}
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data = (await formApi.getValues()) as MallCommentApi.Comment;
    try {
      await createComment(data);
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      // 关闭时重置表单状态
      selectedSku.value = undefined;
      await formApi.setValues({ spuId: undefined, skuId: undefined });
      return;
    }
    // 加载数据
    const data = modalApi.getData<MallCommentApi.Comment>();
    if (!data || !data.id) {
      return;
    }
    // 编辑模式:加载数据
    modalApi.lock();
    try {
      formData.value = await getComment(data.id);
      // 设置到 values
      await formApi.setValues(formData.value);
      // 回显已选的商品规格
      if (formData.value?.spuId && formData.value?.skuId) {
        const spu = await getSpu(formData.value.spuId);
        const sku = spu.skus?.find((item) => item.id === formData.value!.skuId);
        if (sku) {
          selectedSku.value = sku;
        }
      } else {
        selectedSku.value = undefined;
      }
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal class="w-2/5" :title="getTitle">
    <Form class="mx-4">
      <template #spuId>
        <SpuShowcase
          v-model="(formData as any).spuId"
          :limit="1"
          @change="handleSpuChange"
        />
      </template>
      <template #skuId>
        <div class="flex items-center gap-2">
          <Button
            type="primary"
            :disabled="!formData?.spuId"
            @click="openSkuSelect"
          >
            选择规格
          </Button>
          <span
            v-if="
              selectedSku &&
              selectedSku.properties &&
              selectedSku.properties.length > 0
            "
          >
            已选:
            {{ selectedSku.properties.map((p: any) => p.valueName).join('/') }}
          </span>
          <span v-else-if="selectedSku">已选:{{ selectedSku.id }}</span>
        </div>
      </template>
    </Form>
    <SkuTableSelect ref="skuTableSelectRef" @change="handleSkuSelected" />
  </Modal>
</template>