dialog.vue 13 KB

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