chat.vue 9.5 KB

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