gui-im-input.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template name="gui-im-input">
  2. <view>
  3. <view class="gui-im-footer gui-bg-gray gui-dark-bg-level-2">
  4. <view
  5. class="gui-flex gui-row gui-nowrap gui-space-between gui-align-items-center">
  6. <view
  7. class="gui-im-menus gui-icons gui-secondary-text"
  8. hover-class="gui-tap"
  9. v-if="voiceBtnShow"
  10. @tap="showRec">&#xe617;</view>
  11. <view
  12. class="gui-im-menus gui-icons gui-secondary-text"
  13. hover-class="gui-tap"
  14. @tap="chooseImg">&#xe63d;</view>
  15. <view
  16. class="gui-im-input gui-bg-white gui-dark-bg-level-3">
  17. <textarea
  18. type="text"
  19. v-model="inputMsg"
  20. :fixed="true"
  21. :maxlength="-1"
  22. @confirm="sendTextMsg"
  23. :cursor-spacing="35"></textarea>
  24. </view>
  25. <view
  26. class="gui-items gui-primary-text"
  27. style="padding:0 20rpx; margin-right:10rpx;"
  28. hover-class="gui-tap"
  29. @tap="sendTextMsg">发送</view>
  30. </view>
  31. <view>
  32. <gui-iphone-bottom></gui-iphone-bottom>
  33. </view>
  34. </view>
  35. <!-- 语音输入 -->
  36. <view
  37. class="gui-im-record gui-bg-gray gui-dark-bg-level-2"
  38. v-if="recShow">
  39. <view class="gui-im-record-txt">
  40. {{recTxt}}<text v-if="recing">已录音 : {{recLength}} s</text>
  41. </view>
  42. <view class="gui-im-record-btn"
  43. @tap="rec" :class="[recing ? 'gui-im-recording' : '']"></view>
  44. <view class="gui-im-send-voice"
  45. v-if="tmpVoice != ''">
  46. <text @tap="sendVoiceMsg">发送语音</text>
  47. </view>
  48. <view
  49. class="gui-im-record-close gui-icons"
  50. @tap="closeRec"
  51. v-if="!recing">&#xe78a;</view>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. var bgAudioMannager = uni.createInnerAudioContext();
  57. export default {
  58. name : "gui-im-input",
  59. props : { },
  60. data() {
  61. return {
  62. paddingB : '0',
  63. uploading : false,
  64. recShow : false,
  65. recTxt : "请点击绿色按钮开始录音",
  66. inputMsg : "",
  67. recorderManager : null,
  68. recing : false,
  69. recLength : 1,
  70. recTimer : null,
  71. tmpVoice : '',
  72. voiceLen : 0,
  73. voiceBtnShow : false,
  74. // 播放相关
  75. player : null,
  76. playTxt : "试听语音"
  77. }
  78. },
  79. created : function(){
  80. // #ifndef H5
  81. this.voiceBtnShow = true;
  82. this.recorderManager = uni.getRecorderManager();
  83. this.recorderManager.onStop((res) => {
  84. this.tmpVoice = res.tempFilePath;
  85. this.recing = false;
  86. this.recTxt = "... 已录音 "+this.recLength+
  87. "s, 点击绿色按钮重新录音 ...";
  88. clearInterval(this.recTimer);
  89. });
  90. this.recorderManager.onError(() => {
  91. uni.showToast({ title: '录音失败', icon: 'none' });
  92. this.recing = false;
  93. this.recTxt = "请点击绿色按钮开始录音",
  94. clearInterval(this.recTimer);
  95. });
  96. // #endif
  97. },
  98. methods:{
  99. // 录音
  100. rec : function(){
  101. if (this.recing){
  102. this.recorderManager.stop();
  103. this.recing = false;
  104. this.recTxt = "... 已录音 "+this.recLength
  105. +"s, 点击绿色按钮重新录音 ...";
  106. clearInterval(this.recTimer);
  107. } else {
  108. this.recorderManager.start({duration:60000, format:'mp3' });
  109. this.recing = true;
  110. this.recTxt = "... 正在录音 ...";
  111. this.recLength = 1;
  112. this.recTimer = setInterval(()=>{this.recLength++;}, 1000);
  113. }
  114. },
  115. // 发送录音
  116. sendVoiceMsg : function(){
  117. if (this.tmpVoice == '') {
  118. uni.showToast({ title: "请先录制一段语音", icon: "none" });
  119. return;
  120. }
  121. // 关闭界面
  122. this.recShow = false;
  123. this.$emit('sendVoice', this.tmpVoice, this.recLength);
  124. this.tmpVoice = '';
  125. this.recLength = 0;
  126. this.recTxt = "请点击绿色按钮开始录音";
  127. },
  128. // 展示录音界面
  129. showRec : function(){this.recShow = true;},
  130. // 关闭录音界面
  131. closeRec: function (){this.recShow = false;},
  132. // 发送文本消息
  133. sendTextMsg: function () {
  134. if (this.inputMsg < 1) {return false;}
  135. this.$emit('sendText', this.inputMsg);
  136. this.inputMsg = '';
  137. },
  138. // 选择图片
  139. chooseImg : function(){
  140. uni.chooseImage({
  141. count : 1,
  142. sizeType : ['compressed'],
  143. sourceType : ['album', 'camera'],
  144. success : (res)=>{
  145. const tempFilePaths = res.tempFilePaths;
  146. this.$emit('chooseImage', tempFilePaths[0]);
  147. }
  148. });
  149. }
  150. },
  151. emits : ['sendVoice', 'sendText', 'chooseImage']
  152. }
  153. </script>
  154. <style scoped>
  155. .gui-im-footer{width:100%; position:fixed; left:0; bottom:0;}
  156. .gui-im-footer .gui-items{width:auto; line-height:88rpx; flex-shrink:0; font-size:28rpx;}
  157. .gui-im-menus{width:80rpx; height:80rpx; flex-shrink:0; line-height:80rpx; text-align:center;}
  158. .gui-im-input{width:600rpx; margin:10rpx; padding:12rpx 16rpx; border-radius:6rpx;}
  159. .gui-im-input textarea{width:100%; height:40rpx; line-height:40rpx; font-size:28rpx; margin-top:10rpx;}
  160. .gui-im-record{width:100%; position:fixed; left:0; bottom:0; padding:30px 0; padding-bottom:100rpx; z-index:11;}
  161. .gui-im-record-close{width:100rpx; height:100rpx; position:absolute; top:0px; right:0px; z-index:100; text-align:center; line-height:100rpx; font-size:38rpx !important;}
  162. .gui-im-record-txt{text-align:center; font-size:26rpx; line-height:30px; padding-bottom:10px;}
  163. .gui-im-record-btn{width:60px; height:60px; margin:0 auto; border:5px solid #F1F2F3; border-radius:100%; background:#00B26A;}
  164. .gui-im-recording{background:#FF0000; animation:fade linear 2s infinite;}
  165. @keyframes fade{from{opacity:0.1;} 50%{opacity:1;} to{opacity:0.1;}}
  166. .gui-im-record-txt text{color:#00B26A; padding:0 12px;}
  167. .gui-im-send-voice{margin-top:12px; font-size:28rpx; color:#00BA62; text-align:center;}
  168. .gui-im-send-voice text{margin:0 15px; color:#00BA62;}
  169. .gui-icons{font-size:50rpx;}
  170. </style>