chat.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <template>
  2. <view>
  3. <view class="tips color_fff size_12 align_c" :class="{ 'show':ajax.loading }" @tap="getHistoryMsg">
  4. {{ajax.loadText}}
  5. </view>
  6. <view class="box-1" id="list-box">
  7. <view class="talk-list">
  8. <view v-for="(item,index) in talkList" :key="index" :id="`msg-${item.id}`">
  9. <view class="item flex_col" :class=" item.type == 1 ? 'push':'pull' ">
  10. <image :src="item.pic" mode="aspectFill" class="pic"></image>
  11. <view class="content">{{item.content}}</view>
  12. </view>
  13. </view>
  14. </view>
  15. </view>
  16. <view class="box-2">
  17. <view class="flex_col">
  18. <view class="flex_grow">
  19. <input type="text" class="content" v-model="content" placeholder="请输入聊天内容"
  20. placeholder-style="color:#DDD;" :cursor-spacing="6">
  21. </view>
  22. <button class="send" @tap="send">发送</button>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. talkList: [],
  32. ajax: {
  33. rows: 20, //每页数量
  34. page: 1, //页码
  35. flag: true, // 请求开关
  36. loading: true, // 加载中
  37. loadText: '正在获取消息'
  38. },
  39. content: ''
  40. }
  41. },
  42. mounted() {
  43. this.$nextTick(() => {
  44. this.getHistoryMsg();
  45. });
  46. },
  47. onPageScroll(e) {
  48. if (e.scrollTop < 5) {
  49. this.getHistoryMsg();
  50. }
  51. },
  52. methods: {
  53. // 获取历史消息
  54. getHistoryMsg() {
  55. if (!this.ajax.flag) {
  56. return; //
  57. }
  58. // 此处用到 ES7 的 async/await 知识,为使代码更加优美。不懂的请自行学习。
  59. let get = async () => {
  60. this.hideLoadTips();
  61. this.ajax.flag = false;
  62. let data = await this.joinHistoryMsg();
  63. console.log('----- 模拟数据格式,供参考 -----');
  64. console.log(data); // 查看请求返回的数据结构
  65. // 获取待滚动元素选择器,解决插入数据后,滚动条定位时使用
  66. let selector = '';
  67. if (this.ajax.page > 1) {
  68. // 非第一页,则取历史消息数据的第一条信息元素
  69. selector = `#msg-${this.talkList[0].id}`;
  70. } else {
  71. // 第一页,则取当前消息数据的最后一条信息元素
  72. selector = `#msg-${data[data.length-1].id}`;
  73. }
  74. // 将获取到的消息数据合并到消息数组中
  75. this.talkList = [...data, ...this.talkList];
  76. // 数据挂载后执行,不懂的请自行阅读 Vue.js 文档对 Vue.nextTick 函数说明。
  77. this.$nextTick(() => {
  78. // 设置当前滚动的位置
  79. this.setPageScrollTo(selector);
  80. this.hideLoadTips(true);
  81. if (data.length < this.ajax.rows) {
  82. // 当前消息数据条数小于请求要求条数时,则无更多消息,不再允许请求。
  83. // 可在此处编写无更多消息数据时的逻辑
  84. } else {
  85. this.ajax.page++;
  86. // 延迟 200ms ,以保证设置窗口滚动已完成
  87. setTimeout(() => {
  88. this.ajax.flag = true;
  89. }, 200)
  90. }
  91. })
  92. }
  93. get();
  94. },
  95. // 拼接历史记录消息,正式项目可替换为请求历史记录接口
  96. joinHistoryMsg() {
  97. let join = () => {
  98. let arr = [];
  99. //通过当前页码及页数,模拟数据内容
  100. let startIndex = (this.ajax.page - 1) * this.ajax.rows;
  101. let endIndex = startIndex + this.ajax.rows;
  102. for (let i = startIndex; i < endIndex; i++) {
  103. arr.push({
  104. "id": i, // 消息的ID
  105. "content": `这是历史记录的第${i+1}条消息`, // 消息内容
  106. "type": Math.random() > 0.5 ? 1 : 0, // 此为消息类别,设 1 为发出去的消息,0 为收到对方的消息,
  107. "pic": "https://assets.api.uizard.io/api/cdn/stream/db1ab9f9-00bf-4b03-b2b2-1fc8ece9ba19.png" // 头像
  108. })
  109. }
  110. /*
  111. 颠倒数组中元素的顺序。将最新的数据排在本次接口返回数据的最后面。
  112. 后端接口按 消息的时间降序查找出当前页的数据后,再将本页数据按消息时间降序排序返回。
  113. 这是数据的重点,因为页面滚动条和上拉加载历史的问题。
  114. */
  115. arr.reverse();
  116. return arr;
  117. }
  118. // 此处用到 ES6 的 Promise 知识,不懂的请自行学习。
  119. return new Promise((done, fail) => {
  120. // 无数据请求接口,由 setTimeout 模拟,正式项目替换为 ajax 即可。
  121. setTimeout(() => {
  122. let data = join();
  123. done(data);
  124. }, 1500);
  125. })
  126. },
  127. // 设置页面滚动位置
  128. setPageScrollTo(selector) {
  129. let view = uni.createSelectorQuery().in(this).select(selector);
  130. view.boundingClientRect((res) => {
  131. uni.pageScrollTo({
  132. scrollTop: res.top - 30, // -30 为多显示出大半个消息的高度,示意上面还有信息。
  133. duration: 0
  134. });
  135. }).exec();
  136. },
  137. // 隐藏加载提示
  138. hideLoadTips(flag) {
  139. if (flag) {
  140. this.ajax.loadText = '消息获取成功';
  141. setTimeout(() => {
  142. this.ajax.loading = false;
  143. }, 300);
  144. } else {
  145. this.ajax.loading = true;
  146. this.ajax.loadText = '正在获取消息';
  147. }
  148. },
  149. // 发送信息
  150. send() {
  151. if (!this.content) {
  152. uni.showToast({
  153. title: '请输入有效的内容',
  154. icon: 'none'
  155. })
  156. return;
  157. }
  158. uni.showLoading({
  159. title: '正在发送'
  160. })
  161. setTimeout(() => {
  162. uni.hideLoading();
  163. // 将当前发送信息 添加到消息列表。
  164. let data = {
  165. "id": new Date().getTime(),
  166. "content": this.content,
  167. "type": 1,
  168. "pic": "https://assets.api.uizard.io/api/cdn/stream/db1ab9f9-00bf-4b03-b2b2-1fc8ece9ba19.png"
  169. }
  170. this.talkList.push(data);
  171. this.$nextTick(() => {
  172. // 清空内容框中的内容
  173. this.content = '';
  174. uni.pageScrollTo({
  175. scrollTop: 999999, // 设置一个超大值,以保证滚动条滚动到底部
  176. duration: 0
  177. });
  178. })
  179. }, 1500);
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="scss">
  185. /* 设置常用元素尺寸规则 */
  186. view,
  187. textarea,
  188. input,
  189. label,
  190. form,
  191. button,
  192. image {
  193. box-sizing: border-box;
  194. }
  195. /* 按钮样式处理 */
  196. button {
  197. font-size: 28rpx;
  198. }
  199. /* 取消按钮默认的边框线效果 */
  200. button:after {
  201. border: none;
  202. }
  203. /* 设置图片默认样式,取消默认尺寸 */
  204. image {
  205. display: block;
  206. height: auto;
  207. width: auto;
  208. }
  209. /* 输入框默认字体大小 */
  210. textarea,
  211. input {
  212. font-size: 28rpx;
  213. }
  214. ;
  215. /* 列式弹性盒子 */
  216. .flex_col {
  217. display: flex;
  218. flex-direction: row;
  219. flex-wrap: nowrap;
  220. justify-content: flex-start;
  221. align-items: center;
  222. align-content: center;
  223. }
  224. /* 行式弹性盒子 */
  225. .flex_row {
  226. display: flex;
  227. flex-direction: column;
  228. flex-wrap: nowrap;
  229. justify-content: flex-start;
  230. align-items: flex-start;
  231. align-content: flex-start;
  232. }
  233. /* 弹性盒子弹性容器 */
  234. .flex_col .flex_grow {
  235. width: 0;
  236. flex-grow: 1;
  237. }
  238. .flex_row .flex_grow {
  239. flex-grow: 1;
  240. }
  241. /* 弹性盒子允许换行 */
  242. .flex_col.flex_wrap {
  243. flex-wrap: wrap;
  244. }
  245. /* 弹性盒子居中对齐 */
  246. .flex_col.flex_center,
  247. .flex_row.flex_center {
  248. justify-content: center;
  249. }
  250. /* 列式弹性盒子两端对齐 */
  251. .flex_col.flex_space {
  252. justify-content: space-between;
  253. }
  254. /* 弹性盒子快速分栏 ,这里非常郁闷 uniapp 居然不支持 * 选择器 */
  255. .flex_col.flex_col_2>view {
  256. width: 50%;
  257. }
  258. .flex_col.flex_col_3>view {
  259. width: 33.33333%;
  260. }
  261. .flex_col.flex_col_4>view {
  262. width: 25%;
  263. }
  264. .flex_col.flex_col_5>view {
  265. width: 20%;
  266. }
  267. .flex_col.flex_col_6>view {
  268. width: 16.66666%;
  269. }
  270. /* 字体颜色 */
  271. .color_333 {
  272. color: #333;
  273. }
  274. .color_666 {
  275. color: #666;
  276. }
  277. .color_999 {
  278. color: #999;
  279. }
  280. .color_ccc {
  281. color: #ccc;
  282. }
  283. .color_fff {
  284. color: #fff;
  285. }
  286. .color_6dc {
  287. color: #6dca6d;
  288. }
  289. .color_d51 {
  290. color: #d51917;
  291. }
  292. .color_09f {
  293. color: #0099ff;
  294. }
  295. /* 背景色*/
  296. .bg_fff {
  297. background-color: #ffffff;
  298. }
  299. /* 字体大小 */
  300. .size_10 {
  301. font-size: 20rpx;
  302. }
  303. .size_12 {
  304. font-size: 24rpx;
  305. }
  306. .size_14 {
  307. font-size: 28rpx;
  308. }
  309. .size_16 {
  310. font-size: 32rpx;
  311. }
  312. .size_18 {
  313. font-size: 36rpx;
  314. }
  315. .size_20 {
  316. font-size: 40rpx;
  317. }
  318. /* 字体加粗 */
  319. .font_b {
  320. font-weight: bold;
  321. }
  322. /* 对齐方式 */
  323. .align_c {
  324. text-align: center;
  325. }
  326. .align_l {
  327. text-align: left;
  328. }
  329. .align_r {
  330. text-align: right;
  331. }
  332. /* 遮罩 */
  333. .shade {
  334. position: fixed;
  335. top: 0;
  336. right: 0;
  337. bottom: 0;
  338. left: 0;
  339. background-color: rgba(0, 0, 0, 0.8);
  340. z-index: 100;
  341. }
  342. /* 弹窗 */
  343. .shade_box {
  344. position: fixed;
  345. top: 0;
  346. right: 0;
  347. bottom: 0;
  348. left: 0;
  349. margin: auto;
  350. z-index: 101;
  351. min-width: 200rpx;
  352. min-height: 200rpx;
  353. }
  354. /* 加载数据提示 */
  355. .tips {
  356. position: fixed;
  357. left: 0;
  358. top: var(--window-top);
  359. width: 100%;
  360. z-index: 9;
  361. background-color: rgba(0, 0, 0, 0.15);
  362. height: 72rpx;
  363. line-height: 72rpx;
  364. transform: translateY(-80rpx);
  365. transition: transform 0.3s ease-in-out 0s;
  366. &.show {
  367. transform: translateY(0);
  368. }
  369. }
  370. .box-1 {
  371. width: 100%;
  372. height: auto;
  373. padding-bottom: 100rpx;
  374. box-sizing: content-box;
  375. /* 兼容iPhoneX */
  376. margin-bottom: 0;
  377. margin-bottom: constant(safe-area-inset-bottom);
  378. margin-bottom: env(safe-area-inset-bottom);
  379. }
  380. .box-2 {
  381. position: fixed;
  382. left: 0;
  383. width: 100%;
  384. bottom: 0;
  385. height: auto;
  386. z-index: 2;
  387. border-top: #e5e5e5 solid 1px;
  388. box-sizing: content-box;
  389. background-color: #F3F3F3;
  390. /* 兼容iPhoneX */
  391. padding-bottom: 0;
  392. padding-bottom: constant(safe-area-inset-bottom);
  393. padding-bottom: env(safe-area-inset-bottom);
  394. >view {
  395. padding: 0 20rpx;
  396. height: 100rpx;
  397. }
  398. .content {
  399. background-color: #fff;
  400. height: 64rpx;
  401. padding: 0 20rpx;
  402. border-radius: 32rpx;
  403. font-size: 28rpx;
  404. }
  405. .send {
  406. background-color: #42b983;
  407. color: #fff;
  408. height: 64rpx;
  409. margin-left: 20rpx;
  410. border-radius: 32rpx;
  411. padding: 0;
  412. width: 120rpx;
  413. line-height: 62rpx;
  414. &:active {
  415. background-color: #5fc496;
  416. }
  417. }
  418. }
  419. .talk-list {
  420. padding-bottom: 20rpx;
  421. /* 消息项,基础类 */
  422. .item {
  423. padding: 20rpx 20rpx 0 20rpx;
  424. align-items: flex-start;
  425. align-content: flex-start;
  426. color: #333;
  427. .pic {
  428. width: 92rpx;
  429. height: 92rpx;
  430. border-radius: 50%;
  431. border: #fff solid 1px;
  432. }
  433. .content {
  434. padding: 20rpx;
  435. border-radius: 4px;
  436. max-width: 500rpx;
  437. word-break: break-all;
  438. line-height: 52rpx;
  439. position: relative;
  440. }
  441. /* 收到的消息 */
  442. &.pull {
  443. .content {
  444. margin-left: 32rpx;
  445. background-color: #fff;
  446. &::after {
  447. content: '';
  448. display: block;
  449. width: 0;
  450. height: 0;
  451. border-top: 16rpx solid transparent;
  452. border-bottom: 16rpx solid transparent;
  453. border-right: 20rpx solid #fff;
  454. position: absolute;
  455. top: 30rpx;
  456. left: -18rpx;
  457. }
  458. }
  459. }
  460. /* 发出的消息 */
  461. &.push {
  462. /* 主轴为水平方向,起点在右端。使不修改DOM结构,也能改变元素排列顺序 */
  463. flex-direction: row-reverse;
  464. .content {
  465. margin-right: 32rpx;
  466. background-color: #a0e959;
  467. &::after {
  468. content: '';
  469. display: block;
  470. width: 0;
  471. height: 0;
  472. border-top: 16rpx solid transparent;
  473. border-bottom: 16rpx solid transparent;
  474. border-left: 20rpx solid #a0e959;
  475. position: absolute;
  476. top: 30rpx;
  477. right: -18rpx;
  478. }
  479. }
  480. }
  481. }
  482. }
  483. </style>