dialog.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. // 实际执行请求的方法
  139. _executeRequest() {
  140. return new Promise((resolve, reject) => {
  141. requestTask = uni.request({
  142. url: `${BaseApi}/core/chat/messages`,
  143. method: 'POST',
  144. timeout: 10000,
  145. data:{
  146. query: this.question,
  147. timestamp:Date.now()
  148. },
  149. header: {
  150. 'Content-Type': 'application/json',
  151. 'token': uni.getStorageSync('token') || ''
  152. },
  153. enableChunked: true, // 启用流式接收
  154. responseType:'text',
  155. success: (res) => {
  156. if (res.statusCode === 200) {
  157. this._handleSuccess(res.data);
  158. resolve();
  159. } else {
  160. this._handleError(`状态码异常: ${res.statusCode}`, resolve, reject);
  161. }
  162. },
  163. fail: (err) => {
  164. this._handleError(err.errMsg, resolve, reject);
  165. },
  166. complete: (com) => {
  167. console.log('请求完成',com)
  168. }
  169. });
  170. requestTask.onChunkReceived(async (res) => {
  171. const uint8Array = new Uint8Array(res.data);
  172. const decoder = new TextEncoding.TextDecoder("utf-8");
  173. const decodedString = decoder.decode(uint8Array);
  174. try {
  175. let newtext = decodedString.replaceAll('data:','')
  176. let ntArr = newtext.split(/\s+/);
  177. if(ntArr.length){
  178. ntArr.forEach(n=>{
  179. if(!n.trim()) return
  180. let nj = JSON.parse(n);
  181. if(nj.event=='agent_message'){
  182. let answer = this.dialogList[this.dialogList.length-1].answer+nj.answer;
  183. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  184. this.$set(this.dialogList[this.dialogList.length-1],'id',nj.id);
  185. this.$set(this.dialogList[this.dialogList.length-1],'task_id',nj.task_id);
  186. this.$set(this.dialogList[this.dialogList.length-1],'message_id',nj.message_id);
  187. this.$set(this.dialogList[this.dialogList.length-1],'conversation_id',nj.conversation_id);
  188. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  189. }
  190. })
  191. setTimeout(()=>{
  192. this.scrollToBottom();
  193. },100)
  194. }
  195. } catch (e) {
  196. console.error('解析失败', e, '原始数据:', decodedString);
  197. }
  198. });
  199. });
  200. },
  201. // 成功处理
  202. _handleSuccess(data) {
  203. if (data) {
  204. this.streamingResponse += data;
  205. }
  206. this.currentRetry = 0; // 重置重试计数器
  207. },
  208. // 错误处理
  209. _handleError(errorMsg, resolve, reject) {
  210. if (this._shouldRetry(errorMsg)) {
  211. this.currentRetry++;
  212. setTimeout(() => {
  213. this._executeRequest().then(resolve).catch(reject);
  214. }, this._getRetryDelay());
  215. } else {
  216. reject(errorMsg);
  217. }
  218. },
  219. // 判断是否需要重试
  220. _shouldRetry(errorMsg) {
  221. const retryableErrors = [
  222. 'timeout',
  223. 'request:fail',
  224. 'Network Error'
  225. ];
  226. return this.currentRetry < this.retryCount && retryableErrors.some(e => errorMsg.includes(e));
  227. },
  228. // 获取指数退避延迟时间
  229. _getRetryDelay() {
  230. return Math.min(1000 * Math.pow(2, this.currentRetry), 10000);
  231. },
  232. sendQuestion(){
  233. if(!this.question) return this.$showToast('请输入您的问题');
  234. let qa = {
  235. question:JSON.parse(JSON.stringify(this.question)),
  236. answer:'',
  237. copy:false,
  238. upvote:false,
  239. comment:'',
  240. share:false,
  241. think:true
  242. }
  243. this.dialogList = [...this.dialogList,...[qa]];
  244. this.$nextTick(()=>{
  245. this.scrollToBottom();
  246. this.sendRequestWithRetry();
  247. this.question = '';
  248. })
  249. },
  250. // 滚动到底部
  251. scrollToBottom() {
  252. this.$nextTick(()=>{
  253. this.$nextTick(()=>{
  254. uni.pageScrollTo({
  255. scrollTop:99999,
  256. duration:300
  257. })
  258. })
  259. })
  260. },
  261. toCopy(item,index){
  262. uni.setClipboardData({
  263. data:item.answer,
  264. success: (res) => {
  265. this.$showToast('复制成功');
  266. },
  267. fail: (err) => {
  268. this.$showToast('复制失败');
  269. }
  270. })
  271. },
  272. toUpvote(item,index){
  273. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  274. },
  275. toComment(item,index){
  276. this.cindex = index;
  277. this.commentShow = true;
  278. },
  279. toShare(item,index){
  280. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  281. },
  282. commentConfirm(){
  283. this.$set(this.dialogList[this.cindex],'comment',this.content);
  284. this.commentShow = false;
  285. },
  286. commentCancel(){
  287. this.content = '';
  288. this.commentShow = false;
  289. },
  290. changeChzq(){
  291. this.chzq = !this.chzq;
  292. },
  293. changeLwss(){
  294. this.lwss = !this.lwss;
  295. },
  296. }
  297. }
  298. </script>
  299. <style scoped lang="scss">
  300. ::v-deep .u-textarea{
  301. border: none !important;
  302. padding: 0 !important;
  303. }
  304. ::v-deep .u-textarea textarea{
  305. min-height: 96rpx !important;
  306. }
  307. .page{
  308. background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);
  309. padding: 0 30rpx 302rpx;
  310. box-sizing: border-box;
  311. .welcome{
  312. margin-top: 259rpx;
  313. padding: 0 34rpx;
  314. image{
  315. width: 88rpx;
  316. height: 87rpx;
  317. margin-left: 20rpx;
  318. }
  319. p{
  320. font-family: PingFang-SC, PingFang-SC;
  321. font-weight: bold;
  322. font-size: 36rpx;
  323. color: #252525;
  324. line-height: 36rpx;
  325. margin-top: 36rpx;
  326. &.tip{
  327. font-family: PingFangSC, PingFang SC;
  328. font-weight: 400;
  329. font-size: 26rpx;
  330. color: #646464;
  331. line-height: 40rpx;
  332. margin-top: 20rpx;
  333. }
  334. }
  335. }
  336. .dialogs{
  337. width: 100%;
  338. padding-top: 34rpx;
  339. box-sizing: border-box;
  340. overflow-y: auto;
  341. .d_answer{
  342. margin-top: 40rpx;
  343. &.init{
  344. margin-top: 0;
  345. }
  346. .da_top{
  347. image{
  348. width: 48rpx;
  349. height: 48rpx;
  350. }
  351. text{
  352. font-family: PingFang-SC, PingFang-SC;
  353. font-weight: bold;
  354. font-size: 30rpx;
  355. color: #505050;
  356. line-height: 48rpx;
  357. margin-left: 20rpx;
  358. }
  359. }
  360. .da_content{
  361. padding: 30rpx 32rpx;
  362. margin-top: 20rpx;
  363. background: #FFFFFF;
  364. border-radius: 4rpx 24rpx 24rpx 24rpx;
  365. .dac_think{
  366. image{
  367. width: 40rpx;
  368. height: 40rpx;
  369. }
  370. text{
  371. font-size: 30rpx;
  372. margin-left: 10rpx;
  373. }
  374. }
  375. .dc_btns{
  376. margin-top: 44rpx;
  377. image{
  378. width: 48rpx;
  379. height: 48rpx;
  380. }
  381. .db_l{
  382. image{
  383. margin-right: 40rpx;
  384. }
  385. }
  386. }
  387. }
  388. }
  389. .d_question{
  390. margin-top: 40rpx;
  391. display: flex;
  392. justify-content: flex-end;
  393. .dq_text{
  394. background: #833478;
  395. border-radius: 24rpx 4rpx 24rpx 24rpx;
  396. font-family: PingFangSC, PingFang SC;
  397. font-weight: 400;
  398. font-size: 30rpx;
  399. color: #FFFFFF;
  400. line-height: 48rpx;
  401. text-align: right;
  402. padding: 26rpx 30rpx;
  403. }
  404. }
  405. }
  406. .ask_box{
  407. width: 100%;
  408. height: 282rpx;
  409. background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);
  410. padding: 0 30rpx 72rpx;
  411. position: fixed;
  412. left: 0;
  413. bottom: 0;
  414. box-sizing: border-box;
  415. }
  416. .ask{
  417. height: 210rpx;
  418. background: #FFFFFF;
  419. border-radius: 24rpx;
  420. border: 2rpx solid #F0F2F8;
  421. display: flex;
  422. flex-direction: column;
  423. padding: 24rpx;
  424. box-sizing: border-box;
  425. .a_inp{
  426. flex: 1;
  427. overflow-y: auto;
  428. }
  429. .a_btn{
  430. padding-top: 20rpx;
  431. height: 54rpx;
  432. .ab_l{
  433. .al_pre{
  434. width: 162rpx;
  435. height: 54rpx;
  436. background: #FFFFFF;
  437. border-radius: 27rpx;
  438. border: 1rpx solid #E0E0E0;
  439. margin-right: 24rpx;
  440. image{
  441. width: 24rpx;
  442. height: 24rpx;
  443. }
  444. text{
  445. font-family: PingFangSC, PingFang SC;
  446. font-weight: 400;
  447. font-size: 22rpx;
  448. color: #393939;
  449. line-height: 22rpx;
  450. margin-left: 10rpx;
  451. }
  452. &.active{
  453. background: rgba(118,30,106,0.06);
  454. border-radius: 27px;
  455. border: 1px solid #761E6A;
  456. text{
  457. color: #761E6A;
  458. }
  459. }
  460. }
  461. }
  462. .ab_r{
  463. image{
  464. width: 48rpx;
  465. height: 48rpx;
  466. margin-left: 30rpx;
  467. }
  468. }
  469. }
  470. }
  471. }
  472. </style>