chat-record.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <view class="chat-record">
  3. <view class="chat-record-bar" id="chat-record-bar" :style="recordBarStyle"
  4. @click.stop=""
  5. @touchstart.prevent="onStartRecord"
  6. @touchmove.prevent="onTouchMove"
  7. @touchend.prevent="onEndRecord">{{recording?'正在录音':'长按 说话'}}</view>
  8. <view v-if="recording" class="chat-record-window" :style="recordWindowStyle">
  9. <view class="rc-wave">
  10. <text class="note" style="--d: 0"></text>
  11. <text class="note" style="--d: 1"></text>
  12. <text class="note" style="--d: 2"></text>
  13. <text class="note" style="--d: 3"></text>
  14. <text class="note" style="--d: 4"></text>
  15. <text class="note" style="--d: 5"></text>
  16. <text class="note" style="--d: 6"></text>
  17. </view>
  18. <view class="rc-tip">{{recordTip}}</view>
  19. <view class="cancel-btn" @click="onCancel">
  20. <uni-icons :class="moveToCancel?'red':'black'" type="clear"
  21. :size="moveToCancel?45:40"></uni-icons>
  22. </view>
  23. <view class="opt-tip" :class="moveToCancel?'red':'black'">{{moveToCancel? '松手取消':'松手发送,上划取消'}}</view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. name: "chat-record",
  30. data() {
  31. return {
  32. recording: false,
  33. moveToCancel: false,
  34. recordBarTop: 0,
  35. druation: 0,
  36. rcTimer: null
  37. }
  38. },
  39. methods: {
  40. onTouchMove(e) {
  41. const moveY = e.touches[0].clientY;
  42. this.moveToCancel = moveY < this.recordBarTop-40;
  43. },
  44. onCancel(){
  45. if(this.recording){
  46. this.moveToCancel = true;
  47. this.onEndRecord();
  48. }
  49. },
  50. onStartRecord() {
  51. /* 用户第一次使用语音会唤醒录音权限请求,此时会导致@touchend失效,
  52. 一直处于录音状态,这里允许用户再次点击发送语音并结束录音 */
  53. if(this.recording){
  54. this.onEndRecord();
  55. return;
  56. }
  57. console.log("开始录音")
  58. this.moveToCancel = false;
  59. this.initRecordBar();
  60. this.$rc.start().then(() => {
  61. this.recording = true;
  62. console.log("开始录音成功")
  63. // 开始计时
  64. this.startTimer();
  65. }).catch((e) => {
  66. console.log("录音失败"+JSON.stringify(e))
  67. uni.showToast({
  68. title: "录音失败",
  69. icon: "none"
  70. });
  71. });
  72. },
  73. onEndRecord() {
  74. this.recording = false;
  75. // 停止录音
  76. this.$rc.pause();
  77. // 停止计时
  78. this.StopTimer();
  79. // 触屏位置是否移动到了取消区域
  80. if (this.moveToCancel) {
  81. this.$rc.close();
  82. console.log("录音取消")
  83. return;
  84. }
  85. // 小于1秒不发送
  86. if (this.druation == 0) {
  87. uni.showToast({
  88. title: "说话时间太短",
  89. icon: 'none'
  90. })
  91. this.$rc.close();
  92. return;
  93. }
  94. this.$rc.upload().then((data) => {
  95. this.$emit("send", data);
  96. }).catch((e) => {
  97. uni.showToast({
  98. title: e,
  99. icon: 'none'
  100. })
  101. }).finally(() => {
  102. this.$rc.close();
  103. console.log("录音完成")
  104. })
  105. },
  106. startTimer() {
  107. this.druation = 0;
  108. this.StopTimer();
  109. this.rcTimer = setInterval(() => {
  110. this.druation++;
  111. // 大于60s,直接结束
  112. if(this.druation >= 60 ){
  113. this.onEndRecord();
  114. }
  115. }, 1000)
  116. },
  117. StopTimer() {
  118. this.rcTimer && clearInterval(this.rcTimer);
  119. this.rcTimer = null;
  120. },
  121. initRecordBar() {
  122. const query = uni.createSelectorQuery().in(this);
  123. query.select('#chat-record-bar').boundingClientRect((rect) => {
  124. // 顶部高度位置
  125. this.recordBarTop = rect.top;
  126. }).exec()
  127. }
  128. },
  129. computed: {
  130. recordWindowStyle() {
  131. const windowHeight = uni.getSystemInfoSync().windowHeight;
  132. const bottom = windowHeight - this.recordBarTop + 12;
  133. return `bottom:${bottom}px;`
  134. },
  135. recordBarStyle() {
  136. const bgColor = this.recording ? "royalblue" : "#f8f8f8";
  137. return `background-color:${bgColor};`
  138. },
  139. recordTip(){
  140. if(this.druation > 50){
  141. return `${60-this.druation}s后将停止录音`;
  142. }
  143. return `录音时长:${this.druation}s`;
  144. }
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. .chat-record {
  150. .rc-wave {
  151. display: flex;
  152. align-items: flex-end;
  153. justify-content: center;
  154. position: relative;
  155. height: 80rpx;
  156. .note {
  157. background: linear-gradient(to top, #395ff3 0%, #89aff3 100%);
  158. width: 4px;
  159. height: 50%;
  160. border-radius: 5rpx;
  161. margin-right: 4px;
  162. animation: loading 0.5s infinite linear;
  163. animation-delay: calc(0.1s * var(--d));
  164. @keyframes loading {
  165. 0% {
  166. background-image: linear-gradient(to right, #395ff3 0%, #89aff3 100%);
  167. height: 20%;
  168. border-radius: 5rpx;
  169. }
  170. 50% {
  171. background-image: linear-gradient(to top, #395ff3 0%, #a9cff3 100%);
  172. height: 80%;
  173. border-radius: 5rpx;
  174. }
  175. 100% {
  176. background-image: linear-gradient(to top, #395ff3 0%, #a9cff3 100%);
  177. height: 20%;
  178. border-radius: 5rpx;
  179. }
  180. }
  181. }
  182. }
  183. .chat-record-bar {
  184. padding: 10rpx;
  185. margin: 10rpx;
  186. border-radius: 10rpx;
  187. text-align: center;
  188. }
  189. .chat-record-window {
  190. position: fixed;
  191. left: 0;
  192. height: 360rpx;
  193. width: 100%;
  194. background-color: rgba(255, 255, 255, 0.95);
  195. padding: 30rpx;
  196. .icon-microphone {
  197. text-align: center;
  198. font-size: 80rpx;
  199. padding: 10rpx;
  200. }
  201. .rc-tip {
  202. text-align: center;
  203. font-size: 30rpx;
  204. margin-top: 20rpx;
  205. }
  206. .cancel-btn {
  207. text-align: center;
  208. margin-top: 40rpx;
  209. height: 80rpx;
  210. }
  211. .opt-tip {
  212. text-align: center;
  213. font-size: 30rpx;
  214. padding: 20rpx;
  215. }
  216. .red {
  217. color: red !important;
  218. }
  219. .black {
  220. color: gray;
  221. }
  222. }
  223. }
  224. </style>