AIPainting.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <!-- 智能绘画 -->
  3. <div class="home-container">
  4. <!-- 左侧折叠面板 -->
  5. <div class="left-group">
  6. <div class="title-box">
  7. <div class="box-icon" @click="goBack">
  8. <el-icon class="left-icon"><ArrowLeftBold /></el-icon>
  9. 智能绘画
  10. </div>
  11. </div>
  12. </div>
  13. <!-- 右侧AI问答 -->
  14. <div class="number-people">
  15. <div class="content-box">
  16. <!-- AI对话框 -->
  17. <div class="chat-dialog">
  18. <!-- 对话消息列表 -->
  19. <div class="message-list">
  20. <div v-for="(item, index) in imageAllList" :key="index">
  21. <!-- 用户消息 -->
  22. <div class="user-message" v-if="item.type === 'user'">
  23. {{ item.content }}
  24. </div>
  25. <!-- AI生成图片对话框 -->
  26. <div class="ai-message" v-if="item.type !== 'user'">
  27. {{ item.content }}
  28. <div class="image-list" v-if="item.imageList">
  29. <el-image
  30. v-for="(image, index) in item.imageList"
  31. :key="index"
  32. style=" width: fit-content; height: 150px; margin: 10px;"
  33. :src="image"
  34. :preview-src-list="item.imageList"
  35. fit="cover"
  36. show-progress
  37. >
  38. <template
  39. #toolbar="{ actions, prev, next, reset, activeIndex, setActiveItem }"
  40. >
  41. <el-icon @click="prev"><Back /></el-icon>
  42. <el-icon @click="next"><Right /></el-icon>
  43. <el-icon @click="setActiveItem(item.imageList.length - 1)">
  44. <DArrowRight />
  45. </el-icon>
  46. <el-icon @click="actions('zoomOut')"><ZoomOut /></el-icon>
  47. <el-icon
  48. @click="actions('zoomIn', { enableTransition: false, zoomRate: 2 })">
  49. <ZoomIn />
  50. </el-icon>
  51. <el-icon
  52. @click="actions('clockwise', { rotateDeg: 180, enableTransition: false })">
  53. <RefreshRight />
  54. </el-icon>
  55. <el-icon @click="actions('anticlockwise')"><RefreshLeft /></el-icon>
  56. <el-icon @click="reset"><Refresh /></el-icon>
  57. <el-icon @click="download(activeIndex)"><Download /></el-icon>
  58. </template>
  59. </el-image>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. <!-- 输入框和发送按钮 -->
  65. <div class="input-section">
  66. <input
  67. type="text"
  68. v-model="inputMessage"
  69. placeholder="说说你的灵感..."
  70. />
  71. <button @click="sendMessage">发送</button>
  72. </div>
  73. </div>
  74. </div>
  75. </div>
  76. </div>
  77. </template>
  78. <script setup>
  79. import { ref, onMounted,onUnmounted} from 'vue'
  80. import {AiImageStatusEnum, CreatePainting, PaintingGetMys} from '@/api/questions.js'
  81. import { useRouter, useRoute } from 'vue-router'
  82. import {
  83. Document,
  84. Menu as IconMenu,
  85. Location,
  86. Setting,
  87. ArrowLeftBold
  88. } from '@element-plus/icons-vue'
  89. // 返回上一页
  90. const goBack = () => {
  91. router.go(-1)
  92. }
  93. const router = useRouter()
  94. const route = useRoute()
  95. // AI生成图片
  96. // CreatePainting({
  97. // modelId: 56,
  98. // prompt:''
  99. // }).then(res=>{
  100. // console.log(res);
  101. // })
  102. // 获取绘图记录
  103. // CreatePaintingGetMy().then(res=>{
  104. // console.log(res);
  105. // })
  106. // 消息列表和输入内容的响应式变量
  107. const messages = ref([])
  108. const inputMessage = ref('')
  109. // 发送消息函数
  110. const sendMessage = () => {
  111. console.log(inputMessage.value)
  112. if (inputMessage.value.trim()) {
  113. // messages.value.push(inputMessage.value.trim())
  114. // inputMessage.value = ''
  115. let content = inputMessage.value;
  116. inputMessage.value = ''
  117. imageAllList.value.push({
  118. type: 'user',
  119. content: content,
  120. })
  121. imageAllList.value.push({
  122. type: 'ai',
  123. content: "正在为您生成图片,请稍等...",
  124. })
  125. CreatePainting({
  126. "modelId": 56,
  127. "prompt":content,
  128. "width":1024,
  129. "height":1024
  130. }).then(res=>{
  131. console.log("生成图片",res)
  132. //目前写死调用已生成的图片,全部通了后再改
  133. inProgressImageMap.value[res.data] = {id:res.data,status:AiImageStatusEnum.IN_PROGRESS}
  134. // inProgressImageMap.value[260] = {id:260,status:AiImageStatusEnum.IN_PROGRESS}
  135. })
  136. }
  137. }
  138. // 生成图片
  139. import { ElIcon } from 'element-plus'
  140. import {
  141. Back,
  142. DArrowRight,
  143. Download,
  144. Refresh,
  145. RefreshLeft,
  146. RefreshRight,
  147. Right,
  148. ZoomIn,
  149. ZoomOut,
  150. } from '@element-plus/icons-vue'
  151. const imageAllList = ref([]) // 对话的消息列表
  152. const imageList = ref([]) // image 列表
  153. // 图片轮询相关的参数(正在生成中的)
  154. const inProgressImageMap = ref({}) // 监听的 image 映射,一般是生成中(需要轮询),key 为 image 编号,value 为 image
  155. const inProgressTimer = ref() // 生成中的 image 定时器,轮询生成进展
  156. /** 轮询生成中的 image 列表 */
  157. const refreshWatchImages = async () => {
  158. const imageIds = Object.keys(inProgressImageMap.value).map(Number)
  159. if (imageIds.length === 0) {
  160. return
  161. }
  162. console.log("轮询生成中的 image 列表",imageIds,imageIds.join(','))
  163. const list = await PaintingGetMys(imageIds)
  164. console.log("轮询生成中的 image 列表",list)
  165. const newWatchImages = {}
  166. list.data.forEach((image) => {
  167. if (image.status === AiImageStatusEnum.IN_PROGRESS) {
  168. newWatchImages[image.id] = image
  169. } else {
  170. imageAllList.value.pop();
  171. imageAllList.value.push({
  172. type: 'ai',
  173. content: "已为您生成图片:",
  174. imageList: [image.picUrl],
  175. })
  176. }
  177. })
  178. inProgressImageMap.value = newWatchImages
  179. if (newWatchImages.size === 0) {
  180. inProgressTimerFun()
  181. }
  182. }
  183. /** 组件挂在的时候 */
  184. onMounted(async () => {
  185. refreshWatchImagesFun()
  186. })
  187. /** 组件取消挂在的时候 */
  188. onUnmounted(async () => {
  189. inProgressTimerFun()
  190. })
  191. // 自动刷新 image 列表
  192. const refreshWatchImagesFun = () => {
  193. inProgressTimer.value = setInterval(async () => {
  194. await refreshWatchImages()
  195. }, 1000 * 3)
  196. }
  197. // 停止刷新image列表
  198. const inProgressTimerFun = () => {
  199. if (inProgressTimer.value) {
  200. clearInterval(inProgressTimer.value)
  201. }
  202. }
  203. </script>
  204. <style scoped lang="scss">
  205. @use 'sass:math';
  206. // 定义rpx转换函数
  207. @function rpx($px) {
  208. @return math.div($px, 750) * 100vw;
  209. }
  210. .home-container {
  211. position: fixed;
  212. top: 0;
  213. left: 0;
  214. right: 0;
  215. bottom: 0;
  216. display: flex;
  217. flex-direction: row;
  218. gap: rpx(0);
  219. }
  220. // 侧边栏
  221. .left-group {
  222. width: rpx(150);
  223. height: 100%;
  224. background-color: #ece9fd;
  225. }
  226. .left-group img {
  227. width: rpx(120);
  228. height: auto;
  229. margin-top: rpx(30);
  230. }
  231. .title-box {
  232. height: rpx(50);
  233. }
  234. .box-icon {
  235. width: 100%;
  236. height: 100%;
  237. flex: 1;
  238. display: flex; // 添加 flex 布局
  239. align-items: center; // 垂直居中
  240. color: black; // 设置图标颜色为白色
  241. padding-left: rpx(15);
  242. font-size: rpx(10); // 设置图标大小,可按需调整
  243. cursor: pointer; // 添加鼠标指针样式
  244. }
  245. .box-icon .left-icon {
  246. margin-left: rpx(10);
  247. margin-right: rpx(5); // 设置图标和文字之间的间距 ;
  248. }
  249. .number-people {
  250. flex: 1;
  251. height: 100%;
  252. display: flex;
  253. background-color: #ece9fd;
  254. }
  255. .content-box {
  256. flex: 1;
  257. margin-top: rpx(10);
  258. margin-bottom: rpx(10);
  259. margin-right: rpx(10);
  260. border-radius: rpx(15);
  261. background: rgba($color: #ffffff, $alpha: 0.5);
  262. }
  263. // 对话框
  264. .chat-dialog {
  265. display: flex;
  266. flex-direction: column;
  267. height: 100%;
  268. }
  269. .message-list {
  270. flex: 1;
  271. overflow-y: auto;
  272. padding: rpx(15);
  273. }
  274. .message-list .user-message {
  275. background-color: #ffffff;
  276. margin-left: auto; // 消息靠右显示
  277. margin-right: 0; // 重置右边距
  278. max-width: rpx(400);
  279. font-size: rpx(8);
  280. width: fit-content; // 宽度随文字内容变化
  281. border-radius: rpx(5);
  282. padding: rpx(5);
  283. text-align: left; // 文字左对齐
  284. }
  285. .message-list .ai-message {
  286. background-color: #ffdd55;
  287. margin-left: 0; // 消息靠左显示
  288. margin-right: auto; // 重置右边距
  289. margin-bottom: rpx(10);
  290. width: fit-content;
  291. max-width: rpx(400);
  292. padding: rpx(5);
  293. font-size: rpx(8);
  294. border-radius: rpx(5);
  295. text-align: left; // 文字左对齐
  296. }
  297. .input-section {
  298. display: flex;
  299. padding: rpx(10);
  300. gap: rpx(10);
  301. }
  302. .input-section input {
  303. flex: 1;
  304. padding: rpx(5);
  305. font-size: rpx(7);
  306. border: 1px solid #ccc;
  307. border-radius: rpx(5);
  308. }
  309. .input-section button {
  310. padding: rpx(5) rpx(15);
  311. background: linear-gradient(
  312. to bottom,
  313. #fee78a,
  314. #ffce1b
  315. ); /* 设置悬停、聚焦、点击状态下的背景色 */
  316. color: black;
  317. border: none;
  318. font-size: rpx(7);
  319. border-radius: rpx(5);
  320. cursor: pointer;
  321. box-shadow: 0 4px 8px rgba(202, 52, 52, 0.3);
  322. }
  323. .image-list {
  324. display: flex;
  325. flex-wrap: wrap;
  326. }
  327. </style>