dialog.vue 14 KB

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