index.vue 5.8 KB

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