reportAsk.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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="https://oss.familydaf.cn/sxsnfile/20260129/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. ::v-deep .u-textarea{
  293. border: none !important;
  294. padding: 0 !important;
  295. }
  296. ::v-deep .u-textarea textarea{
  297. min-height: 64rpx !important;
  298. }
  299. .page{
  300. background: url('https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/dialog_bg.png') no-repeat;
  301. background-size: 100% 100%;
  302. padding: 0 30rpx 240rpx;
  303. box-sizing: border-box;
  304. .welcome{
  305. margin-top: 259rpx;
  306. padding: 0 34rpx;
  307. image{
  308. width: 88rpx;
  309. height: 87rpx;
  310. margin-left: 20rpx;
  311. }
  312. p{
  313. font-family: PingFang-SC, PingFang-SC;
  314. font-weight: bold;
  315. font-size: 36rpx;
  316. color: #252525;
  317. line-height: 36rpx;
  318. margin-top: 36rpx;
  319. &.tip{
  320. font-family: PingFangSC, PingFang SC;
  321. font-weight: 400;
  322. font-size: 26rpx;
  323. color: #646464;
  324. line-height: 40rpx;
  325. margin-top: 20rpx;
  326. }
  327. }
  328. }
  329. .dialogs{
  330. width: 100%;
  331. padding-top: 34rpx;
  332. box-sizing: border-box;
  333. overflow-y: auto;
  334. .d_answer{
  335. margin-top: 40rpx;
  336. &.init{
  337. margin-top: 0;
  338. }
  339. .da_top{
  340. image{
  341. width: 48rpx;
  342. height: 48rpx;
  343. }
  344. text{
  345. font-family: PingFang-SC, PingFang-SC;
  346. font-weight: bold;
  347. font-size: 30rpx;
  348. color: #505050;
  349. line-height: 48rpx;
  350. margin-left: 20rpx;
  351. }
  352. }
  353. .da_content{
  354. padding: 30rpx 32rpx;
  355. margin-top: 20rpx;
  356. background: #FFFFFF;
  357. border-radius: 4rpx 24rpx 24rpx 24rpx;
  358. .dac_think{
  359. image{
  360. width: 48rpx;
  361. height: 48rpx;
  362. }
  363. text{
  364. font-family: PingFangSC, PingFang SC;
  365. font-weight: 400;
  366. font-size: 28rpx;
  367. color: #002846;
  368. line-height: 54rpx;
  369. margin-left: 20rpx;
  370. }
  371. }
  372. .dc_btns{
  373. margin-top: 44rpx;
  374. image{
  375. width: 48rpx;
  376. height: 48rpx;
  377. }
  378. .db_l{
  379. image{
  380. margin-right: 40rpx;
  381. }
  382. }
  383. }
  384. }
  385. }
  386. .d_question{
  387. margin-top: 40rpx;
  388. display: flex;
  389. justify-content: flex-end;
  390. .dq_text{
  391. background: #33A7A7;
  392. border-radius: 24rpx 4rpx 24rpx 24rpx;
  393. font-family: PingFangSC, PingFang SC;
  394. font-weight: 400;
  395. font-size: 30rpx;
  396. color: #FFFFFF;
  397. line-height: 48rpx;
  398. // text-align: right;
  399. padding: 26rpx 30rpx;
  400. }
  401. }
  402. .d_pdf{
  403. margin-top: 20rpx;
  404. display: flex;
  405. justify-content: flex-end;
  406. .dp_box{
  407. width: 364rpx;
  408. background: #FFFFFF;
  409. border-radius: 16rpx;
  410. border: 1rpx solid #F0F2F8;
  411. padding: 27rpx 21rpx;
  412. .dq_text{
  413. background: #FFFFFF;
  414. font-family: PingFang-SC, PingFang-SC;
  415. font-weight: bold;
  416. font-size: 26rpx;
  417. color: #252525;
  418. line-height: 37rpx;
  419. }
  420. .dp_type{
  421. margin-top: 15rpx;
  422. image{
  423. width: 25rpx;
  424. height: 30rpx;
  425. }
  426. span{
  427. font-family: PingFang-SC, PingFang-SC;
  428. font-weight: bold;
  429. font-size: 24rpx;
  430. color: #999999;
  431. line-height: 33rpx;
  432. margin-left: 5rpx;
  433. }
  434. }
  435. }
  436. }
  437. }
  438. .ask_box{
  439. width: 100%;
  440. min-height: 176rpx;
  441. padding: 0 30rpx 60rpx;
  442. position: fixed;
  443. left: 0;
  444. box-sizing: border-box;
  445. background: #F6F6F6;
  446. .ask_memo{
  447. font-family: PingFangSC, PingFang SC;
  448. font-weight: 400;
  449. font-size: 24rpx;
  450. color: #b1b1b1;
  451. line-height: 34rpx;
  452. text-align: center;
  453. margin-top: 16rpx;
  454. }
  455. }
  456. .ask{
  457. min-height: 116rpx;
  458. background: #FFFFFF;
  459. border-radius: 24rpx;
  460. border: 2rpx solid #F0F2F8;
  461. padding: 24rpx;
  462. box-sizing: border-box;
  463. .a_inp{
  464. display: flex;
  465. align-items: flex-end;
  466. max-height: 300rpx;
  467. overflow-y: auto;
  468. .ai_l{
  469. width: calc(100% - 64rpx);
  470. padding-right: 20rpx;
  471. box-sizing: border-box;
  472. }
  473. .ai_r{
  474. width: 64rpx;
  475. image{
  476. width: 64rpx;
  477. height: 64rpx;
  478. }
  479. }
  480. }
  481. }
  482. }
  483. </style>