reportAsk.vue 14 KB

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