index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <div class="container">
  3. <div class="content">
  4. <div class="tab-box">
  5. <div
  6. v-for="(item, i) in tabList"
  7. :key="i"
  8. class="tab-text"
  9. :class="type == i ? 'active-color' : ''"
  10. @click="changeTab(i)"
  11. >
  12. {{ item }}
  13. </div>
  14. </div>
  15. <div class="list-box">
  16. <!-- 加载中 -->
  17. <van-loading v-if="loading" class="load-box" size="24px">
  18. 加载中...
  19. </van-loading>
  20. <!-- 消息列表 -->
  21. <div
  22. class="box-item"
  23. v-for="(item, i) in msgList"
  24. :key="i"
  25. v-else
  26. >
  27. <van-image
  28. class="box-img"
  29. round
  30. :src="formatAvatarUrl(item)"
  31. />
  32. <div class="list-cont">
  33. <div class="box-name">
  34. {{
  35. item.sender
  36. ? item.sender.nickname
  37. : item.nickname ||
  38. item.fromUsername ||
  39. '匿名用户'
  40. }}
  41. <span class="ml12">{{ item.createDate }}</span>
  42. </div>
  43. <!-- 文本信息 -->
  44. <div
  45. class="box-content"
  46. v-if="item.contentType === MsgType.MSG_TYPE.TEXT"
  47. >
  48. {{ item.content }}
  49. </div>
  50. <!-- 图片消息 -->
  51. <div
  52. class="img-message"
  53. v-else-if="
  54. item.contentType === MsgType.MSG_TYPE.IMAGE
  55. "
  56. >
  57. <van-image
  58. :src="item?.localUrl || IM_PATH + item.url"
  59. style="max-width: 120px; border-radius: 8px"
  60. @click="previewImage(item)"
  61. />
  62. </div>
  63. <!-- 录音消息 -->
  64. <div
  65. class="audio-message"
  66. v-else-if="
  67. item.contentType === MsgType.MSG_TYPE.AUDIO
  68. "
  69. >
  70. <messageAudio
  71. :src="item?.localUrl || IM_PATH + item.url"
  72. />
  73. </div>
  74. <!-- 视频消息 -->
  75. <video
  76. class="video-message"
  77. controls
  78. v-else-if="
  79. item.contentType === MsgType.MSG_TYPE.VIDEO
  80. "
  81. :src="item?.localUrl || IM_PATH + item.url"
  82. ></video>
  83. </div>
  84. </div>
  85. <!-- 暂无数据 -->
  86. <template v-if="!loading && msgList && msgList.length === 0">
  87. <div class="no-more">
  88. <svg-icon class="no-more-img" name="no-more" />
  89. <div>暂无数据</div>
  90. </div>
  91. </template>
  92. </div>
  93. </div>
  94. </div>
  95. </template>
  96. <script setup>
  97. import { getMessageApi } from '@/api/path/im.api';
  98. import messageAudio from '@/views/im/components/messageAudio/index.vue';
  99. import { useWebSocketStore } from '@/stores/modules/webSocketStore.js';
  100. import { useWalletStore } from '@/stores/modules/walletStore.js';
  101. import * as MsgType from '@/common/constant/msgType';
  102. import { showImagePreview } from 'vant';
  103. const IM_PATH = import.meta.env.VITE_IM_PATH_FIlE;
  104. const walletStore = useWalletStore();
  105. const wsStore = useWebSocketStore();
  106. const type = ref(0);
  107. const tabList = ['全部', '文本', '图片', '视频'];
  108. const loading = ref(false); // 👈 新增 loading
  109. const msgList = ref([]);
  110. const contentTypeMap = {
  111. 0: -1,
  112. 1: MsgType.MSG_TYPE.TEXT,
  113. 2: MsgType.MSG_TYPE.IMAGE,
  114. 3: MsgType.MSG_TYPE.VIDEO
  115. };
  116. const getMessage = async () => {
  117. msgList.value = [];
  118. loading.value = true; // 👈 请求前显示 loading
  119. let params = {
  120. uuid: wsStore.toUserInfo.uuid,
  121. messageType: wsStore.toUserInfo.type == 'user' ? 1 : 2,
  122. friendUsername: walletStore.account,
  123. contentType: contentTypeMap[type.value] ?? ''
  124. };
  125. try {
  126. const res = await getMessageApi(params);
  127. if (res.code == 200) {
  128. msgList.value = res.data || [];
  129. }
  130. } catch (error) {
  131. console.error(error);
  132. } finally {
  133. loading.value = false; // 👈 请求结束隐藏 loading
  134. }
  135. };
  136. const changeTab = i => {
  137. type.value = i;
  138. getMessage();
  139. };
  140. const formatAvatarUrl = item => {
  141. const url = item?.sender?.avatar || item?.fromAvatar || '';
  142. if (/^https?:\/\//.test(url)) {
  143. return url;
  144. }
  145. return IM_PATH + url;
  146. };
  147. // 预览图片
  148. const previewImage = item => {
  149. const imageList = msgList.value
  150. .filter(m => m.contentType === MsgType.MSG_TYPE.IMAGE)
  151. .map(m => m.localUrl || IM_PATH + m.url);
  152. const index = imageList.findIndex(
  153. url => url === (item.localUrl || IM_PATH + item.url)
  154. );
  155. showImagePreview({
  156. images: imageList,
  157. startPosition: index
  158. });
  159. };
  160. onMounted(() => {
  161. getMessage();
  162. });
  163. </script>
  164. <style lang="less" scoped>
  165. .container {
  166. height: 100%;
  167. display: flex;
  168. flex-direction: column;
  169. .content {
  170. display: flex;
  171. flex-direction: column;
  172. height: 100%;
  173. .tab-box {
  174. display: flex;
  175. align-items: center;
  176. font-family:
  177. PingFang SC,
  178. PingFang SC;
  179. font-weight: 400;
  180. font-size: 15px;
  181. color: #4f4f4f;
  182. height: 42px;
  183. line-height: 42px;
  184. background-color: #fff;
  185. .tab-text {
  186. width: 25%;
  187. text-align: center;
  188. }
  189. .active-color {
  190. font-weight: 500;
  191. color: #4765dd;
  192. }
  193. }
  194. .list-box {
  195. margin: 16px;
  196. flex: 1;
  197. overflow: auto;
  198. .load-box{
  199. text-align: center;
  200. }
  201. .box-item {
  202. display: flex;
  203. margin-bottom: 16px;
  204. font-family:
  205. PingFang SC,
  206. PingFang SC;
  207. font-weight: 400;
  208. font-size: 12px;
  209. color: #8d8d8d;
  210. .list-cont {
  211. font-family:
  212. PingFang SC,
  213. PingFang SC;
  214. font-weight: 400;
  215. font-size: 12px;
  216. color: #8d8d8d;
  217. max-width: 70%;
  218. display: flex;
  219. flex-direction: column;
  220. align-items: flex-start;
  221. }
  222. .box-img {
  223. width: 44px;
  224. height: 44px;
  225. margin-right: 12px;
  226. flex-shrink: 0;
  227. }
  228. .box-name {
  229. margin-bottom: 8px;
  230. .ml12 {
  231. margin-left: 12px;
  232. }
  233. }
  234. .business-card {
  235. width: 199px;
  236. height: 93px;
  237. background: #ffffff;
  238. border-radius: 10px;
  239. margin-top: 8px;
  240. padding: 10px;
  241. box-sizing: border-box;
  242. .business-card-cont {
  243. display: flex;
  244. align-items: center;
  245. font-family:
  246. PingFang SC,
  247. PingFang SC;
  248. font-weight: 400;
  249. font-size: 15px;
  250. color: #000000;
  251. }
  252. .list-img {
  253. width: 44px;
  254. height: 44px;
  255. flex-shrink: 0;
  256. margin-right: 12px;
  257. }
  258. .line {
  259. height: 1px;
  260. background: #f2f2f2;
  261. margin: 10px 0 6px;
  262. }
  263. .business-card-text {
  264. font-family:
  265. PingFang SC,
  266. PingFang SC;
  267. font-weight: 400;
  268. font-size: 10px;
  269. color: #8d8d8d;
  270. }
  271. }
  272. .box-content {
  273. background: #ffffff;
  274. color: #000;
  275. border-radius: 10px;
  276. margin-top: 8px;
  277. padding: 8px 17px;
  278. word-break: break-word;
  279. white-space: pre-wrap;
  280. max-width: 100%;
  281. font-family:
  282. PingFang SC,
  283. PingFang SC;
  284. font-weight: 400;
  285. font-size: 15px;
  286. }
  287. .img-message {
  288. margin-top: 8px;
  289. }
  290. .video-message {
  291. // margin-top: 8px;
  292. // width: 200px;
  293. height: 200px;
  294. }
  295. }
  296. .no-more {
  297. margin-top: 178px;
  298. text-align: center;
  299. font-family:
  300. PingFang SC,
  301. PingFang SC;
  302. font-weight: 400;
  303. font-size: 15px;
  304. color: #8d8d8d;
  305. .no-more-img {
  306. width: 302px;
  307. height: 222px;
  308. }
  309. }
  310. }
  311. .list-box::-webkit-scrollbar {
  312. width: 0;
  313. }
  314. .foot-page {
  315. display: flex;
  316. align-items: center;
  317. height: 44px;
  318. background: #ffffff;
  319. padding: 10px 16px;
  320. box-sizing: border-box;
  321. .foot-btn {
  322. box-sizing: border-box;
  323. width: 50px;
  324. border-radius: 4px;
  325. padding: 4px 6px;
  326. background: #f2f2f2;
  327. font-family:
  328. PingFang SC,
  329. PingFang SC;
  330. font-weight: 400;
  331. font-size: 12px;
  332. color: #4f4f4f;
  333. }
  334. .foot-total {
  335. flex: 1;
  336. text-align: center;
  337. font-family:
  338. PingFang SC,
  339. PingFang SC;
  340. font-weight: 400;
  341. font-size: 12px;
  342. color: #4f4f4f;
  343. }
  344. }
  345. }
  346. }
  347. </style>