reportAsk.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'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. });
  100. },
  101. onUnload() {
  102. uni.offKeyboardHeightChange();
  103. this.fixBottom = 0;
  104. },
  105. methods:{
  106. // 封装带重试机制的请求方法
  107. async sendRequestWithRetry() {
  108. if (this.isRequesting) return;
  109. this.isRequesting = true;
  110. this.currentRetry = 0;
  111. try {
  112. await this._executeRequest();
  113. } catch (error) {
  114. this.$showToast('请求失败,请稍后重试')
  115. } finally {
  116. this.isRequesting = false;
  117. }
  118. },
  119. // 实际执行请求的方法
  120. _executeRequest() {
  121. return new Promise((resolve, reject) => {
  122. requestTask = uni.request({
  123. url: `${BaseApi}/core/chat/sendChatMessageStream`,
  124. method: 'POST',
  125. timeout: 10000,
  126. data:{
  127. query: this.question,
  128. identity:'被教练者',
  129. conversation_id: this.conversation_id,
  130. report:this.pdfUrl
  131. },
  132. header: {
  133. 'Content-Type': 'application/json',
  134. 'token': uni.getStorageSync('token') || ''
  135. },
  136. enableChunked: true, // 启用流式接收
  137. responseType:'text',
  138. success: (res) => {
  139. if (res.statusCode === 200) {
  140. this._handleSuccess(res.data);
  141. resolve();
  142. } else {
  143. this._handleError(`状态码异常: ${res.statusCode}`, resolve, reject);
  144. }
  145. },
  146. fail: (err) => {
  147. this._handleError(err.errMsg, resolve, reject);
  148. },
  149. complete: (com) => {
  150. console.log('请求完成',com)
  151. }
  152. });
  153. requestTask.onChunkReceived(async (res) => {
  154. const uint8Array = new Uint8Array(res.data);
  155. const decoder = new TextEncoding.TextDecoder("utf-8");
  156. const decodedString = decoder.decode(uint8Array);
  157. try {
  158. let newtext = decodedString.replaceAll('data:','').replaceAll(':keepAlive','');
  159. let ntArr = newtext.split('\n\n');
  160. if(ntArr.length){
  161. ntArr.forEach(n=>{
  162. if(!n.trim()) return
  163. let nj = JSON.parse(n);
  164. if(nj.event=='message'){
  165. this.conversation_id = nj.conversation_id;
  166. let answer = this.dialogList[this.dialogList.length-1].answer+nj.answer?.replace(/(\r\n|\n|\r)+/g, '<br>').replace(/<think>.*?<\/think>/gs, '');
  167. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  168. this.$set(this.dialogList[this.dialogList.length-1],'id',nj.id);
  169. this.$set(this.dialogList[this.dialogList.length-1],'task_id',nj.task_id);
  170. this.$set(this.dialogList[this.dialogList.length-1],'message_id',nj.message_id);
  171. this.$set(this.dialogList[this.dialogList.length-1],'conversation_id',nj.conversation_id);
  172. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  173. }
  174. })
  175. setTimeout(()=>{
  176. this.scrollToBottom();
  177. },100)
  178. }
  179. } catch (e) {
  180. console.error('解析失败', e, '原始数据:', decodedString);
  181. }
  182. });
  183. });
  184. },
  185. // 成功处理
  186. _handleSuccess(data) {
  187. if (data) {
  188. this.streamingResponse += data;
  189. }
  190. this.currentRetry = 0; // 重置重试计数器
  191. },
  192. // 错误处理
  193. _handleError(errorMsg, resolve, reject) {
  194. if (this._shouldRetry(errorMsg)) {
  195. this.currentRetry++;
  196. setTimeout(() => {
  197. this._executeRequest().then(resolve).catch(reject);
  198. }, this._getRetryDelay());
  199. } else {
  200. reject(errorMsg);
  201. }
  202. },
  203. // 判断是否需要重试
  204. _shouldRetry(errorMsg) {
  205. const retryableErrors = [
  206. 'timeout',
  207. 'request:fail',
  208. 'Network Error'
  209. ];
  210. return this.currentRetry < this.retryCount && retryableErrors.some(e => errorMsg.includes(e));
  211. },
  212. // 获取指数退避延迟时间
  213. _getRetryDelay() {
  214. return Math.min(1000 * Math.pow(2, this.currentRetry), 10000);
  215. },
  216. sendQuestion(){
  217. if(!this.question) return this.$showToast('请输入您的问题');
  218. let qa = {
  219. question:JSON.parse(JSON.stringify(this.question)),
  220. answer:'',
  221. copy:false,
  222. upvote:false,
  223. comment:'',
  224. share:false,
  225. think:true
  226. }
  227. this.dialogList = [...this.dialogList,...[qa]];
  228. this.$nextTick(()=>{
  229. this.scrollToBottom();
  230. this.sendRequestWithRetry();
  231. this.question = '';
  232. })
  233. },
  234. // 滚动到底部
  235. scrollToBottom() {
  236. this.$nextTick(()=>{
  237. this.$nextTick(()=>{
  238. uni.pageScrollTo({
  239. scrollTop:99999,
  240. duration:300
  241. })
  242. })
  243. })
  244. },
  245. toCopy(item,index){
  246. uni.setClipboardData({
  247. data:item.answer,
  248. success: (res) => {
  249. this.$showToast('复制成功');
  250. },
  251. fail: (err) => {
  252. this.$showToast('复制失败');
  253. }
  254. })
  255. },
  256. toUpvote(item,index){
  257. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  258. },
  259. toComment(item,index){
  260. this.cindex = index;
  261. this.commentShow = true;
  262. },
  263. toShare(item,index){
  264. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  265. },
  266. commentConfirm(){
  267. this.$set(this.dialogList[this.cindex],'comment',this.content);
  268. this.commentShow = false;
  269. },
  270. commentCancel(){
  271. this.content = '';
  272. this.commentShow = false;
  273. },
  274. }
  275. }
  276. </script>
  277. <style scoped lang="scss">
  278. ::v-deep .u-textarea{
  279. border: none !important;
  280. padding: 0 !important;
  281. }
  282. ::v-deep .u-textarea textarea{
  283. min-height: 64rpx !important;
  284. }
  285. .page{
  286. background: url('https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/dialog_bg.png') no-repeat;
  287. background-size: 100% 100%;
  288. padding: 0 30rpx 240rpx;
  289. box-sizing: border-box;
  290. .welcome{
  291. margin-top: 259rpx;
  292. padding: 0 34rpx;
  293. image{
  294. width: 88rpx;
  295. height: 87rpx;
  296. margin-left: 20rpx;
  297. }
  298. p{
  299. font-family: PingFang-SC, PingFang-SC;
  300. font-weight: bold;
  301. font-size: 36rpx;
  302. color: #252525;
  303. line-height: 36rpx;
  304. margin-top: 36rpx;
  305. &.tip{
  306. font-family: PingFangSC, PingFang SC;
  307. font-weight: 400;
  308. font-size: 26rpx;
  309. color: #646464;
  310. line-height: 40rpx;
  311. margin-top: 20rpx;
  312. }
  313. }
  314. }
  315. .dialogs{
  316. width: 100%;
  317. padding-top: 34rpx;
  318. box-sizing: border-box;
  319. overflow-y: auto;
  320. .d_answer{
  321. margin-top: 40rpx;
  322. &.init{
  323. margin-top: 0;
  324. }
  325. .da_top{
  326. image{
  327. width: 48rpx;
  328. height: 48rpx;
  329. }
  330. text{
  331. font-family: PingFang-SC, PingFang-SC;
  332. font-weight: bold;
  333. font-size: 30rpx;
  334. color: #505050;
  335. line-height: 48rpx;
  336. margin-left: 20rpx;
  337. }
  338. }
  339. .da_content{
  340. padding: 30rpx 32rpx;
  341. margin-top: 20rpx;
  342. background: #FFFFFF;
  343. border-radius: 4rpx 24rpx 24rpx 24rpx;
  344. .dac_think{
  345. image{
  346. width: 60rpx;
  347. height: 60rpx;
  348. }
  349. text{
  350. font-size: 30rpx;
  351. margin-left: 10rpx;
  352. }
  353. }
  354. .dc_btns{
  355. margin-top: 44rpx;
  356. image{
  357. width: 48rpx;
  358. height: 48rpx;
  359. }
  360. .db_l{
  361. image{
  362. margin-right: 40rpx;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. .d_question{
  369. margin-top: 40rpx;
  370. display: flex;
  371. justify-content: flex-end;
  372. .dq_text{
  373. background: #33A7A7;
  374. border-radius: 24rpx 4rpx 24rpx 24rpx;
  375. font-family: PingFangSC, PingFang SC;
  376. font-weight: 400;
  377. font-size: 30rpx;
  378. color: #FFFFFF;
  379. line-height: 48rpx;
  380. // text-align: right;
  381. padding: 26rpx 30rpx;
  382. }
  383. }
  384. .d_pdf{
  385. margin-top: 20rpx;
  386. display: flex;
  387. justify-content: flex-end;
  388. .dp_box{
  389. width: 364rpx;
  390. background: #FFFFFF;
  391. border-radius: 16rpx;
  392. border: 1rpx solid #F0F2F8;
  393. padding: 27rpx 21rpx;
  394. .dq_text{
  395. background: #FFFFFF;
  396. font-family: PingFang-SC, PingFang-SC;
  397. font-weight: bold;
  398. font-size: 26rpx;
  399. color: #252525;
  400. line-height: 37rpx;
  401. }
  402. .dp_type{
  403. margin-top: 15rpx;
  404. image{
  405. width: 25rpx;
  406. height: 30rpx;
  407. }
  408. span{
  409. font-family: PingFang-SC, PingFang-SC;
  410. font-weight: bold;
  411. font-size: 24rpx;
  412. color: #999999;
  413. line-height: 33rpx;
  414. margin-left: 5rpx;
  415. }
  416. }
  417. }
  418. }
  419. }
  420. .ask_box{
  421. width: 100%;
  422. min-height: 176rpx;
  423. padding: 0 30rpx 60rpx;
  424. position: fixed;
  425. left: 0;
  426. box-sizing: border-box;
  427. background: #F6F6F6;
  428. .ask_memo{
  429. font-family: PingFangSC, PingFang SC;
  430. font-weight: 400;
  431. font-size: 24rpx;
  432. color: #b1b1b1;
  433. line-height: 34rpx;
  434. text-align: center;
  435. margin-top: 16rpx;
  436. }
  437. }
  438. .ask{
  439. min-height: 116rpx;
  440. background: #FFFFFF;
  441. border-radius: 24rpx;
  442. border: 2rpx solid #F0F2F8;
  443. padding: 24rpx;
  444. box-sizing: border-box;
  445. .a_inp{
  446. display: flex;
  447. align-items: flex-end;
  448. max-height: 300rpx;
  449. overflow-y: auto;
  450. .ai_l{
  451. width: calc(100% - 64rpx);
  452. padding-right: 20rpx;
  453. box-sizing: border-box;
  454. }
  455. .ai_r{
  456. width: 64rpx;
  457. image{
  458. width: 64rpx;
  459. height: 64rpx;
  460. }
  461. }
  462. }
  463. }
  464. }
  465. </style>