index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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.sendControl = true
  67. this.aiMsgList()
  68. },
  69. onLoad() {},
  70. mounted() {},
  71. methods: {
  72. selectStyle(e) {
  73. this.chatType = e
  74. this.initialData.forEach((item) => {
  75. if (this.chatType === item.type) {
  76. this.typeName = item.name
  77. let data = {
  78. msg_type: 2,
  79. content: '您好,我是' + this.typeName + 'AI小助手,有什么能帮助您吗?',
  80. created_at: tools.getDate(),
  81. }
  82. this.chatData.push(data)
  83. }
  84. })
  85. // console.log(e);
  86. },
  87. aiSend() {
  88. let that = this
  89. let data = {
  90. msg_type: 2,
  91. content: '',
  92. created_at: tools.getDate(),
  93. showType: true,
  94. }
  95. this.chatData.push(data)
  96. let key = this.chatData.length - 1
  97. aiSend({
  98. content: this.chatText,
  99. }).then((res) => {
  100. if (res.code == 1) {
  101. this.imgUrl = ''
  102. this.fileUrl = ''
  103. this.voice_url = ''
  104. this.chatText = ''
  105. this.sendControl = true
  106. // this.chatData[key].voice_url = res.data.voice_url
  107. this.chatData[key].content = res.data.content
  108. this.$nextTick(() => {
  109. this.scrollToBottom()
  110. })
  111. } else {
  112. this.chatData[key].content = msg
  113. }
  114. })
  115. },
  116. refresh(num) {
  117. // this.$nextTick(() => {
  118. if (num) {
  119. // console.log("高度");
  120. // this.scrollTop = 99999
  121. console.log('最终')
  122. setTimeout(() => {
  123. this.scrollToBottom(50)
  124. }, 50)
  125. } else {
  126. console.log('回调执行')
  127. this.scrollToBottom(0)
  128. // setTimeout(()=>{
  129. // this.scrollToBottom()
  130. // },10)
  131. }
  132. // })
  133. },
  134. lookHistory() {
  135. if (this.total > this.chatData.length) {
  136. ++this.page
  137. this.aiMsgList()
  138. }
  139. },
  140. // aiMsgList() {
  141. // aiMsgList({
  142. // page: this.page
  143. // }).then((res) => {
  144. // if (res.code == 1) {
  145. // this.total = res.data.total
  146. // let data = res.data.items.reverse()
  147. // data.forEach((itm) => {
  148. // itm.showType = false
  149. // })
  150. // this.chatData.unshift(...data)
  151. // let that = this
  152. // // this.$nextTick(() => {
  153. // // this.scrollToBottom()
  154. // // })
  155. // setTimeout(() => {
  156. // that.scrollToBottom()
  157. // }, 50)
  158. // }
  159. // })
  160. // },
  161. aiMsgList() {
  162. aiMsgList({
  163. page: this.page
  164. }).then((res) => {
  165. if (res.code == 1) {
  166. this.total = res.data.total;
  167. let data = res.data.items.reverse();
  168. if (data.length === 0) {
  169. console.log('没有新数据加载');
  170. return;
  171. }
  172. if (data.length > 0) {
  173. data.forEach((itm) => {
  174. itm.showType = false;
  175. });
  176. if (this.page === 1) {
  177. this.chatData.unshift(...data);
  178. setTimeout(() => {
  179. this.scrollToBottom()
  180. }, 50)
  181. } else {
  182. // 记录当前 chat-box 的高度
  183. let lodQuery = uni.createSelectorQuery().in(this);
  184. lodQuery.select('#chat-box').boundingClientRect((rect) => {
  185. console.log(rect, 'rect');
  186. if (rect) {
  187. let oldHeight = rect.height; // 记录旧高度
  188. // 插入新数据
  189. this.chatData.unshift(...data);
  190. // 计算新高度并滚动到正确位置
  191. this.$nextTick(() => {
  192. lodQuery.select('#chat-box').boundingClientRect((newRect) => {
  193. if (newRect) {
  194. let newHeight = newRect.height;
  195. let scrollTop = newHeight - oldHeight; // 计算需要滚动的距离
  196. // 设置 scrollTop
  197. this.scrollTop = scrollTop;
  198. console.log(scrollTop,'???????????');
  199. this.$forceUpdate();
  200. }
  201. })
  202. });
  203. }
  204. }).exec()
  205. }
  206. }
  207. }
  208. })
  209. },
  210. leftClick() {
  211. uni.switchTab({
  212. url: 'pages/index/index'
  213. })
  214. // uni.showTabBar()
  215. },
  216. confirm(event) {
  217. if (this.sendControl) {
  218. if (event.target.value) {
  219. let timeData = tools.getDate()
  220. let text = {
  221. created_at: timeData,
  222. msg_type: 1,
  223. content: event.target.value,
  224. img_url: this.imgUrl,
  225. file_url: this.fileUrl
  226. }
  227. this.chatData.push(text)
  228. this.inputHeadShow = false
  229. this.sendControl = false
  230. this.$nextTick(() => {
  231. this.scrollToBottom()
  232. })
  233. this.aiSend()
  234. // this.lading()
  235. this.chatText = ''
  236. } else {
  237. uni.showToast({
  238. title: '请输入问题',
  239. icon: 'none',
  240. duration: 1000
  241. });
  242. }
  243. }
  244. },
  245. scrolll(e) {
  246. // console.log(e, 'eeee');
  247. },
  248. // scrollToBottom(num) {
  249. // let query = uni.createSelectorQuery().in(this)
  250. // query.select('#scroll-view-css').boundingClientRect()
  251. // query.select('#chat-box').boundingClientRect()
  252. // query.exec((res) => {
  253. // console.log(res[1].height, res[0].height, 'res[0].height');
  254. // //如果子元素高度大于父元素高度(显示高度)
  255. // if (res[1].height > res[0].height) {
  256. // //计算出二者差值就是需要滚动的距离
  257. // let scrollTop = res[1].height - res[0].height
  258. // let differenceNum = scrollTop - this.scrollTop
  259. // console.log(differenceNum, 'differenceNum');
  260. // console.log(scrollTop, 'scrollTop');
  261. // console.log(num, 'num');
  262. // if (differenceNum > 50) {
  263. // this.scrollTop = scrollTop + 50
  264. // console.log('differenceNum > 50 1111111111', this.scrollTop);
  265. // } else if (num > 0) {
  266. // this.scrollTop = scrollTop + num
  267. // console.log('num > 0');
  268. // } else {
  269. // this.scrollTop = scrollTop
  270. // console.log('differenceNum > 50 33333333333333', this.scrollTop);
  271. // }
  272. // } else {
  273. // this.scrollTop = 9999999999999
  274. // }
  275. // })
  276. // }
  277. scrollToBottom(num) {
  278. let query = uni.createSelectorQuery().in(this);
  279. query.select('#scroll-view-css').boundingClientRect();
  280. query.select('#chat-box').boundingClientRect();
  281. query.exec((res) => {
  282. if (res[1].height > res[0].height) {
  283. let scrollTop = res[1].height - res[0].height;
  284. let differenceNum = scrollTop - this.scrollTop;
  285. if (differenceNum > 50) {
  286. let topNUm = scrollTop
  287. this.scrollTop = topNUm + 50;
  288. console.log(scrollTop, 'scrollTop1111111111');
  289. } else if (num > 0) {
  290. let topNUm = scrollTop
  291. this.scrollTop = topNUm + num;
  292. console.log(scrollTop, 'scrollTop122222222');
  293. } else {
  294. let topNUm = scrollTop
  295. this.scrollTop = topNUm + 50;
  296. console.log(scrollTop, 'scrollTop333333333');
  297. }
  298. this.$forceUpdate();
  299. } else {
  300. this.scrollTop = 9999999999999;
  301. }
  302. });
  303. }
  304. },
  305. }
  306. </script>
  307. <style lang="scss" scoped>
  308. .box {
  309. background: #fff;
  310. position: relative;
  311. .popup-block {
  312. border-radius: 20rpx 20rpx 0rpx 0rpx;
  313. overflow: hidden;
  314. background-color: #f5f5f5;
  315. .popup-row {
  316. height: 100rpx;
  317. background-color: #fff;
  318. text-align: center;
  319. line-height: 100rpx;
  320. border-bottom: 2rpx solid #f5f5f5;
  321. &:nth-child(4) {
  322. margin-bottom: 20rpx;
  323. }
  324. &:last-child {
  325. height: 112rpx;
  326. border: none;
  327. line-height: 112rpx;
  328. }
  329. &:active {
  330. background-color: rgb(244, 244, 244);
  331. }
  332. }
  333. }
  334. .chat-box {
  335. padding: 22rpx 32rpx 1rpx;
  336. box-sizing: border-box;
  337. .dialogue-box {
  338. // width: 100%;
  339. height: auto;
  340. margin: 0 0 40rpx 0;
  341. display: flex;
  342. flex-direction: column;
  343. .time {
  344. font-size: 24rpx;
  345. color: #999999;
  346. margin: 0 0 8rpx 0;
  347. }
  348. .dialogue {
  349. padding: 24rpx;
  350. box-sizing: border-box;
  351. word-break: break-all;
  352. font-size: 30rpx;
  353. .dialogue-text {}
  354. .dialogue-text-one {
  355. color: #333333;
  356. }
  357. .dialogue-text-two {
  358. color: #FFFFFF;
  359. }
  360. .initial-box {
  361. .initial-title {
  362. font-size: 30rpx;
  363. font-weight: 600;
  364. color: #333333;
  365. margin: 0 0 12rpx 0;
  366. }
  367. .initial-style {
  368. font-size: 30rpx;
  369. color: #2F80ED;
  370. margin: 0 0 12rpx 0;
  371. }
  372. .initial-style:last-child {
  373. margin: 0;
  374. }
  375. }
  376. }
  377. .dialogue-one {
  378. background: #F7F8FC;
  379. border-radius: 10rpx 10rpx 10rpx 0;
  380. }
  381. .dialogue-two {
  382. background: #FDAB26;
  383. border-radius: 10rpx 10rpx 0 10rpx;
  384. }
  385. }
  386. .dialogue-box-one {
  387. align-items: flex-start;
  388. }
  389. }
  390. .bottom-box {
  391. position: relative;
  392. .input-box {
  393. width: 100%;
  394. height: 100rpx;
  395. height: auto;
  396. background: #fff;
  397. padding: 10rpx 32rpx;
  398. box-sizing: border-box;
  399. border-top: 1rpx solid #EEEEEE;
  400. transition: all .1s linear;
  401. }
  402. }
  403. }
  404. .input {
  405. background: #F2F2F2;
  406. border-radius: 200rpx;
  407. width: 100%;
  408. height: 100%;
  409. padding: 20rpx 170rpx 20rpx 24rpx;
  410. box-sizing: border-box;
  411. }
  412. </style>