answerDetail.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <template>
  2. <view class="answerdetail-page flex-column">
  3. <view class="question-wrap">
  4. <view class="question-item">
  5. <view class="question-info">
  6. <view style="flex: 1" class="question-title">
  7. <text class="item-label">{{
  8. questionItem.chart_permission_name
  9. }}</text>
  10. {{ questionItem.question_content }}
  11. </view>
  12. <view class="item-answer" v-if="questionItem.reply_status === 3">
  13. <view class="answer" @click.stop="handleAudio(questionItem)">
  14. <template v-if="!questionItem.loading">
  15. <image
  16. class="music-img"
  17. :src="questionItem.answer.isplay ? playImgSrc : pauseImgSrc"
  18. mode="widthFix"
  19. />
  20. <template
  21. v-if="
  22. questionItem.answer.isplay || questionItem.answer.ispause
  23. "
  24. >
  25. <text>{{
  26. questionItem.answer.audioTime -
  27. currentAudioMsg.audioCurrentTime >
  28. 0
  29. ? moment(
  30. questionItem.answer.audioTime -
  31. currentAudioMsg.audioCurrentTime
  32. ).format("mm:ss")
  33. : "00:00"
  34. }}</text>
  35. </template>
  36. <template v-else>
  37. <text>{{
  38. moment(questionItem.answer.audioTime).format("mm:ss")
  39. }}</text>
  40. </template>
  41. </template>
  42. <template v-else>
  43. <image
  44. class="load-img"
  45. src="../static/loading.png"
  46. mode="aspectFill"
  47. />
  48. <text>{{
  49. moment(questionItem.answer.audioTime).format("mm:ss")
  50. }}</text>
  51. </template>
  52. </view>
  53. </view>
  54. </view>
  55. <text class="item-time">提问时间:{{ questionItem.create_time }}</text>
  56. </view>
  57. </view>
  58. <view
  59. class="record-wrap flex-column"
  60. v-if="questionItem.reply_status === 2"
  61. >
  62. <view class="record flex-column" v-if="questionItem.recordStatus !== 4">
  63. <view class="no-record" v-if="questionItem.recordStatus === 1">
  64. <image src="../static/question/record.png" mode="widthFix" />
  65. <view>无录音(录音时长超过三分钟自动结束)</view>
  66. </view>
  67. <view class="record-time" v-else>{{ audioTime }}</view>
  68. </view>
  69. <view
  70. class="record-tool flex-column"
  71. v-if="questionItem.recordStatus !== 4"
  72. >
  73. <view class="hint" v-if="questionItem.recordStatus === 1"
  74. >点击开始录音</view
  75. >
  76. <view class="record-btn-wrap">
  77. <text @click="handleRecode('delete')">删除</text>
  78. <view class="switch" @click="changeRecodeStatus">
  79. <image
  80. v-if="questionItem.recordStatus >= 2"
  81. :src="questionItem.recordStatus === 2 ? playImgSrc : pauseImgSrc"
  82. :style="{
  83. 'margin-left': questionItem.recordStatus !== 2 ? '7rpx' : 0,
  84. }"
  85. mode="widthFix"
  86. />
  87. </view>
  88. <text @click="handleRecode('finish')">完成</text>
  89. </view>
  90. </view>
  91. <view class="record-play" v-if="questionItem.recordStatus === 4">
  92. <view class="audio-wrap">
  93. <view v-if="pageLoading">音频生成中...</view>
  94. <view class="play" v-else>
  95. <van-icon
  96. :name="isplay ? 'pause' : 'play'"
  97. @click="handleAudioByReplay"
  98. color="#E6B77DFF"
  99. size="64rpx"
  100. style="align-items: flex-start; margin-left: -20rpx"
  101. />
  102. <!-- 进度条 -->
  103. <view class="slider-box">
  104. <slider
  105. :value="currentAudioMsg.audioCurrentTime"
  106. :max="currentAudioMsg.audioTime"
  107. @change="sliderChange($event)"
  108. activeColor="#E6B77DFF"
  109. backgroundColor="#EBEBEBFF"
  110. block-color="#E6B77DFF"
  111. block-size="12"
  112. />
  113. <view class="slider-time">
  114. <text>{{
  115. moment(currentAudioMsg.audioCurrentTime * 1000).format(
  116. "mm:ss"
  117. )
  118. }}</text>
  119. <text>{{
  120. moment(currentAudioMsg.audioTime * 1000).format("mm:ss")
  121. }}</text>
  122. </view>
  123. </view>
  124. </view>
  125. </view>
  126. <view class="audio-delete" @click="handleRecode('delete')">
  127. <image src="../static/question/delerecord.png" mode="heightFix" />
  128. <text>删除</text></view
  129. >
  130. <view class="audio-pub" @click="handleRecode('pub')">发布</view>
  131. </view>
  132. </view>
  133. </view>
  134. </template>
  135. <script>
  136. import mixin from "../mixin/questionMixin";
  137. import { apiReplayAsk } from "@/api/question";
  138. import { uploadToServer, uploadAudioToServer } from "@/utils/upload";
  139. export default {
  140. mixins: [mixin],
  141. data() {
  142. return {
  143. questionItem: {
  144. id: 1,
  145. chart_permission_id: 1,
  146. question_content: "疫情下全球苯乙烯市场有什么动荡和影响",
  147. create_time: "2022.5.23 14:40",
  148. answer: {
  149. source:
  150. "https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3",
  151. audioTime: 21 * 1000,
  152. isplay: false,
  153. ispause: false,
  154. },
  155. loading: false,
  156. reply_status: 2,
  157. chart_permission_name: "苯乙烯",
  158. recordStatus: 1, //1:未录音;2:正在录音;3:已暂停;4:完成录音
  159. },
  160. pauseImgSrc: "../static/question/recordplay.png",
  161. playImgSrc: "../static/question/recordpause.png",
  162. innerAudio: null, //该页面的音频
  163. audioCount: 0, //录音计时
  164. audioTime: "00:00", //录音时间
  165. timer: null,
  166. audioItem: {},
  167. pageLoading: false,
  168. isPlayed: false, //拖动进度条前音频是否为播放状态
  169. playIconKey: 0,
  170. isplay: false,
  171. };
  172. },
  173. onLoad(options) {
  174. this.initAudio();
  175. //this.getQuestionItem(options.id)
  176. },
  177. methods: {
  178. //初始化audio
  179. initAudio() {
  180. this.innerAudio = uni.createInnerAudioContext();
  181. this.handleAudioFun();
  182. this.handleRecorderFun();
  183. },
  184. //audio事件
  185. handleAudioFun() {
  186. this.innerAudio.onPlay(() => {
  187. this.isplay = true;
  188. console.log("播放录音了");
  189. this.questionItem.loading = false;
  190. });
  191. this.innerAudio.onTimeUpdate(() => {
  192. console.log("时间更新", this.innerAudio.currentTime);
  193. this.currentAudioMsg.audioCurrentTime = parseInt(
  194. this.innerAudio.currentTime
  195. );
  196. });
  197. this.innerAudio.onSeeked(() => {
  198. //取this.innerAudio.currentTime为0
  199. console.log("seek完成");
  200. });
  201. this.innerAudio.onPause(() => {
  202. this.isplay = false;
  203. console.log("暂停");
  204. console.log(this.innerAudio.paused);
  205. });
  206. this.innerAudio.onEnded(() => {
  207. console.log("音频播放完毕");
  208. this.questionItem.answer.isplay = false;
  209. this.questionItem.answer.ispause = false;
  210. this.changeCurrentAudio({
  211. id: "",
  212. answer: {
  213. source: "",
  214. audioTime: 0,
  215. },
  216. });
  217. });
  218. },
  219. //录音事件
  220. handleRecorderFun() {
  221. this.globalRecorder.onStart(() => {
  222. console.log("开始录音");
  223. });
  224. this.globalRecorder.onPause(() => {
  225. console.log("暂停录音");
  226. /* this.cleanTime(); */
  227. });
  228. this.globalRecorder.onStop((res) => {
  229. console.log("录音完成");
  230. console.log("res", JSON.stringify(res));
  231. //this.innerAudio.src = res.tempFilePath;
  232. this.cleanTime();
  233. //初始化音频播放
  234. this.innerAudio.src = res.tempFilePath;
  235. this.changeCurrentAudio({
  236. id: "",
  237. answer: {
  238. source: res.tempFilePath,
  239. audioTime: res.duration / 1000,
  240. },
  241. });
  242. this.pageLoading = false;
  243. });
  244. this.globalRecorder.onError((res) => {
  245. console.log("err", res);
  246. });
  247. /* this.globalRecorder.onFrameRecorded((res) => {
  248. console.log("?", res);
  249. }); */
  250. },
  251. changeRecodeStatus() {
  252. //根据questionItem.recordStatus
  253. if (this.questionItem.recordStatus === 1) {
  254. //开始录音
  255. this.globalRecorder.start({ duration: 180000, format: "mp3" });
  256. this.clockTime();
  257. this.questionItem.recordStatus = 2;
  258. } else if (this.questionItem.recordStatus === 2) {
  259. //暂停录音
  260. this.globalRecorder.pause();
  261. this.cleanTime();
  262. this.questionItem.recordStatus = 3;
  263. } else if (this.questionItem.recordStatus === 3) {
  264. //继续录音
  265. this.globalRecorder.resume();
  266. this.clockTime();
  267. this.questionItem.recordStatus = 2;
  268. } else {
  269. //结束录音
  270. this.globalRecorder.stop();
  271. this.cleanTime();
  272. }
  273. },
  274. //上传音频
  275. async uploadAudio() {
  276. const res = await uploadAudioToServer(this.innerAudio.src)
  277. if (res.code === 200) {
  278. this.audioItem = res.data;
  279. } else {
  280. //重新录
  281. this.questionItem.recordStatus = 1;
  282. this.audioCount = 0;
  283. this.audioTime = this.moment(this.audioCount * 1000).format("mm:ss");
  284. }
  285. },
  286. async handleRecode(type) {
  287. this.questionItem.recordStatus = 4;
  288. this.changeRecodeStatus();
  289. if (type === "finish") {
  290. //生成音频,更改页面布局
  291. this.pageLoading = true;
  292. } else if (type === "delete") {
  293. //重新录
  294. this.questionItem.recordStatus = 1;
  295. this.audioCount = 0;
  296. this.audioTime = this.moment(this.audioCount * 1000).format("mm:ss");
  297. } else {
  298. //发布
  299. await this.uploadAudio();
  300. //如果上传音频成功
  301. if (this.questionItem.recordStatus === 4) {
  302. //发布回答
  303. const res = await apiReplayAsk({
  304. question_id: 5,
  305. audio_list: [{...this.audioItem,sort:1}],
  306. });
  307. if (res.code === 200) {
  308. //关闭当前页面,跳转到我的回答
  309. uni.navigateBack({ delta: 1 });
  310. }
  311. }
  312. }
  313. },
  314. //(提问者)问题已被回答,点击回答音频
  315. handleAudio(item) {
  316. const { source, isplay, ispause } = item.answer;
  317. if (isplay) {
  318. //说明是播放->暂停
  319. this.innerAudio.pause();
  320. this.questionItem.answer.isplay = false;
  321. this.questionItem.answer.ispause = true;
  322. } else if (ispause) {
  323. //说明是暂停->播放
  324. this.innerAudio.play();
  325. this.questionItem.answer.isplay = true;
  326. this.questionItem.answer.ispause = false;
  327. } else {
  328. console.log("aaa", source, this.innerAudio.src);
  329. //说明是第一次播放
  330. this.changeCurrentAudio(item);
  331. this.innerAudio.stop();
  332. this.innerAudio.src = source;
  333. this.handleAudioPlay();
  334. this.questionItem.answer.isplay = true;
  335. }
  336. },
  337. //(回答者)问题被回答,点击回答音频
  338. handleAudioByReplay() {
  339. this.isplay = !this.isplay;
  340. if (this.innerAudio.paused) {
  341. this.innerAudio.play();
  342. } else {
  343. this.innerAudio.pause();
  344. }
  345. this.playIconKey++;
  346. },
  347. //拖动音频进度条
  348. sliderChange(e) {
  349. console.log("拖动完成?");
  350. //this.innerAudio.pause();
  351. const value = e.detail.value;
  352. this.innerAudio.seek(value);
  353. this.currentAudioMsg.audioCurrentTime = value;
  354. },
  355. //切换当前播放音频
  356. changeCurrentAudio(item) {
  357. const { id } = item;
  358. const { source, audioTime } = item.answer;
  359. this.currentAudioMsg = {
  360. id: id,
  361. audioCurrentTime: 0 * 1000,
  362. audioTime: audioTime,
  363. audioCurrentUrl: source,
  364. };
  365. if (id) {
  366. this.questionItem.loading = true;
  367. }
  368. },
  369. //录音计时
  370. clockTime() {
  371. console.log("开始计时");
  372. this.timer = setInterval(() => {
  373. if (this.timer) {
  374. this.audioCount++;
  375. this.audioTime = this.moment(this.audioCount * 1000).format("mm:ss");
  376. }
  377. }, 1000);
  378. },
  379. //清除录音计时
  380. cleanTime() {
  381. this.timer = null;
  382. this.audioTime = this.moment(this.audioCount * 1000).format("mm:ss");
  383. },
  384. },
  385. };
  386. </script>
  387. <style scoped lang="scss">
  388. .flex-column {
  389. display: flex;
  390. flex-direction: column;
  391. }
  392. .answerdetail-page {
  393. padding: 50rpx 30rpx 0 30rpx;
  394. height: calc(100vh - calc(50px + env(safe-area-inset-bottom)));
  395. box-sizing: border-box;
  396. .question-wrap {
  397. .question-item::after {
  398. height: 0;
  399. }
  400. }
  401. .record-wrap {
  402. margin: 0 -30rpx;
  403. flex: 1;
  404. .record {
  405. flex: 1;
  406. justify-content: center;
  407. align-items: center;
  408. .no-record {
  409. text-align: center;
  410. color: #999999ff;
  411. font-size: 28rpx;
  412. image {
  413. width: 94rpx;
  414. }
  415. }
  416. .record-time {
  417. justify-self: flex-end;
  418. font-size: 60rpx;
  419. }
  420. }
  421. .record-tool {
  422. justify-content: center;
  423. align-items: center;
  424. border-top: 1rpx solid #e6e6e6ff;
  425. height: 400rpx;
  426. position: relative;
  427. .hint {
  428. color: #ee3636ff;
  429. font-size: 28rpx;
  430. position: absolute;
  431. top: 50rpx;
  432. }
  433. .record-btn-wrap {
  434. margin-top: 38rpx;
  435. display: flex;
  436. width: 100%;
  437. justify-content: space-around;
  438. align-items: center;
  439. text {
  440. color: #999999ff;
  441. font-size: 32rpx;
  442. &.active {
  443. color: #ee3636ff;
  444. }
  445. }
  446. .switch {
  447. width: 118rpx;
  448. height: 118rpx;
  449. padding: 12rpx;
  450. box-sizing: border-box;
  451. border-radius: 50%;
  452. border: 1rpx solid #ee3636ff;
  453. /* background-color: rgb(68, 46, 46); */
  454. position: relative;
  455. display: flex;
  456. align-items: center;
  457. justify-content: center;
  458. image {
  459. position: absolute;
  460. width: 36rpx;
  461. z-index: 5;
  462. }
  463. &::after {
  464. content: "";
  465. display: inline-block;
  466. width: 100%;
  467. height: 100%;
  468. border-radius: 50%;
  469. background-color: #ee3636ff;
  470. }
  471. }
  472. }
  473. }
  474. .record-play {
  475. margin: 20rpx 30rpx;
  476. .audio-wrap {
  477. background-color: #fdf8f2ff;
  478. padding: 30rpx;
  479. border-radius: 16rpx;
  480. }
  481. .play {
  482. display: flex;
  483. justify-content: space-between;
  484. .slider-box {
  485. margin-left: 15rpx;
  486. flex: 1;
  487. slider {
  488. margin: 15rpx 0 0 0;
  489. }
  490. .slider-time {
  491. display: flex;
  492. justify-content: space-between;
  493. text {
  494. color: #999999ff;
  495. font-size: 22rpx;
  496. }
  497. }
  498. }
  499. }
  500. .audio-delete {
  501. margin-top: 20rpx;
  502. width: 100%;
  503. height: 40rpx;
  504. display: flex;
  505. justify-content: flex-end;
  506. align-items: center;
  507. image {
  508. height: 40rpx;
  509. }
  510. text {
  511. margin-left: 10rpx;
  512. color: #999999ff;
  513. font-size: 28rpx;
  514. }
  515. }
  516. .audio-pub {
  517. margin-top: 120rpx;
  518. background-color: #e6b77dff;
  519. color: #fff;
  520. height: 80rpx;
  521. line-height: 80rpx;
  522. border-radius: 40rpx;
  523. text-align: center;
  524. position: relative;
  525. width: 390rpx;
  526. left: 50%;
  527. margin-left: -170rpx;
  528. }
  529. }
  530. }
  531. }
  532. </style>