serveChat.vue 9.6 KB

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