dialog.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <u-navbar title="对话" bgColor="#F6F6F8" :titleStyle="{'font-size':'32rpx','font-weight':'bold'}">
  4. <view class="u-nav-slot" slot="left" style="display: flex;background-color: transparent;">
  5. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/10/2f7cc90a-848c-4369-b573-4448125591a9.png" style="width: 45rpx;height: 45rpx;" @tap="toBack"></image>
  6. <!-- <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/10/3580b8d2-8ddb-4385-8516-fb6f47ce5fea.png" style="width: 42rpx;height: 42rpx;margin-left: 40rpx;" @tap="startNewDialog"></image> -->
  7. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/10/cb27247a-8924-4ea7-a750-238ec9debd28.png" style="width: 42rpx;height: 42rpx;margin-left: 40rpx;" @tap="startNewDialog"></image>
  8. </view>
  9. </u-navbar>
  10. <template v-if="dialogList.length===0">
  11. <div class="welcome">
  12. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/10/09ed81ea-ab7c-457b-a128-1d0ed43aaffd.png"></image>
  13. <p>Hi,很高兴见到你~</p>
  14. <p class="tip">我可以帮你读语文,写作各种创意内容,请把你的任务交给我吧~ </p>
  15. </div>
  16. </template>
  17. <template v-else>
  18. <div class="dialogs container" ref="messageContainer">
  19. <div class="d_answer init">
  20. <div class="da_top adfac">
  21. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/10/09ed81ea-ab7c-457b-a128-1d0ed43aaffd.png"></image>
  22. <text>AI智能终端</text>
  23. </div>
  24. <div class="da_content">Hi,我可以帮你解答问题,写作各种创意内容,请把你的任务交给我吧~~</div>
  25. </div>
  26. <div v-for="(item,index) in dialogList" :key="index">
  27. <div class="d_question">
  28. <div class="dq_text">{{ item.question }}</div>
  29. </div>
  30. <div class="d_answer">
  31. <div class="da_top adfac">
  32. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/10/09ed81ea-ab7c-457b-a128-1d0ed43aaffd.png"></image>
  33. <text>AI智能终端</text>
  34. </div>
  35. <div class="da_content">
  36. <template v-if="item.think">
  37. <view class="dac_think adfac">
  38. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/07/13c5dd16-2032-464a-8b1c-2722d201cfe2.gif"></image>
  39. <text>正在思考中</text>
  40. </view>
  41. </template>
  42. <template v-else>
  43. <u-parse :content="item.answer" style="font-size: 28rpx;line-height: 54rpx;color: #252525;"></u-parse>
  44. </template>
  45. <div class="dc_btns adfacjb" v-if="item.answer">
  46. <div class="db_l">
  47. <image :src="item.copy?require('@/static/copy_active.png'):require('@/static/copy.png')" @tap="toCopy(item,index)"></image>
  48. <image :src="item.upvote?require('@/static/upvote_active.png'):require('@/static/upvote.png')" @tap="toUpvote(item,index)"></image>
  49. <image :src="item.comment?require('@/static/comment_active.png'):require('@/static/comment.png')" @tap="toComment(item,index)"></image>
  50. </div>
  51. <div class="db_r">
  52. <image :src="item.share?require('@/static/share_active.png'):require('@/static/share.png')" @tap="toShare(item,index)"></image>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. <div class="ask_box" :style="{'bottom':fixBottom+'px'}">
  61. <div class="ask">
  62. <div class="a_inp">
  63. <div class="ai_l">
  64. <u-textarea v-model="question" placeholder="请输入您的问题" autoHeight :showConfirmBar="false" :adjustPosition="false" maxlength="999999"></u-textarea>
  65. </div>
  66. <div class="ai_r">
  67. <image @tap="sendQuestion" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/10/a4a2718d-733e-467a-8bc2-7dc1548d2767.png"></image>
  68. </div>
  69. </div>
  70. </div>
  71. <div class="ask_memo">本服务为AI生成内容,结果仅供参考</div>
  72. </div>
  73. <u-modal :show="commentShow" title="评论" @confirm="commentConfirm" @cancel="commentCancel" @close="commentCancel" :showCancelButton="true">
  74. <u-textarea v-model="content" placeholder="对于我们的回答您是否不满意,您有更好的答案建议吗?请在此输入."></u-textarea>
  75. </u-modal>
  76. </view>
  77. </template>
  78. <script>
  79. var timer = null;
  80. let requestTask = null;
  81. import { BaseApi } from '../http/baseApi.js'
  82. import * as TextEncoding from "text-encoding-shim";
  83. import { webSocketConnect } from '@/common/xiaoZhiConnect.js';
  84. export default {
  85. data(){
  86. return {
  87. question:'',
  88. dialogList:[],
  89. commentShow:false,
  90. content:'',
  91. cindex:'',
  92. chzq:false,
  93. lwss:false,
  94. fixBottom: 0,
  95. // 对话
  96. config: {
  97. deviceMac: '',
  98. deviceName: '小智AI设备',
  99. clientId: 'uniapp_xiaozhi_client',
  100. token: 'xiaozhi-ai',
  101. otaUrl: 'http://i.ringzle.com:8002/xiaozhi/ota/',
  102. serverUrl: 'ws://i.ringzle.com:8000/xiaozhi/v1/'
  103. },
  104. isConnected: false,
  105. textMessage: '',
  106. socketTask: null,
  107. }
  108. },
  109. computed: {
  110. displayMac() {
  111. return this.config.deviceMac;
  112. },
  113. displayClient() {
  114. return this.config.clientId;
  115. }
  116. },
  117. onReady() {
  118. uni.onKeyboardHeightChange(res => {
  119. this.fixBottom = res.height||0;
  120. });
  121. },
  122. onLoad(option) {
  123. this.config.deviceMac = option?.deviceMac;
  124. this.connectToServer();
  125. },
  126. onUnload() {
  127. this.disconnectFromServer();
  128. uni.offKeyboardHeightChange();
  129. this.fixBottom = 0;
  130. },
  131. methods:{
  132. async connectToServer() {
  133. if (this.socketTask) return;
  134. console.log('开始连接流程...');
  135. const task = await webSocketConnect(
  136. this.config.otaUrl,
  137. this.config.serverUrl,
  138. this.config
  139. );
  140. if(task) {
  141. this.socketTask = task;
  142. this.setupSocketListeners();
  143. }
  144. },
  145. setupSocketListeners() {
  146. this.socketTask.onOpen(() => {
  147. this.isConnected = true;
  148. console.log('WebSocket连接已打开', 'success');
  149. this.sendHelloMessage();
  150. });
  151. this.socketTask.onClose(() => {
  152. this.isConnected = false;
  153. console.log('WebSocket连接已关闭', 'info');
  154. this.socketTask = null;
  155. });
  156. this.socketTask.onError((err) => {
  157. this.isConnected = false;
  158. console.log(`WebSocket错误: ${JSON.stringify(err)}`, 'error');
  159. });
  160. this.socketTask.onMessage((res) => {
  161. if (typeof res.data === 'string') {
  162. this.handleTextMessage(res.data);
  163. } else {
  164. this.handleBinaryMessage(res.data);
  165. }
  166. });
  167. },
  168. disconnectFromServer() {
  169. if (this.socketTask) {
  170. this.socketTask.close({
  171. code: 1000,
  172. reason: 'manual disconnection'
  173. });
  174. console.log('手动断开连接', 'info');
  175. }
  176. },
  177. sendSocketMessage(data) {
  178. if (!this.isConnected || !this.socketTask) {
  179. console.log('无法发送消息,连接未建立', 'error');
  180. return;
  181. }
  182. uni.sendSocketMessage({
  183. data: data,
  184. fail: (err) => {
  185. console.log(`发送消息失败: ${JSON.stringify(err)}`, 'error');
  186. }
  187. });
  188. },
  189. sendHelloMessage() {
  190. const helloMessage = {
  191. type: 'hello',
  192. device_id: this.config.deviceMac,
  193. device_name: this.config.deviceName,
  194. device_mac: this.config.deviceMac,
  195. token: this.config.token,
  196. features: { mcp: true }
  197. };
  198. console.log('发送hello握手消息', 'info');
  199. this.sendSocketMessage(JSON.stringify(helloMessage));
  200. },
  201. addConversationMessage(text, isUser = false) {
  202. console.log(text,isUser);
  203. text = text+'<br>'
  204. if(!isUser){
  205. let answer = this.dialogList[this.dialogList.length-1].answer+text?.replace(/(\r\n|\n|\r)+/g, '<br>');
  206. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  207. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  208. setTimeout(()=>{
  209. this.scrollToBottom();
  210. },100)
  211. }
  212. },
  213. sendTextMessage() {
  214. const message = this.question.trim();
  215. if (!message) return;
  216. const listenMessage = {
  217. type: 'listen',
  218. mode: 'manual',
  219. state: 'detect',
  220. text: message
  221. };
  222. this.sendSocketMessage(JSON.stringify(listenMessage));
  223. this.addConversationMessage(message, true);
  224. console.log(`发送文本: ${message}`, 'info');
  225. },
  226. handleTextMessage(jsonString) {
  227. try {
  228. const message = JSON.parse(jsonString);
  229. console.log(`收到文本消息: ${message.type}`, 'debug');
  230. switch (message.type) {
  231. case 'hello':
  232. console.log(`服务器回应: Session ID ${message.session_id}`, 'success');
  233. break;
  234. case 'tts':
  235. if(message.state === 'sentence_start' && message.text) {
  236. this.addConversationMessage(message.text);
  237. }
  238. break;
  239. case 'llm':
  240. if(message.text && message.text !== '😊') {
  241. this.addConversationMessage(message.text);
  242. }
  243. break;
  244. default:
  245. console.log(`收到消息: ${jsonString}`, 'info');
  246. }
  247. } catch (e) {
  248. console.log(`解析JSON失败: ${e.message}`, 'error');
  249. console.log(`原始数据: ${jsonString}`, 'debug');
  250. }
  251. },
  252. handleBinaryMessage(arrayBuffer) {
  253. console.log(`收到二进制数据,大小: ${arrayBuffer.byteLength}字节`, 'debug');
  254. },
  255. toBack(){
  256. uni.navigateBack();
  257. },
  258. startNewDialog(){
  259. uni.showModal({
  260. title:'温馨提示',
  261. content:'是否开启新的一轮对话?',
  262. success: (res) => {
  263. if(res.confirm){
  264. clearInterval(timer)
  265. this.dialogList = [];
  266. this.question = '';
  267. }
  268. }
  269. })
  270. },
  271. sendQuestion(){
  272. if(!this.question) return this.$showToast('请输入您的问题');
  273. if(!this.isLogin()) return
  274. let qa = {
  275. question:JSON.parse(JSON.stringify(this.question)),
  276. answer:'',
  277. copy:false,
  278. upvote:false,
  279. comment:'',
  280. share:false,
  281. think:true
  282. }
  283. this.dialogList = [...this.dialogList,...[qa]];
  284. this.$nextTick(()=>{
  285. this.scrollToBottom();
  286. this.sendTextMessage();
  287. this.question = '';
  288. })
  289. },
  290. // 滚动到底部
  291. scrollToBottom() {
  292. this.$nextTick(()=>{
  293. this.$nextTick(()=>{
  294. uni.pageScrollTo({
  295. scrollTop:99999,
  296. duration:300
  297. })
  298. })
  299. })
  300. },
  301. toCopy(item,index){
  302. uni.setClipboardData({
  303. data:item.answer,
  304. success: (res) => {
  305. this.$showToast('复制成功');
  306. },
  307. fail: (err) => {
  308. this.$showToast('复制失败');
  309. }
  310. })
  311. },
  312. toUpvote(item,index){
  313. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  314. },
  315. toComment(item,index){
  316. this.cindex = index;
  317. this.commentShow = true;
  318. },
  319. toShare(item,index){
  320. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  321. },
  322. commentConfirm(){
  323. this.$set(this.dialogList[this.cindex],'comment',this.content);
  324. this.commentShow = false;
  325. },
  326. commentCancel(){
  327. this.content = '';
  328. this.commentShow = false;
  329. },
  330. changeChzq(){
  331. this.chzq = !this.chzq;
  332. },
  333. changeLwss(){
  334. this.lwss = !this.lwss;
  335. },
  336. }
  337. }
  338. </script>
  339. <style scoped lang="scss">
  340. ::v-deep .u-textarea{
  341. border: none !important;
  342. padding: 0 !important;
  343. }
  344. ::v-deep .u-textarea textarea{
  345. min-height: 64rpx !important;
  346. }
  347. .page{
  348. background: #F6F6F8;
  349. padding: 0 30rpx 200rpx;
  350. box-sizing: border-box;
  351. .welcome{
  352. margin-top: 259rpx;
  353. padding: 0 34rpx;
  354. image{
  355. width: 88rpx;
  356. height: 87rpx;
  357. margin-left: 20rpx;
  358. }
  359. p{
  360. font-family: PingFang-SC, PingFang-SC;
  361. font-weight: bold;
  362. font-size: 36rpx;
  363. color: #252525;
  364. line-height: 36rpx;
  365. margin-top: 36rpx;
  366. &.tip{
  367. font-family: PingFangSC, PingFang SC;
  368. font-weight: 400;
  369. font-size: 26rpx;
  370. color: #646464;
  371. line-height: 40rpx;
  372. margin-top: 20rpx;
  373. }
  374. }
  375. }
  376. .dialogs{
  377. width: 100%;
  378. padding-top: 34rpx;
  379. box-sizing: border-box;
  380. overflow-y: auto;
  381. padding-bottom: 50rpx;
  382. .d_answer{
  383. margin-top: 40rpx;
  384. &.init{
  385. margin-top: 0;
  386. }
  387. .da_top{
  388. image{
  389. width: 48rpx;
  390. height: 48rpx;
  391. }
  392. text{
  393. font-family: PingFang-SC, PingFang-SC;
  394. font-weight: bold;
  395. font-size: 30rpx;
  396. color: #505050;
  397. line-height: 48rpx;
  398. margin-left: 20rpx;
  399. }
  400. }
  401. .da_content{
  402. padding: 30rpx 32rpx;
  403. margin-top: 20rpx;
  404. background: #FFFFFF;
  405. border-radius: 4rpx 24rpx 24rpx 24rpx;
  406. .dac_think{
  407. image{
  408. width: 40rpx;
  409. height: 40rpx;
  410. }
  411. text{
  412. font-size: 30rpx;
  413. margin-left: 10rpx;
  414. }
  415. }
  416. .dc_btns{
  417. margin-top: 44rpx;
  418. image{
  419. width: 48rpx;
  420. height: 48rpx;
  421. }
  422. .db_l{
  423. image{
  424. margin-right: 40rpx;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. .d_question{
  431. margin-top: 40rpx;
  432. display: flex;
  433. justify-content: flex-end;
  434. .dq_text{
  435. background: #D9F159;
  436. border-radius: 24rpx 4rpx 24rpx 24rpx;
  437. font-family: PingFangSC, PingFang SC;
  438. font-weight: 400;
  439. font-size: 28rpx;
  440. color: #252525;
  441. line-height: 40rpx;
  442. // text-align: right;
  443. padding: 26rpx 30rpx;
  444. }
  445. }
  446. }
  447. .ask_box{
  448. width: 100%;
  449. min-height: 176rpx;
  450. background: #F6F6F8;
  451. padding: 0 30rpx 60rpx;
  452. position: fixed;
  453. left: 0;
  454. box-sizing: border-box;
  455. .ask_memo{
  456. font-family: PingFangSC, PingFang SC;
  457. font-weight: 400;
  458. font-size: 24rpx;
  459. color: #b1b1b1;
  460. line-height: 34rpx;
  461. text-align: center;
  462. margin-top: 16rpx;
  463. }
  464. }
  465. .ask{
  466. min-height: 116rpx;
  467. background: #FFFFFF;
  468. border-radius: 24rpx;
  469. border: 2rpx solid #F0F2F8;
  470. padding: 24rpx;
  471. box-sizing: border-box;
  472. .a_inp{
  473. display: flex;
  474. align-items: flex-end;
  475. max-height: 300rpx;
  476. overflow-y: auto;
  477. .ai_l{
  478. width: calc(100% - 64rpx);
  479. padding-right: 20rpx;
  480. box-sizing: border-box;
  481. }
  482. .ai_r{
  483. width: 64rpx;
  484. image{
  485. width: 64rpx;
  486. height: 64rpx;
  487. }
  488. }
  489. }
  490. }
  491. }
  492. </style>