chat-record.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view class="chat-record">
  3. <view class="chat-record-bar" :class="{ recording: recording }" id="chat-record-bar" @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.StopTimer();
  74. // 停止录音
  75. this.$rc.close();
  76. // 触屏位置是否移动到了取消区域
  77. if (this.moveToCancel) {
  78. console.log("录音取消")
  79. return;
  80. }
  81. // 小于1秒不发送
  82. if (this.druation == 0) {
  83. uni.showToast({
  84. title: "说话时间太短",
  85. icon: 'none'
  86. })
  87. return;
  88. }
  89. this.$rc.upload().then((data) => {
  90. this.$emit("send", data);
  91. }).catch((e) => {
  92. uni.showToast({
  93. title: e,
  94. icon: 'none'
  95. })
  96. }).finally(() => {
  97. this.$rc.close();
  98. })
  99. },
  100. startTimer() {
  101. this.druation = 0;
  102. this.StopTimer();
  103. this.rcTimer = setInterval(() => {
  104. this.druation++;
  105. // 大于60s,直接结束
  106. if (this.druation >= 60) {
  107. this.onEndRecord();
  108. }
  109. }, 1000)
  110. },
  111. StopTimer() {
  112. this.rcTimer && clearInterval(this.rcTimer);
  113. this.rcTimer = null;
  114. },
  115. initRecordBar() {
  116. const query = uni.createSelectorQuery().in(this);
  117. query.select('#chat-record-bar').boundingClientRect((rect) => {
  118. // 顶部高度位置
  119. this.recordBarTop = rect.top;
  120. }).exec()
  121. }
  122. },
  123. computed: {
  124. recordWindowStyle() {
  125. const windowHeight = uni.getSystemInfoSync().windowHeight;
  126. const bottom = windowHeight - this.recordBarTop + 12;
  127. return `bottom:${bottom}px;`
  128. },
  129. recordTip() {
  130. if (this.druation > 50) {
  131. return `${60 - this.druation}s后将停止录音`;
  132. }
  133. return `录音时长:${this.druation}s`;
  134. }
  135. }
  136. }
  137. </script>
  138. <style lang="scss" scoped>
  139. .chat-record {
  140. .rc-wave {
  141. display: flex;
  142. align-items: flex-end;
  143. justify-content: center;
  144. position: relative;
  145. height: 80rpx;
  146. .note {
  147. background: linear-gradient(to top, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  148. width: 4px;
  149. height: 50%;
  150. border-radius: 5rpx;
  151. margin-right: 4px;
  152. animation: loading 0.5s infinite linear;
  153. animation-delay: calc(0.1s * var(--d));
  154. @keyframes loading {
  155. 0% {
  156. background-image: linear-gradient(to right, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  157. height: 20%;
  158. border-radius: 5rpx;
  159. }
  160. 50% {
  161. background-image: linear-gradient(to top, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  162. height: 80%;
  163. border-radius: 5rpx;
  164. }
  165. 100% {
  166. background-image: linear-gradient(to top, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  167. height: 20%;
  168. border-radius: 5rpx;
  169. }
  170. }
  171. }
  172. }
  173. .chat-record-bar {
  174. padding: 10rpx;
  175. margin: 10rpx;
  176. border-radius: 10rpx;
  177. text-align: center;
  178. box-shadow: $im-box-shadow;
  179. &.recording {
  180. background-color: $im-color-primary;
  181. color: #fff;
  182. }
  183. }
  184. .chat-record-window {
  185. position: fixed;
  186. left: 0;
  187. right: 0;
  188. height: 360rpx;
  189. background-color: rgba(255, 255, 255, 0.95);
  190. padding: 30rpx;
  191. .icon-microphone {
  192. text-align: center;
  193. font-size: 80rpx;
  194. padding: 10rpx;
  195. }
  196. .rc-tip {
  197. text-align: center;
  198. font-size: $im-font-size-small;
  199. color: $im-text-color-light;
  200. margin-top: 20rpx;
  201. }
  202. .cancel-btn {
  203. text-align: center;
  204. margin-top: 40rpx;
  205. height: 80rpx;
  206. }
  207. .opt-tip {
  208. text-align: center;
  209. font-size: 30rpx;
  210. padding: 20rpx;
  211. }
  212. .red {
  213. color: $im-color-danger !important;
  214. }
  215. }
  216. }
  217. </style>