reportAsk.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header title='对报告提问' bgColor="#FFFFFF"></cus-header>
  4. <div class="dialogs container" ref="messageContainer">
  5. <div class="d_pdf">
  6. <div class="dp_box">
  7. <div class="dq_text">{{ '初始调查 - 甜梦巧克力市场部-首次团队测评' }}</div>
  8. <div class="dp_type adfac">
  9. <image src="@/static/pdf.png"></image>
  10. <span>PDF</span>
  11. </div>
  12. </div>
  13. </div>
  14. <div v-for="(item,index) in dialogList" :key="index">
  15. <div class="d_question">
  16. <div class="dq_text">{{ item.question }}</div>
  17. </div>
  18. <div class="d_answer">
  19. <div class="da_top adfac">
  20. <image src="@/static/logo.png"></image>
  21. <text>创衡正念企业教练</text>
  22. </div>
  23. <div 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. <u-parse :content="item.answer"></u-parse>
  32. </template>
  33. <div class="dc_btns adfacjb" v-if="item.answer">
  34. <div class="db_l">
  35. <image :src="item.copy?require('@/static/copy_active.png'):require('@/static/copy.png')" @tap="toCopy(item,index)"></image>
  36. <image :src="item.upvote?require('@/static/upvote_active.png'):require('@/static/upvote.png')" @tap="toUpvote(item,index)"></image>
  37. <image :src="item.comment?require('@/static/comment_active.png'):require('@/static/comment.png')" @tap="toComment(item,index)"></image>
  38. </div>
  39. <div class="db_r">
  40. <!-- <image :src="item.share?require('@/static/share_active.png'):require('@/static/share.png')" @tap="toShare(item,index)"></image> -->
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. <div class="ask_box" :style="{'bottom':fixBottom+'px'}">
  48. <div class="ask">
  49. <div class="a_inp">
  50. <div class="ai_l">
  51. <u-textarea v-model="question" placeholder="请输入您的问题" autoHeight :showConfirmBar="false" :adjustPosition="false" maxlength="999999"></u-textarea>
  52. </div>
  53. <div class="ai_r">
  54. <image @tap="sendQuestion" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/16/2429556b-54b7-4878-97c0-6b440b546ee4.png"></image>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. <u-modal :show="commentShow" title="评论" @confirm="commentConfirm" @cancel="commentCancel" @close="commentCancel" :showCancelButton="true">
  60. <u-textarea v-model="content" placeholder="对于我们的回答您是否不满意,您有更好的答案建议吗?请在此输入."></u-textarea>
  61. </u-modal>
  62. </view>
  63. </template>
  64. <script>
  65. var timer = null;
  66. let requestTask = null;
  67. import { BaseApi } from '../http/baseApi.js'
  68. import * as TextEncoding from "text-encoding-shim";
  69. export default {
  70. data(){
  71. return {
  72. pdfUrl:'',
  73. retryCount: 3, // 最大重试次数
  74. currentRetry: 0, // 当前重试次数
  75. isRequesting: false, // 请求状态锁
  76. question:'',
  77. dialogList:[],
  78. streamingResponse:'',
  79. receivedData:'',
  80. windex:0,
  81. commentShow:false,
  82. content:'',
  83. cindex:'',
  84. fixBottom: 0
  85. }
  86. },
  87. onLoad(option) {
  88. this.pdfUrl = option.pdfUrl;
  89. },
  90. onReady() {
  91. uni.onKeyboardHeightChange(res => {
  92. this.fixBottom = res.height||0;
  93. });
  94. },
  95. onUnload() {
  96. uni.offKeyboardHeightChange();
  97. this.fixBottom = 0;
  98. },
  99. methods:{
  100. // 封装带重试机制的请求方法
  101. async sendRequestWithRetry() {
  102. if (this.isRequesting) return;
  103. this.isRequesting = true;
  104. this.currentRetry = 0;
  105. try {
  106. await this._executeRequest();
  107. } catch (error) {
  108. this.$showToast('请求失败,请稍后重试')
  109. } finally {
  110. this.isRequesting = false;
  111. }
  112. },
  113. // 实际执行请求的方法
  114. _executeRequest() {
  115. return new Promise((resolve, reject) => {
  116. requestTask = uni.request({
  117. url: `${BaseApi}/core/chat/sendChatMessageStream`,
  118. method: 'POST',
  119. timeout: 10000,
  120. data:{
  121. query: this.question,
  122. identity:'被教练者',
  123. report:this.pdfUrl
  124. },
  125. header: {
  126. 'Content-Type': 'application/json',
  127. 'token': uni.getStorageSync('token') || ''
  128. },
  129. enableChunked: true, // 启用流式接收
  130. responseType:'text',
  131. success: (res) => {
  132. if (res.statusCode === 200) {
  133. this._handleSuccess(res.data);
  134. resolve();
  135. } else {
  136. this._handleError(`状态码异常: ${res.statusCode}`, resolve, reject);
  137. }
  138. },
  139. fail: (err) => {
  140. this._handleError(err.errMsg, resolve, reject);
  141. },
  142. complete: (com) => {
  143. console.log('请求完成',com)
  144. }
  145. });
  146. requestTask.onChunkReceived(async (res) => {
  147. const uint8Array = new Uint8Array(res.data);
  148. const decoder = new TextEncoding.TextDecoder("utf-8");
  149. const decodedString = decoder.decode(uint8Array);
  150. try {
  151. let newtext = decodedString.replaceAll('data:','')
  152. let ntArr = newtext.split('\n\n');
  153. if(ntArr.length){
  154. ntArr.forEach(n=>{
  155. if(!n.trim()) return
  156. let nj = JSON.parse(n);
  157. if(nj.event=='message'){
  158. let answer = this.dialogList[this.dialogList.length-1].answer+nj.answer?.replace(/(\r\n|\n|\r)+/g, '<br>');
  159. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  160. this.$set(this.dialogList[this.dialogList.length-1],'id',nj.id);
  161. this.$set(this.dialogList[this.dialogList.length-1],'task_id',nj.task_id);
  162. this.$set(this.dialogList[this.dialogList.length-1],'message_id',nj.message_id);
  163. this.$set(this.dialogList[this.dialogList.length-1],'conversation_id',nj.conversation_id);
  164. this.$set(this.dialogList[this.dialogList.length-1],'think',false);
  165. }
  166. })
  167. setTimeout(()=>{
  168. this.scrollToBottom();
  169. },100)
  170. }
  171. } catch (e) {
  172. console.error('解析失败', e, '原始数据:', decodedString);
  173. }
  174. });
  175. });
  176. },
  177. // 成功处理
  178. _handleSuccess(data) {
  179. if (data) {
  180. this.streamingResponse += data;
  181. }
  182. this.currentRetry = 0; // 重置重试计数器
  183. },
  184. // 错误处理
  185. _handleError(errorMsg, resolve, reject) {
  186. if (this._shouldRetry(errorMsg)) {
  187. this.currentRetry++;
  188. setTimeout(() => {
  189. this._executeRequest().then(resolve).catch(reject);
  190. }, this._getRetryDelay());
  191. } else {
  192. reject(errorMsg);
  193. }
  194. },
  195. // 判断是否需要重试
  196. _shouldRetry(errorMsg) {
  197. const retryableErrors = [
  198. 'timeout',
  199. 'request:fail',
  200. 'Network Error'
  201. ];
  202. return this.currentRetry < this.retryCount && retryableErrors.some(e => errorMsg.includes(e));
  203. },
  204. // 获取指数退避延迟时间
  205. _getRetryDelay() {
  206. return Math.min(1000 * Math.pow(2, this.currentRetry), 10000);
  207. },
  208. sendQuestion(){
  209. if(!this.question) return this.$showToast('请输入您的问题');
  210. let qa = {
  211. question:JSON.parse(JSON.stringify(this.question)),
  212. answer:'',
  213. copy:false,
  214. upvote:false,
  215. comment:'',
  216. share:false,
  217. think:true
  218. }
  219. this.dialogList = [...this.dialogList,...[qa]];
  220. this.$nextTick(()=>{
  221. this.scrollToBottom();
  222. this.sendRequestWithRetry();
  223. this.question = '';
  224. })
  225. },
  226. // 滚动到底部
  227. scrollToBottom() {
  228. this.$nextTick(()=>{
  229. this.$nextTick(()=>{
  230. uni.pageScrollTo({
  231. scrollTop:99999,
  232. duration:300
  233. })
  234. })
  235. })
  236. },
  237. toCopy(item,index){
  238. uni.setClipboardData({
  239. data:item.answer,
  240. success: (res) => {
  241. this.$showToast('复制成功');
  242. },
  243. fail: (err) => {
  244. this.$showToast('复制失败');
  245. }
  246. })
  247. },
  248. toUpvote(item,index){
  249. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  250. },
  251. toComment(item,index){
  252. this.cindex = index;
  253. this.commentShow = true;
  254. },
  255. toShare(item,index){
  256. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  257. },
  258. commentConfirm(){
  259. this.$set(this.dialogList[this.cindex],'comment',this.content);
  260. this.commentShow = false;
  261. },
  262. commentCancel(){
  263. this.content = '';
  264. this.commentShow = false;
  265. },
  266. }
  267. }
  268. </script>
  269. <style scoped lang="scss">
  270. ::v-deep .u-textarea{
  271. border: none !important;
  272. padding: 0 !important;
  273. }
  274. ::v-deep .u-textarea textarea{
  275. min-height: 64rpx !important;
  276. }
  277. .page{
  278. background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);
  279. padding: 0 30rpx 200rpx;
  280. box-sizing: border-box;
  281. .welcome{
  282. margin-top: 259rpx;
  283. padding: 0 34rpx;
  284. image{
  285. width: 88rpx;
  286. height: 87rpx;
  287. margin-left: 20rpx;
  288. }
  289. p{
  290. font-family: PingFang-SC, PingFang-SC;
  291. font-weight: bold;
  292. font-size: 36rpx;
  293. color: #252525;
  294. line-height: 36rpx;
  295. margin-top: 36rpx;
  296. &.tip{
  297. font-family: PingFangSC, PingFang SC;
  298. font-weight: 400;
  299. font-size: 26rpx;
  300. color: #646464;
  301. line-height: 40rpx;
  302. margin-top: 20rpx;
  303. }
  304. }
  305. }
  306. .dialogs{
  307. width: 100%;
  308. padding-top: 34rpx;
  309. box-sizing: border-box;
  310. overflow-y: auto;
  311. .d_answer{
  312. margin-top: 40rpx;
  313. &.init{
  314. margin-top: 0;
  315. }
  316. .da_top{
  317. image{
  318. width: 48rpx;
  319. height: 48rpx;
  320. }
  321. text{
  322. font-family: PingFang-SC, PingFang-SC;
  323. font-weight: bold;
  324. font-size: 30rpx;
  325. color: #505050;
  326. line-height: 48rpx;
  327. margin-left: 20rpx;
  328. }
  329. }
  330. .da_content{
  331. padding: 30rpx 32rpx;
  332. margin-top: 20rpx;
  333. background: #FFFFFF;
  334. border-radius: 4rpx 24rpx 24rpx 24rpx;
  335. .dac_think{
  336. image{
  337. width: 40rpx;
  338. height: 40rpx;
  339. }
  340. text{
  341. font-size: 30rpx;
  342. margin-left: 10rpx;
  343. }
  344. }
  345. .dc_btns{
  346. margin-top: 44rpx;
  347. image{
  348. width: 48rpx;
  349. height: 48rpx;
  350. }
  351. .db_l{
  352. image{
  353. margin-right: 40rpx;
  354. }
  355. }
  356. }
  357. }
  358. }
  359. .d_question{
  360. margin-top: 40rpx;
  361. display: flex;
  362. justify-content: flex-end;
  363. .dq_text{
  364. background: #833478;
  365. border-radius: 24rpx 4rpx 24rpx 24rpx;
  366. font-family: PingFangSC, PingFang SC;
  367. font-weight: 400;
  368. font-size: 30rpx;
  369. color: #FFFFFF;
  370. line-height: 48rpx;
  371. // text-align: right;
  372. padding: 26rpx 30rpx;
  373. }
  374. }
  375. .d_pdf{
  376. margin-top: 20rpx;
  377. display: flex;
  378. justify-content: flex-end;
  379. .dp_box{
  380. width: 364rpx;
  381. background: #FFFFFF;
  382. border-radius: 16rpx;
  383. border: 1rpx solid #F0F2F8;
  384. padding: 27rpx 21rpx;
  385. .dq_text{
  386. background: #FFFFFF;
  387. font-family: PingFang-SC, PingFang-SC;
  388. font-weight: bold;
  389. font-size: 26rpx;
  390. color: #252525;
  391. line-height: 37rpx;
  392. }
  393. .dp_type{
  394. margin-top: 15rpx;
  395. image{
  396. width: 25rpx;
  397. height: 30rpx;
  398. }
  399. span{
  400. font-family: PingFang-SC, PingFang-SC;
  401. font-weight: bold;
  402. font-size: 24rpx;
  403. color: #999999;
  404. line-height: 33rpx;
  405. margin-left: 5rpx;
  406. }
  407. }
  408. }
  409. }
  410. }
  411. .ask_box{
  412. width: 100%;
  413. min-height: 176rpx;
  414. background: linear-gradient( 227deg, #EEEFF8 0%, #F6ECF4 100%, #F6ECF4 100%);
  415. padding: 0 30rpx 60rpx;
  416. position: fixed;
  417. left: 0;
  418. box-sizing: border-box;
  419. }
  420. .ask{
  421. min-height: 116rpx;
  422. background: #FFFFFF;
  423. border-radius: 24rpx;
  424. border: 2rpx solid #F0F2F8;
  425. padding: 24rpx;
  426. box-sizing: border-box;
  427. .a_inp{
  428. display: flex;
  429. align-items: flex-end;
  430. max-height: 300rpx;
  431. overflow-y: auto;
  432. .ai_l{
  433. width: calc(100% - 64rpx);
  434. padding-right: 20rpx;
  435. box-sizing: border-box;
  436. }
  437. .ai_r{
  438. width: 64rpx;
  439. image{
  440. width: 64rpx;
  441. height: 64rpx;
  442. }
  443. }
  444. }
  445. }
  446. }
  447. </style>