dialog.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <u-navbar title="创衡汇教练AI智能体" 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>创衡汇教练AI智能体</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>创衡汇教练AI智能体</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" :style="{'bottom':fixBottom+'px'}">
  60. <div class="ask">
  61. <div class="a_inp">
  62. <div class="ai_l">
  63. <u-textarea v-model="question" placeholder="请输入您的问题" autoHeight :showConfirmBar="false" :adjustPosition="false" maxlength="999999"></u-textarea>
  64. </div>
  65. <div class="ai_r">
  66. <image @tap="sendQuestion" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/2429556b-54b7-4878-97c0-6b440b546ee4.png"></image>
  67. </div>
  68. </div>
  69. </div>
  70. <div class="ask_memo">本服务为AI生成内容,结果仅供参考</div>
  71. </div>
  72. <u-modal :show="commentShow" title="评论" @confirm="commentConfirm" @cancel="commentCancel" @close="commentCancel" :showCancelButton="true">
  73. <u-textarea v-model="content" placeholder="对于我们的回答您是否不满意,您有更好的答案建议吗?请在此输入."></u-textarea>
  74. </u-modal>
  75. </view>
  76. </template>
  77. <script>
  78. var timer = null;
  79. let requestTask = null;
  80. import { BaseApi } from '../http/baseApi.js'
  81. import * as TextEncoding from "text-encoding-shim";
  82. export default {
  83. data(){
  84. return {
  85. isDialog:false,
  86. retryCount: 3, // 最大重试次数
  87. currentRetry: 0, // 当前重试次数
  88. isRequesting: false, // 请求状态锁
  89. question:'',
  90. streamingResponse:'',
  91. receivedData:'',
  92. dialogList:[],
  93. windex:0,
  94. commentShow:false,
  95. content:'',
  96. cindex:'',
  97. chzq:false,
  98. lwss:false,
  99. fixBottom: 0
  100. }
  101. },
  102. onReady() {
  103. uni.onKeyboardHeightChange(res => {
  104. this.fixBottom = res.height||0;
  105. });
  106. },
  107. onUnload() {
  108. uni.offKeyboardHeightChange();
  109. this.fixBottom = 0;
  110. },
  111. methods:{
  112. toBack(){
  113. uni.reLaunch({
  114. url:'/pages/home'
  115. })
  116. },
  117. startNewDialog(){
  118. clearInterval(timer)
  119. this.dialogList = [];
  120. this.question = '';
  121. this.streamingResponse = '';
  122. },
  123. // 封装带重试机制的请求方法
  124. async sendRequestWithRetry() {
  125. if (this.isRequesting) return;
  126. this.isRequesting = true;
  127. this.currentRetry = 0;
  128. try {
  129. await this._executeRequest();
  130. } catch (error) {
  131. this.$showToast('请求失败,请稍后重试')
  132. } finally {
  133. this.isRequesting = false;
  134. }
  135. },
  136. _executeRequest2(){
  137. return new Promise((resolve, reject) => {
  138. this.$api.post('/core/chat/sendChatMessageStream',{query:this.question},false).then(res=>{
  139. if(res.data.code!==0) return this.$showToast(res.data.msg)
  140. let answer = this.dialogList[this.dialogList.length-1].answer+res.data.data;
  141. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  142. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  143. setTimeout(()=>{
  144. this.scrollToBottom();
  145. },100)
  146. resolve()
  147. })
  148. })
  149. },
  150. // 实际执行请求的方法
  151. _executeRequest() {
  152. return new Promise((resolve, reject) => {
  153. requestTask = uni.request({
  154. url: `${BaseApi}/core/chat/sendChatMessageStream`,
  155. method: 'POST',
  156. timeout: 10000,
  157. data:{
  158. query: this.question,
  159. identity:'被教练者',
  160. },
  161. header: {
  162. 'Content-Type': 'application/json',
  163. 'token': uni.getStorageSync('token') || ''
  164. },
  165. enableChunked: true, // 启用流式接收
  166. responseType:'text',
  167. success: (res) => {
  168. if (res.statusCode === 200) {
  169. this._handleSuccess(res.data);
  170. resolve();
  171. } else {
  172. this._handleError(`状态码异常: ${res.statusCode}`, resolve, reject);
  173. }
  174. },
  175. fail: (err) => {
  176. this._handleError(err.errMsg, resolve, reject);
  177. },
  178. complete: (com) => {
  179. console.log('请求完成',com)
  180. }
  181. });
  182. requestTask.onChunkReceived(async (res) => {
  183. const uint8Array = new Uint8Array(res.data);
  184. const decoder = new TextEncoding.TextDecoder("utf-8");
  185. const decodedString = decoder.decode(uint8Array);
  186. try {
  187. let newtext = decodedString.replaceAll('data:','').replaceAll(':keepAlive','');
  188. let ntArr = newtext.split('\n\n');
  189. if(ntArr.length){
  190. ntArr.forEach(n=>{
  191. if(!n.trim()) return
  192. let nj = JSON.parse(n);
  193. if(nj.event=='message'){
  194. let answer = this.dialogList[this.dialogList.length-1].answer+nj.answer?.replace(/(\r\n|\n|\r)+/g, '<br>');
  195. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  196. this.$set(this.dialogList[this.dialogList.length-1],'id',nj.id);
  197. this.$set(this.dialogList[this.dialogList.length-1],'task_id',nj.task_id);
  198. this.$set(this.dialogList[this.dialogList.length-1],'message_id',nj.message_id);
  199. this.$set(this.dialogList[this.dialogList.length-1],'conversation_id',nj.conversation_id);
  200. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  201. }
  202. })
  203. setTimeout(()=>{
  204. this.scrollToBottom();
  205. },100)
  206. }
  207. } catch (e) {
  208. console.error('解析失败', e, '原始数据:', decodedString);
  209. }
  210. });
  211. });
  212. },
  213. // 成功处理
  214. _handleSuccess(data) {
  215. if (data) {
  216. this.streamingResponse += data;
  217. }
  218. this.currentRetry = 0; // 重置重试计数器
  219. },
  220. // 错误处理
  221. _handleError(errorMsg, resolve, reject) {
  222. if (this._shouldRetry(errorMsg)) {
  223. this.currentRetry++;
  224. setTimeout(() => {
  225. this._executeRequest().then(resolve).catch(reject);
  226. }, this._getRetryDelay());
  227. } else {
  228. reject(errorMsg);
  229. }
  230. },
  231. // 判断是否需要重试
  232. _shouldRetry(errorMsg) {
  233. const retryableErrors = [
  234. 'timeout',
  235. 'request:fail',
  236. 'Network Error'
  237. ];
  238. return this.currentRetry < this.retryCount && retryableErrors.some(e => errorMsg.includes(e));
  239. },
  240. // 获取指数退避延迟时间
  241. _getRetryDelay() {
  242. return Math.min(1000 * Math.pow(2, this.currentRetry), 10000);
  243. },
  244. sendQuestion(){
  245. if(!this.question) return this.$showToast('请输入您的问题');
  246. if(!this.isLogin()) return
  247. let qa = {
  248. question:JSON.parse(JSON.stringify(this.question)),
  249. answer:'',
  250. copy:false,
  251. upvote:false,
  252. comment:'',
  253. share:false,
  254. think:true
  255. }
  256. this.dialogList = [...this.dialogList,...[qa]];
  257. this.$nextTick(()=>{
  258. this.scrollToBottom();
  259. this.sendRequestWithRetry();
  260. this.question = '';
  261. })
  262. },
  263. // 滚动到底部
  264. scrollToBottom() {
  265. this.$nextTick(()=>{
  266. this.$nextTick(()=>{
  267. uni.pageScrollTo({
  268. scrollTop:99999,
  269. duration:300
  270. })
  271. })
  272. })
  273. },
  274. toCopy(item,index){
  275. uni.setClipboardData({
  276. data:item.answer,
  277. success: (res) => {
  278. this.$showToast('复制成功');
  279. },
  280. fail: (err) => {
  281. this.$showToast('复制失败');
  282. }
  283. })
  284. },
  285. toUpvote(item,index){
  286. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  287. },
  288. toComment(item,index){
  289. this.cindex = index;
  290. this.commentShow = true;
  291. },
  292. toShare(item,index){
  293. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  294. },
  295. commentConfirm(){
  296. this.$set(this.dialogList[this.cindex],'comment',this.content);
  297. this.commentShow = false;
  298. },
  299. commentCancel(){
  300. this.content = '';
  301. this.commentShow = false;
  302. },
  303. changeChzq(){
  304. this.chzq = !this.chzq;
  305. },
  306. changeLwss(){
  307. this.lwss = !this.lwss;
  308. },
  309. }
  310. }
  311. </script>
  312. <style scoped lang="scss">
  313. ::v-deep .u-textarea{
  314. border: none !important;
  315. padding: 0 !important;
  316. }
  317. ::v-deep .u-textarea textarea{
  318. min-height: 64rpx !important;
  319. }
  320. .page{
  321. background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);
  322. padding: 0 30rpx 200rpx;
  323. box-sizing: border-box;
  324. .welcome{
  325. margin-top: 259rpx;
  326. padding: 0 34rpx;
  327. image{
  328. width: 88rpx;
  329. height: 87rpx;
  330. margin-left: 20rpx;
  331. }
  332. p{
  333. font-family: PingFang-SC, PingFang-SC;
  334. font-weight: bold;
  335. font-size: 36rpx;
  336. color: #252525;
  337. line-height: 36rpx;
  338. margin-top: 36rpx;
  339. &.tip{
  340. font-family: PingFangSC, PingFang SC;
  341. font-weight: 400;
  342. font-size: 26rpx;
  343. color: #646464;
  344. line-height: 40rpx;
  345. margin-top: 20rpx;
  346. }
  347. }
  348. }
  349. .dialogs{
  350. width: 100%;
  351. padding-top: 34rpx;
  352. box-sizing: border-box;
  353. overflow-y: auto;
  354. .d_answer{
  355. margin-top: 40rpx;
  356. &.init{
  357. margin-top: 0;
  358. }
  359. .da_top{
  360. image{
  361. width: 48rpx;
  362. height: 48rpx;
  363. }
  364. text{
  365. font-family: PingFang-SC, PingFang-SC;
  366. font-weight: bold;
  367. font-size: 30rpx;
  368. color: #505050;
  369. line-height: 48rpx;
  370. margin-left: 20rpx;
  371. }
  372. }
  373. .da_content{
  374. padding: 30rpx 32rpx;
  375. margin-top: 20rpx;
  376. background: #FFFFFF;
  377. border-radius: 4rpx 24rpx 24rpx 24rpx;
  378. .dac_think{
  379. image{
  380. width: 40rpx;
  381. height: 40rpx;
  382. }
  383. text{
  384. font-size: 30rpx;
  385. margin-left: 10rpx;
  386. }
  387. }
  388. .dc_btns{
  389. margin-top: 44rpx;
  390. image{
  391. width: 48rpx;
  392. height: 48rpx;
  393. }
  394. .db_l{
  395. image{
  396. margin-right: 40rpx;
  397. }
  398. }
  399. }
  400. }
  401. }
  402. .d_question{
  403. margin-top: 40rpx;
  404. display: flex;
  405. justify-content: flex-end;
  406. .dq_text{
  407. background: #833478;
  408. border-radius: 24rpx 4rpx 24rpx 24rpx;
  409. font-family: PingFangSC, PingFang SC;
  410. font-weight: 400;
  411. font-size: 30rpx;
  412. color: #FFFFFF;
  413. line-height: 48rpx;
  414. // text-align: right;
  415. padding: 26rpx 30rpx;
  416. }
  417. }
  418. }
  419. .ask_box{
  420. width: 100%;
  421. min-height: 176rpx;
  422. background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);
  423. padding: 0 30rpx 60rpx;
  424. position: fixed;
  425. left: 0;
  426. box-sizing: border-box;
  427. .ask_memo{
  428. font-family: PingFangSC, PingFang SC;
  429. font-weight: 400;
  430. font-size: 24rpx;
  431. color: #b1b1b1;
  432. line-height: 34rpx;
  433. text-align: center;
  434. margin-top: 16rpx;
  435. }
  436. }
  437. .ask{
  438. min-height: 116rpx;
  439. background: #FFFFFF;
  440. border-radius: 24rpx;
  441. border: 2rpx solid #F0F2F8;
  442. padding: 24rpx;
  443. box-sizing: border-box;
  444. .a_inp{
  445. display: flex;
  446. align-items: flex-end;
  447. max-height: 300rpx;
  448. overflow-y: auto;
  449. .ai_l{
  450. width: calc(100% - 64rpx);
  451. padding-right: 20rpx;
  452. box-sizing: border-box;
  453. }
  454. .ai_r{
  455. width: 64rpx;
  456. image{
  457. width: 64rpx;
  458. height: 64rpx;
  459. }
  460. }
  461. }
  462. }
  463. }
  464. </style>