chat-record.vue 5.1 KB

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