dialog.vue 12 KB

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