dialog.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. uni.pageScrollTo({ scrollTop: 99999, duration: 300});
  227. this.sendRequestWithRetry();
  228. this.question = '';
  229. },
  230. toCopy(item,index){
  231. this.$set(this.dialogList[index],'copy',!this.dialogList[index].copy);
  232. },
  233. toUpvote(item,index){
  234. this.$set(this.dialogList[index],'upvote',!this.dialogList[index].upvote);
  235. },
  236. toComment(item,index){
  237. this.cindex = index;
  238. this.commentShow = true;
  239. },
  240. toShare(item,index){
  241. this.$set(this.dialogList[index],'share',!this.dialogList[index].share);
  242. },
  243. commentConfirm(){
  244. this.$set(this.dialogList[this.cindex],'comment',this.content);
  245. this.commentShow = false;
  246. },
  247. commentCancel(){
  248. this.content = '';
  249. this.commentShow = false;
  250. },
  251. }
  252. }
  253. </script>
  254. <style>
  255. .history .u-navbar .u-status-bar,.history .u-navbar .u-navbar__content{
  256. width: 672rpx !important;
  257. }
  258. </style>
  259. <style scoped lang="scss">
  260. .tabPage{
  261. position: relative;
  262. background: #F7F2F6;
  263. display: flex;
  264. flex-direction: column;
  265. .history{
  266. width: 672rpx;
  267. height: 100vh;
  268. background: #FFFFFF;
  269. box-sizing: border-box;
  270. display: flex;
  271. flex-direction: column;
  272. .h_list{
  273. flex: 1;
  274. width: 100%;
  275. padding: 0 64rpx;
  276. box-sizing: border-box;
  277. margin-top: 20rpx;
  278. overflow-y: auto;
  279. .hl_item{
  280. margin-top: 64rpx;
  281. &:first-child{
  282. margin-top: 0;
  283. }
  284. .hi_date{
  285. font-family: PingFangSC, PingFang SC;
  286. font-weight: 400;
  287. font-size: 30rpx;
  288. color: #A4A4A4;
  289. line-height: 42rpx;
  290. }
  291. .hl_pre{
  292. font-family: PingFangSC, PingFang SC;
  293. font-weight: 400;
  294. font-size: 30rpx;
  295. color: #000000;
  296. line-height: 42rpx;
  297. margin-top: 48rpx;
  298. }
  299. }
  300. }
  301. }
  302. .dialogs{
  303. width: 100%;
  304. padding: 34rpx 36rpx 164rpx;
  305. flex: 1;
  306. box-sizing: border-box;
  307. overflow-y: auto;
  308. .d_answer{
  309. margin-top: 40rpx;
  310. &.init{
  311. margin-top: 0;
  312. }
  313. .da_top{
  314. image{
  315. width: 48rpx;
  316. height: 48rpx;
  317. }
  318. text{
  319. font-family: PingFang-SC, PingFang-SC;
  320. font-weight: bold;
  321. font-size: 30rpx;
  322. color: #505050;
  323. line-height: 42rpx;
  324. margin-left: 20rpx;
  325. }
  326. }
  327. .da_content{
  328. padding: 30rpx 32rpx;
  329. margin-top: 20rpx;
  330. background: #FFFFFF;
  331. border-radius: 4rpx 24rpx 24rpx 24rpx;
  332. .dc_btns{
  333. margin-top: 44rpx;
  334. image{
  335. width: 48rpx;
  336. height: 48rpx;
  337. }
  338. .db_l{
  339. image{
  340. margin-right: 40rpx;
  341. }
  342. }
  343. }
  344. }
  345. }
  346. .d_question{
  347. margin-top: 40rpx;
  348. display: flex;
  349. justify-content: flex-end;
  350. .dq_text{
  351. background: #833478;
  352. border-radius: 24rpx 4rpx 24rpx 24rpx;
  353. font-family: PingFangSC, PingFang SC;
  354. font-weight: 400;
  355. font-size: 30rpx;
  356. color: #FFFFFF;
  357. line-height: 42rpx;
  358. text-align: right;
  359. padding: 26rpx 30rpx;
  360. }
  361. }
  362. }
  363. .ask{
  364. width: 100%;
  365. height: 130rpx;
  366. background: #FFFFFF;
  367. padding: 21rpx 30rpx;
  368. box-sizing: border-box;
  369. position: fixed;
  370. left: 0;
  371. bottom: 172rpx;
  372. z-index: 2;
  373. .a_l{
  374. width: calc(100% - 102rpx);
  375. height: 88rpx;
  376. padding: 20rpx 30rpx;
  377. box-sizing: border-box;
  378. border-radius: 44rpx;
  379. border: 2rpx solid rgba(131,52,120,0.74);
  380. input{
  381. border: none;
  382. font-family: PingFangSC, PingFang SC;
  383. font-weight: 400;
  384. font-size: 28rpx;
  385. color: #252525;
  386. line-height: 48rpx;
  387. &::placeholder{
  388. color: #808080;
  389. }
  390. }
  391. }
  392. .a_r{
  393. width: 72rpx;
  394. height: 72rpx;
  395. border-radius: 36rpx;
  396. }
  397. }
  398. }
  399. </style>