dialog.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <u-navbar title="创衡正念企业教练" bgColor="transparent" :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. <u-parse :content="item.answer"></u-parse>
  36. <div class="dc_btns adfacjb" v-if="item.answer">
  37. <div class="db_l">
  38. <image :src="item.copy?require('@/static/copy_active.png'):require('@/static/copy.png')" @tap="toCopy(item,index)"></image>
  39. <image :src="item.upvote?require('@/static/upvote_active.png'):require('@/static/upvote.png')" @tap="toUpvote(item,index)"></image>
  40. <image :src="item.comment?require('@/static/comment_active.png'):require('@/static/comment.png')" @tap="toComment(item,index)"></image>
  41. </div>
  42. <div class="db_r">
  43. <image :src="item.share?require('@/static/share_active.png'):require('@/static/share.png')" @tap="toShare(item,index)"></image>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </template>
  51. <div class="ask">
  52. <div class="a_inp">
  53. <u-textarea v-model="question" placeholder="请输入您的问题" autoHeight @confirm="sendQuestion"></u-textarea>
  54. </div>
  55. <div class="a_btn adfacjb">
  56. <div class="ab_l adfac">
  57. <div class="al_pre adfacjc">
  58. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/451ac13c-5fdc-4d15-8f35-9f4d238e87c1.png"></image>
  59. <text>创衡增强</text>
  60. </div>
  61. <div class="al_pre adfacjc">
  62. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/d4668dcc-1d28-47ff-8bae-84dc6475794b.png"></image>
  63. <text>联网搜索</text>
  64. </div>
  65. </div>
  66. <div class="ab_r">
  67. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/caf6075c-0967-4c99-a269-ea453537075c.png"></image>
  68. <image @tap="sendQuestion" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/2429556b-54b7-4878-97c0-6b440b546ee4.png"></image>
  69. </div>
  70. </div>
  71. </div>
  72. <u-modal :show="commentShow" title="评论" @confirm="commentConfirm" @cancel="commentCancel" @close="commentCancel" :showCancelButton="true">
  73. <u-textarea v-model="content" placeholder="对于我们的回答您是否不满意,您有更好的答案建议吗?请在此输入."></u-textarea>
  74. </u-modal>
  75. </view>
  76. </template>
  77. <script>
  78. var timer = null;
  79. let requestTask = null;
  80. import { BaseApi } from '../http/baseApi.js'
  81. import * as TextEncoding from "text-encoding-shim";
  82. export default {
  83. data(){
  84. return {
  85. isDialog:false,
  86. retryCount: 3, // 最大重试次数
  87. currentRetry: 0, // 当前重试次数
  88. isRequesting: false, // 请求状态锁
  89. question:'',
  90. streamingResponse:'',
  91. receivedData:'',
  92. dialogList:[],
  93. windex:0,
  94. commentShow:false,
  95. content:'',
  96. cindex:'',
  97. }
  98. },
  99. methods:{
  100. toBack(){
  101. uni.reLaunch({
  102. url:'/pages/home'
  103. })
  104. },
  105. startNewDialog(){
  106. clearInterval(timer)
  107. this.dialogList = [];
  108. this.question = '';
  109. this.streamingResponse = '';
  110. },
  111. // 封装带重试机制的请求方法
  112. async sendRequestWithRetry() {
  113. if (this.isRequesting) return;
  114. this.isRequesting = true;
  115. this.currentRetry = 0;
  116. try {
  117. await this._executeRequest();
  118. } catch (error) {
  119. this.$showToast('请求失败,请稍后重试')
  120. } finally {
  121. this.isRequesting = false;
  122. }
  123. },
  124. // 实际执行请求的方法
  125. _executeRequest() {
  126. return new Promise((resolve, reject) => {
  127. requestTask = uni.request({
  128. url: `${BaseApi}/core/chat/streamingMessage`,
  129. method: 'POST',
  130. timeout: 10000,
  131. data:{
  132. query: this.question,
  133. timestamp:Date.now()
  134. },
  135. header: {
  136. 'Content-Type': 'application/json',
  137. 'token': uni.getStorageSync('token') || ''
  138. },
  139. enableChunked: true, // 启用流式接收
  140. responseType:'text',
  141. success: (res) => {
  142. if (res.statusCode === 200) {
  143. this._handleSuccess(res.data);
  144. resolve();
  145. } else {
  146. this._handleError(`状态码异常: ${res.statusCode}`, resolve, reject);
  147. }
  148. },
  149. fail: (err) => {
  150. this._handleError(err.errMsg, resolve, reject);
  151. },
  152. complete: (com) => {
  153. console.log('请求完成',com)
  154. }
  155. });
  156. requestTask.onChunkReceived(async (res) => {
  157. const uint8Array = new Uint8Array(res.data);
  158. const decoder = new TextEncoding.TextDecoder("utf-8");
  159. const decodedString = decoder.decode(uint8Array);
  160. try {
  161. let newtext = decodedString.replaceAll('data:','')
  162. let ntArr = newtext.split(/\s+/);
  163. if(ntArr.length){
  164. ntArr.forEach(n=>{
  165. if(!n.trim()) return
  166. let nj = JSON.parse(n);
  167. if(nj.event=='agent_message'){
  168. let answer = this.dialogList[this.dialogList.length-1].answer+nj.answer;
  169. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  170. this.$set(this.dialogList[this.dialogList.length-1],'id',nj.id);
  171. this.$set(this.dialogList[this.dialogList.length-1],'task_id',nj.task_id);
  172. this.$set(this.dialogList[this.dialogList.length-1],'message_id',nj.message_id);
  173. this.$set(this.dialogList[this.dialogList.length-1],'conversation_id',nj.conversation_id);
  174. uni.pageScrollTo({ scrollTop: 99999, duration: 300});
  175. }
  176. })
  177. }
  178. } catch (e) {
  179. console.error('解析失败', e, '原始数据:', decodedString);
  180. }
  181. });
  182. });
  183. },
  184. // 成功处理
  185. _handleSuccess(data) {
  186. if (data) {
  187. this.streamingResponse += data;
  188. }
  189. this.currentRetry = 0; // 重置重试计数器
  190. },
  191. // 错误处理
  192. _handleError(errorMsg, resolve, reject) {
  193. if (this._shouldRetry(errorMsg)) {
  194. this.currentRetry++;
  195. setTimeout(() => {
  196. this._executeRequest().then(resolve).catch(reject);
  197. }, this._getRetryDelay());
  198. } else {
  199. reject(errorMsg);
  200. }
  201. },
  202. // 判断是否需要重试
  203. _shouldRetry(errorMsg) {
  204. const retryableErrors = [
  205. 'timeout',
  206. 'request:fail',
  207. 'Network Error'
  208. ];
  209. return this.currentRetry < this.retryCount && retryableErrors.some(e => errorMsg.includes(e));
  210. },
  211. // 获取指数退避延迟时间
  212. _getRetryDelay() {
  213. return Math.min(1000 * Math.pow(2, this.currentRetry), 10000);
  214. },
  215. sendQuestion(){
  216. if(!this.question) return this.$showToast('请输入您的问题');
  217. let qa = {
  218. question:JSON.parse(JSON.stringify(this.question)),
  219. answer:'',
  220. copy:false,
  221. upvote:false,
  222. comment:'',
  223. share:false,
  224. }
  225. this.dialogList = [...this.dialogList,...[qa]];
  226. this.$nextTick(()=>{
  227. uni.pageScrollTo({ scrollTop: 99999, duration: 300});
  228. this.sendRequestWithRetry();
  229. this.question = '';
  230. })
  231. },
  232. toCopy(item,index){
  233. uni.setClipboardData({
  234. data:item.answer,
  235. success: (res) => {
  236. this.$showToast('复制成功');
  237. },
  238. fail: (err) => {
  239. this.$showToast('复制失败');
  240. }
  241. })
  242. },
  243. toUpvote(item,index){
  244. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  245. },
  246. toComment(item,index){
  247. this.cindex = index;
  248. this.commentShow = true;
  249. },
  250. toShare(item,index){
  251. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  252. },
  253. commentConfirm(){
  254. this.$set(this.dialogList[this.cindex],'comment',this.content);
  255. this.commentShow = false;
  256. },
  257. commentCancel(){
  258. this.content = '';
  259. this.commentShow = false;
  260. },
  261. }
  262. }
  263. </script>
  264. <style scoped lang="scss">
  265. ::v-deep .u-textarea{
  266. border: none !important;
  267. padding: 0 !important;
  268. }
  269. ::v-deep .u-textarea textarea{
  270. min-height: 96rpx !important;
  271. }
  272. .page{
  273. background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);
  274. padding: 0 30rpx 302rpx;
  275. box-sizing: border-box;
  276. .welcome{
  277. margin-top: 259rpx;
  278. padding: 0 34rpx;
  279. image{
  280. width: 88rpx;
  281. height: 87rpx;
  282. margin-left: 20rpx;
  283. }
  284. p{
  285. font-family: PingFang-SC, PingFang-SC;
  286. font-weight: bold;
  287. font-size: 36rpx;
  288. color: #252525;
  289. line-height: 36rpx;
  290. margin-top: 36rpx;
  291. &.tip{
  292. font-family: PingFangSC, PingFang SC;
  293. font-weight: 400;
  294. font-size: 26rpx;
  295. color: #646464;
  296. line-height: 40rpx;
  297. margin-top: 20rpx;
  298. }
  299. }
  300. }
  301. .dialogs{
  302. width: 100%;
  303. height: calc(100vh - 282rpx);
  304. padding-top: 34rpx;
  305. box-sizing: border-box;
  306. overflow-y: auto;
  307. .d_answer{
  308. margin-top: 40rpx;
  309. &.init{
  310. margin-top: 0;
  311. }
  312. .da_top{
  313. image{
  314. width: 48rpx;
  315. height: 48rpx;
  316. }
  317. text{
  318. font-family: PingFang-SC, PingFang-SC;
  319. font-weight: bold;
  320. font-size: 30rpx;
  321. color: #505050;
  322. line-height: 48rpx;
  323. margin-left: 20rpx;
  324. }
  325. }
  326. .da_content{
  327. padding: 30rpx 32rpx;
  328. margin-top: 20rpx;
  329. background: #FFFFFF;
  330. border-radius: 4rpx 24rpx 24rpx 24rpx;
  331. .dc_btns{
  332. margin-top: 44rpx;
  333. image{
  334. width: 48rpx;
  335. height: 48rpx;
  336. }
  337. .db_l{
  338. image{
  339. margin-right: 40rpx;
  340. }
  341. }
  342. }
  343. }
  344. }
  345. .d_question{
  346. margin-top: 40rpx;
  347. display: flex;
  348. justify-content: flex-end;
  349. .dq_text{
  350. background: #833478;
  351. border-radius: 24rpx 4rpx 24rpx 24rpx;
  352. font-family: PingFangSC, PingFang SC;
  353. font-weight: 400;
  354. font-size: 30rpx;
  355. color: #FFFFFF;
  356. line-height: 48rpx;
  357. text-align: right;
  358. padding: 26rpx 30rpx;
  359. }
  360. }
  361. }
  362. .ask{
  363. width: calc(100% - 60rpx);
  364. height: 210rpx;
  365. background: #FFFFFF;
  366. border-radius: 24rpx;
  367. border: 2rpx solid #F0F2F8;
  368. position: fixed;
  369. left: 30rpx;
  370. bottom: 72rpx;
  371. display: flex;
  372. flex-direction: column;
  373. padding: 24rpx;
  374. box-sizing: border-box;
  375. .a_inp{
  376. flex: 1;
  377. overflow-y: auto;
  378. }
  379. .a_btn{
  380. padding-top: 20rpx;
  381. height: 54rpx;
  382. .ab_l{
  383. .al_pre{
  384. width: 162rpx;
  385. height: 54rpx;
  386. background: #FFFFFF;
  387. border-radius: 27rpx;
  388. border: 1rpx solid #E0E0E0;
  389. margin-right: 24rpx;
  390. image{
  391. width: 24rpx;
  392. height: 24rpx;
  393. }
  394. text{
  395. font-family: PingFangSC, PingFang SC;
  396. font-weight: 400;
  397. font-size: 22rpx;
  398. color: #393939;
  399. line-height: 22rpx;
  400. margin-left: 10rpx;
  401. }
  402. }
  403. }
  404. .ab_r{
  405. image{
  406. width: 48rpx;
  407. height: 48rpx;
  408. margin-left: 30rpx;
  409. }
  410. }
  411. }
  412. }
  413. }
  414. </style>