message.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <template>
  2. <div class="cl-chat-message" v-loading="!visible && loading" element-loading-text="消息加载中">
  3. <div
  4. class="cl-chat-message__scroller scroller1"
  5. ref="scroller"
  6. :style="{
  7. opacity: visible ? 1 : 0
  8. }"
  9. >
  10. <!-- 加载更多 -->
  11. <div class="cl-chat-message__more" v-show="list.length > 0">
  12. <el-button round size="mini" :loading="loading" @click="onLoadmore"
  13. >加载更多</el-button
  14. >
  15. </div>
  16. <!-- 消息列表 -->
  17. <div class="cl-chat-message__list">
  18. <div
  19. class="cl-chat-message__item"
  20. v-for="item in list"
  21. :key="item.id || item.uid"
  22. :class="[item.type == 0 ? `is-right` : `is-left`, `is-${item.mode}`]"
  23. >
  24. <!-- 日期 -->
  25. <div class="date" v-if="item._date">
  26. <span>{{ item._date }}</span>
  27. </div>
  28. <!-- 内容 -->
  29. <div class="main">
  30. <!-- 头像 -->
  31. <div class="avatar">
  32. <img :src="item.avatarUrl" />
  33. </div>
  34. <div class="det">
  35. <!-- 昵称 -->
  36. <span class="name">{{ item.nickName }}</span>
  37. <div
  38. class="content"
  39. v-loading="item.loading"
  40. :element-loading-text="item.progress"
  41. @click="onTap(item)"
  42. >
  43. <!-- 文本 -->
  44. <template v-if="item.mode === 'text'">{{
  45. item.content.text
  46. }}</template>
  47. <!-- 图片 -->
  48. <template v-else-if="item.mode === 'image'">
  49. <el-image
  50. :key="item.uid"
  51. :src="item.content.imageUrl"
  52. :preview-src-list="[item.content.imageUrl]"
  53. :z-index="3000"
  54. :style="item.style"
  55. >
  56. <template #placeholder>
  57. <img
  58. :src="item.content.imageUrl"
  59. :style="item.style"
  60. alt=""
  61. />
  62. </template>
  63. </el-image>
  64. </template>
  65. <!-- 表情 -->
  66. <template v-else-if="item.mode === 'emoji'">
  67. <img :src="item.content.imageUrl" />
  68. </template>
  69. <!-- 语音 -->
  70. <template v-else-if="item.mode === 'voice'">
  71. <icon-voice :play="item.isPlay"></icon-voice>
  72. <span class="duration"
  73. >{{ item.content.duration | duration }}"</span
  74. >
  75. </template>
  76. <!-- 视频 -->
  77. <template v-else-if="item.mode === 'video'">
  78. <div class="item">
  79. <video
  80. :poster="item.content.videoUrl | video_poster"
  81. :src="item.content.videoUrl"
  82. controls
  83. ></video>
  84. </div>
  85. </template>
  86. <!-- 未知 -->
  87. <template v-else>
  88. <span>待扩展消息类型</span>
  89. <i class="el-icon-warning-outline"></i>
  90. </template>
  91. </div>
  92. </div>
  93. </div>
  94. </div>
  95. <!-- 音频 -->
  96. <div class="voice">
  97. <audio style="display: none" ref="voice" :src="voice.url" controls></audio>
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. </template>
  103. <script>
  104. import dayjs from "dayjs";
  105. import { mapGetters } from "vuex";
  106. import { isString } from "cl-admin/utils";
  107. import eventBus from "../utils/event-bus";
  108. import IconVoice from "./icon-voice";
  109. export default {
  110. components: {
  111. IconVoice
  112. },
  113. inject: ["chat"],
  114. data() {
  115. return {
  116. loading: false,
  117. visible: false,
  118. pagination: {
  119. page: 1,
  120. size: 20,
  121. total: 0
  122. },
  123. voice: {
  124. url: "",
  125. timer: null
  126. },
  127. refreshRd: null
  128. };
  129. },
  130. filters: {
  131. duration(val) {
  132. return Math.ceil((val || 1) / 1000);
  133. }
  134. },
  135. computed: {
  136. ...mapGetters(["userInfo", "session", "messageList"]),
  137. list() {
  138. let date = "";
  139. return this.messageList.map(e => {
  140. // 时间间隔
  141. e._date = date
  142. ? dayjs(e.createTime).isBefore(dayjs(date).add(1, "minute"))
  143. ? ""
  144. : e.createTime
  145. : e.createTime;
  146. // 发送时间
  147. date = e.createTime;
  148. // 解析内容
  149. if (isString(e)) {
  150. e = JSON.parse(e);
  151. }
  152. if (isString(e.content)) {
  153. e.content = JSON.parse(e.content);
  154. }
  155. // 解析昵称
  156. const nickName = e.type == 0 ? this.userInfo.nickName : this.session.nickname;
  157. // 解析头像
  158. const avatarUrl =
  159. e.type == 0
  160. ? this.userInfo.avatarUrl || require("../static/images/custom-avatar.png")
  161. : this.session.headimgurl;
  162. return {
  163. ...e,
  164. avatarUrl,
  165. nickName,
  166. mode: this.chat.modes[e.contentType]
  167. };
  168. });
  169. }
  170. },
  171. beforeCreate() {
  172. // 销毁事件
  173. eventBus.$off("message.refresh");
  174. eventBus.$off("message.scrollToBottom");
  175. },
  176. created() {
  177. // 监听列表刷新
  178. eventBus.$on("message.refresh", this.refresh);
  179. // 滚动到底部
  180. eventBus.$on("message.scrollToBottom", this.scrollToBottom);
  181. },
  182. destroyed() {
  183. // 清除播放
  184. clearTimeout(this.voice.timer);
  185. this.messageList.map(e => {
  186. e.isPlay = false;
  187. });
  188. },
  189. methods: {
  190. // 点击
  191. onTap(item) {
  192. // 播放语音
  193. if (item.mode == "voice") {
  194. this.messageList.map(e => {
  195. this.$set(e, "isPlay", e.id == item.id ? e.isPlay : false);
  196. });
  197. item.isPlay = !item.isPlay;
  198. if (item.isPlay) {
  199. this.voice.url = item.content.voiceUrl;
  200. this.$nextTick(() => {
  201. this.$refs["voice"].play();
  202. });
  203. } else {
  204. this.$refs["voice"].pause();
  205. item.isPlay = false;
  206. }
  207. clearTimeout(this.voice.timer);
  208. this.voice.timer = setTimeout(() => {
  209. item.isPlay = false;
  210. }, item.content.duration);
  211. }
  212. },
  213. // 刷新列表
  214. refresh(params) {
  215. // 请求随机值
  216. const rd = (this.refreshRd = Math.random());
  217. // 请求参数
  218. const data = {
  219. ...this.pagination,
  220. ...params,
  221. sessionId: this.session.id,
  222. order: "createTime",
  223. sort: "desc"
  224. };
  225. // 加载动画
  226. this.loading = true;
  227. // 首页处理
  228. if (data.page === 1) {
  229. this.visible = false;
  230. this.$store.commit("CLEAR_MESSAGE_LIST");
  231. }
  232. // 完成
  233. const done = () => {
  234. this.loading = false;
  235. this.visible = true;
  236. };
  237. this.$service.im.message
  238. .page(data)
  239. .then(res => {
  240. // 防止脏数据
  241. if (rd != this.refreshRd) {
  242. return false;
  243. }
  244. // 分页信息
  245. this.pagination = res.pagination;
  246. // 追加数据
  247. this.$store.commit("PREPEND_MESSAGE_LIST", res.list);
  248. if (data.page === 1) {
  249. this.scrollToBottom();
  250. // 首次滚动隐藏
  251. setTimeout(done, 0);
  252. } else {
  253. done();
  254. }
  255. })
  256. .catch(() => {
  257. this.$message.error(err);
  258. done();
  259. });
  260. },
  261. // 加载更多
  262. onLoadmore() {
  263. this.refresh({ page: this.pagination.page + 1 });
  264. },
  265. // 滚动到底部
  266. scrollToBottom() {
  267. this.$nextTick(() => {
  268. if (this.$refs["scroller"]) {
  269. this.$refs["scroller"].scrollTo({
  270. top: 99999,
  271. behavior: this.visible ? "smooth" : "auto"
  272. });
  273. }
  274. });
  275. }
  276. }
  277. };
  278. </script>
  279. <style lang="scss" scoped>
  280. .cl-chat-message {
  281. height: calc(100% - 5px);
  282. overflow: hidden;
  283. margin-bottom: 5px;
  284. &__scroller {
  285. height: calc(100% - 10px);
  286. border-radius: 5px;
  287. margin: 5px 0px 5px 5px;
  288. padding: 10px;
  289. box-sizing: border-box;
  290. }
  291. &__more {
  292. display: flex;
  293. justify-content: center;
  294. margin-bottom: 20px;
  295. }
  296. &__item {
  297. margin-bottom: 20px;
  298. .date {
  299. text-align: center;
  300. margin: 10px 0;
  301. span {
  302. background-color: #dadada;
  303. font-size: 12px;
  304. color: #fff;
  305. border-radius: 3px;
  306. padding: 3px 5px 2px 5px;
  307. letter-spacing: 1px;
  308. }
  309. }
  310. .main {
  311. display: flex;
  312. .avatar {
  313. flex-shrink: 0;
  314. img {
  315. display: block;
  316. height: 40px;
  317. width: 40px;
  318. border-radius: 3px;
  319. background-color: #fff;
  320. }
  321. }
  322. .det {
  323. display: flex;
  324. flex-direction: column;
  325. max-width: 60%;
  326. .name {
  327. margin-bottom: 5px;
  328. }
  329. .content {
  330. display: inline-block;
  331. border-radius: 8px;
  332. box-sizing: border-box;
  333. font-size: 12px;
  334. }
  335. }
  336. }
  337. &.is-left {
  338. .main {
  339. .det {
  340. margin-left: 10px;
  341. align-items: flex-start;
  342. .content {
  343. border-top-left-radius: 0;
  344. background-color: #fff;
  345. }
  346. }
  347. }
  348. &.is-voice {
  349. .content {
  350. justify-content: flex-start;
  351. }
  352. }
  353. }
  354. &.is-right {
  355. .main {
  356. flex-direction: row-reverse;
  357. .det {
  358. margin-right: 10px;
  359. align-items: flex-end;
  360. .content {
  361. border-top-right-radius: 0;
  362. background-color: $color-primary;
  363. color: #fff;
  364. }
  365. }
  366. }
  367. &.is-voice {
  368. .content {
  369. justify-content: flex-end;
  370. }
  371. }
  372. }
  373. &.is-text {
  374. .content {
  375. max-width: 100%;
  376. min-width: 40px;
  377. word-wrap: break-word;
  378. }
  379. }
  380. &.is-text,
  381. &.is-voice {
  382. .content {
  383. padding: 10px;
  384. line-height: 20px;
  385. letter-spacing: 1px;
  386. }
  387. }
  388. &.is-emoji {
  389. .content {
  390. padding: 10px;
  391. img {
  392. height: 20px;
  393. width: 20px;
  394. }
  395. }
  396. }
  397. &.is-voice {
  398. .content {
  399. display: flex;
  400. align-items: center;
  401. width: 65px;
  402. cursor: pointer;
  403. &:hover {
  404. opacity: 0.8;
  405. }
  406. }
  407. }
  408. &.is-video {
  409. .item {
  410. video {
  411. display: block;
  412. max-width: 300px;
  413. max-height: 300px;
  414. border-radius: 10px;
  415. }
  416. }
  417. }
  418. &.is-image {
  419. .main {
  420. .det {
  421. .content {
  422. background-color: #fff;
  423. /deep/.el-image {
  424. display: block;
  425. border-radius: 6px;
  426. max-width: 200px;
  427. min-width: 80px;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. &.is-undefined {
  434. .main {
  435. .det {
  436. .content {
  437. display: flex;
  438. align-items: center;
  439. padding: 10px;
  440. letter-spacing: 1px;
  441. background-color: #f56c6c;
  442. color: #fff;
  443. .el-icon-warning-outline {
  444. font-size: 15px;
  445. margin-left: 4px;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. </style>