dialog.vue 14 KB

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