index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view class="container">
  3. <chatCustom @showDr="handleShowDrawer"/>
  4. <view class="cm_ai_container_main" :style="{ paddingBottom: isIphoneX ? '208rpx' : '174rpx', paddingTop:CustomBar+'px' }">
  5. <scroll-view class="cm_ai_container" scroll-y="true" show-scrollbar="true" ref="scrollView" id="scrollChatView">
  6. <!--提问回答element-->
  7. <view class="cm_ai_answer_main" id="cm_ai_answer">
  8. <chat-html ref="chatHtml" :messages='messages' @scrollUpdate="scrollToBottom" />
  9. </view>
  10. </scroll-view>
  11. <!--提问文本框-->
  12. <view class="cm_ai_content" :style="{ height: isIphoneX ? (120+68) +'rpx' : (120+34) +'rpx', paddingBottom: isIphoneX ? '68rpx' : '34rpx' }">
  13. <view class="cm_ai_input">
  14. <input class="cm_ai_textarea" v-model="questionTextarea" cursor-spacing="30" confirm-type="send" maxlength="200"
  15. placeholder="请输入您想了解的内容" @confirm="handleAskQuestion" />
  16. <view class="cm_ai_button" :class="isLoading ? 'none' : ''" @click="handleAskQuestion">
  17. <text class="iconfont icon-fasong"></text>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. <!-- 对话记录 -->
  23. <chatDrawer :rightDrawer="rightDrawer" :probeIndex="probeIndex" @chat-detail="handleCHatDetail" @chat-news="handleCreateChat" v-if="rightDrawer"/>
  24. </view>
  25. </template>
  26. <script>
  27. import { mapState, mapMutations } from 'vuex'
  28. import chatCustom from './components/chat-custom'
  29. import chatDrawer from './components/chat-drawer'
  30. import chatHtml from './components/chat-html'
  31. export default {
  32. components: {
  33. chatCustom,
  34. chatDrawer,
  35. chatHtml
  36. },
  37. data() {
  38. return {
  39. StaticUrl: this.$Static,
  40. isIphoneX: this.$store.state.isIphoneX,
  41. CustomBar:this.CustomBar,// 顶部导航栏高度
  42. rightDrawer:false,
  43. isLoading: false,
  44. nvabarData: { //顶部自定义导航
  45. haveBack:true,
  46. haveHome:true,
  47. showCapsule:1, // 是否显示左上角图标 1表示显示 0表示不显示,
  48. showSearch: 0,
  49. title: '', // 导航栏 中间的标题
  50. bgColor: '#f5f4f6 ', // 导航栏 中间的标题
  51. textLeft:this.$store.state.isIphone
  52. },
  53. chatParams: {
  54. userId: '',
  55. question: ''
  56. },
  57. questionTextarea: '',
  58. probeChatId: '',
  59. probeIndex: 0,
  60. messages: [],
  61. }
  62. },
  63. onLoad(option) {
  64. },
  65. filters: {
  66. },
  67. computed: {
  68. ...mapState(['hasLogin', 'userInfo', ])
  69. },
  70. methods: {
  71. // 初始化
  72. async initGetStotage() {
  73. const userInfo = await this.$api.getStorage()
  74. this.chatParams.userId = userInfo.userId
  75. this.questionTextarea = ''
  76. this.probeIndex = 0
  77. this.messages = []
  78. },
  79. // 发送
  80. async handleAskQuestion() {
  81. if (!this.hasLogin) {
  82. this.$util.msg('请您登陆后使用AI助手', 2000)
  83. setTimeout(() => {
  84. this.$api.navigateTo('/pages/login/login')
  85. }, 2000)
  86. return
  87. } else {
  88. this.chatParams.question = this.questionTextarea
  89. if (this.isLoading) {
  90. return
  91. }
  92. if (!this.chatParams.question) {
  93. this.$util.msg('请输入内容', 2000)
  94. return
  95. }
  96. this.messages.push({
  97. question: this.chatParams.question,
  98. from: 'me',
  99. typing: false,
  100. currentLength: 0
  101. })
  102. if (this.probeIndex === 0) {
  103. this.messages.push({
  104. question: `老板,稍等一下哈,为您查询${this.chatParams.question}的相关信息。`,
  105. from: 'bot',
  106. typing: false,
  107. currentLength: 0
  108. })
  109. }
  110. this.questionTextarea = ''
  111. this.isLoading = true
  112. setTimeout(() => {
  113. if (this.probeIndex === 0) {
  114. this.handleUserNewChat(this.chatParams)
  115. } else {
  116. this.handleUserSecondChat({ chatId: this.probeChatId, ...this.chatParams })
  117. }
  118. }, 1000)
  119. }
  120. },
  121. //初次提问
  122. async handleUserNewChat(chatParams) {
  123. try {
  124. const res = await this.UserService.userNewChat(chatParams)
  125. const data = res.data
  126. this.probeIndex++
  127. this.probeChatId = data.chatId
  128. this.messages.push({ question: data.result, from: 'bot', typing: false, currentLength: 0 })
  129. this.isLoading = false
  130. } catch (error) {
  131. console.log('error',error)
  132. const msgMap = {
  133. '-1':`您好,目前没有找到与${chatParams.question}相关的信息,可以换个说法试试。`,
  134. '-2':'您的此次对话已超出对话轮次上限,可以刷新页面或新建对话重试。',
  135. '-3':'您的单日对话次数已超出上限,请明日再来尝试提问。'
  136. }
  137. this.messages.push({ question: msgMap[error.code], from: 'bot', typing: false, currentLength: 0 })
  138. this.isLoading = false
  139. }
  140. },
  141. // 追问
  142. async handleUserSecondChat(chatParams) {
  143. try {
  144. const res = await this.UserService.userSecondChat(chatParams)
  145. const data = res.data
  146. this.messages.push({ question: data.result, from: 'bot', typing: false, currentLength: 0 })
  147. this.isLoading = false
  148. } catch (error) {
  149. console.log('error',error)
  150. }
  151. },
  152. handleShowDrawer(){
  153. this.rightDrawer = true
  154. },
  155. handleCreateChat(){
  156. //新建对话
  157. this.probeIndex = 0
  158. this.messages = []
  159. },
  160. handleCHatDetail(chatId){
  161. // 查看详情
  162. this.messages =[]
  163. this.probeIndex = 1
  164. this.probeChatId = chatId
  165. this.userNewChatDetail({ chatId : chatId ,userId:this.chatParams.userId })
  166. },
  167. // 查询详情
  168. async userNewChatDetail(params) {
  169. try {
  170. const res = await this.UserService.userNewChatDetail(params)
  171. const data = res.data
  172. data.forEach(chats => {
  173. let from = chats.identity === 0 ? 'me' : 'bot'
  174. this.messages.push({ question: chats.message,from: from,typing: false,currentLength: 0 })
  175. })
  176. } catch (error) {
  177. console.log('error',error)
  178. }
  179. },
  180. scrollToBottom(flag) {
  181. // console.log('flag',flag)
  182. // this.$nextTick( async () => {
  183. // const scrollChatView = await this.$util.boundingClientRect(this, '#scrollChatView', false)
  184. // // console.log('scrollChatView',scrollChatView.height)
  185. // uni.pageScrollTo({
  186. // duration: 300, //过渡时间必须为0,uniapp bug,否则运行到手机会报错
  187. // scrollTop: scrollChatView.height,
  188. // })
  189. // // this.$refs.scrollView.scrollTop = lastCHatMain.top + lastCHatMain.height
  190. // })
  191. },
  192. //滑动到最底部
  193. async handleScrollUpdate(flag) {
  194. if (flag) {
  195. this.$refs.scrollView.scrollIntoView('#lastElementId', {
  196. behavior: 'smooth' // 平滑滚动
  197. })
  198. }
  199. }
  200. },
  201. onPullDownRefresh() {
  202. // 下拉刷新
  203. setTimeout(() => {
  204. this.probeIndex = 0
  205. this.messages = []
  206. uni.stopPullDownRefresh()
  207. }, 200)
  208. },
  209. onShow() {
  210. this.initGetStotage()
  211. }
  212. }
  213. </script>
  214. <style lang="scss">
  215. page {
  216. width: 100%;
  217. height: 100%;
  218. background-color: #e6ebf7 !important;
  219. }
  220. .cm_ai_container_main {
  221. width: 100%;
  222. height: 100%;
  223. box-sizing: border-box;
  224. 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);
  225. .cm_ai_content {
  226. width: 100%;
  227. padding:20rpx 24rpx 0 24rpx;
  228. background-color: #e6ebf7 !important;
  229. box-sizing: border-box;
  230. position: fixed;
  231. left: 0;
  232. bottom: 0;
  233. .cm_ai_input {
  234. display: flex;
  235. align-items: center;
  236. justify-content: space-between;
  237. width: 100%;
  238. height: 100%;
  239. background-color: #FFFFFF;
  240. box-shadow: 0 16rpx 20rpx 0 rgba(174, 167, 223, .2);
  241. border-radius: 50rpx;
  242. box-sizing: border-box;
  243. padding: 10rpx 100rpx 10rpx 10rpx;
  244. position: relative;
  245. .cm_ai_textarea {
  246. width: 100%;
  247. height: 80rpx !important;
  248. font-size: 30rpx;
  249. line-height: 80rpx;
  250. background-color: transparent;
  251. border: none;
  252. box-shadow: none;
  253. overflow: auto;
  254. scrollbar-width: none;
  255. resize: none;
  256. box-sizing: border-box;
  257. padding-left: 24rpx;
  258. }
  259. .cm_ai_button {
  260. width: 100rpx;
  261. height: 100rpx;
  262. line-height: 100rpx;
  263. border-radius: 20rpx;
  264. display: inline-flex;
  265. align-items: center;
  266. justify-content: center;
  267. font-size: 24rpx;
  268. position: absolute;
  269. right: 0;
  270. &.none {
  271. background-image: radial-gradient(circle at 14% 85%, #e7ecf7 0, rgba(231, 236, 247, 0) 37%);
  272. .icon-fasong {
  273. color: #e6e6e6;
  274. }
  275. }
  276. .icon-fasong {
  277. font-size: 50rpx;
  278. color: #ff5b00;
  279. }
  280. }
  281. }
  282. }
  283. .cm_ai_container {
  284. width: 100%;
  285. height: 100%;
  286. display: flex;
  287. flex-direction: column;
  288. box-sizing: border-box;
  289. padding: 0 24rpx;
  290. .cm_ai_answer_main {
  291. width: 100%;
  292. min-height: 600rpx;
  293. overflow-y: auto;
  294. flex: 9;
  295. }
  296. }
  297. }
  298. </style>