123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <template>
- <div class="container">
- <div class="content">
- <div class="tab-box">
- <div
- v-for="(item, i) in tabList"
- :key="i"
- class="tab-text"
- :class="type == i ? 'active-color' : ''"
- @click="changeTab(i)"
- >
- {{ item }}
- </div>
- </div>
- <div class="list-box">
- <!-- 加载中 -->
- <van-loading v-if="loading" class="load-box" size="24px">
- 加载中...
- </van-loading>
- <!-- 消息列表 -->
- <div
- class="box-item"
- v-for="(item, i) in msgList"
- :key="i"
- v-else
- >
- <van-image
- class="box-img"
- round
- :src="formatAvatarUrl(item)"
- />
- <div class="list-cont">
- <div class="box-name">
- {{
- item.sender
- ? item.sender.nickname
- : item.nickname ||
- item.fromUsername ||
- '匿名用户'
- }}
- <span class="ml12">{{ item.createDate }}</span>
- </div>
- <!-- 文本信息 -->
- <div
- class="box-content"
- v-if="item.contentType === MsgType.MSG_TYPE.TEXT"
- >
- {{ item.content }}
- </div>
- <!-- 图片消息 -->
- <div
- class="img-message"
- v-else-if="
- item.contentType === MsgType.MSG_TYPE.IMAGE
- "
- >
- <van-image
- :src="item?.localUrl || IM_PATH + item.url"
- style="max-width: 120px; border-radius: 8px"
- @click="previewImage(item)"
- />
- </div>
- <!-- 录音消息 -->
- <div
- class="audio-message"
- v-else-if="
- item.contentType === MsgType.MSG_TYPE.AUDIO
- "
- >
- <messageAudio
- :src="item?.localUrl || IM_PATH + item.url"
- />
- </div>
- <!-- 视频消息 -->
- <video
- class="video-message"
- controls
- v-else-if="
- item.contentType === MsgType.MSG_TYPE.VIDEO
- "
- :src="item?.localUrl || IM_PATH + item.url"
- ></video>
- </div>
- </div>
- <!-- 暂无数据 -->
- <template v-if="!loading && msgList && msgList.length === 0">
- <div class="no-more">
- <svg-icon class="no-more-img" name="no-more" />
- <div>暂无数据</div>
- </div>
- </template>
- </div>
- </div>
- </div>
- </template>
- <script setup>
- import { getMessageApi } from '@/api/path/im.api';
- import messageAudio from '@/views/im/components/messageAudio/index.vue';
- import { useWebSocketStore } from '@/stores/modules/webSocketStore.js';
- import { useWalletStore } from '@/stores/modules/walletStore.js';
- import * as MsgType from '@/common/constant/msgType';
- import { showImagePreview } from 'vant';
- const IM_PATH = import.meta.env.VITE_IM_PATH_FIlE;
- const walletStore = useWalletStore();
- const wsStore = useWebSocketStore();
- const type = ref(0);
- const tabList = ['全部', '文本', '图片', '视频'];
- const loading = ref(false); // 👈 新增 loading
- const msgList = ref([]);
- const contentTypeMap = {
- 0: -1,
- 1: MsgType.MSG_TYPE.TEXT,
- 2: MsgType.MSG_TYPE.IMAGE,
- 3: MsgType.MSG_TYPE.VIDEO
- };
- const getMessage = async () => {
- msgList.value = [];
- loading.value = true; // 👈 请求前显示 loading
- let params = {
- uuid: wsStore.toUserInfo.uuid,
- messageType: wsStore.toUserInfo.type == 'user' ? 1 : 2,
- friendUsername: walletStore.account,
- contentType: contentTypeMap[type.value] ?? ''
- };
- try {
- const res = await getMessageApi(params);
- if (res.code == 200) {
- msgList.value = res.data || [];
- }
- } catch (error) {
- console.error(error);
- } finally {
- loading.value = false; // 👈 请求结束隐藏 loading
- }
- };
- const changeTab = i => {
- type.value = i;
- getMessage();
- };
- const formatAvatarUrl = item => {
- const url = item?.sender?.avatar || item?.fromAvatar || '';
- if (/^https?:\/\//.test(url)) {
- return url;
- }
- return IM_PATH + url;
- };
- // 预览图片
- const previewImage = item => {
- const imageList = msgList.value
- .filter(m => m.contentType === MsgType.MSG_TYPE.IMAGE)
- .map(m => m.localUrl || IM_PATH + m.url);
- const index = imageList.findIndex(
- url => url === (item.localUrl || IM_PATH + item.url)
- );
- showImagePreview({
- images: imageList,
- startPosition: index
- });
- };
- onMounted(() => {
- getMessage();
- });
- </script>
- <style lang="less" scoped>
- .container {
- height: 100%;
- display: flex;
- flex-direction: column;
- .content {
- display: flex;
- flex-direction: column;
- height: 100%;
- .tab-box {
- display: flex;
- align-items: center;
- font-family:
- PingFang SC,
- PingFang SC;
- font-weight: 400;
- font-size: 15px;
- color: #4f4f4f;
- height: 42px;
- line-height: 42px;
- background-color: #fff;
- .tab-text {
- width: 25%;
- text-align: center;
- }
- .active-color {
- font-weight: 500;
- color: #4765dd;
- }
- }
- .list-box {
- margin: 16px;
- flex: 1;
- overflow: auto;
- .load-box{
- text-align: center;
- }
- .box-item {
- display: flex;
- margin-bottom: 16px;
- font-family:
- PingFang SC,
- PingFang SC;
- font-weight: 400;
- font-size: 12px;
- color: #8d8d8d;
- .list-cont {
- font-family:
- PingFang SC,
- PingFang SC;
- font-weight: 400;
- font-size: 12px;
- color: #8d8d8d;
- max-width: 70%;
- display: flex;
- flex-direction: column;
- align-items: flex-start;
- }
- .box-img {
- width: 44px;
- height: 44px;
- margin-right: 12px;
- flex-shrink: 0;
- }
- .box-name {
- margin-bottom: 8px;
- .ml12 {
- margin-left: 12px;
- }
- }
- .business-card {
- width: 199px;
- height: 93px;
- background: #ffffff;
- border-radius: 10px;
- margin-top: 8px;
- padding: 10px;
- box-sizing: border-box;
- .business-card-cont {
- display: flex;
- align-items: center;
- font-family:
- PingFang SC,
- PingFang SC;
- font-weight: 400;
- font-size: 15px;
- color: #000000;
- }
- .list-img {
- width: 44px;
- height: 44px;
- flex-shrink: 0;
- margin-right: 12px;
- }
- .line {
- height: 1px;
- background: #f2f2f2;
- margin: 10px 0 6px;
- }
- .business-card-text {
- font-family:
- PingFang SC,
- PingFang SC;
- font-weight: 400;
- font-size: 10px;
- color: #8d8d8d;
- }
- }
- .box-content {
- background: #ffffff;
- color: #000;
- border-radius: 10px;
- margin-top: 8px;
- padding: 8px 17px;
- word-break: break-word;
- white-space: pre-wrap;
- max-width: 100%;
- font-family:
- PingFang SC,
- PingFang SC;
- font-weight: 400;
- font-size: 15px;
- }
- .img-message {
- margin-top: 8px;
- }
- .video-message {
- // margin-top: 8px;
- // width: 200px;
- height: 200px;
- }
- }
- .no-more {
- margin-top: 178px;
- text-align: center;
- font-family:
- PingFang SC,
- PingFang SC;
- font-weight: 400;
- font-size: 15px;
- color: #8d8d8d;
- .no-more-img {
- width: 302px;
- height: 222px;
- }
- }
- }
- .list-box::-webkit-scrollbar {
- width: 0;
- }
- .foot-page {
- display: flex;
- align-items: center;
- height: 44px;
- background: #ffffff;
- padding: 10px 16px;
- box-sizing: border-box;
- .foot-btn {
- box-sizing: border-box;
- width: 50px;
- border-radius: 4px;
- padding: 4px 6px;
- background: #f2f2f2;
- font-family:
- PingFang SC,
- PingFang SC;
- font-weight: 400;
- font-size: 12px;
- color: #4f4f4f;
- }
- .foot-total {
- flex: 1;
- text-align: center;
- font-family:
- PingFang SC,
- PingFang SC;
- font-weight: 400;
- font-size: 12px;
- color: #4f4f4f;
- }
- }
- }
- }
- </style>
|