ny
22 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import type { FormInstance } from 'ant-design-vue';
 
import { computed, nextTick, reactive, ref, toRefs, unref } from 'vue';
 
import { useAttrs, useMessage } from '@jnpf/hooks';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
import { formValidate } from '@jnpf/utils';
 
import { LoadingOutlined } from '@ant-design/icons-vue';
import { omit } from 'lodash-es';
 
import { create, getAiInfo } from '#/api/onlineDev/visualDev';
import { buildAiFormData } from '#/components/FormGenerator/src/helper/aiUtils';
 
interface State {
  dataForm: any;
  content: string;
  loading: boolean;
  categoryOptions: any[];
  showForm: boolean;
}
 
const emit = defineEmits(['register', 'reload']);
const { createMessage } = useMessage();
const [registerModal, { changeOkLoading, closeModal }] = useModalInner(init);
const formElRef = ref<FormInstance>();
const state = reactive<State>({
  dataForm: {},
  content: '',
  loading: false,
  categoryOptions: [],
  showForm: false,
});
const { dataForm, content, loading, categoryOptions, showForm } = toRefs(state);
const attrs = useAttrs({ excludeDefaultKeys: false });
const rules = {
  fullName: [{ required: true, message: '必填', trigger: 'blur' }],
  enCode: [{ validator: formValidate('enCode'), trigger: 'blur' }],
  category: [{ required: true, message: '必填', trigger: 'change' }],
};
 
const getBindValue = computed(() => {
  const attr: any = {
    ...unref(attrs),
    title: 'AI建表',
    okButtonProps: {
      disabled: state.loading,
    },
  };
  if (!state.showForm) attr.footer = null;
  return attr;
});
 
function init(data) {
  state.categoryOptions = data.categoryList || [];
  resetData();
  nextTick(() => {
    formElRef.value?.clearValidate();
  });
}
function resetData() {
  state.content = '';
  state.loading = false;
  state.showForm = false;
  state.dataForm = {};
}
function handleAiSubmit() {
  if (!state.content) return;
  state.loading = true;
  getAiInfo({ keyword: state.content })
    .then((res) => {
      state.showForm = true;
      state.loading = false;
      state.dataForm = { ...res.data, category: state.dataForm.category };
    })
    .catch(() => {
      state.loading = false;
    });
}
async function handleSubmit() {
  try {
    const values = await formElRef.value?.validate();
    if (!values) return;
    state.dataForm = { ...buildAiFormData(state.dataForm.aiModelList || []), ...omit(state.dataForm, ['aiModelList']) };
    changeOkLoading(true);
    create(state.dataForm)
      .then((res) => {
        changeOkLoading(false);
        closeModal();
        createMessage.success(res.msg);
        emit('reload', res.data?.id);
      })
      .catch(() => {
        changeOkLoading(false);
      });
  } catch {}
}
</script>
<template>
  <BasicModal v-bind="getBindValue" @register="registerModal" @ok="handleSubmit" destroy-on-close>
    <div class="text-tips">通过AI生成表单 ,根据文字描述智能生成推荐表单</div>
    <div class="ai-textarea">
      <jnpf-textarea v-model:value="content" placeholder="输入你的需求描述" :maxlength="100" />
      <div class="ai-button">
        <LoadingOutlined class="mr-[5px]" v-if="loading" />
        <i class="ym-custom ym-custom-send cursor-pointer" :class="{ 'icon-selected': content }" v-else @click="handleAiSubmit"></i>
      </div>
    </div>
    <a-form :colon="false" :label-col="{ style: { width: '77px' } }" :model="dataForm" :rules="rules" ref="formElRef" v-if="showForm">
      <div class="text-tips">表单已生成,请继续确认表单名称、编码及分类</div>
      <a-form-item label="表单名称" name="fullName">
        <jnpf-input v-model:value="dataForm.fullName" placeholder="请输入" :maxlength="100" />
      </a-form-item>
      <a-form-item label="表单编码" name="enCode">
        <jnpf-input v-model:value="dataForm.enCode" placeholder="请输入" :maxlength="50" />
      </a-form-item>
      <a-form-item label="表单分类" name="category">
        <jnpf-select v-model:value="dataForm.category" :options="categoryOptions" show-search />
      </a-form-item>
    </a-form>
  </BasicModal>
</template>
<style lang="scss" scoped>
.ai-textarea {
  position: relative;
  margin-bottom: 35px;
 
  .ai-button {
    position: absolute;
    right: 8px;
    bottom: 3px;
  }
}
 
.icon-selected {
  color: var(--primary-color) !important;
}
 
.text-tips {
  margin: 10px 20px 20px 0;
  color: var(--text-color-label);
}
</style>