index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <template>
  2. <el-dialog v-model="dialogVisible" title="场景图生成" width="600px" :close-on-click-modal="false"
  3. :close-on-press-escape="false" custom-class="scene-prompt-dialog" @close="handleClose">
  4. <div class="scene-prompt-container">
  5. <!-- 场景提示词输入区域 -->
  6. <div class="input-section">
  7. <div class="input-wrapper">
  8. <el-input v-model="scenePrompt" type="textarea" :rows="8" placeholder="请输入场景提示词" class="scene-input"
  9. resize="none" maxlength="500" />
  10. <!-- AI帮我写按钮 -->
  11. <div class="ai-help-button-container">
  12. <el-button type="primary" @click="handleAIHelp" :loading="aiLoading" class="ai-help-button">
  13. AI帮我写
  14. </el-button>
  15. </div>
  16. </div>
  17. </div>
  18. </div>
  19. <template #footer>
  20. <div class="dialog-footer">
  21. <el-button type="primary" @click="handleConfirm" :disabled="!canConfirm" class="confirm-button">
  22. 确认
  23. </el-button>
  24. </div>
  25. </template>
  26. </el-dialog>
  27. </template>
  28. <script setup lang="ts">
  29. import { ref, computed, watch } from 'vue'
  30. import { ElMessage } from 'element-plus'
  31. import { expandCameraWordsApi } from '@/apis/other'
  32. // 定义组件的 props
  33. interface Props {
  34. modelValue: boolean
  35. initialPrompt?: string
  36. }
  37. const props = defineProps<Props>()
  38. // 定义组件的事件
  39. const emit = defineEmits<{
  40. 'update:modelValue': [value: boolean]
  41. confirm: [prompt: string]
  42. cancel: []
  43. }>()
  44. // Dialog 显示状态
  45. const dialogVisible = computed({
  46. get: () => props.modelValue,
  47. set: (value) => emit('update:modelValue', value)
  48. })
  49. // 场景提示词
  50. const scenePrompt = ref('')
  51. // 本地缓存 key
  52. const SCENE_PROMPT_CACHE_KEY = 'scene_prompt_cache'
  53. // 从本地缓存读取
  54. const loadScenePromptFromCache = () => {
  55. try {
  56. const cache = localStorage.getItem(SCENE_PROMPT_CACHE_KEY)
  57. if (cache && !props.initialPrompt) {
  58. scenePrompt.value = cache
  59. }
  60. } catch {}
  61. }
  62. // 写入本地缓存
  63. const saveScenePromptToCache = () => {
  64. try {
  65. const value = (scenePrompt.value || '').trim()
  66. if (value) {
  67. localStorage.setItem(SCENE_PROMPT_CACHE_KEY, value)
  68. }
  69. } catch {}
  70. }
  71. // AI 加载状态
  72. const aiLoading = ref(false)
  73. // 是否可以确认
  74. const canConfirm = computed(() => {
  75. return scenePrompt.value.trim().length > 0
  76. })
  77. // AI 帮我写
  78. const handleAIHelp = async () => {
  79. if (aiLoading.value) return
  80. aiLoading.value = true
  81. try {
  82. // 调用后端接口生成场景提示词
  83. const res: any = await expandCameraWordsApi()
  84. const result = res?.data?.result || ''
  85. if (result) {
  86. scenePrompt.value = result
  87. ElMessage.success('AI 已为您生成场景提示词')
  88. } else {
  89. ElMessage.error('AI 未返回结果,请稍后重试')
  90. }
  91. } catch (error) {
  92. ElMessage.error('AI 生成失败,请重试')
  93. } finally {
  94. aiLoading.value = false
  95. }
  96. }
  97. // 确认
  98. const handleConfirm = () => {
  99. if (!canConfirm.value) return
  100. saveScenePromptToCache()
  101. emit('confirm', scenePrompt.value.trim())
  102. dialogVisible.value = false
  103. resetForm()
  104. }
  105. // 关闭弹窗
  106. const handleClose = () => {
  107. emit('cancel')
  108. }
  109. // 重置表单
  110. const resetForm = () => {
  111. scenePrompt.value = ''
  112. }
  113. // 监听弹窗显示状态变化,初始化场景提示词
  114. watch(dialogVisible, (newValue) => {
  115. if (newValue) {
  116. if (props.initialPrompt) {
  117. scenePrompt.value = props.initialPrompt
  118. } else {
  119. loadScenePromptFromCache()
  120. }
  121. }
  122. }, { immediate: true })
  123. </script>
  124. <style lang="scss" scoped>
  125. .scene-prompt-container {
  126. padding: 20px;
  127. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  128. background: #EAECED;
  129. }
  130. .input-section {
  131. position: relative;
  132. }
  133. .input-wrapper {
  134. position: relative;
  135. background: #f8f9fa;
  136. border-radius: 8px;
  137. border: 1px solid #e4e7ed;
  138. }
  139. .scene-input {
  140. :deep(.el-textarea__inner) {
  141. border: none;
  142. background: transparent;
  143. font-size: 16px;
  144. line-height: 1.6;
  145. color: #303133;
  146. resize: none;
  147. &::placeholder {
  148. color: #c0c4cc;
  149. }
  150. &:focus {
  151. box-shadow: none;
  152. }
  153. }
  154. }
  155. .ai-help-button-container {
  156. position: absolute;
  157. bottom: 15px;
  158. height: 25px;
  159. right: 6px;
  160. }
  161. .ai-help-button {
  162. background: #2957FF;
  163. border: none;
  164. border-radius: 20px;
  165. padding: 8px 16px;
  166. font-size: 13px;
  167. font-weight: 500;
  168. color: white;
  169. box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
  170. transition: all 0.3s ease;
  171. &:hover {
  172. transform: translateY(-2px);
  173. box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
  174. }
  175. &:active {
  176. transform: translateY(0);
  177. }
  178. .ai-icon {
  179. margin-right: 6px;
  180. font-size: 14px;
  181. }
  182. }
  183. .dialog-footer {
  184. display: flex;
  185. justify-content: center;
  186. gap: 16px;
  187. padding: 14px 20px;
  188. }
  189. .confirm-button {
  190. background: linear-gradient(135deg, #409eff 0%, #a855f7 100%);
  191. border: none;
  192. border-radius: 6px;
  193. height: 36px;
  194. padding: 12px 32px;
  195. font-size: 14px;
  196. font-weight: 500;
  197. color: white;
  198. box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
  199. transition: all 0.3s ease;
  200. &:hover {
  201. transform: translateY(-1px);
  202. box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
  203. }
  204. &:active {
  205. transform: translateY(0);
  206. }
  207. &:disabled {
  208. background: #c0c4cc;
  209. transform: none;
  210. box-shadow: none;
  211. cursor: not-allowed;
  212. }
  213. }
  214. </style>
  215. <style lang="scss">
  216. .scene-prompt-dialog {
  217. .el-dialog__body {
  218. padding: 0;
  219. }
  220. .el-dialog__footer {
  221. padding: 0 !important;
  222. background: #EAECED;
  223. }
  224. .el-dialog {
  225. border-radius: 12px !important;
  226. overflow: hidden;
  227. }
  228. .el-dialog__header {
  229. background: #f8f9fa;
  230. }
  231. .el-dialog__title {
  232. font-size: 18px !important;
  233. font-weight: 600 !important;
  234. color: #303133 !important;
  235. text-align: center !important;
  236. }
  237. }
  238. </style>