刘光辉
8 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
<script setup lang="ts">
import { ref, watch } from 'vue';
 
const props = defineProps<{
  open: boolean;
  suggestCode?: (text: string) => Promise<string>;
  title: string;
}>();
const emit = defineEmits<{
  confirm: [code: string];
  'update:open': [open: boolean];
}>();
 
const text = ref('');
const suggestion = ref('');
const error = ref('');
const loading = ref(false);
 
watch(
  () => props.open,
  (open) => {
    if (!open) return;
    text.value = '';
    suggestion.value = '';
    error.value = '';
  },
);
watch(text, () => {
  suggestion.value = '';
  error.value = '';
});
 
function close(): void {
  if (!loading.value) emit('update:open', false);
}
 
async function generateSuggestion(): Promise<void> {
  const normalizedText = text.value.trim();
  if (!normalizedText || loading.value) return;
  if (!props.suggestCode) {
    error.value = '编码生成服务不可用';
    return;
  }
  loading.value = true;
  error.value = '';
  try {
    suggestion.value = await props.suggestCode(normalizedText);
  } catch (reason) {
    error.value = reason instanceof Error ? reason.message : '编码生成失败';
  } finally {
    loading.value = false;
  }
}
 
function confirmSuggestion(): void {
  if (!suggestion.value || loading.value) return;
  emit('confirm', suggestion.value);
  emit('update:open', false);
}
 
function submitCurrentAction(): void {
  if (suggestion.value) confirmSuggestion();
  else void generateSuggestion();
}
</script>
 
<template>
  <div v-if="open" data-test="code-assist-modal">
    <a-modal
      :closable="!loading"
      :get-container="false"
      :keyboard="!loading"
      :mask-closable="false"
      :open="open"
      :title="title"
      @cancel="close">
      <a-form layout="vertical">
        <a-form-item label="中文名称">
          <a-input
            v-model:value="text"
            autofocus
            data-test="code-assist-text"
            :disabled="loading"
            placeholder="请输入中文名称"
            @press-enter="submitCurrentAction" />
        </a-form-item>
        <a-form-item v-if="suggestion" label="建议编码">
          <a-input data-test="code-assist-suggestion" readonly :value="suggestion" />
        </a-form-item>
        <a-alert v-if="error" data-test="code-assist-error" :message="error" show-icon type="error" />
      </a-form>
      <template #footer>
        <a-button :disabled="loading" @click="close">取消</a-button>
        <a-button
          v-if="!suggestion"
          data-test="code-assist-generate"
          :disabled="!text.trim()"
          :loading="loading"
          type="primary"
          @click="generateSuggestion">
          生成编码
        </a-button>
        <a-button v-else data-test="code-assist-confirm" type="primary" @click="confirmSuggestion">确认填入</a-button>
      </template>
    </a-modal>
  </div>
</template>