message.vue 9.1 KB

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