chat-record.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. })
  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. recordBarStyle() {
  132. const bgColor = this.recording ? "royalblue" : "white";
  133. const textColor = this.recording ? "white" : "black";
  134. return `background-color:${bgColor};
  135. color:${textColor};`
  136. },
  137. recordTip() {
  138. if (this.druation > 50) {
  139. return `${60-this.druation}s后将停止录音`;
  140. }
  141. return `录音时长:${this.druation}s`;
  142. }
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. .chat-record {
  148. .rc-wave {
  149. display: flex;
  150. align-items: flex-end;
  151. justify-content: center;
  152. position: relative;
  153. height: 80rpx;
  154. .note {
  155. background: linear-gradient(to top, #395ff3 0%, #89aff3 100%);
  156. width: 4px;
  157. height: 50%;
  158. border-radius: 5rpx;
  159. margin-right: 4px;
  160. animation: loading 0.5s infinite linear;
  161. animation-delay: calc(0.1s * var(--d));
  162. @keyframes loading {
  163. 0% {
  164. background-image: linear-gradient(to right, #395ff3 0%, #89aff3 100%);
  165. height: 20%;
  166. border-radius: 5rpx;
  167. }
  168. 50% {
  169. background-image: linear-gradient(to top, #395ff3 0%, #a9cff3 100%);
  170. height: 80%;
  171. border-radius: 5rpx;
  172. }
  173. 100% {
  174. background-image: linear-gradient(to top, #395ff3 0%, #a9cff3 100%);
  175. height: 20%;
  176. border-radius: 5rpx;
  177. }
  178. }
  179. }
  180. }
  181. .chat-record-bar {
  182. padding: 10rpx;
  183. margin: 10rpx;
  184. border-radius: 10rpx;
  185. text-align: center;
  186. }
  187. .chat-record-window {
  188. position: fixed;
  189. left: 0;
  190. height: 360rpx;
  191. width: 100%;
  192. background-color: rgba(255, 255, 255, 0.95);
  193. padding: 30rpx;
  194. .icon-microphone {
  195. text-align: center;
  196. font-size: 80rpx;
  197. padding: 10rpx;
  198. }
  199. .rc-tip {
  200. text-align: center;
  201. font-size: 30rpx;
  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: red !important;
  216. }
  217. .black {
  218. color: gray;
  219. }
  220. }
  221. }
  222. </style>