reportAsk.vue 13 KB

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