reportAsk.vue 12 KB

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