index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. aiMsgList() {
  161. aiMsgList({
  162. page: this.page
  163. }).then((res) => {
  164. if (res.code == 1) {
  165. this.total = res.data.total;
  166. let data = res.data.items.reverse();
  167. if (data.length === 0) {
  168. console.log('没有新数据加载');
  169. return;
  170. }
  171. if (data.length > 0) {
  172. data.forEach((itm) => {
  173. itm.showType = false;
  174. });
  175. if (this.page === 1) {
  176. this.chatData.unshift(...data);
  177. setTimeout(() => {
  178. this.scrollToBottom()
  179. }, 50)
  180. } else {
  181. // 记录当前 chat-box 的高度
  182. let lodQuery = uni.createSelectorQuery().in(this);
  183. lodQuery.select('#chat-box').boundingClientRect((rect) => {
  184. console.log(rect, 'rect');
  185. if (rect) {
  186. let oldHeight = rect.height; // 记录旧高度
  187. // 插入新数据
  188. this.chatData.unshift(...data);
  189. // 计算新高度并滚动到正确位置
  190. this.$nextTick(() => {
  191. lodQuery.select('#chat-box').boundingClientRect((newRect) => {
  192. if (newRect) {
  193. let newHeight = newRect.height;
  194. let scrollTop = newHeight - oldHeight; // 计算需要滚动的距离
  195. // 设置 scrollTop
  196. this.scrollTop = scrollTop;
  197. console.log(scrollTop,'???????????');
  198. this.$forceUpdate();
  199. }
  200. })
  201. });
  202. }
  203. }).exec()
  204. }
  205. }
  206. }
  207. })
  208. },
  209. leftClick() {
  210. uni.switchTab({
  211. url: 'pages/index/index'
  212. })
  213. // uni.showTabBar()
  214. },
  215. confirm(event) {
  216. if (this.sendControl) {
  217. if (event.target.value) {
  218. let timeData = tools.getDate()
  219. let text = {
  220. created_at: timeData,
  221. msg_type: 1,
  222. content: event.target.value,
  223. img_url: this.imgUrl,
  224. file_url: this.fileUrl
  225. }
  226. this.chatData.push(text)
  227. this.inputHeadShow = false
  228. this.sendControl = false
  229. this.$nextTick(() => {
  230. this.scrollToBottom()
  231. })
  232. this.aiSend()
  233. // this.lading()
  234. this.chatText = ''
  235. } else {
  236. uni.showToast({
  237. title: '请输入问题',
  238. icon: 'none',
  239. duration: 1000
  240. });
  241. }
  242. }
  243. },
  244. scrolll(e) {
  245. // console.log(e, 'eeee');
  246. },
  247. // scrollToBottom(num) {
  248. // let query = uni.createSelectorQuery().in(this)
  249. // query.select('#scroll-view-css').boundingClientRect()
  250. // query.select('#chat-box').boundingClientRect()
  251. // query.exec((res) => {
  252. // console.log(res[1].height, res[0].height, 'res[0].height');
  253. // //如果子元素高度大于父元素高度(显示高度)
  254. // if (res[1].height > res[0].height) {
  255. // //计算出二者差值就是需要滚动的距离
  256. // let scrollTop = res[1].height - res[0].height
  257. // let differenceNum = scrollTop - this.scrollTop
  258. // console.log(differenceNum, 'differenceNum');
  259. // console.log(scrollTop, 'scrollTop');
  260. // console.log(num, 'num');
  261. // if (differenceNum > 50) {
  262. // this.scrollTop = scrollTop + 50
  263. // console.log('differenceNum > 50 1111111111', this.scrollTop);
  264. // } else if (num > 0) {
  265. // this.scrollTop = scrollTop + num
  266. // console.log('num > 0');
  267. // } else {
  268. // this.scrollTop = scrollTop
  269. // console.log('differenceNum > 50 33333333333333', this.scrollTop);
  270. // }
  271. // } else {
  272. // this.scrollTop = 9999999999999
  273. // }
  274. // })
  275. // }
  276. scrollToBottom(num) {
  277. let query = uni.createSelectorQuery().in(this);
  278. query.select('#scroll-view-css').boundingClientRect();
  279. query.select('#chat-box').boundingClientRect();
  280. query.exec((res) => {
  281. if (res[1].height > res[0].height) {
  282. let scrollTop = res[1].height - res[0].height;
  283. let differenceNum = scrollTop - this.scrollTop;
  284. if (differenceNum > 50) {
  285. let topNUm = scrollTop
  286. this.scrollTop = topNUm + 50;
  287. console.log(scrollTop, 'scrollTop1111111111');
  288. } else if (num > 0) {
  289. let topNUm = scrollTop
  290. this.scrollTop = topNUm + num;
  291. console.log(scrollTop, 'scrollTop122222222');
  292. } else {
  293. let topNUm = scrollTop
  294. this.scrollTop = topNUm + 50;
  295. console.log(scrollTop, 'scrollTop333333333');
  296. }
  297. this.$forceUpdate();
  298. } else {
  299. this.scrollTop = 9999999999999;
  300. }
  301. });
  302. }
  303. },
  304. }
  305. </script>
  306. <style lang="scss" scoped>
  307. .box {
  308. background: #fff;
  309. position: relative;
  310. .popup-block {
  311. border-radius: 20rpx 20rpx 0rpx 0rpx;
  312. overflow: hidden;
  313. background-color: #f5f5f5;
  314. .popup-row {
  315. height: 100rpx;
  316. background-color: #fff;
  317. text-align: center;
  318. line-height: 100rpx;
  319. border-bottom: 2rpx solid #f5f5f5;
  320. &:nth-child(4) {
  321. margin-bottom: 20rpx;
  322. }
  323. &:last-child {
  324. height: 112rpx;
  325. border: none;
  326. line-height: 112rpx;
  327. }
  328. &:active {
  329. background-color: rgb(244, 244, 244);
  330. }
  331. }
  332. }
  333. .chat-box {
  334. padding: 22rpx 32rpx 1rpx;
  335. box-sizing: border-box;
  336. .dialogue-box {
  337. // width: 100%;
  338. height: auto;
  339. margin: 0 0 40rpx 0;
  340. display: flex;
  341. flex-direction: column;
  342. .time {
  343. font-size: 24rpx;
  344. color: #999999;
  345. margin: 0 0 8rpx 0;
  346. }
  347. .dialogue {
  348. padding: 24rpx;
  349. box-sizing: border-box;
  350. word-break: break-all;
  351. font-size: 30rpx;
  352. .dialogue-text {}
  353. .dialogue-text-one {
  354. color: #333333;
  355. }
  356. .dialogue-text-two {
  357. color: #FFFFFF;
  358. }
  359. .initial-box {
  360. .initial-title {
  361. font-size: 30rpx;
  362. font-weight: 600;
  363. color: #333333;
  364. margin: 0 0 12rpx 0;
  365. }
  366. .initial-style {
  367. font-size: 30rpx;
  368. color: #2F80ED;
  369. margin: 0 0 12rpx 0;
  370. }
  371. .initial-style:last-child {
  372. margin: 0;
  373. }
  374. }
  375. }
  376. .dialogue-one {
  377. background: #F7F8FC;
  378. border-radius: 10rpx 10rpx 10rpx 0;
  379. }
  380. .dialogue-two {
  381. background: #FDAB26;
  382. border-radius: 10rpx 10rpx 0 10rpx;
  383. }
  384. }
  385. .dialogue-box-one {
  386. align-items: flex-start;
  387. }
  388. }
  389. .bottom-box {
  390. position: relative;
  391. .input-box {
  392. width: 100%;
  393. height: 100rpx;
  394. height: auto;
  395. background: #fff;
  396. padding: 10rpx 32rpx;
  397. box-sizing: border-box;
  398. border-top: 1rpx solid #EEEEEE;
  399. transition: all .1s linear;
  400. }
  401. }
  402. }
  403. .input {
  404. background: #F2F2F2;
  405. border-radius: 200rpx;
  406. width: 100%;
  407. height: 100%;
  408. padding: 20rpx 170rpx 20rpx 24rpx;
  409. box-sizing: border-box;
  410. }
  411. </style>