reportAsk.vue 13 KB

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