chat.vue 9.7 KB

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