Left.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <!-- 定义 tab 组件:撰写/回复等 -->
  3. <DefineTab v-slot="{ active, text, itemClick }">
  4. <span
  5. class="inline-block w-1/2 rounded-full cursor-pointer text-center leading-[30px] relative z-1 text-[5C6370] hover:text-black"
  6. :class="active ? 'text-black shadow-md' : 'hover:bg-[#DDDFE3]'"
  7. @click="itemClick"
  8. >
  9. {{ text }}
  10. </span>
  11. </DefineTab>
  12. <!-- 定义 label 组件:长度/格式/语气/语言等 -->
  13. <DefineLabel v-slot="{ label, hint, hintClick }">
  14. <h3 class="mt-5 mb-3 flex items-center justify-between text-[14px]">
  15. <span>{{ label }}</span>
  16. <span
  17. @click="hintClick"
  18. v-if="hint"
  19. class="flex items-center text-[12px] text-[#846af7] cursor-pointer select-none"
  20. >
  21. <Icon icon="ep:question-filled" />
  22. {{ hint }}
  23. </span>
  24. </h3>
  25. </DefineLabel>
  26. <div class="flex flex-col" v-bind="$attrs">
  27. <!-- tab -->
  28. <div class="w-full pt-2 bg-[#f5f7f9] flex justify-center">
  29. <div class="w-[303px] rounded-full bg-[#DDDFE3] p-1 z-10">
  30. <div
  31. class="flex items-center relative after:content-[''] after:block after:bg-white after:h-[30px] after:w-1/2 after:absolute after:top-0 after:left-0 after:transition-transform after:rounded-full"
  32. :class="
  33. selectedTab === AiWriteTypeEnum.REPLY && 'after:transform after:translate-x-[100%]'
  34. "
  35. >
  36. <ReuseTab
  37. v-for="tab in tabs"
  38. :key="tab.value"
  39. :text="tab.text"
  40. :active="tab.value === selectedTab"
  41. :itemClick="() => switchTab(tab.value)"
  42. />
  43. </div>
  44. </div>
  45. </div>
  46. <div
  47. class="px-7 pb-2 flex-grow overflow-y-auto lg:block w-[380px] box-border bg-[#f5f7f9] h-full"
  48. >
  49. <div>
  50. <template v-if="selectedTab === 1">
  51. <ReuseLabel label="写作内容" hint="示例" :hint-click="() => example('write')" />
  52. <el-input
  53. type="textarea"
  54. :rows="5"
  55. :maxlength="500"
  56. v-model="formData.prompt"
  57. placeholder="请输入写作内容"
  58. showWordLimit
  59. />
  60. </template>
  61. <template v-else>
  62. <ReuseLabel label="原文" hint="示例" :hint-click="() => example('reply')" />
  63. <el-input
  64. type="textarea"
  65. :rows="5"
  66. :maxlength="500"
  67. v-model="formData.originalContent"
  68. placeholder="请输入原文"
  69. showWordLimit
  70. />
  71. <ReuseLabel label="回复内容" />
  72. <el-input
  73. type="textarea"
  74. :rows="5"
  75. :maxlength="500"
  76. v-model="formData.prompt"
  77. placeholder="请输入回复内容"
  78. showWordLimit
  79. />
  80. </template>
  81. <ReuseLabel label="长度" />
  82. <Tag v-model="formData.length" :tags="getIntDictOptions(DICT_TYPE.AI_WRITE_LENGTH)" />
  83. <ReuseLabel label="格式" />
  84. <Tag v-model="formData.format" :tags="getIntDictOptions(DICT_TYPE.AI_WRITE_FORMAT)" />
  85. <ReuseLabel label="语气" />
  86. <Tag v-model="formData.tone" :tags="getIntDictOptions(DICT_TYPE.AI_WRITE_TONE)" />
  87. <ReuseLabel label="语言" />
  88. <Tag v-model="formData.language" :tags="getIntDictOptions(DICT_TYPE.AI_WRITE_LANGUAGE)" />
  89. <div class="flex items-center justify-center mt-3">
  90. <el-button :disabled="isWriting" @click="reset">重置</el-button>
  91. <el-button :loading="isWriting" @click="submit" color="#846af7">生成</el-button>
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. </template>
  97. <script setup lang="ts">
  98. import { createReusableTemplate } from '@vueuse/core'
  99. import { ref } from 'vue'
  100. import Tag from './Tag.vue'
  101. import { WriteVO } from 'src/api/ai/write'
  102. import { omit } from 'lodash-es'
  103. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  104. import { AiWriteTypeEnum, WriteExample } from '@/views/ai/utils/constants'
  105. type TabType = WriteVO['type']
  106. const message = useMessage() // 消息弹窗
  107. defineProps<{
  108. isWriting: boolean
  109. }>()
  110. const emits = defineEmits<{
  111. (e: 'submit', params: Partial<WriteVO>)
  112. (e: 'example', param: 'write' | 'reply')
  113. (e: 'reset')
  114. }>()
  115. /** 点击示例的时候,将定义好的文章作为示例展示出来 **/
  116. const example = (type: 'write' | 'reply') => {
  117. formData.value = {
  118. ...initData,
  119. ...omit(WriteExample[type], ['data'])
  120. }
  121. emits('example', type)
  122. }
  123. /** 重置,将表单值作为初选值 **/
  124. const reset = () => {
  125. formData.value = { ...initData }
  126. emits('reset')
  127. }
  128. const selectedTab = ref<TabType>(AiWriteTypeEnum.WRITING)
  129. const tabs: {
  130. text: string
  131. value: TabType
  132. }[] = [
  133. { text: '撰写', value: AiWriteTypeEnum.WRITING },
  134. { text: '回复', value: AiWriteTypeEnum.REPLY }
  135. ]
  136. const [DefineTab, ReuseTab] = createReusableTemplate<{
  137. active?: boolean
  138. text: string
  139. itemClick: () => void
  140. }>()
  141. /**
  142. * 可以在 template 里边定义可复用的组件,DefineLabel,ReuseLabel 是采用的解构赋值,都是 Vue 组件
  143. *
  144. * 直接通过组件的形式使用,<DefineLabel v-slot="{ label, hint, hintClick }"> 中间是需要复用的组件代码 <DefineLabel />,通过 <ReuseLabel /> 来使用定义的组件
  145. * DefineLabel 里边的 v-slot="{ label, hint, hintClick }"相当于是解构了组件的 prop,需要注意的是 boolean 类型,需要显式的赋值比如 <ReuseLabel :flag="true" />
  146. * 事件也得以 prop 形式传入,不能是 @event的形式,比如下面的 hintClick 需要<ReuseLabel :hintClick="() => { doSomething }"/>
  147. *
  148. * @see https://vueuse.org/createReusableTemplate
  149. */
  150. const [DefineLabel, ReuseLabel] = createReusableTemplate<{
  151. label: string
  152. class?: string
  153. hint?: string
  154. hintClick?: () => void
  155. }>()
  156. const initData: WriteVO = {
  157. type: 1,
  158. prompt: '',
  159. originalContent: '',
  160. tone: 1,
  161. language: 1,
  162. length: 1,
  163. format: 1
  164. }
  165. const formData = ref<WriteVO>({ ...initData })
  166. /** 用来记录切换之前所填写的数据,切换的时候给赋值回来 **/
  167. const recordFormData = {} as Record<AiWriteTypeEnum, WriteVO>
  168. /** 切换tab **/
  169. const switchTab = (value: TabType) => {
  170. if (value !== selectedTab.value) {
  171. // 保存之前的久数据
  172. recordFormData[selectedTab.value] = formData.value
  173. selectedTab.value = value
  174. // 将之前的旧数据赋值回来
  175. formData.value = { ...initData, ...recordFormData[value] }
  176. }
  177. }
  178. /** 提交写作 */
  179. const submit = () => {
  180. if (selectedTab.value === 2 && !formData.value.originalContent) {
  181. message.warning('请输入原文')
  182. return
  183. }
  184. if (!formData.value.prompt) {
  185. message.warning(`请输入${selectedTab.value === 1 ? '写作' : '回复'}内容`)
  186. return
  187. }
  188. emits('submit', {
  189. /** 撰写的时候没有 originalContent 字段**/
  190. ...(selectedTab.value === 1 ? omit(formData.value, ['originalContent']) : formData.value),
  191. /** 使用选中 tab 值覆盖当前的 type 类型 **/
  192. type: selectedTab.value
  193. })
  194. }
  195. </script>