index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <view class="box">
  3. <view class="flex-common-css iPhone-bottom box-border-box">
  4. <Nav title="AI助手" titleColor="#333" bgckgroundBox='#fff' :leftShow='false'></Nav>
  5. <scroll-view :scroll-y="true" id="scroll-view-css" class="scroll-view-css" :scroll-top="scrollTop"
  6. :scroll-with-animation="true" @scroll="scrolll">
  7. <view v-if="total > chatData.length" class="text-align-center fs-24 fc-9" @click="lookHistory">
  8. 查看历史消息
  9. </view>
  10. <view class="chat-box" id="chat-box">
  11. <msgItem v-for="(item,index) in chatData" :key="index" :item="item" :showType="item.showType"
  12. :msgText="item.content" @selectStyle="selectStyle" @refresh="refresh"></msgItem>
  13. </view>
  14. </scroll-view>
  15. <view class="bottom-box">
  16. <view class="input-box">
  17. <input type="text" v-model="chatText" placeholder="有问题尽管问我" class="input" cursor-spacing="20"
  18. @confirm="confirm" :disabled='!sendControl'>
  19. </view>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. import tools from "@/service/tools.js";
  26. import msgItem from "./msg-item.vue";
  27. // import {
  28. // qianfanSend,
  29. // qianfanMsg_list,
  30. // } from "@/api/qianfan.js"
  31. import {
  32. aiSend,
  33. aiMsgList
  34. } from "@/api/ai.js"
  35. export default {
  36. components: {
  37. msgItem
  38. },
  39. data() {
  40. return {
  41. total: 0,
  42. page: 1,
  43. chatText: '',
  44. chatType: 1,
  45. scrollTop: 20,
  46. chatData: [],
  47. initialData: [],
  48. // loadingStyle: false,
  49. Ajax: false,
  50. timer: null, //settimeout
  51. showText: "",
  52. typeName: '',
  53. isScrolll: false,
  54. scrolllTimeServe: null,
  55. imgUrl: '',
  56. fileUrl: '',
  57. voiceUrl: '',
  58. inputHeadShow: true,
  59. sendControl: true
  60. };
  61. },
  62. onShow() {
  63. this.page = 1
  64. this.scrollTop = 20
  65. this.chatData = []
  66. this.aiMsgList()
  67. },
  68. onLoad() {},
  69. mounted() {},
  70. methods: {
  71. selectStyle(e) {
  72. this.chatType = e
  73. this.initialData.forEach((item) => {
  74. if (this.chatType === item.type) {
  75. this.typeName = item.name
  76. let data = {
  77. msg_type: 2,
  78. content: '您好,我是' + this.typeName + 'AI小助手,有什么能帮助您吗?',
  79. created_at: tools.getDate(),
  80. }
  81. this.chatData.push(data)
  82. }
  83. })
  84. // console.log(e);
  85. },
  86. aiSend() {
  87. let that = this
  88. let data = {
  89. msg_type: 2,
  90. content: '',
  91. created_at: tools.getDate(),
  92. showType: true,
  93. }
  94. this.chatData.push(data)
  95. let key = this.chatData.length - 1
  96. aiSend({
  97. content: this.chatText,
  98. }).then((res) => {
  99. if (res.code == 1) {
  100. this.imgUrl = ''
  101. this.fileUrl = ''
  102. this.voice_url = ''
  103. this.chatText = ''
  104. this.sendControl = true
  105. // this.chatData[key].voice_url = res.data.voice_url
  106. this.chatData[key].content = res.data.content
  107. this.$nextTick(() => {
  108. this.scrollToBottom()
  109. })
  110. } else {
  111. this.chatData[key].content = msg
  112. }
  113. })
  114. },
  115. refresh(num) {
  116. // this.$nextTick(() => {
  117. if (num) {
  118. // console.log("高度");
  119. // this.scrollTop = 99999
  120. console.log('最终')
  121. setTimeout(() => {
  122. this.scrollToBottom(50)
  123. }, 50)
  124. } else {
  125. console.log('回调执行')
  126. this.scrollToBottom(0)
  127. // setTimeout(()=>{
  128. // this.scrollToBottom()
  129. // },10)
  130. }
  131. // })
  132. },
  133. lookHistory() {
  134. if (this.total > this.chatData.length) {
  135. ++this.page
  136. this.aiMsgList()
  137. }
  138. },
  139. aiMsgList() {
  140. aiMsgList({
  141. page: this.page
  142. }).then((res) => {
  143. if (res.code == 1) {
  144. this.total = res.data.total
  145. let data = res.data.items.reverse()
  146. data.forEach((itm) => {
  147. itm.showType = false
  148. })
  149. this.chatData.unshift(...data)
  150. let that = this
  151. // this.$nextTick(() => {
  152. // this.scrollToBottom()
  153. // })
  154. setTimeout(() => {
  155. that.scrollToBottom()
  156. }, 50)
  157. }
  158. })
  159. },
  160. // onTabItemTap() {
  161. // uni.hideTabBar()
  162. // },
  163. leftClick() {
  164. uni.switchTab({
  165. url: 'pages/index/index'
  166. })
  167. // uni.showTabBar()
  168. },
  169. confirm(event) {
  170. if (this.sendControl) {
  171. if (event.target.value) {
  172. let timeData = tools.getDate()
  173. let text = {
  174. created_at: timeData,
  175. msg_type: 1,
  176. content: event.target.value,
  177. img_url: this.imgUrl,
  178. file_url: this.fileUrl
  179. }
  180. this.chatData.push(text)
  181. this.inputHeadShow = false
  182. this.sendControl = false
  183. this.$nextTick(() => {
  184. this.scrollToBottom()
  185. })
  186. this.aiSend()
  187. // this.lading()
  188. this.chatText = ''
  189. } else {
  190. uni.showToast({
  191. title: '请输入问题',
  192. icon: 'none',
  193. duration: 1000
  194. });
  195. }
  196. }
  197. },
  198. scrolll(e) {
  199. // console.log(e, 'eeee');
  200. },
  201. scrollToBottom(num) {
  202. let query = uni.createSelectorQuery().in(this)
  203. query.select('#scroll-view-css').boundingClientRect()
  204. query.select('#chat-box').boundingClientRect()
  205. query.exec((res) => {
  206. console.log(res[1].height, res[0].height, 'res[0].height');
  207. //如果子元素高度大于父元素高度(显示高度)
  208. if (res[1].height > res[0].height) {
  209. //计算出二者差值就是需要滚动的距离
  210. let scrollTop = res[1].height - res[0].height
  211. let differenceNum = scrollTop - this.scrollTop
  212. console.log(differenceNum, 'differenceNum');
  213. console.log(scrollTop, 'scrollTop');
  214. console.log(num, 'num');
  215. if (differenceNum > 50) {
  216. this.scrollTop = scrollTop + 50
  217. console.log('differenceNum > 50 1111111111', this.scrollTop);
  218. } else if (num > 0) {
  219. this.scrollTop = scrollTop + num
  220. console.log('num > 0');
  221. } else {
  222. // this.$nextTick(() => {
  223. this.scrollTop = scrollTop
  224. console.log('differenceNum > 50 33333333333333', this.scrollTop);
  225. // console.log(this.scrollTop,'this.scrollTop 9999999999999');
  226. // this.$forceUpdate()
  227. // })
  228. }
  229. } else {
  230. this.scrollTop = 9999999999999
  231. }
  232. })
  233. },
  234. },
  235. }
  236. </script>
  237. <style lang="scss" scoped>
  238. .box {
  239. background: #fff;
  240. position: relative;
  241. .popup-block {
  242. border-radius: 20rpx 20rpx 0rpx 0rpx;
  243. overflow: hidden;
  244. background-color: #f5f5f5;
  245. .popup-row {
  246. height: 100rpx;
  247. background-color: #fff;
  248. text-align: center;
  249. line-height: 100rpx;
  250. border-bottom: 2rpx solid #f5f5f5;
  251. &:nth-child(4) {
  252. margin-bottom: 20rpx;
  253. }
  254. &:last-child {
  255. height: 112rpx;
  256. border: none;
  257. line-height: 112rpx;
  258. }
  259. &:active {
  260. background-color: rgb(244, 244, 244);
  261. }
  262. }
  263. }
  264. .chat-box {
  265. padding: 22rpx 32rpx 1rpx;
  266. box-sizing: border-box;
  267. .dialogue-box {
  268. // width: 100%;
  269. height: auto;
  270. margin: 0 0 40rpx 0;
  271. display: flex;
  272. flex-direction: column;
  273. .time {
  274. font-size: 24rpx;
  275. color: #999999;
  276. margin: 0 0 8rpx 0;
  277. }
  278. .dialogue {
  279. padding: 24rpx;
  280. box-sizing: border-box;
  281. word-break: break-all;
  282. font-size: 30rpx;
  283. .dialogue-text {}
  284. .dialogue-text-one {
  285. color: #333333;
  286. }
  287. .dialogue-text-two {
  288. color: #FFFFFF;
  289. }
  290. .initial-box {
  291. .initial-title {
  292. font-size: 30rpx;
  293. font-weight: 600;
  294. color: #333333;
  295. margin: 0 0 12rpx 0;
  296. }
  297. .initial-style {
  298. font-size: 30rpx;
  299. color: #2F80ED;
  300. margin: 0 0 12rpx 0;
  301. }
  302. .initial-style:last-child {
  303. margin: 0;
  304. }
  305. }
  306. }
  307. .dialogue-one {
  308. background: #F7F8FC;
  309. border-radius: 10rpx 10rpx 10rpx 0;
  310. }
  311. .dialogue-two {
  312. background: #FDAB26;
  313. border-radius: 10rpx 10rpx 0 10rpx;
  314. }
  315. }
  316. .dialogue-box-one {
  317. align-items: flex-start;
  318. }
  319. }
  320. .bottom-box {
  321. position: relative;
  322. .input-box {
  323. width: 100%;
  324. height: 100rpx;
  325. height: auto;
  326. background: #fff;
  327. padding: 10rpx 32rpx;
  328. box-sizing: border-box;
  329. border-top: 1rpx solid #EEEEEE;
  330. transition: all .1s linear;
  331. }
  332. }
  333. }
  334. .input {
  335. background: #F2F2F2;
  336. border-radius: 200rpx;
  337. width: 100%;
  338. height: 100%;
  339. padding: 20rpx 170rpx 20rpx 24rpx;
  340. box-sizing: border-box;
  341. }
  342. </style>