answerDetail.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <template>
  2. <view class="answerdetail-page flex-column">
  3. <template v-if="questionItem">
  4. <view class="question-wrap">
  5. <view class="question-item">
  6. <view class="question-info">
  7. <view style="flex: 1" class="question-title">
  8. <text class="item-label">{{
  9. questionItem.chart_permission_name
  10. }}</text>
  11. {{ questionItem.question_content }}
  12. </view>
  13. <view class="item-answer" v-if="questionItem.reply_status === 3">
  14. <view class="answer" @click.stop="handleAudio(questionItem)">
  15. <template v-if="!questionItem.loading">
  16. <image
  17. class="music-img"
  18. :src="questionItem.answer.isplay ? playImgSrc : pauseImgSrc"
  19. mode="widthFix"
  20. />
  21. <template
  22. v-if="
  23. questionItem.answer.isplay || questionItem.answer.ispause
  24. "
  25. >
  26. <text>{{
  27. questionItem.answer.audioTime -
  28. currentAudioMsg.audioCurrentTime >
  29. 0
  30. ? moment(
  31. (questionItem.answer.audioTime -
  32. currentAudioMsg.audioCurrentTime) *
  33. 1000
  34. ).format("mm:ss")
  35. : "00:00"
  36. }}</text>
  37. </template>
  38. <template v-else>
  39. <text>{{
  40. moment(questionItem.answer.audioTime * 1000).format(
  41. "mm:ss"
  42. )
  43. }}</text>
  44. </template>
  45. </template>
  46. <template v-else>
  47. <image
  48. class="load-img"
  49. src="../static/loading.png"
  50. mode="aspectFill"
  51. />
  52. <text>{{
  53. moment(questionItem.answer.audioTime).format("mm:ss")
  54. }}</text>
  55. </template>
  56. </view>
  57. </view>
  58. </view>
  59. <text class="item-time">提问时间:{{ questionItem.create_time }}</text>
  60. </view>
  61. </view>
  62. <view
  63. class="record-wrap flex-column"
  64. v-if="questionItem.reply_status === 2"
  65. >
  66. <view class="record flex-column" v-if="questionItem.recordStatus !== 4">
  67. <!-- <view class="no-record" v-if="questionItem.recordStatus === 1">
  68. <image
  69. src="../static/question/record.png"
  70. mode="widthFix"
  71. style="width: 90rpx; height: 180rpx"
  72. />
  73. <view>无录音(录音时长超过三分钟自动结束)</view>
  74. </view>
  75. <view class="record-time" v-else>{{ audioTime }}</view> -->
  76. <view class="recode-image">
  77. <scroll-view scroll-x style="height: 100rpx" class="scroll-view" :scroll-left="scrollTop" @scrolltolower="handleScrolltolower" :show-scrollbar="false">
  78. <view style="width:100%;height:100rpx;display:inline-block;"></view>
  79. <image
  80. src="../static/question/record-img.png"
  81. mode="scaleToFill"
  82. />
  83. <image
  84. src="../static/question/record-img.png"
  85. mode="scaleToFill"
  86. />
  87. <image
  88. src="../static/question/record-img.png"
  89. mode="scaleToFill"
  90. />
  91. </scroll-view>
  92. </view>
  93. </view>
  94. <view
  95. class="record-tool flex-column"
  96. v-if="questionItem.recordStatus !== 4"
  97. >
  98. <view class="hint" v-if="questionItem.recordStatus === 1"
  99. >点击开始录音</view
  100. >
  101. <view class="record-time" v-else>{{ audioTime }}</view>
  102. <view
  103. class="record-btn-wrap center"
  104. v-if="questionItem.recordStatus === 1"
  105. >
  106. <view class="switch" @click="changeRecodeStatus"> </view>
  107. </view>
  108. <view class="record-btn-wrap" v-else>
  109. <text
  110. @click="questionItem.recordStatus === 3 && handleRecode('delete')"
  111. v-if="questionItem.recordStatus >= 2"
  112. :class="{ active: questionItem.recordStatus === 3 }"
  113. >删除</text
  114. >
  115. <view class="switch" @click="changeRecodeStatus">
  116. <image
  117. v-if="questionItem.recordStatus >= 2"
  118. :src="
  119. questionItem.recordStatus === 2 ? playImgSrc : pauseImgSrc
  120. "
  121. :key="playIconKey"
  122. :style="{
  123. 'margin-left': questionItem.recordStatus !== 2 ? '7rpx' : 0,
  124. width: '36rpx',
  125. height: '44rpx',
  126. }"
  127. mode="widthFix"
  128. />
  129. </view>
  130. <text
  131. @click="handleRecode('finish')"
  132. v-if="questionItem.recordStatus >= 2"
  133. :class="{ active: questionItem.recordStatus >= 2 }"
  134. >完成</text
  135. >
  136. </view>
  137. </view>
  138. <view class="record-play" v-if="questionItem.recordStatus === 4">
  139. <view class="audio-wrap">
  140. <view v-if="pageLoading">音频生成中...</view>
  141. <view class="play" v-else>
  142. <van-icon
  143. :name="isplay ? 'pause' : 'play'"
  144. @click="handleAudioByReplay"
  145. color="#E6B77DFF"
  146. size="64rpx"
  147. style="align-items: flex-start; margin-left: -20rpx"
  148. />
  149. <!-- 进度条 -->
  150. <view class="slider-box">
  151. <slider
  152. :value="currentAudioMsg.audioCurrentTime"
  153. :max="currentAudioMsg.audioTime"
  154. @change="sliderChange($event)"
  155. @changing="sliderChanging"
  156. activeColor="#E6B77DFF"
  157. backgroundColor="#EBEBEBFF"
  158. block-color="#E6B77DFF"
  159. block-size="12"
  160. />
  161. <view class="slider-time">
  162. <text>{{
  163. moment(currentAudioMsg.audioCurrentTime * 1000).format(
  164. "mm:ss.SS"
  165. )
  166. }}</text>
  167. <text>{{
  168. moment(currentAudioMsg.audioTime * 1000).format("mm:ss.SS")
  169. }}</text>
  170. </view>
  171. </view>
  172. </view>
  173. </view>
  174. <view class="audio-delete" @click="handleRecode('delete')">
  175. <image
  176. src="../static/question/delerecord.png"
  177. mode="heightFix"
  178. style="width: 32rpx; height: 35rpx"
  179. />
  180. <text>删除</text></view
  181. >
  182. <view class="audio-pub" @click="handleRecode('pub')">发布</view>
  183. </view>
  184. </view>
  185. </template>
  186. </view>
  187. </template>
  188. <script>
  189. import mixin from "../mixin/questionMixin";
  190. import { apiReplayAsk, apiGetQuestion, apiSetRead } from "@/api/question";
  191. import { uploadAudioToServer } from "@/utils/upload";
  192. export default {
  193. mixins: [mixin],
  194. data() {
  195. return {
  196. questionItem: null /* {
  197. recordStatus: 1, //1:未录音;2:正在录音;3:已暂停;4:完成录音
  198. }, */,
  199. pauseImgSrc: "../static/question/recordplay.png",
  200. playImgSrc: "../static/question/recordpause.png",
  201. innerAudio: null, //该页面的音频
  202. audioCount: 0, //录音计时,毫秒
  203. recordStartTime: null, //录音开始时间
  204. recordStopTime: null, //录音停止的时间
  205. audioTime: "00:00", //录音时间(格式化后):string
  206. timer: null,
  207. audioItem: null,
  208. pageLoading: false,
  209. isPlayed: false, //拖动进度条前音频是否为播放状态
  210. playIconKey: 0,
  211. isplay: false,
  212. isSlider: false,
  213. scrollTop:0,
  214. };
  215. },
  216. onLoad(options) {
  217. this.initAudio();
  218. this.getQuestionItem(options.id);
  219. },
  220. methods: {
  221. //初始化audio
  222. initAudio() {
  223. this.innerAudio = uni.createInnerAudioContext();
  224. this.handleAudioFun();
  225. this.handleRecorderFun();
  226. },
  227. //audio事件
  228. handleAudioFun() {
  229. this.innerAudio.onPlay(() => {
  230. this.isplay = true;
  231. console.log("播放录音了");
  232. this.questionItem.loading = false;
  233. });
  234. this.innerAudio.onTimeUpdate(() => {
  235. //console.log("时间更新", this.innerAudio.currentTime);
  236. /* this.currentAudioMsg.audioCurrentTime = parseInt(
  237. this.innerAudio.currentTime
  238. ); */
  239. this.currentAudioMsg.audioCurrentTime = this.innerAudio.currentTime;
  240. });
  241. this.innerAudio.onSeeked(() => {
  242. //取this.innerAudio.currentTime为0
  243. console.log("seek完成");
  244. this.isSlider = false;
  245. });
  246. this.innerAudio.onPause(() => {
  247. this.isplay = false;
  248. console.log("暂停");
  249. console.log(this.innerAudio.paused);
  250. });
  251. this.innerAudio.onEnded(() => {
  252. console.log("音频播放完毕");
  253. this.questionItem.answer.isplay = false;
  254. this.questionItem.answer.ispause = false;
  255. /* this.changeCurrentAudio({
  256. id: "",
  257. answer: {
  258. source: "",
  259. audioTime: 0,
  260. },
  261. }); */
  262. this.currentAudioMsg.audioCurrentTime = 0;
  263. this.isplay = false;
  264. });
  265. },
  266. //录音事件
  267. handleRecorderFun() {
  268. this.globalRecorder.onStart(() => {
  269. console.log("开始录音");
  270. this.clockTime();
  271. });
  272. this.globalRecorder.onPause(() => {
  273. console.log("暂停录音");
  274. this.cleanTime();
  275. });
  276. this.globalRecorder.onStop((res) => {
  277. console.log("录音完成");
  278. //录音自动结束 和 暂停时点击删除 的情况
  279. if (this.questionItem.recordStatus === 2) {
  280. this.questionItem.recordStatus = 4;
  281. }
  282. console.log('status',this.questionItem.recordStatus)
  283. console.log("res", JSON.stringify(res));
  284. this.cleanTime();
  285. //初始化音频播放
  286. this.innerAudio.src = res.tempFilePath;
  287. this.changeCurrentAudio({
  288. id: "",
  289. answer: {
  290. source: res.tempFilePath,
  291. audioTime: res.duration / 1000,
  292. },
  293. });
  294. this.pageLoading = false;
  295. });
  296. this.globalRecorder.onError((res) => {
  297. console.log("err", res);
  298. });
  299. /* this.globalRecorder.onFrameRecorded((res) => {
  300. console.log("?", res);
  301. }); */
  302. },
  303. async getQuestionItem(id) {
  304. const res = await apiGetQuestion({
  305. question_id: id,
  306. });
  307. if (res.code === 200) {
  308. this.questionItem = res.data;
  309. const { audio_list } = res.data;
  310. let temp = {};
  311. if (audio_list.length > 0) {
  312. temp = {
  313. source: res.data.audio_list[0].audio_url,
  314. audioTime: parseInt(res.data.audio_list[0].audio_play_seconds),
  315. isplay: false,
  316. ispause: false,
  317. };
  318. } else {
  319. temp = {
  320. source: "",
  321. audioTime: 0,
  322. isplay: false,
  323. ispause: false,
  324. };
  325. }
  326. let readKey = "";
  327. const { is_inner } = this.userInfo;
  328. if (is_inner === 1) {
  329. readKey = "replier_is_read";
  330. } else {
  331. readKey = "is_read";
  332. }
  333. //console.log('readKey',readKey)
  334. this.questionItem[readKey] !== 1 &&
  335. (await apiSetRead({
  336. question_ids: this.questionItem.community_question_id + "",
  337. }));
  338. this.questionItem.id = res.data.community_question_id;
  339. this.questionItem.answer = temp;
  340. this.questionItem.loading = false;
  341. this.questionItem.recordStatus = res.data.reply_status > 2 ? 4 : 1;
  342. }
  343. },
  344. changeRecodeStatus() {
  345. //根据questionItem.recordStatus
  346. if (this.questionItem.recordStatus === 1) {
  347. //开始录音
  348. this.globalRecorder.start({ duration: 180000, format: "mp3" });
  349. //this.clockTime();
  350. this.questionItem.recordStatus = 2;
  351. } else if (this.questionItem.recordStatus === 2) {
  352. //暂停录音
  353. this.globalRecorder.pause();
  354. //this.cleanTime();
  355. this.questionItem.recordStatus = 3;
  356. } else if (this.questionItem.recordStatus === 3) {
  357. //继续录音
  358. this.globalRecorder.resume();
  359. this.clockTime();
  360. this.questionItem.recordStatus = 2;
  361. } else {
  362. //结束录音
  363. this.globalRecorder.stop();
  364. //this.cleanTime();
  365. }
  366. },
  367. //上传音频
  368. async uploadAudio() {
  369. const res = await uploadAudioToServer(this.innerAudio.src);
  370. if (res.code === 200) {
  371. this.audioItem = res.data;
  372. } else {
  373. //重新录
  374. this.questionItem.recordStatus = 1;
  375. this.audioCount = 0;
  376. this.audioTime = this.moment(this.audioCount * 1000).format("mm:ss");
  377. }
  378. },
  379. //录音操作:完成/删除/发布
  380. async handleRecode(type) {
  381. if (type==='finish') {
  382. this.questionItem.recordStatus = 4;
  383. this.changeRecodeStatus();
  384. }
  385. if (type === "finish") {
  386. //生成音频,更改页面布局
  387. this.pageLoading = true;
  388. } else if (type === "delete") {
  389. //重新录
  390. if(this.questionItem.recordStatus===3)this.globalRecorder.stop();
  391. this.questionItem.recordStatus = 1;
  392. this.audioItem = null;
  393. this.audioCount = 0;
  394. this.scrollTop = 0;
  395. this.recordStartTime = null;
  396. this.audioTime = this.moment(this.audioCount * 1000).format("mm:ss");
  397. } else {
  398. //发布
  399. if (!this.audioItem) {
  400. await this.uploadAudio();
  401. }
  402. //如果上传音频成功
  403. if (this.questionItem.recordStatus === 4) {
  404. //发布回答
  405. const res = await apiReplayAsk({
  406. question_id: this.questionItem.community_question_id,
  407. audio_list: [{ ...this.audioItem, sort: 1 }],
  408. });
  409. if (res.code === 200) {
  410. uni.showToast({
  411. title: "发布成功",
  412. icon: "success",
  413. duration: 500,
  414. });
  415. setTimeout(() => {
  416. //关闭当前页面,跳转到我的回答
  417. uni.navigateBack({ delta: 1 });
  418. }, 500);
  419. }
  420. }
  421. }
  422. },
  423. //(提问者)问题已被回答,点击回答音频
  424. handleAudio(item) {
  425. const { source, isplay, ispause } = item.answer;
  426. if (isplay) {
  427. //说明是播放->暂停
  428. this.innerAudio.pause();
  429. this.questionItem.answer.isplay = false;
  430. this.questionItem.answer.ispause = true;
  431. } else if (ispause) {
  432. //说明是暂停->播放
  433. this.innerAudio.play();
  434. this.questionItem.answer.isplay = true;
  435. this.questionItem.answer.ispause = false;
  436. } else {
  437. //console.log("aaa", source, this.innerAudio.src);
  438. //说明是第一次播放或播放完
  439. this.changeCurrentAudio(item);
  440. this.innerAudio.stop();
  441. this.innerAudio.src = source;
  442. this.handleAudioPlay();
  443. this.questionItem.answer.isplay = true;
  444. }
  445. },
  446. //(回答者)问题被回答,点击回答音频
  447. handleAudioByReplay() {
  448. this.isplay = !this.isplay;
  449. if (this.innerAudio.paused) {
  450. this.innerAudio.play();
  451. } else {
  452. this.innerAudio.pause();
  453. }
  454. this.playIconKey++; //更新音频播放图标
  455. },
  456. //拖动音频进度条
  457. sliderChange(e) {
  458. console.log("拖动完成?");
  459. //this.innerAudio.pause();
  460. const value = e.detail.value;
  461. this.innerAudio.seek(value);
  462. this.currentAudioMsg.audioCurrentTime = value;
  463. },
  464. sliderChanging() {
  465. this.isSlider = true;
  466. },
  467. //切换当前播放音频
  468. changeCurrentAudio(item) {
  469. const { id } = item;
  470. const { source, audioTime } = item.answer;
  471. this.currentAudioMsg = {
  472. id: id,
  473. audioCurrentTime: 0 * 1000,
  474. audioTime: audioTime,
  475. audioCurrentUrl: source,
  476. };
  477. if (id) {
  478. console.log("?");
  479. this.questionItem.loading = true;
  480. }
  481. },
  482. //录音计时
  483. clockTime() {
  484. console.log("开始录音计时");
  485. if (!this.recordStartTime) {
  486. this.recordStartTime = Date.now();
  487. }
  488. //console.log(this.recordStartTime);
  489. this.timer = setInterval(() => {
  490. if (this.timer) {
  491. this.audioCount += 30;
  492. this.audioTime = this.moment(this.audioCount).format("mm:ss.SS");
  493. //this.audioTime = this.moment(Date.now() - this.recordStartTime).format("mm:ss.SS");
  494. this.scrollTop+=1;
  495. }
  496. }, 30);
  497. },
  498. //清除录音计时
  499. cleanTime() {
  500. console.log("结束录音计时");
  501. //console.log('aa',this.recordStopTime)
  502. clearTimeout(this.timer);
  503. this.playIconKey++; //更新录音暂停播放图标
  504. //this.audioTime = this.moment(this.audioCount).format("mm:ss.SS");
  505. },
  506. //scroll-view滑动到最右
  507. handleScrolltolower(){
  508. console.log('a',this.scrollTop)
  509. this.scrollTop-=150;
  510. }
  511. },
  512. };
  513. </script>
  514. <style scoped lang="scss">
  515. .flex-column {
  516. display: flex;
  517. flex-direction: column;
  518. }
  519. .answerdetail-page {
  520. padding: 50rpx 30rpx 0 30rpx;
  521. height: calc(100vh - calc(50px + env(safe-area-inset-bottom)));
  522. box-sizing: border-box;
  523. .question-wrap {
  524. .question-item::after {
  525. height: 0;
  526. }
  527. }
  528. .record-wrap {
  529. margin: 0 -30rpx;
  530. flex: 1;
  531. .record {
  532. flex: 1;
  533. justify-content: center;
  534. align-items: center;
  535. .no-record {
  536. text-align: center;
  537. color: #999999ff;
  538. font-size: 28rpx;
  539. image {
  540. width: 94rpx;
  541. }
  542. }
  543. /* .record-time {
  544. justify-self: flex-end;
  545. font-size: 60rpx;
  546. } */
  547. .recode-image {
  548. width: 100%;
  549. height: 100%;
  550. background-color: #fafafaff;
  551. display: flex;
  552. align-items: center;
  553. .scroll-view {
  554. width: 100%;
  555. white-space: nowrap;
  556. ::-webkit-scrollbar{
  557. width:0;
  558. height:0;
  559. display:none;
  560. color:transparent;
  561. }
  562. image {
  563. width: 421rpx;
  564. height: 100rpx;
  565. margin-right: 6rpx;
  566. }
  567. }
  568. }
  569. }
  570. .record-tool {
  571. justify-content: center;
  572. align-items: center;
  573. border-top: 1rpx solid #e6e6e6ff;
  574. height: 400rpx;
  575. position: relative;
  576. .hint {
  577. color: #ee3636ff;
  578. font-size: 28rpx;
  579. position: absolute;
  580. top: 50rpx;
  581. }
  582. .record-time{
  583. font-size: 60rpx;
  584. }
  585. .record-btn-wrap {
  586. margin-top: 38rpx;
  587. display: flex;
  588. width: 100%;
  589. justify-content: space-around;
  590. align-items: center;
  591. &.center {
  592. justify-content: center;
  593. }
  594. text {
  595. color: #999999ff;
  596. font-size: 32rpx;
  597. &.active {
  598. color: #ee3636ff;
  599. }
  600. }
  601. .switch {
  602. width: 118rpx;
  603. height: 118rpx;
  604. padding: 12rpx;
  605. box-sizing: border-box;
  606. border-radius: 50%;
  607. border: 1rpx solid #ee3636ff;
  608. /* background-color: rgb(68, 46, 46); */
  609. position: relative;
  610. display: flex;
  611. align-items: center;
  612. justify-content: center;
  613. image {
  614. position: absolute;
  615. width: 36rpx;
  616. z-index: 5;
  617. }
  618. &::after {
  619. content: "";
  620. display: inline-block;
  621. width: 100%;
  622. height: 100%;
  623. border-radius: 50%;
  624. background-color: #ee3636ff;
  625. }
  626. }
  627. }
  628. }
  629. .record-play {
  630. margin: 20rpx 30rpx;
  631. .audio-wrap {
  632. background-color: #fdf8f2ff;
  633. padding: 30rpx;
  634. border-radius: 16rpx;
  635. }
  636. .play {
  637. display: flex;
  638. justify-content: space-between;
  639. .slider-box {
  640. margin-left: 15rpx;
  641. flex: 1;
  642. slider {
  643. margin: 15rpx 0 0 0;
  644. }
  645. .slider-time {
  646. display: flex;
  647. justify-content: space-between;
  648. text {
  649. color: #999999ff;
  650. font-size: 22rpx;
  651. }
  652. }
  653. }
  654. }
  655. .audio-delete {
  656. margin-top: 20rpx;
  657. width: 100%;
  658. height: 40rpx;
  659. display: flex;
  660. justify-content: flex-end;
  661. align-items: center;
  662. image {
  663. height: 40rpx;
  664. }
  665. text {
  666. margin-left: 10rpx;
  667. color: #999999ff;
  668. font-size: 28rpx;
  669. }
  670. }
  671. .audio-pub {
  672. margin-top: 120rpx;
  673. background-color: #e6b77dff;
  674. color: #fff;
  675. height: 80rpx;
  676. line-height: 80rpx;
  677. border-radius: 40rpx;
  678. text-align: center;
  679. position: relative;
  680. width: 390rpx;
  681. left: 50%;
  682. margin-left: -170rpx;
  683. }
  684. }
  685. }
  686. }
  687. </style>