dialog.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template>
  2. <view class="tabPage" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <u-navbar title="创衡正念企业教练" bgColor="#FFFFFF" :titleStyle="{'font-size':'32rpx','font-weight':'bold'}">
  4. <view class="u-nav-slot" slot="left" style="display: flex;background-color: transparent;">
  5. <image src="@/static/history_dialog.png" style="width: 42rpx;height: 42rpx;" @tap="reviewHistory"></image>
  6. <image src="@/static/new_dialog.png" style="width: 42rpx;height: 42rpx;margin-left: 40rpx;" @tap="startNewDialog"></image>
  7. </view>
  8. </u-navbar>
  9. <div class="dialogs container" ref="messageContainer">
  10. <div class="d_answer init">
  11. <div class="da_top adfac">
  12. <image src="@/static/logo.png"></image>
  13. <text>创衡正念企业教练</text>
  14. </div>
  15. <div class="da_content">
  16. Hi,我是企业教练,很高兴遇见你!我随时可以回答您的问题!
  17. </div>
  18. </div>
  19. <div v-for="(item,index) in dialogList" :key="index">
  20. <div class="d_question">
  21. <div class="dq_text">{{ item.question }}</div>
  22. </div>
  23. <div class="d_answer">
  24. <div class="da_top adfac">
  25. <image src="@/static/logo.png"></image>
  26. <text>创衡正念企业教练</text>
  27. </div>
  28. <div class="da_content">
  29. <u-parse :content="item.answer"></u-parse>
  30. <div class="dc_btns adfacjb" v-if="item.answer">
  31. <div class="db_l">
  32. <image :src="item.copy?require('@/static/copy_active.png'):require('@/static/copy.png')" @tap="toCopy(item,index)"></image>
  33. <image :src="item.upvote?require('@/static/upvote_active.png'):require('@/static/upvote.png')" @tap="toUpvote(item,index)"></image>
  34. <image :src="item.comment?require('@/static/comment_active.png'):require('@/static/comment.png')" @tap="toComment(item,index)"></image>
  35. </div>
  36. <div class="db_r">
  37. <image :src="item.share?require('@/static/share_active.png'):require('@/static/share.png')" @tap="toShare(item,index)"></image>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. <div class="ask adfacjb">
  45. <div class="a_l">
  46. <input type="text" v-model="question" placeholder="请输入您的问题" @confirm="sendQuestion" ref="questionInp">
  47. </div>
  48. <image class="a_r" src="@/static/send.png" @tap="sendQuestion"></image>
  49. </div>
  50. <u-modal :show="commentShow" title="评论" @confirm="commentConfirm" @cancel="commentCancel" @close="commentCancel" :showCancelButton="true">
  51. <u-textarea v-model="content" placeholder="对于我们的回答您是否不满意,您有更好的答案建议吗?"></u-textarea>
  52. </u-modal>
  53. <u-popup :show="historyShow" mode="left" @close="historyClose">
  54. <view class="history" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  55. <u-navbar title="历史回答" bgColor="#FFFFFF" :titleStyle="{'font-size':'32rpx','font-weight':'bold'}" style="width: 672rpx;">
  56. <view class="u-nav-slot" slot="left" style="display: flex;background-color: transparent;">
  57. <image src="@/static/arrow_left_hei.png" style="width: 40rpx;height: 40rpx;" @tap="historyClose"></image>
  58. </view>
  59. </u-navbar>
  60. <div class="h_list">
  61. <div class="hl_item" v-for="(item,index) in historyList" :key="index">
  62. <div class="hi_date">{{item.date}}</div>
  63. <div class="hl_pre" v-for="(pre,idx) in item.list" :key="idx">
  64. {{pre.name}}
  65. </div>
  66. </div>
  67. </div>
  68. </view>
  69. </u-popup>
  70. <Tabbar :tabbarIndex="0"></Tabbar>
  71. </view>
  72. </template>
  73. <script>
  74. var timer = null;
  75. let requestTask = null;
  76. import { BaseApi } from '../http/baseApi.js'
  77. import Tabbar from '@/components/CusTabbar/index.vue'
  78. import * as TextEncoding from "text-encoding-shim";
  79. export default {
  80. components:{ Tabbar },
  81. data(){
  82. return {
  83. retryCount: 3, // 最大重试次数
  84. currentRetry: 0, // 当前重试次数
  85. isRequesting: false, // 请求状态锁
  86. question:'',
  87. streamingResponse:'',
  88. receivedData:'',
  89. dialogList:[],
  90. windex:0,
  91. commentShow:false,
  92. content:'',
  93. cindex:'',
  94. historyShow:false,
  95. historyList:[
  96. {
  97. date:'今天',
  98. list:[
  99. {name:'教练的标准是什么'},
  100. {name:'教练的标准是什么?'},
  101. ]
  102. },
  103. {
  104. date:'2025-05-12',
  105. list:[
  106. {name:'小众水果网名创意分享'},
  107. {name:'教练的标准是什么?'},
  108. ]
  109. },
  110. ]
  111. }
  112. },
  113. methods:{
  114. reviewHistory(){
  115. this.historyShow = true;
  116. },
  117. startNewDialog(){
  118. clearInterval(timer)
  119. this.dialogList = [];
  120. this.question = '';
  121. this.streamingResponse = '';
  122. },
  123. historyClose(){
  124. this.historyShow = false;
  125. },
  126. // 封装带重试机制的请求方法
  127. async sendRequestWithRetry() {
  128. if (this.isRequesting) return;
  129. this.isRequesting = true;
  130. this.currentRetry = 0;
  131. try {
  132. await this._executeRequest();
  133. } catch (error) {
  134. console.error('最终请求失败:', error);
  135. this.$showToast('请求失败,请稍后重试')
  136. } finally {
  137. this.isRequesting = false;
  138. }
  139. },
  140. // 实际执行请求的方法
  141. _executeRequest() {
  142. return new Promise((resolve, reject) => {
  143. requestTask = uni.request({
  144. url: `${BaseApi}/core/chat/streamingMessage`,
  145. method: 'POST',
  146. timeout: 10000,
  147. data:{
  148. query: this.question,
  149. timestamp:Date.now()
  150. },
  151. header: {
  152. 'Content-Type': 'application/json',
  153. 'token': uni.getStorageSync('token') || ''
  154. },
  155. enableChunked: true, // 启用流式接收
  156. responseType:'text',
  157. success: (res) => {
  158. if (res.statusCode === 200) {
  159. this._handleSuccess(res.data);
  160. resolve();
  161. } else {
  162. this._handleError(`状态码异常: ${res.statusCode}`, resolve, reject);
  163. }
  164. },
  165. fail: (err) => {
  166. this._handleError(err.errMsg, resolve, reject);
  167. },
  168. complete: (com) => {
  169. console.log('请求完成',com)
  170. }
  171. });
  172. requestTask.onChunkReceived(async (res) => {
  173. console.log('res',res)
  174. const uint8Array = new Uint8Array(res.data);
  175. // const decoder = new TextDecoder("utf-8");
  176. const decoder = new TextEncoding.TextDecoder("utf-8");
  177. const decodedString = decoder.decode(uint8Array);
  178. let newtext = decodedString.replaceAll('data:','')
  179. newtext = newtext.replace(/\s+/g,'');
  180. if(newtext){
  181. let answer = this.dialogList[this.dialogList.length-1].answer+newtext;
  182. this.$set(this.dialogList[this.dialogList.length-1],'answer',answer);
  183. uni.pageScrollTo({ scrollTop: 99999, duration: 300});
  184. }
  185. });
  186. });
  187. },
  188. // 成功处理
  189. _handleSuccess(data) {
  190. if (data) {
  191. this.streamingResponse += data;
  192. console.log(this.streamingResponse,'streamingResponse');
  193. }
  194. this.currentRetry = 0; // 重置重试计数器
  195. },
  196. // 错误处理
  197. _handleError(errorMsg, resolve, reject) {
  198. console.error(`请求失败 (${this.currentRetry}/${this.retryCount}):`, errorMsg);
  199. if (this._shouldRetry(errorMsg)) {
  200. this.currentRetry++;
  201. setTimeout(() => {
  202. this._executeRequest().then(resolve).catch(reject);
  203. }, this._getRetryDelay());
  204. } else {
  205. reject(errorMsg);
  206. }
  207. },
  208. // 判断是否需要重试
  209. _shouldRetry(errorMsg) {
  210. const retryableErrors = [
  211. 'timeout',
  212. 'request:fail',
  213. 'Network Error'
  214. ];
  215. return this.currentRetry < this.retryCount && retryableErrors.some(e => errorMsg.includes(e));
  216. },
  217. // 获取指数退避延迟时间
  218. _getRetryDelay() {
  219. return Math.min(1000 * Math.pow(2, this.currentRetry), 10000);
  220. },
  221. sendQuestion(){
  222. if(!this.question) return this.$showToast('请输入问题');
  223. let qa = {
  224. question:JSON.parse(JSON.stringify(this.question)),
  225. answer:'',
  226. copy:false,
  227. upvote:false,
  228. comment:'',
  229. share:false,
  230. }
  231. this.dialogList = [...this.dialogList,...[qa]];
  232. this.$nextTick(()=>{
  233. uni.pageScrollTo({ scrollTop: 99999, duration: 300});
  234. this.sendRequestWithRetry();
  235. this.question = '';
  236. })
  237. },
  238. toCopy(item,index){
  239. this.$set(this.dialogList[index],'copy',!this.dialogList[index].copy);
  240. },
  241. toUpvote(item,index){
  242. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  243. },
  244. toComment(item,index){
  245. this.cindex = index;
  246. this.commentShow = true;
  247. },
  248. toShare(item,index){
  249. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  250. },
  251. commentConfirm(){
  252. this.$set(this.dialogList[this.cindex],'comment',this.content);
  253. this.commentShow = false;
  254. },
  255. commentCancel(){
  256. this.content = '';
  257. this.commentShow = false;
  258. },
  259. }
  260. }
  261. </script>
  262. <style>
  263. .history .u-navbar .u-status-bar,.history .u-navbar .u-navbar__content{
  264. width: 672rpx !important;
  265. }
  266. </style>
  267. <style scoped lang="scss">
  268. .tabPage{
  269. position: relative;
  270. background: #F7F2F6;
  271. display: flex;
  272. flex-direction: column;
  273. .history{
  274. width: 672rpx;
  275. height: 100vh;
  276. background: #FFFFFF;
  277. box-sizing: border-box;
  278. display: flex;
  279. flex-direction: column;
  280. .h_list{
  281. flex: 1;
  282. width: 100%;
  283. padding: 0 64rpx;
  284. box-sizing: border-box;
  285. margin-top: 20rpx;
  286. overflow-y: auto;
  287. .hl_item{
  288. margin-top: 64rpx;
  289. &:first-child{
  290. margin-top: 0;
  291. }
  292. .hi_date{
  293. font-family: PingFangSC, PingFang SC;
  294. font-weight: 400;
  295. font-size: 30rpx;
  296. color: #A4A4A4;
  297. line-height: 42rpx;
  298. }
  299. .hl_pre{
  300. font-family: PingFangSC, PingFang SC;
  301. font-weight: 400;
  302. font-size: 30rpx;
  303. color: #000000;
  304. line-height: 42rpx;
  305. margin-top: 48rpx;
  306. }
  307. }
  308. }
  309. }
  310. .dialogs{
  311. width: 100%;
  312. padding: 34rpx 36rpx 164rpx;
  313. flex: 1;
  314. box-sizing: border-box;
  315. overflow-y: auto;
  316. .d_answer{
  317. margin-top: 40rpx;
  318. &.init{
  319. margin-top: 0;
  320. }
  321. .da_top{
  322. image{
  323. width: 48rpx;
  324. height: 48rpx;
  325. }
  326. text{
  327. font-family: PingFang-SC, PingFang-SC;
  328. font-weight: bold;
  329. font-size: 30rpx;
  330. color: #505050;
  331. line-height: 42rpx;
  332. margin-left: 20rpx;
  333. }
  334. }
  335. .da_content{
  336. padding: 30rpx 32rpx;
  337. margin-top: 20rpx;
  338. background: #FFFFFF;
  339. border-radius: 4rpx 24rpx 24rpx 24rpx;
  340. .dc_btns{
  341. margin-top: 44rpx;
  342. image{
  343. width: 48rpx;
  344. height: 48rpx;
  345. }
  346. .db_l{
  347. image{
  348. margin-right: 40rpx;
  349. }
  350. }
  351. }
  352. }
  353. }
  354. .d_question{
  355. margin-top: 40rpx;
  356. display: flex;
  357. justify-content: flex-end;
  358. .dq_text{
  359. background: #833478;
  360. border-radius: 24rpx 4rpx 24rpx 24rpx;
  361. font-family: PingFangSC, PingFang SC;
  362. font-weight: 400;
  363. font-size: 30rpx;
  364. color: #FFFFFF;
  365. line-height: 42rpx;
  366. text-align: right;
  367. padding: 26rpx 30rpx;
  368. }
  369. }
  370. }
  371. .ask{
  372. width: 100%;
  373. height: 130rpx;
  374. background: #FFFFFF;
  375. padding: 21rpx 30rpx;
  376. box-sizing: border-box;
  377. position: fixed;
  378. left: 0;
  379. bottom: 172rpx;
  380. z-index: 2;
  381. .a_l{
  382. width: calc(100% - 102rpx);
  383. height: 88rpx;
  384. padding: 20rpx 30rpx;
  385. box-sizing: border-box;
  386. border-radius: 44rpx;
  387. border: 2rpx solid rgba(131,52,120,0.74);
  388. input{
  389. border: none;
  390. font-family: PingFangSC, PingFang SC;
  391. font-weight: 400;
  392. font-size: 28rpx;
  393. color: #252525;
  394. line-height: 48rpx;
  395. &::placeholder{
  396. color: #808080;
  397. }
  398. }
  399. }
  400. .a_r{
  401. width: 72rpx;
  402. height: 72rpx;
  403. border-radius: 36rpx;
  404. }
  405. }
  406. }
  407. </style>