makeMusicDetail.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <template>
  2. <view class="make-music-detail">
  3. <!-- 顶部导航 -->
  4. <!-- <view class="status-bar"></view> -->
  5. <view class="nav-bar">
  6. <view class="left">
  7. <view class="uni-btn-icon" @click="goBack">&#xe601;</view>
  8. <view class="create">音乐制作</view>
  9. <image src="@/static/makedetail/cz_icon_lingganchuangzuo.png" class="edit"></image>
  10. </view>
  11. <view class="right">
  12. <view class="coinM" @click="isRecharge ? goPage('/pages/vip/M_purchase') : ''">
  13. <image src="/static/icon/coin_m.png" mode="aspectFit"></image>
  14. <text>{{ myinfo.num_gmm | formatNumberToK }}</text>
  15. <image class="money-add" v-if="isRecharge" src="/static/icon/coin_add.png" mode="aspectFit"></image>
  16. </view>
  17. <view class="coinC" @click="isRecharge ?goPage('/pages/my/job?type=recharge') : ''">
  18. <image src="/static/icon/coin_cd.png" mode="aspectFit"></image>
  19. <text>{{ myinfo.num_gmd | formatNumberToK }}</text>
  20. <image class="money-add" v-if="isRecharge" src="/static/icon/coin_add.png" mode="aspectFit"></image>
  21. </view>
  22. </view>
  23. </view>
  24. <!-- 排队预览区域 -->
  25. <view class="preview-section lineUp-section" v-if="queuing">
  26. <view class="section-title">
  27. <text>创作预览</text>
  28. <view class="member-box">
  29. <image src="@/static/makedetail/wd_icon_vip(1).png" mode="aspectFit"></image>
  30. 升级会员插队加速
  31. </view>
  32. </view>
  33. <view class="preview-card">
  34. <image src="@/static/makedetail/cz_icon_jiazai.png" mode="aspectFit"></image>
  35. <view class="text1">{{ queueMessage }}</view>
  36. <view class="text2">退出不影响继续生成</view>
  37. </view>
  38. </view>
  39. <!-- 创作预览区域 -->
  40. <view class="preview-section" v-if="inQueue">
  41. <view class="section-title">创作预览</view>
  42. <view class="preview-card">
  43. <image src="@/static/makedetail/cz_icon_shengcheng.png" mode="aspectFit"></image>
  44. <view class="text1">生成中{{queueProgress}}%</view>
  45. <view class="text2">退出不影响继续生成</view>
  46. </view>
  47. </view>
  48. <!-- 主要内容区 -->
  49. <view class="content">
  50. <!-- 歌曲名称输入 -->
  51. <view class="input-section" style="margin-top: 0;">
  52. <text class="label">歌曲名称</text>
  53. <input type="text" placeholder="请输入名称..." class="input-field" maxlength="30" v-model="songName"
  54. :disabled="doYouWantToEdit()" style="padding-right: 90rpx;" />
  55. <text class="count lyricCount">{{ songName.length }}/30</text>
  56. </view>
  57. <!-- 创作歌词输入 -->
  58. <view class="input-section">
  59. <text class="label">创作歌词</text>
  60. <textarea placeholder="请在此处输入您想要进行联想的内容或者歌词" class="textarea-field" maxlength="800" v-model="lyrics"
  61. :disabled="doYouWantToEdit()" :style="{ height: textareaHeight + 'px' }" @input="onTextareaInput" />
  62. <view class="textarea-footer">
  63. <text class="count">{{ lyrics.length }}/800</text>
  64. <text class="ai-btn" v-if="false">
  65. <image src="@/static/makedetail/cz_btn_airunse.png"></image>
  66. </text>
  67. </view>
  68. </view>
  69. <!-- 音乐风格选择 -->
  70. <!-- <view class="style-section" style=" animation: fadeIn-data-v-032d5e1e 0.5s ease;">
  71. <text class="label">音乐风格</text>
  72. <view class="tabs">
  73. <text :class="{ 'active': selectedTab === 'emotion' }" @click="selectTab('emotion')">
  74. 情感
  75. <view class="indicator-triangle">
  76. </view>
  77. </text>
  78. <text :class="{ 'active': selectedTab === 'genre' }" @click="selectTab('genre')">
  79. 流派
  80. <view class="indicator-triangle">
  81. </view>
  82. </text>
  83. <text :class="{ 'active': selectedTab === 'era' }" @click="selectTab('era')">
  84. 年代
  85. <view class="indicator-triangle">
  86. </view>
  87. </text>
  88. <text :class="{ 'active': selectedTab === 'instrument' }" @click="selectTab('instrument')">
  89. 乐器
  90. <view class="indicator-triangle">
  91. </view>
  92. </text>
  93. </view>
  94. <view class="tags">
  95. <text v-for="(tag, index) in currentTags" :key="index"
  96. :class="['tag', { active: selectedTags[selectedTab].includes(tag) }]"
  97. @click="doYouWantToEdit() ? state() : toggleTag(tag)">
  98. {{ tag }}
  99. </text>
  100. </view>
  101. </view> -->
  102. <view class="style-section" style=" animation: fadeIn-data-v-032d5e1e 0.5s ease;">
  103. <text class="label">音乐风格</text>
  104. <scroll-view scroll-x class="tabs" :show-scrollbar="false"
  105. scroll-with-animation :scroll-into-view="'tab-' +( activeParentIndex -1)">
  106. <block v-for="(tab ,index) in tagOptions" :key="tab.name">
  107. <text :id="'tab-' + index" :class="{ 'active': selectedTab === tab.name }" @click="selectTab(tab.name,index)">
  108. {{ tab.name }}
  109. <view class="indicator-triangle">
  110. </view>
  111. </text>
  112. </block>
  113. </scroll-view>
  114. <view class="tags">
  115. <text v-for="(tag, index) in currentTags" :key="index"
  116. :class="['tag', { active: selectedTags[selectedTab] && selectedTags[selectedTab].includes(tag.name) }]"
  117. @click="doYouWantToEdit() ? state() : toggleTag(tag)">
  118. {{ tag.name }}
  119. </text>
  120. </view>
  121. </view>
  122. </view>
  123. <!-- 底部按钮 -->
  124. <view class="bottom-button">
  125. <button v-if="!doYouWantToEdit()" class="generate-btn" @click="generateMusic">立即生成
  126. <image src="/static/icon/coin_cd.png" mode="aspectFit"></image>
  127. 20
  128. </button>
  129. <view v-else class="generate-btn prohibit">生成中 </view>
  130. <view v-if="isRecharge" class="promotion-link" @click="goPage('/pages/vip/index')">
  131. <image class="vip" src="/static/makedetail/wd_icon_vip(1).png" mode="aspectFit"></image>
  132. <text> 即刻开通订阅,获取各种福利! </text>
  133. <image class="jiantou" src="/static/makedetail/cz_icon_jiantou.png" mode="aspectFit"></image>
  134. </view>
  135. </view>
  136. <!-- 新手引导组件 -->
  137. <novice-guidance :step="step" ></novice-guidance>
  138. </view>
  139. </template>
  140. <script>
  141. import { mapState } from 'vuex'
  142. export default {
  143. name: 'MakeMusicDetail',
  144. data() {
  145. return {
  146. songName: '',
  147. lyrics: '',
  148. selectedTags: {},
  149. textareaHeight: 120,
  150. minHeight: 120,
  151. selectedTab: '', // 初始为空,将在 getTags 成功后设置第一个tab
  152. tagOptions: [],
  153. allTagsMap: {}, // 用于快速查找标签所属的分类
  154. selectedTabObject: null, // 用于存储当前选中的tab对象,包含children
  155. inQueue: false,//是否创作中
  156. queuing: false,//是否排队中
  157. queueMessage: '',
  158. myinfo: {},
  159. activeParentIndex: 0,
  160. timer: null, // 添加定时器变量
  161. queueProgress:0,
  162. step: {
  163. name: "makeMusicGuide",
  164. guideList: [
  165. {
  166. el: ".right",
  167. tips: "积分可在这里查看,每日签到可获得积分!",
  168. next: "知道了",
  169. },
  170. {
  171. el: ".input-field",
  172. tips: "在这里输入您想要创作的歌曲名称",
  173. next: "知道了",
  174. },
  175. {
  176. el: ".textarea-field",
  177. tips: "在这里输入歌词内容,AI将根据歌词生成音乐",
  178. next: "知道了",
  179. },
  180. {
  181. el: ".tabs",
  182. tips: "选择音乐风格,包括情感、流派、年代和乐器",
  183. next: "知道了",
  184. },
  185. {
  186. el: ".tags",
  187. tips: "点击选择具体的风格标签,可以多选",
  188. next: "知道了",
  189. },
  190. {
  191. el: ".generate-btn",
  192. tips: "点击这里开始生成您的音乐作品",
  193. next: "完成",
  194. }]
  195. }
  196. }
  197. },
  198. onLoad(e) {
  199. // this.checkQueueStatus();
  200. this.getMyInfo();
  201. this.getTags(); // Call getTags on load
  202. if (e.id) {
  203. this.getQueueDetail(e.id)
  204. // 启动轮询
  205. this.startPolling(e.id);
  206. }
  207. },
  208. onHide() {
  209. // 组件卸载时清除定时器
  210. this.clearPolling();
  211. },
  212. computed: {
  213. currentTags() {
  214. // 确保 selectedTabObject 存在且有 children 属性
  215. return this.selectedTabObject && this.selectedTabObject.children ? this.selectedTabObject.children : [];
  216. },
  217. ...mapState('switchingModule', ['isRecharge', 'isGuiding'])
  218. },
  219. methods: {
  220. doYouWantToEdit() {
  221. return this.inQueue || this.queuing
  222. },
  223. goBack() {
  224. uni.navigateBack({
  225. delta: 1
  226. });
  227. },
  228. getMyInfo() {
  229. uni.request({
  230. url: this.$apiHost + '/My/getnum',
  231. method: 'GET',
  232. header: {
  233. 'content-type': 'application/json',
  234. 'sign': getApp().globalData.headerSign
  235. },
  236. data: {
  237. uuid: getApp().globalData.uuid
  238. },
  239. success: (res) => {
  240. console.log("获取用户信息:", res.data);
  241. this.myinfo = res.data
  242. }
  243. })
  244. },
  245. checkQueueStatus() {
  246. uni.request({
  247. url: this.$apiHost + '/WorkAI/queueStatus',
  248. method: 'GET',
  249. header: {
  250. 'content-type': 'application/json',
  251. 'sign': getApp().globalData.headerSign
  252. },
  253. data: {
  254. uuid: getApp().globalData.uuid,
  255. task_type: 2
  256. },
  257. success: (res) => {
  258. console.log("音乐队列状态:", res.data);
  259. if (res.data.success === "yes") {
  260. this.inQueue = res.data.in_queue
  261. if (this.inQueue) {
  262. this.queueMessage = res.data.str
  263. }
  264. }
  265. },
  266. fail: (err) => {
  267. console.log('获取队列状态失败:', err);
  268. uni.showToast({
  269. title: '获取状态失败',
  270. icon: 'none'
  271. });
  272. }
  273. })
  274. this.getMyInfo();
  275. },
  276. generateMusic() {
  277. if (!this.songName.trim()) {
  278. uni.showToast({
  279. title: '请输入歌曲名称',
  280. icon: 'none'
  281. })
  282. return
  283. }
  284. if (!this.lyrics.trim()) {
  285. uni.showToast({
  286. title: '请输入歌词内容',
  287. icon: 'none'
  288. })
  289. return
  290. }
  291. // 合并所有选中的标签
  292. let allSelectedTags = [
  293. ...this.selectedTags.emotion,
  294. ...this.selectedTags.genre,
  295. ...this.selectedTags.era,
  296. ...this.selectedTags.instrument
  297. ];
  298. console.log(this.lyrics, this.songName,);
  299. let that = this
  300. uni.request({
  301. url: this.$apiHost + '/WorkAI/creatorMusic',
  302. data: {
  303. uuid: getApp().globalData.uuid,
  304. name: this.songName,
  305. lyrics: this.lyrics,
  306. style: allSelectedTags.join(',')
  307. },
  308. method: 'POST',
  309. header: {
  310. 'Content-Type': 'application/x-www-form-urlencoded',
  311. 'sign': getApp().globalData.headerSign
  312. },
  313. dataType: 'json',
  314. success: (res) => {
  315. console.log("生成结果:", res.data);
  316. uni.showToast({
  317. title: res.data.str || '请求成功',
  318. icon: 'none'
  319. });
  320. if (res.data.success == "yes") {
  321. setTimeout(function () {
  322. // that.checkQueueStatus()
  323. //返回上一页
  324. // uni.navigateBack()
  325. // 使用全局变量存储状态
  326. getApp().globalData.needSwitchToGenerating = true;
  327. uni.$emit('switchToMyPage', { type: 'generatingInProgress' });
  328. uni.switchTab({ url: '/pages/my/my' });
  329. }, 1500);
  330. }
  331. },
  332. fail: (err) => {
  333. console.log('生成失败:', err);
  334. uni.showToast({
  335. title: '生成请求失败',
  336. icon: 'none'
  337. });
  338. }
  339. })
  340. },
  341. onTextareaInput(e) {
  342. const lineHeight = 20; // 假设每行高度为20px
  343. const padding = 30; // 上下padding各15px
  344. const value = e.detail.value;
  345. const lines = value.split('\n').length;
  346. // 计算每行的平均字符数
  347. const avgCharsPerLine = 30; // 根据实际输入框宽度调整
  348. const textLines = Math.ceil(value.length / avgCharsPerLine);
  349. // 取行数的最大值,确保有足够空间显示
  350. const totalLines = Math.max(lines, textLines);
  351. const newHeight = Math.max(totalLines * lineHeight + padding, this.minHeight);
  352. this.textareaHeight = newHeight;
  353. },
  354. selectTab(tabName ,index) {
  355. this.activeParentIndex = index;
  356. if (this.selectedTab !== tabName) {
  357. this.selectedTab = tabName;
  358. // 找到对应的tab对象并存储
  359. this.selectedTabObject = this.tagOptions.find(tab => tab.name === tabName);
  360. // 确保当前tab的selectedTags数组已初始化
  361. if (!this.selectedTags[tabName]) {
  362. this.$set(this.selectedTags, tabName, []);
  363. }
  364. }
  365. },
  366. state() {
  367. if (this.inQueue) {
  368. uni.showToast({
  369. title: '正在创作中无法修改',
  370. icon: 'none'
  371. })
  372. } else if (this.queuing) {
  373. uni.showToast({
  374. title: '正在排队中无法修改',
  375. icon: 'none'
  376. })
  377. }
  378. },
  379. toggleTag(tag) {
  380. const tagName = tag.name;
  381. const currentTabSelectedTags = this.selectedTags[this.selectedTab];
  382. if (currentTabSelectedTags.includes(tagName)) {
  383. this.selectedTags[this.selectedTab] = currentTabSelectedTags.filter(t => t !== tagName);
  384. } else {
  385. // 计算所有已选标签的总数
  386. const totalSelectedTags = Object.values(this.selectedTags).reduce((total, tags) => total + tags.length, 0);
  387. if (totalSelectedTags >= 5) {
  388. uni.showToast({
  389. title: '最多只能选择5个标签',
  390. icon: 'none'
  391. });
  392. return;
  393. }
  394. currentTabSelectedTags.push(tagName);
  395. }
  396. },
  397. getQueueDetail(id) {
  398. if (!id) {
  399. return
  400. }
  401. let that = this
  402. uni.request({
  403. url: this.$apiHost + '/WorkAI/getQueueDetail',
  404. data: {
  405. uuid: getApp().globalData.uuid,
  406. skey: getApp().globalData.skey,
  407. id: id
  408. },
  409. header: {
  410. 'Content-Type': 'application/x-www-form-urlencoded',
  411. 'sign': getApp().globalData.headerSign
  412. },
  413. dataType: 'json',
  414. success: (res) => {
  415. console.log("查询单个结果:", res.data);
  416. if (res.data.success == "yes") {
  417. var {id,work_id, queuePosition, allPosition, song_name, lyrics, style,progress } = res.data.data
  418. that.songName = song_name
  419. that.lyrics = lyrics
  420. that.queueProgress = progress
  421. if (progress >=100 && work_id) {
  422. uni.showToast({
  423. title: '生成完成',
  424. icon: 'none'
  425. });
  426. setTimeout(function () {
  427. that.goPage("/pages/makedetail/makeDetail?id="+id +'&&queueId'+work_id)
  428. }, 500);
  429. }
  430. // 根据新的tagOptions结构处理style
  431. const styles = style.split(',');
  432. that.selectedTags = {}; // 清空之前的选择
  433. styles.forEach(tagName => {
  434. const categoryName = that.allTagsMap[tagName];
  435. if (categoryName) {
  436. if (!that.selectedTags[categoryName]) {
  437. that.$set(that.selectedTags, categoryName, []);
  438. }
  439. that.selectedTags[categoryName].push(tagName);
  440. }
  441. });
  442. if (queuePosition == allPosition) {
  443. // 创作中逻辑
  444. that.inQueue = true
  445. } else if (queuePosition < allPosition) {
  446. // 排队中逻辑
  447. that.queuing = true
  448. }
  449. }
  450. },
  451. fail: (err) => {
  452. console.log('查询失败失败:', err);
  453. uni.showToast({
  454. title: '查询失败请求失败',
  455. icon: 'none'
  456. });
  457. }
  458. })
  459. },
  460. getTags() {
  461. let that = this
  462. uni.request({
  463. url: this.$apiHost + '/Work/getTags',
  464. method: 'GET',
  465. header: {
  466. 'content-type': 'application/json',
  467. 'sign': getApp().globalData.headerSign
  468. },
  469. data: {
  470. uuid: getApp().globalData.uuid
  471. },
  472. success: (res) => {
  473. console.log("获取标签:", res.data);
  474. if (res.data && res.data.tags) {
  475. that.tagOptions = res.data.tags;
  476. // 初始化 allTagsMap 和 selectedTags
  477. that.tagOptions.forEach(category => {
  478. that.$set(that.selectedTags, category.name, []);
  479. category.children.forEach(tag => {
  480. that.allTagsMap[tag.name] = category.name;
  481. });
  482. });
  483. // 默认选中第一个tab
  484. if (that.tagOptions.length > 0) {
  485. that.selectTab(that.tagOptions[0].name);
  486. }
  487. }
  488. },
  489. fail: (err) => {
  490. console.log('获取标签失败:', err);
  491. uni.showToast({
  492. title: '获取标签失败',
  493. icon: 'none'
  494. });
  495. }
  496. })
  497. },
  498. goPage(page) {
  499. uni.navigateTo({
  500. url: page,
  501. });
  502. },
  503. // 开始轮询
  504. startPolling(queueId) {
  505. this.clearPolling(); // 先清除可能存在的定时器
  506. this.timer = setInterval(() => {
  507. this.getQueueDetail( queueId);
  508. }, 5000); // 10秒轮询一次
  509. },
  510. // 清除轮询
  511. clearPolling() {
  512. if (this.timer) {
  513. clearInterval(this.timer);
  514. this.timer = null;
  515. }
  516. },
  517. }
  518. }
  519. </script>
  520. <style lang="scss">
  521. @import './makeMusicDetail.scss';
  522. </style>