index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <!-- dall3 -->
  2. <template>
  3. <div class="prompt">
  4. <el-text tag="b">画面描述</el-text>
  5. <el-text tag="p">建议使用“形容词+动词+风格”的格式,使用“,”隔开.</el-text>
  6. <el-input
  7. v-model="prompt"
  8. maxlength="1024"
  9. :rows="5"
  10. class="w-100% mt-15px"
  11. input-style="border-radius: 7px;"
  12. placeholder="例如:童话里的小屋应该是什么样子?"
  13. show-word-limit
  14. type="textarea"
  15. />
  16. </div>
  17. <div class="flex flex-col mt-30px">
  18. <div>
  19. <el-text tag="b">随机热词</el-text>
  20. </div>
  21. <el-space wrap class="flex flex-row flex-wrap justify-start mt-15px">
  22. <el-button
  23. round
  24. class="m-0"
  25. :type="selectHotWord === hotWord ? 'primary' : 'default'"
  26. v-for="hotWord in ImageHotWords"
  27. :key="hotWord"
  28. @click="handleHotWordClick(hotWord)"
  29. >
  30. {{ hotWord }}
  31. </el-button>
  32. </el-space>
  33. </div>
  34. <div class="w-full mt-30px">
  35. <div>
  36. <el-text tag="b">尺寸</el-text>
  37. </div>
  38. <el-space wrap class="flex flex-row justify-between w-full mt-20px">
  39. <div
  40. class="flex flex-col items-center cursor-pointer"
  41. v-for="imageSize in MidjourneySizeList"
  42. :key="imageSize.key"
  43. @click="handleSizeClick(imageSize)"
  44. >
  45. <div
  46. :class="selectSize === imageSize.key ? 'flex flex-col items-center justify-center rounded-7px p-4px w-50px h-50px bg-white border-1 border-solid border-#1293ff' : 'flex flex-col items-center justify-center rounded-7px p-4px w-50px h-50px bg-white border-1 border-solid border-white'"
  47. >
  48. <div :style="imageSize.style"></div>
  49. </div>
  50. <div class="text-14px color-#3e3e3e font-bold">{{ imageSize.key }}</div>
  51. </div>
  52. </el-space>
  53. </div>
  54. <div class="mt-30px">
  55. <div>
  56. <el-text tag="b">模型</el-text>
  57. </div>
  58. <el-space wrap class="mt-15px">
  59. <div
  60. :class="selectModel === model.key ? 'flex flex-col items-center w-150px overflow-hidden border-3 border-solid border-#1293ff rounded-5px cursor-pointer' : 'flex flex-col items-center w-150px overflow-hidden border-3 border-solid border-transparent cursor-pointer'"
  61. v-for="model in MidjourneyModels"
  62. :key="model.key"
  63. >
  64. <el-image :src="model.image" fit="contain" @click="handleModelClick(model)" />
  65. <div class="text-14px color-#3e3e3e font-bold">{{ model.name }}</div>
  66. </div>
  67. </el-space>
  68. </div>
  69. <div class="mt-20px">
  70. <div>
  71. <el-text tag="b">版本</el-text>
  72. </div>
  73. <el-space wrap class="mt-20px w-full">
  74. <el-select
  75. v-model="selectVersion"
  76. class="!w-350px"
  77. clearable
  78. placeholder="请选择版本"
  79. >
  80. <el-option
  81. v-for="item in versionList"
  82. :key="item.value"
  83. :label="item.label"
  84. :value="item.value"
  85. />
  86. </el-select>
  87. </el-space>
  88. </div>
  89. <div class="mt-30px">
  90. <div>
  91. <el-text tag="b">参考图</el-text>
  92. </div>
  93. <el-space wrap class="mt-15px">
  94. <UploadImg v-model="referImageUrl" height="120px" width="120px" />
  95. </el-space>
  96. </div>
  97. <div class="flex justify-center mt-50px">
  98. <el-button
  99. type="primary"
  100. size="large"
  101. round
  102. :disabled="prompt.length === 0"
  103. @click="handleGenerateImage"
  104. >
  105. {{ drawIn ? '生成中' : '生成内容' }}
  106. </el-button>
  107. </div>
  108. </template>
  109. <script setup lang="ts">
  110. import { ImageApi, ImageMidjourneyImagineReqVO, ImageVO } from '@/api/ai/image'
  111. import {
  112. AiPlatformEnum,
  113. ImageHotWords,
  114. ImageSizeVO,
  115. ImageModelVO,
  116. MidjourneyModels,
  117. MidjourneySizeList,
  118. MidjourneyVersions,
  119. NijiVersionList
  120. } from '@/views/ai/utils/constants'
  121. import { ModelVO } from '@/api/ai/model/model'
  122. const message = useMessage() // 消息弹窗
  123. // 接收父组件传入的模型列表
  124. const props = defineProps({
  125. models: {
  126. type: Array<ModelVO>,
  127. default: () => [] as ModelVO[]
  128. }
  129. })
  130. const emits = defineEmits(['onDrawStart', 'onDrawComplete']) // 定义 emits
  131. // 定义属性
  132. const drawIn = ref<boolean>(false) // 生成中
  133. const selectHotWord = ref<string>('') // 选中的热词
  134. // 表单
  135. const prompt = ref<string>('') // 提示词
  136. const referImageUrl = ref<any>() // 参考图
  137. const selectModel = ref<string>('midjourney') // 选中的模型
  138. const selectSize = ref<string>('1:1') // 选中 size
  139. const selectVersion = ref<any>('6.0') // 选中的 version
  140. const versionList = ref<any>(MidjourneyVersions) // version 列表
  141. /** 选择热词 */
  142. const handleHotWordClick = async (hotWord: string) => {
  143. // 情况一:取消选中
  144. if (selectHotWord.value == hotWord) {
  145. selectHotWord.value = ''
  146. return
  147. }
  148. // 情况二:选中
  149. selectHotWord.value = hotWord // 选中
  150. prompt.value = hotWord // 设置提示次
  151. }
  152. /** 点击 size 尺寸 */
  153. const handleSizeClick = async (imageSize: ImageSizeVO) => {
  154. selectSize.value = imageSize.key
  155. }
  156. /** 点击 model 模型 */
  157. const handleModelClick = async (model: ImageModelVO) => {
  158. selectModel.value = model.key
  159. if (model.key === 'niji') {
  160. versionList.value = NijiVersionList // 默认选择 niji
  161. } else {
  162. versionList.value = MidjourneyVersions // 默认选择 midjourney
  163. }
  164. selectVersion.value = versionList.value[0].value
  165. }
  166. /** 图片生成 */
  167. const handleGenerateImage = async () => {
  168. // 从 models 中查找匹配的模型
  169. const matchedModel = props.models.find(
  170. (item) => item.model === selectModel.value && item.platform === AiPlatformEnum.MIDJOURNEY
  171. )
  172. if (!matchedModel) {
  173. message.error('该模型不可用,请选择其它模型')
  174. return
  175. }
  176. // 二次确认
  177. await message.confirm(`确认生成内容?`)
  178. try {
  179. // 加载中
  180. drawIn.value = true
  181. // 回调
  182. emits('onDrawStart', AiPlatformEnum.MIDJOURNEY)
  183. // 发送请求
  184. const imageSize = MidjourneySizeList.find(
  185. (item) => selectSize.value === item.key
  186. ) as ImageSizeVO
  187. const req = {
  188. prompt: prompt.value,
  189. modelId: matchedModel.id,
  190. width: imageSize.width,
  191. height: imageSize.height,
  192. version: selectVersion.value,
  193. referImageUrl: referImageUrl.value
  194. } as ImageMidjourneyImagineReqVO
  195. await ImageApi.midjourneyImagine(req)
  196. } finally {
  197. // 回调
  198. emits('onDrawComplete', AiPlatformEnum.MIDJOURNEY)
  199. // 加载结束
  200. drawIn.value = false
  201. }
  202. }
  203. /** 填充值 */
  204. const settingValues = async (detail: ImageVO) => {
  205. // 提示词
  206. prompt.value = detail.prompt
  207. // image size
  208. const imageSize = MidjourneySizeList.find(
  209. (item) => item.key === `${detail.width}:${detail.height}`
  210. ) as ImageSizeVO
  211. selectSize.value = imageSize.key
  212. // 选中模型
  213. const model = MidjourneyModels.find((item) => item.key === detail.options?.model) as ImageModelVO
  214. await handleModelClick(model)
  215. // 版本
  216. selectVersion.value = versionList.value.find(
  217. (item) => item.value === detail.options?.version
  218. ).value
  219. // image
  220. referImageUrl.value = detail.options.referImageUrl
  221. }
  222. /** 暴露组件方法 */
  223. defineExpose({ settingValues })
  224. </script>