chat-record.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. return;
  52. }
  53. console.log("开始录音")
  54. this.moveToCancel = false;
  55. this.initRecordBar();
  56. if(!this.$rc.checkIsEnable()){
  57. return;
  58. }
  59. this.$rc.start().then(() => {
  60. this.recording = true;
  61. console.log("开始录音成功")
  62. // 开始计时
  63. this.startTimer();
  64. }).catch((e) => {
  65. console.log("录音失败" + JSON.stringify(e))
  66. uni.showToast({
  67. title: "录音失败",
  68. icon: "none"
  69. });
  70. });
  71. },
  72. onEndRecord() {
  73. if(!this.recording){
  74. return;
  75. }
  76. this.recording = false;
  77. // 停止计时
  78. this.stopTimer();
  79. // 停止录音
  80. this.$rc.close();
  81. // 触屏位置是否移动到了取消区域
  82. if (this.moveToCancel) {
  83. console.log("录音取消")
  84. return;
  85. }
  86. // 大于1秒才发送
  87. if (this.druation <= 1) {
  88. uni.showToast({
  89. title: "说话时间太短",
  90. icon: 'none'
  91. })
  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. })
  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. recordTip() {
  133. if (this.druation > 50) {
  134. return `${60 - this.druation}s后将停止录音`;
  135. }
  136. return `录音时长:${this.druation}s`;
  137. }
  138. },
  139. unmounted() {
  140. this.stopTimer();
  141. this.recording = false;
  142. }
  143. }
  144. </script>
  145. <style lang="scss" scoped>
  146. .chat-record {
  147. .rc-wave {
  148. display: flex;
  149. align-items: flex-end;
  150. justify-content: center;
  151. position: relative;
  152. height: 80rpx;
  153. .note {
  154. background: linear-gradient(to top, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  155. width: 4px;
  156. height: 50%;
  157. border-radius: 5rpx;
  158. margin-right: 4px;
  159. animation: loading 0.5s infinite linear;
  160. animation-delay: calc(0.1s * var(--d));
  161. @keyframes loading {
  162. 0% {
  163. background-image: linear-gradient(to right, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  164. height: 20%;
  165. border-radius: 5rpx;
  166. }
  167. 50% {
  168. background-image: linear-gradient(to top, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  169. height: 80%;
  170. border-radius: 5rpx;
  171. }
  172. 100% {
  173. background-image: linear-gradient(to top, $im-color-primary-light-1 0%, $im-color-primary-light-6 100%);
  174. height: 20%;
  175. border-radius: 5rpx;
  176. }
  177. }
  178. }
  179. }
  180. .chat-record-bar {
  181. padding: 10rpx;
  182. margin: 10rpx;
  183. border-radius: 10rpx;
  184. text-align: center;
  185. box-shadow: $im-box-shadow;
  186. &.recording {
  187. background-color: $im-color-primary;
  188. color: #fff;
  189. }
  190. }
  191. .chat-record-window {
  192. position: fixed;
  193. left: 0;
  194. right: 0;
  195. height: 360rpx;
  196. background-color: rgba(255, 255, 255, 0.95);
  197. padding: 30rpx;
  198. .icon-microphone {
  199. text-align: center;
  200. font-size: 80rpx;
  201. padding: 10rpx;
  202. }
  203. .rc-tip {
  204. text-align: center;
  205. font-size: $im-font-size-small;
  206. color: $im-text-color-light;
  207. margin-top: 20rpx;
  208. }
  209. .cancel-btn {
  210. text-align: center;
  211. margin-top: 40rpx;
  212. height: 80rpx;
  213. }
  214. .opt-tip {
  215. text-align: center;
  216. font-size: 30rpx;
  217. padding: 20rpx;
  218. }
  219. .red {
  220. color: $im-color-danger !important;
  221. }
  222. }
  223. }
  224. </style>