index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <template>
  2. <view class="page">
  3. <!-- 顶部导航栏 -->
  4. <view class="nav-bar">
  5. <view class="nav-content">
  6. <text class="page-title">生成音频</text>
  7. </view>
  8. </view>
  9. <!-- 主内容区 -->
  10. <view class="main-content">
  11. <!-- 文本输入区 -->
  12. <view class="card input-card">
  13. <view class="card-header">
  14. <text class="card-title">输入文本</text>
  15. <text class="word-count">{{ text.length }} 字</text>
  16. </view>
  17. <textarea
  18. v-model="text"
  19. class="text-input"
  20. placeholder="请输入要转换的文本内容..."
  21. :maxlength="50000"
  22. auto-height
  23. />
  24. <view class="input-actions">
  25. <button class="action-btn ai-btn" @click="goToAIPage">
  26. <text>🤖 AI 生成</text>
  27. </button>
  28. <button class="action-btn" @click="clearText">清空</button>
  29. <button class="action-btn" @click="pasteText">粘贴</button>
  30. </view>
  31. <view class="input-hint">
  32. <text>支持长按粘贴文本内容</text>
  33. </view>
  34. </view>
  35. <!-- 音色选择 -->
  36. <view class="card voice-card">
  37. <text class="card-title">选择音色</text>
  38. <scroll-view scroll-x class="voice-list">
  39. <view
  40. v-for="voice in audioStore.voices"
  41. :key="voice.id"
  42. class="voice-item"
  43. :class="{ active: selectedVoice === voice.id }"
  44. @click="selectedVoice = voice.id"
  45. >
  46. <view class="voice-icon">
  47. <text>{{ voice.gender === 'female' ? '👩' : '👨' }}</text>
  48. </view>
  49. <text class="voice-name">{{ voice.name }}</text>
  50. <text class="voice-desc">{{ voice.description }}</text>
  51. </view>
  52. </scroll-view>
  53. </view>
  54. <!-- 参数调节 -->
  55. <view class="card params-card">
  56. <text class="card-title">参数调节</text>
  57. <view class="param-item">
  58. <text class="param-label">语速:{{ voiceParams.speed.toFixed(1) }}x</text>
  59. <slider
  60. :value="(voiceParams.speed - 0.5) * 100 / 1.5"
  61. :min="0"
  62. :max="100"
  63. @change="(e: any) => voiceParams.speed = Number((0.5 + (e.detail.value / 100) * 1.5).toFixed(1))"
  64. activeColor="#4F46E5"
  65. backgroundColor="#e5e7eb"
  66. block-size="20"
  67. />
  68. </view>
  69. <view class="param-item">
  70. <text class="param-label">音调:{{ voiceParams.pitch > 0 ? '+' : '' }}{{ voiceParams.pitch }}</text>
  71. <slider
  72. :value="(voiceParams.pitch + 500) / 10"
  73. :min="0"
  74. :max="100"
  75. @change="(e: any) => voiceParams.pitch = Math.round(e.detail.value * 10 - 500)"
  76. activeColor="#4F46E5"
  77. backgroundColor="#e5e7eb"
  78. block-size="20"
  79. />
  80. </view>
  81. <view class="param-item">
  82. <text class="param-label">音量:{{ voiceParams.volume }}%</text>
  83. <slider
  84. :value="voiceParams.volume"
  85. :min="0"
  86. :max="100"
  87. @change="(e: any) => voiceParams.volume = e.detail.value"
  88. activeColor="#4F46E5"
  89. backgroundColor="#e5e7eb"
  90. block-size="20"
  91. />
  92. </view>
  93. </view>
  94. <!-- 生成按钮 -->
  95. <button
  96. class="generate-btn"
  97. :disabled="!canGenerate"
  98. @click="handleGenerate"
  99. >
  100. <text class="btn-text">{{ generating ? '生成中...' : '生成音频' }}</text>
  101. </button>
  102. </view>
  103. </view>
  104. </template>
  105. <script setup lang="ts">
  106. import { ref, computed, onMounted } from 'vue';
  107. import { onShow } from '@dcloudio/uni-app';
  108. import { useUserStore } from '../../store/user';
  109. import { useAudioStore } from '../../store/audio';
  110. import type { VoiceParams } from '../../types';
  111. const userStore = useUserStore();
  112. const audioStore = useAudioStore();
  113. // 状态
  114. const text = ref('');
  115. const selectedVoice = ref('cherry');
  116. const voiceParams = ref<VoiceParams>({
  117. speed: 1.0,
  118. pitch: 0,
  119. volume: 50,
  120. });
  121. const generating = ref(false);
  122. // 计算属性
  123. const canGenerate = computed(() => {
  124. return text.value.trim().length > 0 && !generating.value;
  125. });
  126. // 初始化
  127. onMounted(async () => {
  128. await audioStore.fetchVoices();
  129. });
  130. // 页面显示时检查是否有 AI 生成的文本
  131. onShow(() => {
  132. const aiText = uni.getStorageSync('ai_generated_text');
  133. if (aiText) {
  134. text.value = aiText;
  135. uni.removeStorageSync('ai_generated_text');
  136. uni.showToast({ title: '已填充 AI 生成的文本', icon: 'success' });
  137. }
  138. });
  139. // 清空文本
  140. function clearText() {
  141. text.value = '';
  142. }
  143. // 粘贴文本
  144. async function pasteText() {
  145. const content = await uni.getClipboardData();
  146. if (content.data) {
  147. text.value = content.data;
  148. }
  149. }
  150. // 跳转到 AI 生成页面
  151. function goToAIPage() {
  152. uni.navigateTo({ url: '/pages/ai/index' });
  153. }
  154. // 生成音频
  155. async function handleGenerate() {
  156. if (!canGenerate.value) return;
  157. generating.value = true;
  158. try {
  159. const result = await audioStore.generateAudio(
  160. text.value,
  161. selectedVoice.value,
  162. voiceParams.value
  163. );
  164. uni.showToast({ title: '生成成功', icon: 'success' });
  165. // 清空文本,方便下次输入
  166. text.value = '';
  167. // 跳转到播放器
  168. uni.navigateTo({
  169. url: `/pages/player/index?id=${result.audioId}`,
  170. });
  171. } catch (error: any) {
  172. console.error('生成失败:', error);
  173. uni.showToast({
  174. title: error.message || '生成失败,请重试',
  175. icon: 'none'
  176. });
  177. } finally {
  178. generating.value = false;
  179. }
  180. }
  181. </script>
  182. <style scoped>
  183. .page {
  184. min-height: 100vh;
  185. background: #f5f5f5;
  186. }
  187. .nav-bar {
  188. position: fixed;
  189. top: 0;
  190. left: 0;
  191. right: 0;
  192. height: 88rpx;
  193. background: #ffffff;
  194. z-index: 100;
  195. padding-top: 44rpx;
  196. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  197. }
  198. .nav-content {
  199. display: flex;
  200. align-items: center;
  201. justify-content: center;
  202. padding: 0 32rpx;
  203. height: 88rpx;
  204. }
  205. .page-title {
  206. font-size: 34rpx;
  207. font-weight: 600;
  208. color: #1f2937;
  209. }
  210. .main-content {
  211. padding: 132rpx 32rpx 180rpx;
  212. }
  213. .card {
  214. background: #ffffff;
  215. border-radius: 24rpx;
  216. padding: 32rpx;
  217. margin-bottom: 24rpx;
  218. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  219. }
  220. .card-header {
  221. display: flex;
  222. justify-content: space-between;
  223. align-items: center;
  224. margin-bottom: 24rpx;
  225. }
  226. .card-title {
  227. font-size: 32rpx;
  228. font-weight: 600;
  229. color: #1f2937;
  230. }
  231. .word-count {
  232. font-size: 24rpx;
  233. color: #6b7280;
  234. }
  235. .text-input {
  236. width: 100%;
  237. min-height: 300rpx;
  238. font-size: 28rpx;
  239. color: #1f2937;
  240. line-height: 1.6;
  241. }
  242. .input-actions {
  243. display: flex;
  244. justify-content: flex-end;
  245. gap: 16rpx;
  246. margin-top: 16rpx;
  247. }
  248. .input-hint {
  249. margin-top: 12rpx;
  250. text-align: right;
  251. }
  252. .input-hint text {
  253. font-size: 22rpx;
  254. color: #9ca3af;
  255. }
  256. .action-btn {
  257. font-size: 24rpx;
  258. color: #6b7280;
  259. background: #f3f4f6;
  260. padding: 12rpx 24rpx;
  261. border-radius: 12rpx;
  262. border: none;
  263. transition: all 0.2s;
  264. }
  265. .action-btn:active {
  266. opacity: 0.7;
  267. transform: scale(0.98);
  268. }
  269. .action-btn::after {
  270. border: none;
  271. }
  272. .voice-list {
  273. white-space: nowrap;
  274. margin-top: 24rpx;
  275. }
  276. .voice-item {
  277. display: inline-flex;
  278. flex-direction: column;
  279. align-items: center;
  280. width: 160rpx;
  281. padding: 24rpx 16rpx;
  282. margin-right: 16rpx;
  283. border-radius: 16rpx;
  284. background: #f9fafb;
  285. border: 2rpx solid transparent;
  286. transition: all 0.3s;
  287. }
  288. .voice-item.active {
  289. background: linear-gradient(135deg, #4f46e5 0%, #6366f1 100%);
  290. border-color: #4f46e5;
  291. }
  292. .voice-item.active .voice-name,
  293. .voice-item.active .voice-desc {
  294. color: #ffffff;
  295. }
  296. .voice-icon {
  297. font-size: 48rpx;
  298. margin-bottom: 12rpx;
  299. }
  300. .voice-name {
  301. font-size: 26rpx;
  302. font-weight: 500;
  303. color: #1f2937;
  304. margin-bottom: 4rpx;
  305. }
  306. .voice-desc {
  307. font-size: 20rpx;
  308. color: #6b7280;
  309. }
  310. .param-item {
  311. margin-top: 24rpx;
  312. }
  313. .param-label {
  314. font-size: 26rpx;
  315. color: #6b7280;
  316. margin-bottom: 12rpx;
  317. display: block;
  318. }
  319. .generate-btn {
  320. width: 100%;
  321. height: 96rpx;
  322. background: linear-gradient(135deg, #4f46e5 0%, #818cf8 100%);
  323. border-radius: 24rpx;
  324. border: none;
  325. display: flex;
  326. align-items: center;
  327. justify-content: center;
  328. }
  329. .generate-btn::after {
  330. border: none;
  331. }
  332. .generate-btn[disabled] {
  333. background: #e5e7eb;
  334. }
  335. .btn-text {
  336. font-size: 32rpx;
  337. font-weight: 600;
  338. color: #ffffff;
  339. }
  340. .ai-btn {
  341. display: flex;
  342. align-items: center;
  343. gap: 8rpx;
  344. }
  345. </style>