ChatInput.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <script setup>
  2. // const plugin = requirePlugin("WechatSI")
  3. import { ref, unref, onMounted } from 'vue';
  4. import { baseURL } from '@/utils/https';
  5. const modelInpValue = defineModel();
  6. const emit = defineEmits(['on-submit']);
  7. const modelLoading = defineModel('loading');
  8. const isRecording = ref(false);
  9. const recordingTip = ref('松开 发送');
  10. const showVolume = ref(false);
  11. const volumeLevel = ref(50); // 模拟音量级别
  12. let recorderManager = null;
  13. const audioPath = ref(null);
  14. // 实时语音识别
  15. // const manager = plugin.getRecordRecognitionManager();
  16. console.log( "manager", manager );
  17. const innerAudioContext = wx.createInnerAudioContext();
  18. // 类型 input|voice
  19. const inpType = ref('input');
  20. // 提交问题
  21. const onSubmit = () => {
  22. const val = unref(modelInpValue);
  23. if (modelLoading.value) {
  24. return uni.showToast({ title: '当前对话进行中', duration: 3000, icon: 'none' });
  25. }
  26. if (!val) {
  27. return uni.showToast({ title: '请输入您的问题或需求', duration: 3000, icon: 'none' });
  28. }
  29. if (val.length > 2000) {
  30. return uni.showToast({ title: '问题限制2000个字以内', duration: 3000, icon: 'none' });
  31. }
  32. modelInpValue.value = '';
  33. emit('on-submit', { showVal: val, question: val });
  34. };
  35. const onChangeInpType = () => {
  36. inpType.value = inpType.value === 'input' ? 'voice' : 'input';
  37. }
  38. const uploadAudio = (filePath) => {
  39. uni.showLoading({
  40. title: '上传中...'
  41. });
  42. uni.uploadFile({
  43. url: 'https://your-server.com/upload',
  44. filePath: filePath,
  45. name: 'audio', // 后端接收文件的字段名
  46. formData: {
  47. // 可以附加其他表单数据
  48. 'userId': '123',
  49. 'timestamp': new Date().getTime()
  50. },
  51. success: (uploadRes) => {
  52. uni.hideLoading();
  53. console.log('上传成功', uploadRes);
  54. // 处理服务器返回的数据
  55. const data = JSON.parse(uploadRes.data);
  56. uni.showToast({
  57. title: '上传成功',
  58. icon: 'success'
  59. });
  60. },
  61. fail: (error) => {
  62. uni.hideLoading();
  63. console.log('上传失败', error);
  64. uni.showToast({
  65. title: '上传失败',
  66. icon: 'none'
  67. });
  68. }
  69. });
  70. }
  71. const playRecording = () => {
  72. if (innerAudioContext) {
  73. innerAudioContext.src = audioPath.value;
  74. innerAudioContext.play();
  75. innerAudioContext.onPlay(() => {
  76. console.log('开始播放');
  77. });
  78. innerAudioContext.onError((err) => {
  79. console.error('播放错误:', err);
  80. uni.showToast({ title: '播放失败', icon: 'none' });
  81. });
  82. }
  83. }
  84. // 语音播放相关
  85. const startRecording = e => {
  86. isRecording.value = true;
  87. showVolume.value = true;
  88. recorderManager.start({
  89. format: 'mp3',
  90. duration: 60000, // 最长1分钟
  91. sampleRate: 44100,
  92. numberOfChannels: 1,
  93. encodeBitRate: 192000,
  94. frameSize: 50, // 帧大小,影响onFrameRecorded回调频率
  95. });
  96. }
  97. function setupRecorderEvents() {
  98. if (!recorderManager) return;
  99. uni.authorize({
  100. scope: 'scope.record',
  101. success() {
  102. recorderManager.onStart(() => {
  103. console.log('录音开始');
  104. });
  105. recorderManager.onPause(() => {
  106. console.log('录音暂停');
  107. });
  108. recorderManager.onStop((res) => {
  109. audioPath.value = res.tempFilePath;
  110. // setTimeout(() => {
  111. // innerAudioContext.src = 'https://dlink.host/musics/aHR0cHM6Ly9vbmVkcnYtbXkuc2hhcmVwb2ludC5jb20vOnU6L2cvcGVyc29uYWwvc3Rvcl9vbmVkcnZfb25taWNyb3NvZnRfY29tL0VjYzBzQUxiWFk5TWdHQl9GUVNkV2pJQm5wRmM0MktDZWpURnhhMjhELUdXeVE.mp3';
  112. // innerAudioContext.play();
  113. // }, 1000)
  114. console.log('录音停止', res);
  115. // 这里可以处理录音结果,如上传或播放
  116. });
  117. recorderManager.onFrameRecorded((res) => {
  118. // 可以在这里获取实时音量等信息
  119. const volume = res.volumn; // 注意微信可能是volumn而不是volume
  120. if (volume !== undefined) {
  121. volumeLevel.value = Math.min(100, volume * 200); // 调整音量显示比例
  122. }
  123. });
  124. },
  125. fail() {
  126. uni.showToast({ title: '用户拒绝授权录音权限', icon: 'none' });
  127. },
  128. });
  129. }
  130. function stopRecording() {
  131. if (!isRecording.value) return;
  132. isRecording.value = false;
  133. showVolume.value = false;
  134. recorderManager.stop();
  135. // 停止音量检测
  136. // stopVolumeDetection();
  137. }
  138. // 初始化录音管理器
  139. onMounted(() => {
  140. recorderManager = wx.getRecorderManager();
  141. setupRecorderEvents();
  142. });
  143. </script>
  144. <template>
  145. <view class="voice-wrapper" v-show="isRecording">
  146. <view class="voice-inner">
  147. <view class="voice-card">
  148. <view class="voice-tip">
  149. <view class="la-line-scale-pulse-out">
  150. <view></view>
  151. <view></view>
  152. <view></view>
  153. <view></view>
  154. <view></view>
  155. </view>
  156. </view>
  157. </view>
  158. <view class="vocie-btn_wrapper" @touchstart="startRecording" @touchend="stopRecording"
  159. @touchcancel="stopRecording">
  160. <view class="tips">{{ recordingTip }}</view>
  161. <view class="btn">
  162. <uni-icons type="sound" size="20" color="#777"></uni-icons>
  163. </view>
  164. </view>
  165. </view>
  166. </view>
  167. <view class="chat-inp-container">
  168. <!-- <view @click="playRecording">按钮</view> -->
  169. <view class="chat-inp-inner">
  170. <view class="voice-btn" @click="onChangeInpType">
  171. <TheSvgIcon class="icon" src="icon-voice" size="42"></TheSvgIcon>
  172. </view>
  173. <view class="inp-inner">
  174. <textarea v-model.trim="modelInpValue" :show-confirm-bar="false" :cursor-spacing="30" auto-height
  175. :maxlength="2000" class="chat-inp" placeholder="输入您的问题或需求" placeholder-style="color:#9A9A9A"
  176. v-show="inpType == 'input'">
  177. </textarea>
  178. <view class="chat-voice" v-show="inpType == 'voice'" @touchstart="startRecording" @touchend="stopRecording"
  179. @touchcancel="stopRecording">
  180. <text>按住 说话</text>
  181. </view>
  182. </view>
  183. <view class="send-btn" @click="onSubmit">
  184. <TheSvgIcon class="icon" src="icon-send-plane" size="30" v-show="!modelLoading"></TheSvgIcon>
  185. <view class="la-ball-running-dots la-sm" v-show="modelLoading">
  186. <view class="item" v-for="item in 5" :key="item"></view>
  187. </view>
  188. </view>
  189. </view>
  190. </view>
  191. </template>
  192. <style lang="scss" scoped>
  193. .chat-inp-container {
  194. padding: 0 24rpx;
  195. box-sizing: content-box;
  196. .chat-inp-inner {
  197. display: flex;
  198. align-items: flex-end;
  199. padding: 26rpx 28rpx;
  200. border-radius: 32rpx;
  201. background: #FFF;
  202. box-shadow: 0px 8rpx 16rpx 2rpx rgba(172, 200, 224, 0.20);
  203. .voice-btn {
  204. display: flex;
  205. align-items: center;
  206. justify-content: flex-end;
  207. height: 56rpx;
  208. }
  209. .inp-inner {
  210. width: 100%;
  211. min-height: 56rpx;
  212. display: flex;
  213. align-items: center;
  214. // padding-bottom: 10rpx;
  215. .chat-inp {
  216. width: 100%;
  217. max-height: 200rpx;
  218. height: 100%;
  219. padding: 0 16rpx;
  220. font-size: 28rpx;
  221. color: #333;
  222. box-sizing: border-box;
  223. }
  224. .chat-voice {
  225. width: 100%;
  226. font-size: 28rpx;
  227. text-align: center;
  228. }
  229. }
  230. .send-btn {
  231. flex-shrink: 0;
  232. display: flex;
  233. align-items: center;
  234. justify-content: center;
  235. width: 56rpx;
  236. height: 56rpx;
  237. border-radius: 100%;
  238. background: #212121;
  239. .icon {
  240. transition: all 0.3s ease-in-out;
  241. }
  242. }
  243. }
  244. }
  245. .voice-wrapper {
  246. position: absolute;
  247. top: 0;
  248. left: 0;
  249. width: 100vh;
  250. height: 100vh;
  251. background: rgba(0, 0, 0, 0.7);
  252. z-index: 10;
  253. .voice-inner {
  254. display: flex;
  255. justify-content: space-between;
  256. flex-flow: column;
  257. height: 100%;
  258. .voice-card {
  259. flex: 1;
  260. display: flex;
  261. align-items: center;
  262. justify-content: center;
  263. width: 100vw;
  264. .voice-tip {
  265. display: flex;
  266. align-items: center;
  267. justify-content: center;
  268. padding: 10rpx 20rpx;
  269. width: 100px;
  270. border-radius: 10rpx;
  271. background: #00aa5b;
  272. }
  273. }
  274. .vocie-btn_wrapper {
  275. position: relative;
  276. display: flex;
  277. align-items: center;
  278. justify-content: space-between;
  279. flex-flow: column;
  280. flex-shrink: 0;
  281. height: 200rpx;
  282. width: 100vw;
  283. overflow: hidden;
  284. .tips {
  285. padding-bottom: 10rpx;
  286. flex-shrink: 0;
  287. color: #fff;
  288. font-size: 24rpx;
  289. text-align: center;
  290. }
  291. .btn {
  292. flex: 1;
  293. display: flex;
  294. align-items: center;
  295. justify-content: center;
  296. width: 100vw;
  297. height: 120rpx;
  298. border-top-left-radius: 50%;
  299. border-top-right-radius: 50%;
  300. border-top: 1px solid #fff;
  301. background: #b5b5b5;
  302. padding-bottom: env(safe-area-inset-bottom);
  303. }
  304. }
  305. }
  306. }
  307. </style>