chat-record.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.$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. })
  101. },
  102. startTimer() {
  103. this.druation = 0;
  104. this.StopTimer();
  105. this.rcTimer = setInterval(() => {
  106. this.druation++;
  107. // 大于60s,直接结束
  108. if (this.druation >= 60) {
  109. this.onEndRecord();
  110. }
  111. }, 1000)
  112. },
  113. StopTimer() {
  114. this.rcTimer && clearInterval(this.rcTimer);
  115. this.rcTimer = null;
  116. },
  117. initRecordBar() {
  118. const query = uni.createSelectorQuery().in(this);
  119. query.select('#chat-record-bar').boundingClientRect((rect) => {
  120. // 顶部高度位置
  121. this.recordBarTop = rect.top;
  122. }).exec()
  123. }
  124. },
  125. computed: {
  126. recordWindowStyle() {
  127. const windowHeight = uni.getSystemInfoSync().windowHeight;
  128. const bottom = windowHeight - this.recordBarTop + 12;
  129. return `bottom:${bottom}px;`
  130. },
  131. recordTip() {
  132. if (this.druation > 50) {
  133. return `${60 - this.druation}s后将停止录音`;
  134. }
  135. return `录音时长:${this.druation}s`;
  136. }
  137. }
  138. }
  139. </script>
  140. <style lang="scss" scoped>
  141. .chat-record {
  142. .rc-wave {
  143. display: flex;
  144. align-items: flex-end;
  145. justify-content: center;
  146. position: relative;
  147. height: 80rpx;
  148. .note {
  149. background: linear-gradient(to top, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  150. width: 4px;
  151. height: 50%;
  152. border-radius: 5rpx;
  153. margin-right: 4px;
  154. animation: loading 0.5s infinite linear;
  155. animation-delay: calc(0.1s * var(--d));
  156. @keyframes loading {
  157. 0% {
  158. background-image: linear-gradient(to right, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  159. height: 20%;
  160. border-radius: 5rpx;
  161. }
  162. 50% {
  163. background-image: linear-gradient(to top, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  164. height: 80%;
  165. border-radius: 5rpx;
  166. }
  167. 100% {
  168. background-image: linear-gradient(to top, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  169. height: 20%;
  170. border-radius: 5rpx;
  171. }
  172. }
  173. }
  174. }
  175. .chat-record-bar {
  176. padding: 10rpx;
  177. margin: 10rpx;
  178. border-radius: 10rpx;
  179. text-align: center;
  180. box-shadow: $im-box-shadow;
  181. &.recording {
  182. background-color: $im-color-primary;
  183. color: #fff;
  184. }
  185. }
  186. .chat-record-window {
  187. position: fixed;
  188. left: 0;
  189. right: 0;
  190. height: 360rpx;
  191. background-color: rgba(255, 255, 255, 0.95);
  192. padding: 30rpx;
  193. .icon-microphone {
  194. text-align: center;
  195. font-size: 80rpx;
  196. padding: 10rpx;
  197. }
  198. .rc-tip {
  199. text-align: center;
  200. font-size: $im-font-size-small;
  201. color: $im-text-color-light;
  202. margin-top: 20rpx;
  203. }
  204. .cancel-btn {
  205. text-align: center;
  206. margin-top: 40rpx;
  207. height: 80rpx;
  208. }
  209. .opt-tip {
  210. text-align: center;
  211. font-size: 30rpx;
  212. padding: 20rpx;
  213. }
  214. .red {
  215. color: $im-color-danger !important;
  216. }
  217. }
  218. }
  219. </style>