interview.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const _sfc_main = {
  4. data() {
  5. return {
  6. messages: [],
  7. isRecording: false,
  8. recorderManager: null,
  9. innerAudioContext: null,
  10. scrollTop: 0,
  11. apiUrl: "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
  12. };
  13. },
  14. onLoad() {
  15. this.recorderManager = common_vendor.index.getRecorderManager();
  16. this.innerAudioContext = common_vendor.index.createInnerAudioContext();
  17. this.recorderManager.onStop((res) => {
  18. this.processAudio(res.tempFilePath);
  19. });
  20. },
  21. methods: {
  22. startRecording() {
  23. this.isRecording = true;
  24. this.recorderManager.start({
  25. format: "mp3",
  26. sampleRate: 16e3,
  27. numberOfChannels: 1
  28. });
  29. },
  30. stopRecording() {
  31. this.isRecording = false;
  32. this.recorderManager.stop();
  33. },
  34. processAudio(filePath) {
  35. common_vendor.index.showLoading({
  36. title: "处理中..."
  37. });
  38. this.speechToText(filePath).then((text) => {
  39. this.addMessage("user", text);
  40. this.callDashScopeAPI(text);
  41. }).catch((err) => {
  42. common_vendor.index.hideLoading();
  43. common_vendor.index.showToast({
  44. title: "语音识别失败",
  45. icon: "none"
  46. });
  47. console.error(err);
  48. });
  49. },
  50. speechToText(filePath) {
  51. return new Promise((resolve) => {
  52. console.log("录音文件路径:", filePath);
  53. setTimeout(() => {
  54. resolve("你好,我需要帮助");
  55. }, 500);
  56. });
  57. },
  58. callDashScopeAPI(userInput) {
  59. const requestBody = {
  60. model: "qwen-plus",
  61. messages: [
  62. {
  63. role: "system",
  64. content: "You are a helpful assistant."
  65. },
  66. {
  67. role: "user",
  68. content: userInput
  69. }
  70. ]
  71. };
  72. common_vendor.index.request({
  73. url: this.apiUrl,
  74. method: "POST",
  75. header: {
  76. "Content-Type": "application/json",
  77. "Authorization": "Bearer YOUR_API_KEY"
  78. // 替换为您的API密钥
  79. },
  80. data: requestBody,
  81. success: (res) => {
  82. common_vendor.index.hideLoading();
  83. if (res.statusCode === 200 && res.data && res.data.choices && res.data.choices.length > 0) {
  84. const assistantMessage = res.data.choices[0].message.content;
  85. this.addMessage("assistant", assistantMessage);
  86. this.textToSpeech(assistantMessage);
  87. } else {
  88. common_vendor.index.showToast({
  89. title: "获取回复失败",
  90. icon: "none"
  91. });
  92. console.error("API响应错误:", res);
  93. }
  94. },
  95. fail: (err) => {
  96. common_vendor.index.hideLoading();
  97. common_vendor.index.showToast({
  98. title: "网络请求失败",
  99. icon: "none"
  100. });
  101. console.error(err);
  102. }
  103. });
  104. },
  105. addMessage(role, content) {
  106. this.messages.push({ role, content });
  107. this.$nextTick(() => {
  108. this.scrollTop = 9999;
  109. });
  110. },
  111. textToSpeech(text) {
  112. common_vendor.index.request({
  113. url: "您的文本转语音服务URL",
  114. method: "POST",
  115. data: { text },
  116. success: (res) => {
  117. if (res.statusCode === 200 && res.data && res.data.audio_url) {
  118. this.innerAudioContext.src = res.data.audio_url;
  119. this.innerAudioContext.play();
  120. }
  121. }
  122. });
  123. }
  124. }
  125. };
  126. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  127. return {
  128. a: common_vendor.f($data.messages, (message, index, i0) => {
  129. return {
  130. a: common_vendor.t(message.content),
  131. b: index,
  132. c: common_vendor.n(message.role)
  133. };
  134. }),
  135. b: $data.scrollTop,
  136. c: common_vendor.t($data.isRecording ? "松开结束" : "按住说话"),
  137. d: $data.isRecording ? 1 : "",
  138. e: common_vendor.o((...args) => $options.startRecording && $options.startRecording(...args)),
  139. f: common_vendor.o((...args) => $options.stopRecording && $options.stopRecording(...args))
  140. };
  141. }
  142. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  143. wx.createPage(MiniProgramPage);