dialog.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <u-navbar title="创衡正念企业教练" bgColor="#FFFFFF" :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/06/16/ef50e3e8-d0f9-41dd-a4ec-546b157a1de9.png" style="width: 45rpx;height: 45rpx;" @tap="toBack"></image>
  6. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/e536bf8e-3b33-4ea8-b60b-7154c6353bec.png" style="width: 42rpx;height: 42rpx;margin-left: 40rpx;" @tap="startNewDialog"></image>
  7. </view>
  8. </u-navbar>
  9. <template v-if="dialogList.length===0">
  10. <div class="welcome">
  11. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/5292af4c-977b-4620-a4e4-4aadcd5ad092.png"></image>
  12. <p>Hi,我是你的AI团队教练助手~</p>
  13. <p class="tip">任何关于团队、分析报告、教练的问题,你都可以向我提问。我正在快速学习和进化中,有新功能时,我一定第一时间告诉你~ </p>
  14. </div>
  15. </template>
  16. <template v-else>
  17. <div class="dialogs container" ref="messageContainer">
  18. <div class="d_answer init">
  19. <div class="da_top adfac">
  20. <image src="@/static/logo.png"></image>
  21. <text>创衡正念企业教练</text>
  22. </div>
  23. <div class="da_content">Hi,任何关于团队、分析报告、教练的问题,你都可以向我提问~</div>
  24. </div>
  25. <div v-for="(item,index) in dialogList" :key="index">
  26. <div class="d_question">
  27. <div class="dq_text">{{ item.question }}</div>
  28. </div>
  29. <div class="d_answer">
  30. <div class="da_top adfac">
  31. <image src="@/static/logo.png"></image>
  32. <text>创衡正念企业教练</text>
  33. </div>
  34. <div class="da_content">
  35. <template v-if="item.think">
  36. <view class="dac_think adfac">
  37. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/07/13c5dd16-2032-464a-8b1c-2722d201cfe2.gif"></image>
  38. <text>正在思考中</text>
  39. </view>
  40. </template>
  41. <template v-else>
  42. <u-parse :content="item.answer"></u-parse>
  43. </template>
  44. <div class="dc_btns adfacjb" v-if="item.answer">
  45. <div class="db_l">
  46. <image :src="item.copy?require('@/static/copy_active.png'):require('@/static/copy.png')" @tap="toCopy(item,index)"></image>
  47. <image :src="item.upvote?require('@/static/upvote_active.png'):require('@/static/upvote.png')" @tap="toUpvote(item,index)"></image>
  48. <image :src="item.comment?require('@/static/comment_active.png'):require('@/static/comment.png')" @tap="toComment(item,index)"></image>
  49. </div>
  50. <div class="db_r">
  51. <!-- <image :src="item.share?require('@/static/share_active.png'):require('@/static/share.png')" @tap="toShare(item,index)"></image> -->
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </template>
  59. <div class="ask_box">
  60. <div class="ask">
  61. <div class="a_inp">
  62. <u-textarea v-model="question" placeholder="请输入您的问题" autoHeight @confirm="sendQuestion"></u-textarea>
  63. </div>
  64. <div class="a_btn adfacjb">
  65. <div class="ab_l adfac">
  66. <!-- <div class="al_pre adfacjc" :class="{'active':chzq}" @tap="changeChzq">
  67. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/451ac13c-5fdc-4d15-8f35-9f4d238e87c1.png" v-if="!chzq"></image>
  68. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/07/724764f9-3b36-4c90-89c9-cc51875c6767.png" v-else></image>
  69. <text>创衡增强</text>
  70. </div>
  71. <div class="al_pre adfacjc" :class="{'active':lwss}" @tap="changeLwss">
  72. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/d4668dcc-1d28-47ff-8bae-84dc6475794b.png" v-if="!lwss"></image>
  73. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/07/07/017f80b5-9421-4850-a659-38705e514e67.png" v-else></image>
  74. <text>联网搜索</text>
  75. </div> -->
  76. </div>
  77. <div class="ab_r">
  78. <!-- <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/caf6075c-0967-4c99-a269-ea453537075c.png"></image> -->
  79. <image @tap="sendQuestion" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/2429556b-54b7-4878-97c0-6b440b546ee4.png"></image>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. <u-modal :show="commentShow" title="评论" @confirm="commentConfirm" @cancel="commentCancel" @close="commentCancel" :showCancelButton="true">
  85. <u-textarea v-model="content" placeholder="对于我们的回答您是否不满意,您有更好的答案建议吗?请在此输入."></u-textarea>
  86. </u-modal>
  87. </view>
  88. </template>
  89. <script>
  90. var timer = null;
  91. let requestTask = null;
  92. import { BaseApi } from '../http/baseApi.js'
  93. import * as TextEncoding from "text-encoding-shim";
  94. export default {
  95. data(){
  96. return {
  97. isDialog:false,
  98. retryCount: 3, // 最大重试次数
  99. currentRetry: 0, // 当前重试次数
  100. isRequesting: false, // 请求状态锁
  101. question:'',
  102. streamingResponse:'',
  103. receivedData:'',
  104. dialogList:[],
  105. windex:0,
  106. commentShow:false,
  107. content:'',
  108. cindex:'',
  109. chzq:false,
  110. lwss:false,
  111. }
  112. },
  113. methods:{
  114. toBack(){
  115. uni.reLaunch({
  116. url:'/pages/home'
  117. })
  118. },
  119. startNewDialog(){
  120. clearInterval(timer)
  121. this.dialogList = [];
  122. this.question = '';
  123. this.streamingResponse = '';
  124. },
  125. // 封装带重试机制的请求方法
  126. async sendRequestWithRetry() {
  127. if (this.isRequesting) return;
  128. this.isRequesting = true;
  129. this.currentRetry = 0;
  130. try {
  131. await this._executeRequest();
  132. } catch (error) {
  133. this.$showToast('请求失败,请稍后重试')
  134. } finally {
  135. this.isRequesting = false;
  136. }
  137. },
  138. _executeRequest(){
  139. return new Promise((resolve, reject) => {
  140. this.$api.post('/core/chat/sendChatMessageStream',{query:this.question},false).then(res=>{
  141. if(res.data.code!==0) return this.$showToast(res.data.msg)
  142. let answer = this.dialogList[this.dialogList.length-1].answer+res.data.data;
  143. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  144. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  145. setTimeout(()=>{
  146. this.scrollToBottom();
  147. },100)
  148. resolve()
  149. })
  150. })
  151. },
  152. // 实际执行请求的方法
  153. _executeRequest2() {
  154. return new Promise((resolve, reject) => {
  155. requestTask = uni.request({
  156. url: `${BaseApi}/core/chat/sendChatMessageStream`,
  157. method: 'POST',
  158. timeout: 10000,
  159. data:{
  160. query: this.question,
  161. timestamp:Date.now()
  162. },
  163. header: {
  164. 'Content-Type': 'application/json',
  165. 'token': uni.getStorageSync('token') || ''
  166. },
  167. enableChunked: true, // 启用流式接收
  168. responseType:'text',
  169. success: (res) => {
  170. if (res.statusCode === 200) {
  171. this._handleSuccess(res.data);
  172. resolve();
  173. } else {
  174. this._handleError(`状态码异常: ${res.statusCode}`, resolve, reject);
  175. }
  176. },
  177. fail: (err) => {
  178. this._handleError(err.errMsg, resolve, reject);
  179. },
  180. complete: (com) => {
  181. console.log('请求完成',com)
  182. }
  183. });
  184. requestTask.onChunkReceived(async (res) => {
  185. const uint8Array = new Uint8Array(res.data);
  186. const decoder = new TextEncoding.TextDecoder("utf-8");
  187. const decodedString = decoder.decode(uint8Array);
  188. try {
  189. let newtext = decodedString.replaceAll('data:','')
  190. let ntArr = newtext.split(/\s+/);
  191. if(ntArr.length){
  192. ntArr.forEach(n=>{
  193. if(!n.trim()) return
  194. let answer = this.dialogList[this.dialogList.length-1].answer+n+'<br/>';
  195. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  196. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  197. // let nj = JSON.parse(n);
  198. // if(nj.event=='agent_message'){
  199. // let answer = this.dialogList[this.dialogList.length-1].answer+nj.answer;
  200. // this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  201. // this.$set(this.dialogList[this.dialogList.length-1],'id',nj.id);
  202. // this.$set(this.dialogList[this.dialogList.length-1],'task_id',nj.task_id);
  203. // this.$set(this.dialogList[this.dialogList.length-1],'message_id',nj.message_id);
  204. // this.$set(this.dialogList[this.dialogList.length-1],'conversation_id',nj.conversation_id);
  205. // this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  206. // }
  207. })
  208. setTimeout(()=>{
  209. this.scrollToBottom();
  210. },100)
  211. }
  212. } catch (e) {
  213. console.error('解析失败', e, '原始数据:', decodedString);
  214. }
  215. });
  216. });
  217. },
  218. // 成功处理
  219. _handleSuccess(data) {
  220. if (data) {
  221. this.streamingResponse += data;
  222. }
  223. this.currentRetry = 0; // 重置重试计数器
  224. },
  225. // 错误处理
  226. _handleError(errorMsg, resolve, reject) {
  227. if (this._shouldRetry(errorMsg)) {
  228. this.currentRetry++;
  229. setTimeout(() => {
  230. this._executeRequest().then(resolve).catch(reject);
  231. }, this._getRetryDelay());
  232. } else {
  233. reject(errorMsg);
  234. }
  235. },
  236. // 判断是否需要重试
  237. _shouldRetry(errorMsg) {
  238. const retryableErrors = [
  239. 'timeout',
  240. 'request:fail',
  241. 'Network Error'
  242. ];
  243. return this.currentRetry < this.retryCount && retryableErrors.some(e => errorMsg.includes(e));
  244. },
  245. // 获取指数退避延迟时间
  246. _getRetryDelay() {
  247. return Math.min(1000 * Math.pow(2, this.currentRetry), 10000);
  248. },
  249. sendQuestion(){
  250. if(!this.question) return this.$showToast('请输入您的问题');
  251. let qa = {
  252. question:JSON.parse(JSON.stringify(this.question)),
  253. answer:'',
  254. copy:false,
  255. upvote:false,
  256. comment:'',
  257. share:false,
  258. think:true
  259. }
  260. this.dialogList = [...this.dialogList,...[qa]];
  261. this.$nextTick(()=>{
  262. this.scrollToBottom();
  263. this.sendRequestWithRetry();
  264. this.question = '';
  265. })
  266. },
  267. // 滚动到底部
  268. scrollToBottom() {
  269. this.$nextTick(()=>{
  270. this.$nextTick(()=>{
  271. uni.pageScrollTo({
  272. scrollTop:99999,
  273. duration:300
  274. })
  275. })
  276. })
  277. },
  278. toCopy(item,index){
  279. uni.setClipboardData({
  280. data:item.answer,
  281. success: (res) => {
  282. this.$showToast('复制成功');
  283. },
  284. fail: (err) => {
  285. this.$showToast('复制失败');
  286. }
  287. })
  288. },
  289. toUpvote(item,index){
  290. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  291. },
  292. toComment(item,index){
  293. this.cindex = index;
  294. this.commentShow = true;
  295. },
  296. toShare(item,index){
  297. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  298. },
  299. commentConfirm(){
  300. this.$set(this.dialogList[this.cindex],'comment',this.content);
  301. this.commentShow = false;
  302. },
  303. commentCancel(){
  304. this.content = '';
  305. this.commentShow = false;
  306. },
  307. changeChzq(){
  308. this.chzq = !this.chzq;
  309. },
  310. changeLwss(){
  311. this.lwss = !this.lwss;
  312. },
  313. }
  314. }
  315. </script>
  316. <style scoped lang="scss">
  317. ::v-deep .u-textarea{
  318. border: none !important;
  319. padding: 0 !important;
  320. }
  321. ::v-deep .u-textarea textarea{
  322. min-height: 96rpx !important;
  323. }
  324. .page{
  325. background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);
  326. padding: 0 30rpx 302rpx;
  327. box-sizing: border-box;
  328. .welcome{
  329. margin-top: 259rpx;
  330. padding: 0 34rpx;
  331. image{
  332. width: 88rpx;
  333. height: 87rpx;
  334. margin-left: 20rpx;
  335. }
  336. p{
  337. font-family: PingFang-SC, PingFang-SC;
  338. font-weight: bold;
  339. font-size: 36rpx;
  340. color: #252525;
  341. line-height: 36rpx;
  342. margin-top: 36rpx;
  343. &.tip{
  344. font-family: PingFangSC, PingFang SC;
  345. font-weight: 400;
  346. font-size: 26rpx;
  347. color: #646464;
  348. line-height: 40rpx;
  349. margin-top: 20rpx;
  350. }
  351. }
  352. }
  353. .dialogs{
  354. width: 100%;
  355. padding-top: 34rpx;
  356. box-sizing: border-box;
  357. overflow-y: auto;
  358. .d_answer{
  359. margin-top: 40rpx;
  360. &.init{
  361. margin-top: 0;
  362. }
  363. .da_top{
  364. image{
  365. width: 48rpx;
  366. height: 48rpx;
  367. }
  368. text{
  369. font-family: PingFang-SC, PingFang-SC;
  370. font-weight: bold;
  371. font-size: 30rpx;
  372. color: #505050;
  373. line-height: 48rpx;
  374. margin-left: 20rpx;
  375. }
  376. }
  377. .da_content{
  378. padding: 30rpx 32rpx;
  379. margin-top: 20rpx;
  380. background: #FFFFFF;
  381. border-radius: 4rpx 24rpx 24rpx 24rpx;
  382. .dac_think{
  383. image{
  384. width: 40rpx;
  385. height: 40rpx;
  386. }
  387. text{
  388. font-size: 30rpx;
  389. margin-left: 10rpx;
  390. }
  391. }
  392. .dc_btns{
  393. margin-top: 44rpx;
  394. image{
  395. width: 48rpx;
  396. height: 48rpx;
  397. }
  398. .db_l{
  399. image{
  400. margin-right: 40rpx;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. .d_question{
  407. margin-top: 40rpx;
  408. display: flex;
  409. justify-content: flex-end;
  410. .dq_text{
  411. background: #833478;
  412. border-radius: 24rpx 4rpx 24rpx 24rpx;
  413. font-family: PingFangSC, PingFang SC;
  414. font-weight: 400;
  415. font-size: 30rpx;
  416. color: #FFFFFF;
  417. line-height: 48rpx;
  418. text-align: right;
  419. padding: 26rpx 30rpx;
  420. }
  421. }
  422. }
  423. .ask_box{
  424. width: 100%;
  425. height: 282rpx;
  426. background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);
  427. padding: 0 30rpx 72rpx;
  428. position: fixed;
  429. left: 0;
  430. bottom: 0;
  431. box-sizing: border-box;
  432. }
  433. .ask{
  434. height: 210rpx;
  435. background: #FFFFFF;
  436. border-radius: 24rpx;
  437. border: 2rpx solid #F0F2F8;
  438. display: flex;
  439. flex-direction: column;
  440. padding: 24rpx;
  441. box-sizing: border-box;
  442. .a_inp{
  443. flex: 1;
  444. overflow-y: auto;
  445. }
  446. .a_btn{
  447. padding-top: 20rpx;
  448. height: 54rpx;
  449. .ab_l{
  450. .al_pre{
  451. width: 162rpx;
  452. height: 54rpx;
  453. background: #FFFFFF;
  454. border-radius: 27rpx;
  455. border: 1rpx solid #E0E0E0;
  456. margin-right: 24rpx;
  457. image{
  458. width: 24rpx;
  459. height: 24rpx;
  460. }
  461. text{
  462. font-family: PingFangSC, PingFang SC;
  463. font-weight: 400;
  464. font-size: 22rpx;
  465. color: #393939;
  466. line-height: 22rpx;
  467. margin-left: 10rpx;
  468. }
  469. &.active{
  470. background: rgba(118,30,106,0.06);
  471. border-radius: 27px;
  472. border: 1px solid #761E6A;
  473. text{
  474. color: #761E6A;
  475. }
  476. }
  477. }
  478. }
  479. .ab_r{
  480. image{
  481. width: 48rpx;
  482. height: 48rpx;
  483. margin-left: 30rpx;
  484. }
  485. }
  486. }
  487. }
  488. }
  489. </style>