index.vue 7.4 KB

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