index.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <view class="container">
  3. <view class="cm_ai_container_main">
  4. <view class="cm_ai_container">
  5. <!--提问回答element-->
  6. <view class="cm_ai_answer_main" id="cm_ai_answer">
  7. <chat-html ref="chatHtml" :messages='messages' />
  8. </view>
  9. <!--提问文本框-->
  10. <view class="cm_ai_content">
  11. <view class="cm_ai_input">
  12. <input class="cm_ai_textarea" v-model="questionTextarea" placeholder="请输入您想了解的内容"
  13. @confirm="handleAskQuestion" />
  14. <view class="cm_ai_button" :class="isLoading ? 'none' : ''" @click="handleAskQuestion">
  15. <text class="iconfont icon-fasong"></text>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import { mapState, mapMutations } from 'vuex'
  25. import chatHtml from './components/chat-html' //地址信息
  26. export default {
  27. components: {
  28. chatHtml
  29. },
  30. data() {
  31. return {
  32. StaticUrl: this.$Static,
  33. isIphoneX: this.$store.state.isIphoneX,
  34. isLoading: false,
  35. chatParams: {
  36. userId: '',
  37. question: ''
  38. },
  39. questionTextarea: '',
  40. probeChatId: '',
  41. probeIndex: 0,
  42. messages: []
  43. }
  44. },
  45. onLoad(option) {
  46. },
  47. filters: {
  48. },
  49. computed: {
  50. ...mapState(['hasLogin', 'userInfo', ])
  51. },
  52. methods: {
  53. async initGetStotage() {
  54. const userInfo = await this.$api.getStorage()
  55. this.chatParams.userId = userInfo.userId
  56. this.questionTextarea = ''
  57. },
  58. async handleAskQuestion() {
  59. this.chatParams.question = this.questionTextarea
  60. if (this.isLoading) { return }
  61. if (!this.chatParams.question) {
  62. this.$util.msg('请输入内容', 2000)
  63. return
  64. }
  65. this.messages.push({ question: this.chatParams.question, from: 'me', typing: false,
  66. currentLength: 0 })
  67. if (this.probeIndex === 0) {
  68. this.messages.push({ question: `老板,稍等一下哈,我为你查询一下${this.chatParams.question}的相关信息。`, from: 'bot',
  69. typing: false, currentLength: 0 })
  70. }
  71. this.isLoading = true
  72. setTimeout(() => {
  73. if (this.probeIndex === 0) {
  74. this.handleUserNewChat(this.chatParams)
  75. } else {
  76. this.handleUserSecondChat({ chatId: this.probeChatId, ...this.chatParams })
  77. }
  78. }, 1000)
  79. },
  80. async handleUserNewChat(chatParams) {
  81. try {
  82. this.questionTextarea = ''
  83. const res = await this.UserService.userNewChat(chatParams)
  84. const data = res.data
  85. this.probeIndex++
  86. this.probeChatId = data.chatId
  87. this.messages.push({ question: data.result, from: 'bot', typing: false, currentLength: 0 })
  88. this.isLoading = false
  89. } catch (error) {
  90. console.error('Error fetching new chats:', error)
  91. }
  92. },
  93. async handleUserSecondChat(chatParams) {
  94. try {
  95. this.questionTextarea = ''
  96. const res = await this.UserService.userSecondChat(chatParams)
  97. const data = res.data
  98. this.messages.push({ question: data.result, from: 'bot', typing: false, currentLength: 0 })
  99. this.isLoading = false
  100. } catch (error) {
  101. console.error('Error fetching new chats:', error)
  102. }
  103. }
  104. },
  105. onShow() {
  106. this.initGetStotage()
  107. }
  108. }
  109. </script>
  110. <style lang="scss">
  111. page {
  112. width: 100%;
  113. height: 100%;
  114. background-color: #FFFFFF;
  115. }
  116. .cm_ai_container_main {
  117. width: 100%;
  118. height: 100%;
  119. background-image: radial-gradient(circle at 14% 85%, #e7ecf7 0, rgba(231, 236, 247, 0) 37%), radial-gradient(circle at 3.4% 3.7%, rgba(245, 237, 241, .5) 0, rgba(245, 237, 241, 0) 28%), radial-gradient(circle at 100% 18%, #e8ebea 0, hsla(160, 7%, 92%, 0) 30%), linear-gradient(180deg, #f5f4f6, #e6ebf7);
  120. .cm_ai_container {
  121. width: 100%;
  122. height: 100%;
  123. display: flex;
  124. flex-direction: column;
  125. box-sizing: border-box;
  126. padding: 0 24rpx 40rpx 24rpx;
  127. .cm_ai_content {
  128. width: 100%;
  129. height: 88rpx;
  130. background-color: #FFFFFF;
  131. box-shadow: 0 16rpx 20rpx 0 rgba(174, 167, 223, .2);
  132. border-radius: 40rpx;
  133. box-sizing: border-box;
  134. padding: 14rpx 24rpx;
  135. margin: 20rpx 0;
  136. position: relative;
  137. .cm_ai_input {
  138. display: flex;
  139. align-items: center;
  140. justify-content: space-between;
  141. width: 100%;
  142. height: 60rpx;
  143. overflow: hidden;
  144. box-sizing: border-box;
  145. padding-right: 60rpx;
  146. position: relative;
  147. input {
  148. width: 100%;
  149. height: 60rpx !important;
  150. font-size: 24rpx;
  151. line-height: 60rpx;
  152. background-color: transparent;
  153. border: none;
  154. box-shadow: none;
  155. overflow: auto;
  156. scrollbar-width: none;
  157. resize: none;
  158. box-sizing: border-box;
  159. padding-left: 4rpx;
  160. }
  161. .cm_ai_button {
  162. width: 60rpx;
  163. height: 60rpx;
  164. line-height: 60rpx;
  165. border-radius: 20rpx;
  166. display: inline-flex;
  167. align-items: center;
  168. justify-content: center;
  169. font-size: 24rpx;
  170. position: absolute;
  171. right: 0;
  172. &.none {
  173. background-image: radial-gradient(circle at 14% 85%, #e7ecf7 0, rgba(231, 236, 247, 0) 37%);
  174. .icon-fasong {
  175. color: #e6e6e6;
  176. }
  177. }
  178. .icon-fasong {
  179. font-size: 50rpx;
  180. color: #ff5b00;
  181. }
  182. }
  183. }
  184. }
  185. .cm_ai_answer_main {
  186. width: 100%;
  187. min-height: 600rpx;
  188. overflow-y: auto;
  189. flex: 9;
  190. }
  191. }
  192. }
  193. </style>