chat.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <view class="chat-box">
  3. <view class="chat-tips" v-if="postData.loading">
  4. {{postData.loadText}}
  5. </view>
  6. <view class="chat-list">
  7. <view :id="`msg-${item.time}`" v-for="(item,index) in chatList" :key="index">
  8. <view class="chat-item" :class="item.from == nowUserId ? 'push':'pull' ">
  9. <image :src="item.fromUserInfo.avatarUrl" mode="aspectFill" class="avatar"></image>
  10. <view class="content-box">
  11. <view class="content" v-if="item.type === 'text'">{{item.body.text}}</view>
  12. <image :src="item.body.thumbnailUrl" mode="aspectFill"
  13. @tap="previewImage(item.body.originalUrl)" :class="[returnImageClass(item.body)]" v-else>
  14. </image>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. <view class="chat-submit">
  20. <input type="text" class="message-content" v-model="message" placeholder="请输入聊天内容"
  21. placeholder-class="message-placeholder" :cursor-spacing="6" />
  22. <view class="send-image send-button" @tap="sendImage">
  23. <uni-icons type="images-filled" color="#8c8c8c" size="24"></uni-icons>
  24. </view>
  25. <view class="send-submit send-button" @tap="send">
  26. <uni-icons type="paperplane-filled" color="#fff" size="24"></uni-icons>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import {
  33. YeIMUniSDK,
  34. YeIMUniSDKDefines
  35. } from '@/uni_modules/wzJun1-YeIM-Uni-SDK/js_sdk/yeim-uni-sdk.min.js'
  36. import {
  37. login
  38. } from '@/request/api/chat.js'
  39. import md5 from '@/js_sdk/js-md5/build/md5.min.js';
  40. export default {
  41. data() {
  42. return {
  43. message: '',
  44. chatList: [],
  45. postData: {
  46. rows: 10, //每页数量
  47. page: 1, //页码
  48. flag: true, // 请求开关
  49. loading: true, // 加载中
  50. loadText: '正在获取消息...'
  51. },
  52. nextMessageId: '',
  53. nowUserId: ''
  54. }
  55. },
  56. onShow() {
  57. //监听新消息
  58. YeIMUniSDK.getInstance().addEventListener(YeIMUniSDKDefines.EVENT.MESSAGE_RECEIVED, this.onMessage);
  59. },
  60. onHide() {
  61. YeIMUniSDK.getInstance().removeEventListener(YeIMUniSDKDefines.EVENT.MESSAGE_RECEIVED, this.onMessage);
  62. },
  63. onLoad(body) {
  64. uni.setNavigationBarTitle({
  65. title: body.userName
  66. });
  67. this.nowUserId = this.$store.getters.user.userId;
  68. this.userId = body.userId;
  69. this.getHistoryMsg();
  70. },
  71. onPageScroll(e) {
  72. if (e.scrollTop < 5) {
  73. this.getHistoryMsg();
  74. }
  75. },
  76. methods: {
  77. returnImageClass(item) {
  78. let w = item.thumbnailWidth,
  79. h = item.thumbnailHeight;
  80. let str = ''
  81. if (w > h) str = 'image-width';
  82. if (w == h) str = 'image-width-height';
  83. if (w < h) str = 'image-height';
  84. return str + ' image-box';
  85. },
  86. onMessage(e) {
  87. let message = e;
  88. this.insertMessage(message);
  89. },
  90. insertMessage(message) {
  91. try {
  92. this.chatList.push(message);
  93. this.message = '';
  94. setTimeout(() => {
  95. uni.pageScrollTo({
  96. scrollTop: 99999999, // -30 为多显示出大半个消息的高度,示意上面还有信息。
  97. duration: 0
  98. });
  99. }, 200)
  100. } catch (e) {
  101. console.log(e)
  102. }
  103. },
  104. getHistoryMsg() {
  105. if (!this.postData.flag) {
  106. return; //
  107. }
  108. // 此处用到 ES7 的 async/await 知识,为使代码更加优美。不懂的请自行学习。
  109. let get = async () => {
  110. this.toggleTips();
  111. this.postData.flag = false;
  112. let data = await this.joinHistoryMsg();
  113. // 获取待滚动元素选择器,解决插入数据后,滚动条定位时使用
  114. let selector = '';
  115. if (this.postData.page > 1) {
  116. // 非第一页,则取历史消息数据的第一条信息元素
  117. selector = `#msg-${this.chatList[0].time}`;
  118. } else {
  119. if (data.length > 0) {
  120. // 第一页,则取当前消息数据的最后一条信息元素
  121. selector = `#msg-${data[data.length-1].time}`;
  122. }
  123. }
  124. // 将获取到的消息数据合并到消息数组中
  125. this.chatList = [...data, ...this.chatList];
  126. // 数据挂载后执行,不懂的请自行阅读 Vue.js 文档对 Vue.nextTick 函数说明。
  127. this.$nextTick(() => {
  128. // 设置当前滚动的位置
  129. this.setPageScrollTo(selector);
  130. this.toggleTips(true);
  131. if (data.length < this.postData.rows) {
  132. // 当前消息数据条数小于请求要求条数时,则无更多消息,不再允许请求。
  133. // 可在此处编写无更多消息数据时的逻辑
  134. this.postData.flag = true;
  135. } else {
  136. this.postData.page++;
  137. // 延迟 200ms ,以保证设置窗口滚动已完成
  138. setTimeout(() => {
  139. this.postData.flag = true;
  140. }, 200)
  141. }
  142. })
  143. }
  144. get();
  145. },
  146. // 设置页面滚动位置
  147. setPageScrollTo(selector) {
  148. if (!selector) return;
  149. let view = uni.createSelectorQuery().in(this).select(selector);
  150. view.boundingClientRect((res) => {
  151. uni.pageScrollTo({
  152. scrollTop: res.top - 30, // -30 为多显示出大半个消息的高度,示意上面还有信息。
  153. duration: 0
  154. });
  155. }).exec();
  156. },
  157. toggleTips(flag) {
  158. if (flag) {
  159. this.postData.loadText = '消息获取成功';
  160. setTimeout(() => {
  161. this.postData.loading = false;
  162. }, 300);
  163. } else {
  164. this.postData.loading = true;
  165. this.postData.loadText = '正在获取消息...';
  166. }
  167. },
  168. joinHistoryMsg() {
  169. return new Promise((done, fail) => {
  170. this.$chat.getHistoryMessageList(this.nextMessageId, this.userId, res => {
  171. this.nextMessageId = res.data.nextMessageId;
  172. done(!res.data ? [] : res.data.list);
  173. }, () => {
  174. done([]);
  175. });
  176. })
  177. },
  178. previewImage(url) {
  179. // 预览图片
  180. uni.previewImage({
  181. urls: [url]
  182. });
  183. },
  184. sendImage() {
  185. this.$chat.sendImage(this.userId, this.sendSuccess);
  186. },
  187. // 发送信息
  188. send() {
  189. if (!this.message) return this.$toast('内容不能为空');
  190. this.$chat.sendText(this.userId, this.message, this.sendSuccess);
  191. },
  192. sendSuccess(res) {
  193. this.chatList.push(res.data);
  194. this.message = '';
  195. setTimeout(() => {
  196. uni.pageScrollTo({
  197. scrollTop: 99999999, // -30 为多显示出大半个消息的高度,示意上面还有信息。
  198. duration: 0
  199. });
  200. }, 200)
  201. }
  202. },
  203. }
  204. </script>
  205. <style lang="scss">
  206. .chat-box {
  207. .chat-tips {
  208. position: fixed;
  209. left: 0;
  210. top: 0;
  211. width: 100%;
  212. z-index: 9;
  213. background-color: $uni-primary;
  214. height: 72rpx;
  215. line-height: 72rpx;
  216. color: #fff;
  217. text-align: center;
  218. opacity: 0.9;
  219. }
  220. .chat-list {
  221. width: 100%;
  222. height: auto;
  223. padding-bottom: 120rpx;
  224. box-sizing: content-box;
  225. /* 兼容iPhoneX */
  226. margin-bottom: 0;
  227. margin-bottom: constant(safe-area-inset-bottom);
  228. margin-bottom: env(safe-area-inset-bottom);
  229. .chat-item {
  230. display: flex;
  231. padding: 20rpx 20rpx 0 20rpx;
  232. align-items: flex-start;
  233. align-content: flex-start;
  234. .avatar {
  235. width: 80rpx;
  236. height: 80rpx;
  237. border-radius: 50%;
  238. border: #fff solid 1px;
  239. }
  240. .content-box {
  241. flex: 1;
  242. width: 0;
  243. display: flex;
  244. }
  245. .image-box {
  246. border-radius: 16rpx;
  247. }
  248. .image-width {
  249. width: 280rpx;
  250. height: 200rpx;
  251. }
  252. .image-height {
  253. width: 200rpx;
  254. height: 280rpx;
  255. }
  256. .image-width-height {
  257. width: 200rpx;
  258. height: 200rpx;
  259. }
  260. .content {
  261. padding: 20rpx 30rpx;
  262. border-radius: 16rpx;
  263. word-break: break-all;
  264. line-height: 42rpx;
  265. position: relative;
  266. font-weight: 300;
  267. }
  268. /* 收到的消息 */
  269. &.pull {
  270. .content-box {
  271. margin-right: 100rpx;
  272. }
  273. .image-box {
  274. margin-left: 20rpx;
  275. }
  276. .content {
  277. margin-left: 20rpx;
  278. background-color: #fff;
  279. }
  280. }
  281. /* 发出的消息 */
  282. &.push {
  283. /* 主轴为水平方向,起点在右端。使不修改DOM结构,也能改变元素排列顺序 */
  284. flex-direction: row-reverse;
  285. .content-box {
  286. flex-direction: row-reverse;
  287. margin-left: 100rpx;
  288. }
  289. .image-box {
  290. margin-right: 20rpx;
  291. }
  292. .content {
  293. margin-right: 20rpx;
  294. background-color: $uni-primary;
  295. color: #fff;
  296. box-shadow: 0px 1px 12px rgba(3, 3, 3, 0.08);
  297. }
  298. }
  299. }
  300. }
  301. .chat-submit {
  302. position: fixed;
  303. left: 0;
  304. width: 100%;
  305. bottom: 0;
  306. height: 100rpx;
  307. z-index: 2;
  308. background-color: #fff;
  309. box-shadow: 0px 2px 10px rgba(3, 3, 3, 0.1);
  310. /* 兼容iPhoneX */
  311. padding-bottom: 0;
  312. padding-bottom: constant(safe-area-inset-bottom);
  313. padding-bottom: env(safe-area-inset-bottom);
  314. display: flex;
  315. align-items: center;
  316. .message-content {
  317. flex: 1;
  318. margin: 0 30rpx;
  319. border: 1px solid $uni-border-1;
  320. border-radius: 68rpx;
  321. height: 68rpx;
  322. padding: 0 20rpx;
  323. }
  324. .message-placeholder {
  325. color: #8c8c8c;
  326. font-weight: 300;
  327. }
  328. .send-button {
  329. width: 68rpx;
  330. height: 68rpx;
  331. border-radius: 50%;
  332. display: flex;
  333. align-items: center;
  334. justify-content: center;
  335. &.send-image {
  336. border: 1px solid $uni-border-1;
  337. margin-right: 20rpx;
  338. }
  339. &.send-submit {
  340. background: $uni-primary;
  341. margin-right: 30rpx;
  342. }
  343. }
  344. }
  345. }
  346. </style>