UserHome.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <div class="user-home-container">
  3. <div class="user-header-wrapper">
  4. <el-card v-if="user" class="user-info-card" :body-style="{ padding: '20px' }">
  5. <div class="info-content-top">
  6. <div class="avatar-section">
  7. <el-avatar :size="90" :src="user.avatarUrl" class="user-avatar-main" />
  8. <el-tag
  9. v-if="user.vip"
  10. size="mini"
  11. effect="dark"
  12. type="warning"
  13. class="vip-tag-overlay"
  14. >
  15. VIP
  16. </el-tag>
  17. </div>
  18. <div class="action-section">
  19. <el-button
  20. :type="followButton.text === '已关注' ? 'info' : 'danger'"
  21. round
  22. size="medium"
  23. :icon="followButton.icon"
  24. @click="followUser(user.userId)"
  25. >
  26. {{ followButton.text }}
  27. </el-button>
  28. <el-button icon="el-icon-message" round size="medium" @click="sendMessage(user.userId)">发消息</el-button>
  29. <el-dropdown trigger="click" class="more-btn">
  30. <el-button icon="el-icon-more" circle plain size="medium"></el-button>
  31. <el-dropdown-menu slot="dropdown">
  32. <el-dropdown-item v-if="user.biliUserId">
  33. <a :href="'https://space.bilibili.com/' + user.biliUserId" target="_blank" class="no-link-style">访问B站空间</a>
  34. </el-dropdown-item>
  35. <el-dropdown-item divider>举报用户</el-dropdown-item>
  36. </el-dropdown-menu>
  37. </el-dropdown>
  38. </div>
  39. </div>
  40. <div class="info-text-bottom">
  41. <h2 class="user-name">
  42. {{ user.screenName }}
  43. <el-tag v-if="user.vip" size="mini" type="warning" effect="plain" class="name-vip-tag">会员</el-tag>
  44. <el-tag v-if="user.biliUserId" size="mini" effect="plain" class="bili-tag">B站认证</el-tag>
  45. </h2>
  46. <p class="user-signature">{{ user.signature || '这个人很心大,居然没有写签名~' }}</p>
  47. <div class="user-stats">
  48. <router-link :to="`/user/${user.userId}/following`" class="stat-item">
  49. <span class="stat-value">{{ user.following }}</span>
  50. <span class="stat-label">关注</span>
  51. </router-link>
  52. <div class="stat-divider"></div>
  53. <router-link :to="`/user/${user.userId}/follower`" class="stat-item">
  54. <span class="stat-value">{{ user.follower }}</span>
  55. <span class="stat-label">粉丝</span>
  56. </router-link>
  57. </div>
  58. </div>
  59. </el-card>
  60. </div>
  61. <div class="user-content-section">
  62. <el-tabs v-if="userContentData" v-model="activeName" class="custom-tabs" @tab-click="tabClick">
  63. <el-tab-pane name="video">
  64. <span slot="label">视频 <span class="tab-count">{{ userContentData.videoCount }}</span></span>
  65. <el-row :gutter="15" class="grid-container" v-loading="loading">
  66. <el-col v-for="(video, index) in dataList" :key="index" :xs="12" :sm="8" :md="6">
  67. <video-card :video="video" class="content-card-hover" />
  68. </el-col>
  69. </el-row>
  70. </el-tab-pane>
  71. <el-tab-pane name="image">
  72. <span slot="label">图片 <span class="tab-count">{{ userContentData.imageCount }}</span></span>
  73. <el-row :gutter="15" class="grid-container" v-loading="loading">
  74. <el-col v-for="(album, index) in dataList" :key="index" :xs="12" :sm="8" :md="6">
  75. <image-album-card :image-album="album" class="content-card-hover" />
  76. </el-col>
  77. </el-row>
  78. </el-tab-pane>
  79. <el-tab-pane name="album">
  80. <span slot="label">收藏夹 <span class="tab-count">{{ userContentData.albumCount }}</span></span>
  81. <el-row :gutter="15" class="grid-container" v-loading="loading">
  82. <el-col v-for="(item, index) in dataList" :key="index" :xs="12" :sm="8" :md="6">
  83. <el-card :body-style="{ padding: '0px' }" class="playlist-card content-card-hover">
  84. <router-link :to="`/playlist/${item.albumId}`" class="no-link-style">
  85. <div class="playlist-cover-wrapper">
  86. <el-image lazy fit="cover" :src="item.coverUrl" class="playlist-img" />
  87. <div class="playlist-mask">
  88. <i class="el-icon-collection"></i> <span>{{ item.total }}</span>
  89. </div>
  90. </div>
  91. <div class="playlist-info">
  92. <span class="playlist-title">{{ item.albumName }}</span>
  93. </div>
  94. </router-link>
  95. </el-card>
  96. </el-col>
  97. </el-row>
  98. </el-tab-pane>
  99. </el-tabs>
  100. <el-empty v-if="showEmpty && !loading" :image-size="200" description="该分类下空空如也" />
  101. <div class="pagination-wrapper" v-if="totalSize > pageSize">
  102. <el-pagination
  103. background
  104. layout="prev, pager, next"
  105. :current-page="currentPage"
  106. :page-size="pageSize"
  107. :total="totalSize"
  108. @current-change="handleCurrentChange"
  109. />
  110. </div>
  111. </div>
  112. </div>
  113. </template>
  114. <script>
  115. import VideoCard from '@/components/card/VideoCard'
  116. import ImageAlbumCard from '@/components/card/ImageAlbumCard'
  117. import { getUserInfo, checkRelation, followUser, unfollowUser } from '@/api/user'
  118. import { getUserContentData, getUserVideos } from '@/api/video'
  119. import { getAlbumImage1 } from '@/api/image'
  120. import { getUserPlaylist } from '@/api/collect'
  121. export default {
  122. name: 'UserHome',
  123. components: { VideoCard, ImageAlbumCard },
  124. data() {
  125. return {
  126. user: null,
  127. userId: null,
  128. followButton: { icon: 'el-icon-plus', text: '关注' },
  129. activeName: 'video',
  130. currentPage: 1,
  131. pageSize: 12,
  132. totalSize: 0,
  133. dataList: [],
  134. userContentData: null,
  135. loading: false,
  136. showEmpty: false
  137. }
  138. },
  139. watch: {
  140. '$route.query': {
  141. handler() {
  142. this.loadDataFromRoute();
  143. },
  144. deep: true
  145. }
  146. },
  147. created() {
  148. this.userId = this.$route.params.id;
  149. this.initUserContext();
  150. },
  151. methods: {
  152. initUserContext() {
  153. Promise.all([
  154. getUserInfo(this.userId),
  155. checkRelation(this.userId),
  156. getUserContentData(this.userId)
  157. ]).then(([info, relation, content]) => {
  158. if (info.code === 0) {
  159. this.user = info.data;
  160. this.loadDataFromRoute();
  161. }
  162. if (relation.code === 0 && relation.data) {
  163. this.followButton = { icon: 'el-icon-check', text: '已关注' };
  164. }
  165. if (content.code === 0) {
  166. this.userContentData = content.data;
  167. }
  168. });
  169. },
  170. loadDataFromRoute() {
  171. const query = this.$route.query;
  172. this.activeName = query.tab || 'video';
  173. this.currentPage = query.page ? parseInt(query.page) : 1;
  174. this.updatePageTitle();
  175. this.fetchContentList();
  176. },
  177. syncStateToURL() {
  178. this.$router.push({
  179. path: this.$route.path,
  180. query: { tab: this.activeName, page: this.currentPage }
  181. }).catch(err => {
  182. if (err.name !== 'NavigationDuplicated') throw err;
  183. });
  184. },
  185. fetchContentList() {
  186. this.loading = true;
  187. this.dataList = [];
  188. this.showEmpty = false;
  189. let apiCall;
  190. if (this.activeName === 'video') {
  191. apiCall = getUserVideos(this.userId, this.currentPage);
  192. } else if (this.activeName === 'image') {
  193. apiCall = getAlbumImage1(this.currentPage, this.userId);
  194. } else if (this.activeName === 'album') {
  195. apiCall = getUserPlaylist({ userId: this.userId, pn: this.currentPage });
  196. }
  197. apiCall.then(resp => {
  198. if (resp.code === 0) {
  199. this.dataList = resp.data.list;
  200. this.totalSize = resp.data.totalSize;
  201. this.showEmpty = this.dataList.length === 0;
  202. }
  203. }).finally(() => { this.loading = false; });
  204. },
  205. tabClick(tab) {
  206. this.activeName = tab.name;
  207. this.currentPage = 1;
  208. this.syncStateToURL();
  209. },
  210. handleCurrentChange(page) {
  211. this.currentPage = page;
  212. this.syncStateToURL();
  213. window.scrollTo({ top: 0, behavior: 'smooth' });
  214. },
  215. updatePageTitle() {
  216. if (!this.user) return;
  217. const suffix = { video: '的视频', image: '的图片', album: '的收藏夹' };
  218. document.title = `${this.user.screenName}${suffix[this.activeName] || '的主页'}`;
  219. },
  220. followUser(userId) {
  221. const isFollowing = this.followButton.text === '已关注';
  222. const api = isFollowing ? unfollowUser : followUser;
  223. api(userId).then(resp => {
  224. if (resp.code === 0) {
  225. this.followButton = isFollowing ? { icon: 'el-icon-plus', text: '关注' } : { icon: 'el-icon-check', text: '已关注' };
  226. this.$message.success(isFollowing ? '已取消关注' : '关注成功');
  227. }
  228. });
  229. },
  230. sendMessage() { this.$message.info('私信功能暂未开放'); }
  231. }
  232. }
  233. </script>
  234. <style scoped>
  235. .user-home-container {
  236. background-color: #f4f5f7;
  237. min-height: 100vh;
  238. padding: 20px 0 50px 0;
  239. }
  240. .user-header-wrapper {
  241. max-width: 1100px;
  242. margin: 0 auto 20px;
  243. padding: 0 15px;
  244. }
  245. .user-info-card {
  246. border-radius: 12px;
  247. border: none;
  248. box-shadow: 0 4px 12px rgba(0,0,0,0.05);
  249. }
  250. .info-content-top {
  251. display: flex;
  252. justify-content: space-between;
  253. align-items: center;
  254. margin-bottom: 20px;
  255. }
  256. .avatar-section {
  257. position: relative;
  258. display: flex;
  259. }
  260. .user-avatar-main {
  261. border: 2px solid #fff;
  262. background: #fff;
  263. }
  264. /* 会员 Tag 覆盖在头像上的样式 */
  265. .vip-tag-overlay {
  266. position: absolute;
  267. bottom: 0;
  268. right: -10px;
  269. border-radius: 4px;
  270. border: 1px solid #fff;
  271. font-weight: bold;
  272. transform: scale(0.9);
  273. }
  274. .name-vip-tag {
  275. margin-left: 5px;
  276. font-weight: normal;
  277. vertical-align: middle;
  278. }
  279. .action-section {
  280. display: flex;
  281. gap: 10px;
  282. }
  283. .user-name {
  284. margin: 0 0 8px 0;
  285. font-size: 24px;
  286. display: flex;
  287. align-items: center;
  288. gap: 8px;
  289. }
  290. .user-signature {
  291. color: #9499a0;
  292. font-size: 14px;
  293. margin-bottom: 20px;
  294. }
  295. .user-stats {
  296. display: flex;
  297. align-items: center;
  298. gap: 30px;
  299. }
  300. .stat-item {
  301. text-decoration: none;
  302. display: flex;
  303. gap: 5px;
  304. font-size: 14px;
  305. }
  306. .stat-value { font-weight: bold; color: #18191c; }
  307. .stat-label { color: #9499a0; }
  308. .stat-divider { width: 1px; height: 14px; background: #e3e5e7; }
  309. .user-content-section {
  310. max-width: 1100px;
  311. margin: 0 auto;
  312. padding: 0 15px;
  313. }
  314. .custom-tabs /deep/ .el-tabs__header {
  315. background: #fff;
  316. padding: 0 20px;
  317. border-radius: 8px;
  318. margin-bottom: 20px;
  319. }
  320. .tab-count { font-size: 12px; color: #9499a0; margin-left: 4px; }
  321. .content-card-hover { transition: all 0.3s ease; margin-bottom: 20px; }
  322. .content-card-hover:hover { transform: translateY(-5px); }
  323. .no-link-style { text-decoration: none; color: inherit; }
  324. .playlist-card { border-radius: 8px; overflow: hidden; }
  325. .playlist-cover-wrapper { position: relative; aspect-ratio: 16/9; }
  326. .playlist-img { width: 100%; height: 100%; }
  327. .playlist-mask { position: absolute; bottom: 0; right: 0; background: rgba(0,0,0,0.6); color: #fff; padding: 2px 8px; font-size: 12px; border-top-left-radius: 8px; }
  328. .playlist-info { padding: 10px; text-align: left; }
  329. .playlist-title { font-size: 14px; color: #18191c; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  330. .pagination-wrapper { margin-top: 30px; display: flex; justify-content: center; }
  331. @media screen and (max-width: 768px) {
  332. .user-home-container { padding-top: 10px; }
  333. .user-avatar-main { width: 70px !important; height: 70px !important; }
  334. .user-name { font-size: 20px; }
  335. .vip-tag-overlay { right: -5px; bottom: -2px; }
  336. .action-section .el-button { padding: 8px 12px; font-size: 12px; }
  337. .more-btn { display: none; }
  338. }
  339. </style>