index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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="hot-words">
  18. <div>
  19. <el-text tag="b">随机热词</el-text>
  20. </div>
  21. <el-space wrap class="word-list">
  22. <el-button
  23. round
  24. class="btn"
  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="model">
  35. <div>
  36. <el-text tag="b">模型选择</el-text>
  37. </div>
  38. <el-space wrap class="model-list">
  39. <div
  40. :class="selectModel === model.key ? 'modal-item selectModel' : 'modal-item'"
  41. v-for="model in Dall3Models"
  42. :key="model.key"
  43. >
  44. <el-image :src="model.image" fit="contain" @click="handleModelClick(model)" />
  45. <div class="model-font">{{ model.name }}</div>
  46. </div>
  47. </el-space>
  48. </div>
  49. <div class="image-style">
  50. <div>
  51. <el-text tag="b">风格选择</el-text>
  52. </div>
  53. <el-space wrap class="image-style-list">
  54. <div
  55. :class="style === imageStyle.key ? 'image-style-item selectImageStyle' : 'image-style-item'"
  56. v-for="imageStyle in Dall3StyleList"
  57. :key="imageStyle.key"
  58. >
  59. <el-image :src="imageStyle.image" fit="contain" @click="handleStyleClick(imageStyle)" />
  60. <div class="style-font">{{ imageStyle.name }}</div>
  61. </div>
  62. </el-space>
  63. </div>
  64. <div class="image-size">
  65. <div>
  66. <el-text tag="b">画面比例</el-text>
  67. </div>
  68. <el-space wrap class="size-list">
  69. <div
  70. class="size-item"
  71. v-for="imageSize in Dall3SizeList"
  72. :key="imageSize.key"
  73. @click="handleSizeClick(imageSize)"
  74. >
  75. <div
  76. :class="selectSize === imageSize.key ? 'size-wrapper selectImageSize' : 'size-wrapper'"
  77. >
  78. <div :style="imageSize.style"></div>
  79. </div>
  80. <div class="size-font">{{ imageSize.name }}</div>
  81. </div>
  82. </el-space>
  83. </div>
  84. <div class="btns">
  85. <el-button
  86. type="primary"
  87. size="large"
  88. round
  89. :loading="drawIn"
  90. :disabled="prompt.length === 0"
  91. @click="handleGenerateImage"
  92. >
  93. {{ drawIn ? '生成中' : '生成内容' }}
  94. </el-button>
  95. </div>
  96. </template>
  97. <script setup lang="ts">
  98. import { ImageApi, ImageDrawReqVO, ImageVO } from '@/api/ai/image'
  99. import {
  100. Dall3Models,
  101. Dall3StyleList,
  102. ImageHotWords,
  103. Dall3SizeList,
  104. ImageModelVO,
  105. AiPlatformEnum,
  106. ImageSizeVO
  107. } from '@/views/ai/utils/constants'
  108. import { ModelVO } from '@/api/ai/model/model'
  109. const message = useMessage() // 消息弹窗
  110. // 接收父组件传入的模型列表
  111. const props = defineProps({
  112. models: {
  113. type: Array<ModelVO>,
  114. default: () => [] as ModelVO[]
  115. }
  116. })
  117. const emits = defineEmits(['onDrawStart', 'onDrawComplete']) // 定义 emits
  118. // 定义属性
  119. const prompt = ref<string>('') // 提示词
  120. const drawIn = ref<boolean>(false) // 生成中
  121. const selectHotWord = ref<string>('') // 选中的热词
  122. const selectModel = ref<string>('dall-e-3') // 模型
  123. const selectSize = ref<string>('1024x1024') // 选中 size
  124. const style = ref<string>('vivid') // style 样式
  125. /** 选择热词 */
  126. const handleHotWordClick = async (hotWord: string) => {
  127. // 情况一:取消选中
  128. if (selectHotWord.value == hotWord) {
  129. selectHotWord.value = ''
  130. return
  131. }
  132. // 情况二:选中
  133. selectHotWord.value = hotWord
  134. prompt.value = hotWord
  135. }
  136. /** 选择 model 模型 */
  137. const handleModelClick = async (model: ImageModelVO) => {
  138. selectModel.value = model.key
  139. // 可以在这里添加模型特定的处理逻辑
  140. // 例如,如果未来需要根据不同模型设置不同参数
  141. if (model.key === 'dall-e-3') {
  142. // DALL-E-3 模型特定的处理
  143. style.value = 'vivid' // 默认设置vivid风格
  144. } else if (model.key === 'dall-e-2') {
  145. // DALL-E-2 模型特定的处理
  146. style.value = 'natural' // 如果有其他DALL-E-2适合的默认风格
  147. }
  148. // 更新其他相关参数
  149. // 例如可以默认选择最适合当前模型的尺寸
  150. const recommendedSize = Dall3SizeList.find(
  151. (size) =>
  152. (model.key === 'dall-e-3' && size.key === '1024x1024') ||
  153. (model.key === 'dall-e-2' && size.key === '512x512')
  154. )
  155. if (recommendedSize) {
  156. selectSize.value = recommendedSize.key
  157. }
  158. }
  159. /** 选择 style 样式 */
  160. const handleStyleClick = async (imageStyle: ImageModelVO) => {
  161. style.value = imageStyle.key
  162. }
  163. /** 选择 size 大小 */
  164. const handleSizeClick = async (imageSize: ImageSizeVO) => {
  165. selectSize.value = imageSize.key
  166. }
  167. /** 图片生产 */
  168. const handleGenerateImage = async () => {
  169. // 从 models 中查找匹配的模型
  170. const matchedModel = props.models.find(
  171. (item) => item.model === selectModel.value && item.platform === AiPlatformEnum.OPENAI
  172. )
  173. if (!matchedModel) {
  174. message.error('该模型不可用,请选择其它模型')
  175. return
  176. }
  177. // 二次确认
  178. await message.confirm(`确认生成内容?`)
  179. try {
  180. // 加载中
  181. drawIn.value = true
  182. // 回调
  183. emits('onDrawStart', AiPlatformEnum.OPENAI)
  184. const imageSize = Dall3SizeList.find((item) => item.key === selectSize.value) as ImageSizeVO
  185. const form = {
  186. platform: AiPlatformEnum.OPENAI,
  187. prompt: prompt.value, // 提示词
  188. modelId: matchedModel.id, // 使用匹配到的模型
  189. style: style.value, // 图像生成的风格
  190. width: imageSize.width, // size 不能为空
  191. height: imageSize.height, // size 不能为空
  192. options: {
  193. style: style.value // 图像生成的风格
  194. }
  195. } as ImageDrawReqVO
  196. // 发送请求
  197. await ImageApi.drawImage(form)
  198. } finally {
  199. // 回调
  200. emits('onDrawComplete', AiPlatformEnum.OPENAI)
  201. // 加载结束
  202. drawIn.value = false
  203. }
  204. }
  205. /** 填充值 */
  206. const settingValues = async (detail: ImageVO) => {
  207. prompt.value = detail.prompt
  208. selectModel.value = detail.model
  209. style.value = detail.options?.style
  210. const imageSize = Dall3SizeList.find(
  211. (item) => item.key === `${detail.width}x${detail.height}`
  212. ) as ImageSizeVO
  213. await handleSizeClick(imageSize)
  214. }
  215. /** 暴露组件方法 */
  216. defineExpose({ settingValues })
  217. </script>
  218. <style scoped lang="scss">
  219. // 热词
  220. .hot-words {
  221. display: flex;
  222. flex-direction: column;
  223. margin-top: 30px;
  224. .word-list {
  225. display: flex;
  226. flex-direction: row;
  227. flex-wrap: wrap;
  228. justify-content: start;
  229. margin-top: 15px;
  230. .btn {
  231. margin: 0;
  232. }
  233. }
  234. }
  235. // 模型
  236. .model {
  237. margin-top: 30px;
  238. .model-list {
  239. margin-top: 15px;
  240. .modal-item {
  241. width: 110px;
  242. //outline: 1px solid blue;
  243. overflow: hidden;
  244. display: flex;
  245. flex-direction: column;
  246. align-items: center;
  247. border: 3px solid transparent;
  248. cursor: pointer;
  249. .model-font {
  250. font-size: 14px;
  251. color: #3e3e3e;
  252. font-weight: bold;
  253. }
  254. }
  255. .selectModel {
  256. border: 3px solid #1293ff;
  257. border-radius: 5px;
  258. }
  259. }
  260. }
  261. // 样式 style
  262. .image-style {
  263. margin-top: 30px;
  264. .image-style-list {
  265. margin-top: 15px;
  266. .image-style-item {
  267. width: 110px;
  268. //outline: 1px solid blue;
  269. overflow: hidden;
  270. display: flex;
  271. flex-direction: column;
  272. align-items: center;
  273. border: 3px solid transparent;
  274. cursor: pointer;
  275. .style-font {
  276. font-size: 14px;
  277. color: #3e3e3e;
  278. font-weight: bold;
  279. }
  280. }
  281. .selectImageStyle {
  282. border: 3px solid #1293ff;
  283. border-radius: 5px;
  284. }
  285. }
  286. }
  287. // 尺寸
  288. .image-size {
  289. width: 100%;
  290. margin-top: 30px;
  291. .size-list {
  292. display: flex;
  293. flex-direction: row;
  294. justify-content: space-between;
  295. width: 100%;
  296. margin-top: 20px;
  297. .size-item {
  298. display: flex;
  299. flex-direction: column;
  300. align-items: center;
  301. cursor: pointer;
  302. .size-wrapper {
  303. display: flex;
  304. flex-direction: column;
  305. align-items: center;
  306. justify-content: center;
  307. border-radius: 7px;
  308. padding: 4px;
  309. width: 50px;
  310. height: 50px;
  311. background-color: #fff;
  312. border: 1px solid #fff;
  313. }
  314. .size-font {
  315. font-size: 14px;
  316. color: #3e3e3e;
  317. font-weight: bold;
  318. }
  319. }
  320. }
  321. .selectImageSize {
  322. border: 1px solid #1293ff !important;
  323. }
  324. }
  325. .btns {
  326. display: flex;
  327. justify-content: center;
  328. margin-top: 50px;
  329. }
  330. </style>