reportAsk.vue 12 KB

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