chat-record.vue 5.6 KB

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