dialog.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px', 'padding-bottom': 'calc(240rpx + ' + fixBottom + 'px)'}">
  3. <u-navbar title="创衡正念企业教练AI智能体" bgColor="#E9F4F6" :titleStyle="{'font-size':'32rpx','font-weight':'bold','color':'#002846'}">
  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. <view 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,我是大创教练~</p>
  13. <p class="tip">欢迎与我探讨任何关于团队教练、PERILL团队发展动态评估报告或团队发展的话题。我的对话能力正在进化中,希望能给你的团队教练辅导一些启发~</p>
  14. </view>
  15. </template>
  16. <template v-else>
  17. <view class="dialogs container" ref="messageContainer">
  18. <view class="d_answer init">
  19. <view class="da_top adfac">
  20. <image src="@/static/logo.png"></image>
  21. <text>大创教练</text>
  22. </view>
  23. <view class="da_content">Hi,任何关于团队、分析报告、教练的问题,你都可以向我提问~</view>
  24. </view>
  25. <view v-for="(item,index) in dialogList" :key="index">
  26. <view class="d_question">
  27. <view class="dq_text">{{ item.question }}</view>
  28. </view>
  29. <view class="d_answer">
  30. <view class="da_top adfac">
  31. <image src="@/static/logo.png"></image>
  32. <text>大创教练</text>
  33. </view>
  34. <view class="da_content">
  35. <template v-if="item.think">
  36. <view class="dac_think adfac">
  37. <image src="https://oss.familydaf.cn/sxsnfile/20260129/ce681f9923134f5bbf43654234e15367.gif"></image>
  38. <text v-if="!item.fail">让我思考一下,请稍等~</text>
  39. <text v-else>{{item.errMsg}}</text>
  40. </view>
  41. </template>
  42. <template v-else>
  43. <view style="font-size: 30rpx;color: #002846;line-height: 54rpx;">
  44. <u-parse :content="item.answer"></u-parse>
  45. </view>
  46. </template>
  47. <view class="dc_btns adfacjb" v-if="item.answer">
  48. <view class="db_l">
  49. <image :src="item.copy?require('@/static/copy_active.png'):require('@/static/copy.png')" @tap="toCopy(item,index)"></image>
  50. <image :src="item.upvote?require('@/static/upvote_active.png'):require('@/static/upvote.png')" @tap="toUpvote(item,index)"></image>
  51. <!-- <image :src="item.comment?require('@/static/comment_active.png'):require('@/static/comment.png')" @tap="toComment(item,index)"></image> -->
  52. </view>
  53. <view class="db_r">
  54. <!-- <image :src="item.share?require('@/static/share_active.png'):require('@/static/share.png')" @tap="toShare(item,index)"></image> -->
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. </template>
  62. <view class="ask_box" :style="{'bottom':fixBottom+'px'}">
  63. <view class="ask">
  64. <view class="a_inp">
  65. <view class="ai_l">
  66. <u-textarea v-model="question" placeholder="请输入你的问题" placeholder-style="font-size:30rpx;color:#95A5B1;" @confirm="sendQuestion" autoHeight :showConfirmBar="false" :adjustPosition="false" maxlength="999999"></u-textarea>
  67. </view>
  68. <view class="ai_r">
  69. <image @tap="sendQuestion" :src="imgBase+'dialog_send.png'"></image>
  70. </view>
  71. </view>
  72. </view>
  73. <view class="ask_memo">本服务为AI生成内容,结果仅供参考</view>
  74. </view>
  75. <u-modal :show="commentShow" title="评论" @confirm="commentConfirm" @cancel="commentCancel" @close="commentCancel" :showCancelButton="true">
  76. <u-textarea v-model="content" placeholder="对于我们的回答你是否不满意,你有更好的答案建议吗?请在此输入."></u-textarea>
  77. </u-modal>
  78. </view>
  79. </template>
  80. <script>
  81. var timer = null;
  82. let requestTask = null;
  83. import { BaseApi } from '../http/baseApi.js'
  84. import * as TextEncoding from "text-encoding-shim";
  85. export default {
  86. data(){
  87. return {
  88. isDialog:false,
  89. retryCount: 3, // 最大重试次数
  90. currentRetry: 0, // 当前重试次数
  91. isRequesting: false, // 请求状态锁
  92. question:'',
  93. conversation_id:'',
  94. streamingResponse:'',
  95. receivedData:'',
  96. dialogList:[],
  97. windex:0,
  98. commentShow:false,
  99. content:'',
  100. cindex:'',
  101. chzq:false,
  102. lwss:false,
  103. fixBottom: 0
  104. }
  105. },
  106. onReady() {
  107. uni.onKeyboardHeightChange(res => {
  108. this.fixBottom = res.height||0;
  109. if(this.fixBottom>0){
  110. this.scrollToBottom()
  111. }
  112. });
  113. },
  114. onUnload() {
  115. uni.offKeyboardHeightChange();
  116. this.fixBottom = 0;
  117. },
  118. methods:{
  119. toBack(){
  120. uni.reLaunch({
  121. url:'/pages/home'
  122. })
  123. },
  124. startNewDialog(){
  125. clearInterval(timer)
  126. this.dialogList = [];
  127. this.question = '';
  128. this.streamingResponse = '';
  129. },
  130. // 封装带重试机制的请求方法
  131. async sendRequestWithRetry() {
  132. if (this.isRequesting) return;
  133. this.isRequesting = true;
  134. this.currentRetry = 0;
  135. try {
  136. await this._executeRequest();
  137. } catch (error) {
  138. this.$showToast('请求失败,请稍后重试')
  139. } finally {
  140. this.isRequesting = false;
  141. }
  142. },
  143. _executeRequest2(){
  144. return new Promise((resolve, reject) => {
  145. this.$api.post('/core/chat/sendChatMessageStream',{query:this.question},false).then(res=>{
  146. if(res.data.code!==0) return this.$showToast(res.data.msg)
  147. let answer = this.dialogList[this.dialogList.length-1].answer+res.data.data;
  148. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  149. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  150. setTimeout(()=>{
  151. this.scrollToBottom();
  152. },100)
  153. resolve()
  154. })
  155. })
  156. },
  157. // 实际执行请求的方法
  158. _executeRequest() {
  159. return new Promise((resolve, reject) => {
  160. requestTask = uni.request({
  161. url: `${BaseApi}/core/chat/sendChatMessageStream`,
  162. method: 'POST',
  163. timeout: 20000,
  164. data:{
  165. query: this.question,
  166. identity:'被教练者',
  167. conversation_id: this.conversation_id
  168. },
  169. header: {
  170. 'Content-Type': 'application/json',
  171. 'token': uni.getStorageSync('token') || ''
  172. },
  173. enableChunked: true, // 启用流式接收
  174. responseType:'text',
  175. success: (res) => {
  176. if (res.statusCode === 200) {
  177. this._handleSuccess(res.data);
  178. resolve();
  179. } else {
  180. this._handleError(`状态码异常: ${res.statusCode}`, resolve, reject);
  181. }
  182. },
  183. fail: (err) => {
  184. // if(err.errMsg.indexOf('time out')>-1||err.errMsg.indexOf('timeout')>-1) this.$showToast('请求超时,请稍后再试')
  185. this._handleError(err.errMsg, resolve, reject);
  186. },
  187. complete: (com) => {
  188. console.log('请求完成',com)
  189. }
  190. });
  191. requestTask.onChunkReceived(async (res) => {
  192. const uint8Array = new Uint8Array(res.data);
  193. const decoder = new TextEncoding.TextDecoder("utf-8");
  194. const decodedString = decoder.decode(uint8Array);
  195. if(decodedString.indexOf('"code":500')>-1){
  196. this.$set(this.dialogList[this.dialogList.length-1],'fail',true);
  197. const dsJson = JSON.parse(decodedString);
  198. this.$set(this.dialogList[this.dialogList.length-1],'errMsg',dsJson?(dsJson.msg||'很抱歉,当前网络异常,请稍后再试~'):'很抱歉,当前网络异常,请稍后再试~');
  199. }
  200. try {
  201. let newtext = decodedString.replaceAll('data:','').replaceAll(':keepAlive','');
  202. let ntArr = newtext.split('\n\n');
  203. if(ntArr.length){
  204. ntArr.forEach(n=>{
  205. if(!n.trim()) return
  206. let nj = JSON.parse(n);
  207. if(nj.event=='message'){
  208. this.conversation_id = nj.conversation_id;
  209. let answer = this.dialogList[this.dialogList.length-1].answer+nj.answer?.replace(/(\r\n|\n|\r)+/g, '<br>').replace(/<think>.*?<\/think>/gs, '');
  210. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  211. this.$set(this.dialogList[this.dialogList.length-1],'id',nj.id);
  212. this.$set(this.dialogList[this.dialogList.length-1],'task_id',nj.task_id);
  213. this.$set(this.dialogList[this.dialogList.length-1],'message_id',nj.message_id);
  214. this.$set(this.dialogList[this.dialogList.length-1],'conversation_id',nj.conversation_id);
  215. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  216. this.$set(this.dialogList[this.dialogList.length-1],'fail',false);
  217. this.$set(this.dialogList[this.dialogList.length-1],'errMsg','');
  218. }
  219. })
  220. setTimeout(()=>{
  221. this.scrollToBottom();
  222. },100)
  223. }
  224. } catch (e) {
  225. console.error('解析失败', e, '原始数据:', decodedString);
  226. }
  227. });
  228. });
  229. },
  230. // 成功处理
  231. _handleSuccess(data) {
  232. if (data) {
  233. this.streamingResponse += data;
  234. }
  235. this.currentRetry = 0; // 重置重试计数器
  236. },
  237. // 错误处理
  238. _handleError(errorMsg, resolve, reject) {
  239. if (this._shouldRetry(errorMsg)) {
  240. this.currentRetry++;
  241. setTimeout(() => {
  242. this._executeRequest().then(resolve).catch(reject);
  243. }, this._getRetryDelay());
  244. } else {
  245. reject(errorMsg);
  246. }
  247. },
  248. // 判断是否需要重试
  249. _shouldRetry(errorMsg) {
  250. const retryableErrors = [
  251. 'timeout',
  252. 'request:fail',
  253. 'Network Error'
  254. ];
  255. return this.currentRetry < this.retryCount && retryableErrors.some(e => errorMsg.includes(e));
  256. },
  257. // 获取指数退避延迟时间
  258. _getRetryDelay() {
  259. return Math.min(1000 * Math.pow(2, this.currentRetry), 10000);
  260. },
  261. sendQuestion(){
  262. if(!this.question) return this.$showToast('请输入你的问题');
  263. if(!this.isLogin()) return
  264. let qa = {
  265. question:JSON.parse(JSON.stringify(this.question)),
  266. answer:'',
  267. copy:false,
  268. upvote:false,
  269. comment:'',
  270. share:false,
  271. think:true,
  272. fail:false,
  273. errMsg:''
  274. }
  275. this.dialogList = [...this.dialogList,...[qa]];
  276. this.$nextTick(()=>{
  277. this.scrollToBottom();
  278. this.sendRequestWithRetry();
  279. this.question = '';
  280. })
  281. },
  282. // 滚动到底部
  283. scrollToBottom() {
  284. this.$nextTick(()=>{
  285. this.$nextTick(()=>{
  286. uni.pageScrollTo({
  287. scrollTop:99999,
  288. duration:300
  289. })
  290. })
  291. })
  292. },
  293. toCopy(item,index){
  294. uni.setClipboardData({
  295. data:item.answer,
  296. success: (res) => {
  297. this.$showToast('复制成功');
  298. },
  299. fail: (err) => {
  300. this.$showToast('复制失败');
  301. }
  302. })
  303. },
  304. toUpvote(item,index){
  305. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  306. },
  307. toComment(item,index){
  308. this.cindex = index;
  309. this.commentShow = true;
  310. },
  311. toShare(item,index){
  312. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  313. },
  314. commentConfirm(){
  315. this.$set(this.dialogList[this.cindex],'comment',this.content);
  316. this.commentShow = false;
  317. },
  318. commentCancel(){
  319. this.content = '';
  320. this.commentShow = false;
  321. },
  322. changeChzq(){
  323. this.chzq = !this.chzq;
  324. },
  325. changeLwss(){
  326. this.lwss = !this.lwss;
  327. },
  328. }
  329. }
  330. </script>
  331. <style scoped lang="scss">
  332. ::v-deep .u-textarea{
  333. border: none !important;
  334. padding: 0 !important;
  335. }
  336. ::v-deep .u-textarea textarea{
  337. min-height: 64rpx !important;
  338. font-size: 28rpx;
  339. color: #002846;
  340. }
  341. .page{
  342. background: url('https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/dialog_bg.png') no-repeat;
  343. background-size: 100% 100%;
  344. padding: 0 30rpx 240rpx;
  345. box-sizing: border-box;
  346. .welcome{
  347. margin-top: 259rpx;
  348. padding: 0 34rpx;
  349. image{
  350. width: 88rpx;
  351. height: 87rpx;
  352. margin-left: 20rpx;
  353. }
  354. p{
  355. font-family: PingFang-SC, PingFang-SC;
  356. font-weight: bold;
  357. font-size: 36rpx;
  358. color: #002846;
  359. line-height: 36rpx;
  360. margin-top: 36rpx;
  361. &.tip{
  362. font-family: PingFangSC, PingFang SC;
  363. font-weight: 400;
  364. font-size: 30rpx;
  365. color: #667E90;
  366. line-height: 40rpx;
  367. margin-top: 20rpx;
  368. }
  369. }
  370. }
  371. .dialogs{
  372. width: 100%;
  373. padding-top: 34rpx;
  374. box-sizing: border-box;
  375. overflow-y: auto;
  376. .d_answer{
  377. margin-top: 40rpx;
  378. &.init{
  379. margin-top: 0;
  380. }
  381. .da_top{
  382. image{
  383. width: 48rpx;
  384. height: 48rpx;
  385. }
  386. text{
  387. font-family: PingFang-SC, PingFang-SC;
  388. font-weight: bold;
  389. font-size: 30rpx;
  390. color: #002846;
  391. line-height: 48rpx;
  392. margin-left: 20rpx;
  393. }
  394. }
  395. .da_content{
  396. padding: 30rpx 32rpx;
  397. margin-top: 20rpx;
  398. background: #FFFFFF;
  399. color: #002846;
  400. border-radius: 4rpx 24rpx 24rpx 24rpx;
  401. font-size: 30rpx;
  402. .dac_think{
  403. image{
  404. width: 48rpx;
  405. height: 48rpx;
  406. }
  407. text{
  408. font-family: PingFangSC, PingFang SC;
  409. font-weight: 400;
  410. font-size: 28rpx;
  411. color: #002846;
  412. line-height: 54rpx;
  413. margin-left: 20rpx;
  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: #33A7A7;
  436. border-radius: 24rpx 4rpx 24rpx 24rpx;
  437. font-family: PingFangSC, PingFang SC;
  438. font-weight: 400;
  439. font-size: 30rpx;
  440. color: #FFFFFF;
  441. line-height: 48rpx;
  442. // text-align: right;
  443. padding: 26rpx 30rpx;
  444. }
  445. }
  446. }
  447. .ask_box{
  448. width: 100%;
  449. min-height: 176rpx;
  450. padding: 0 30rpx 60rpx;
  451. position: fixed;
  452. left: 0;
  453. box-sizing: border-box;
  454. background: #F6F6F6;
  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>