reportAsk.vue 13 KB

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