index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view class="article-card">
  3. <view @click="toDetail(localItem)" class="article-content">
  4. <view class="image-container">
  5. <image :src="localItem.coverImage" mode="aspectFill" class="main-image"></image>
  6. <view class="video-image-right-icon"v-if="item.type === 2">
  7. <text class='iconfont icon-sanjiaoxing'></text>
  8. </view>
  9. <view class="status-box" v-if="item.status !== 2">
  10. <view class="item-status">
  11. <uni-icons v-if="item.status === 1" color="#fff" type="eye" size="30"></uni-icons>
  12. <uni-icons v-if="item.status === 3" color="#fff" type="eye-slash" size="30"></uni-icons>
  13. <uni-icons v-if="item.status === 0" customPrefix="iconfont" color="#fff" type="icon-caogaoxiang"
  14. size="30"></uni-icons>
  15. <view class="text">{{ articleStatusMap[item.status] }}</view>
  16. </view>
  17. </view>
  18. </view>
  19. <view class="content-box">
  20. <view class="title-container">
  21. <text class="title line2">{{ localItem.title }}</text>
  22. </view>
  23. </view>
  24. </view>
  25. <view class="article-footer">
  26. <view class="author-detail" @click="toPersonal">
  27. <image :src="localItem.usePicture" class="small-avatar"></image>
  28. <text class="author-name">{{ localItem.userName }}</text>
  29. </view>
  30. <view class="interaction" @click.stop="handleLike">
  31. <uni-icons customPrefix="iconfont" type="icon-dianzan" size="16" v-show="!localItem.likeMark"
  32. class="unlike"></uni-icons>
  33. <uni-icons customPrefix="iconfont" type="icon-dianzanxuanzhong" size="16" color="red"
  34. v-show="localItem.likeMark" :class="['liked', { 'animated heartBeat': showAnimation }]"></uni-icons>
  35. <text class="like-count">{{ localItem.likeCount }}</text>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script setup>
  41. import { ref, watch } from "vue";
  42. import { setUserState } from "@/api/book"
  43. import { useDebounce } from '@/hooks/useDebounceThrottle'
  44. import { useSafeNavigate } from '@/hooks/useSafeNavigate'
  45. import { useAppStore } from "@/stores/app";
  46. const { safeNavigateTo } = useSafeNavigate()
  47. const props = defineProps({
  48. item: { type: Object, required: true },
  49. likeAnimationIds: { type: Array, default: () => [] },
  50. });
  51. const emit = defineEmits(["like", "detail"]);
  52. const appStore = useAppStore();
  53. // 创建本地响应式副本
  54. const localItem = ref({ ...props.item });
  55. // 控制动画显示
  56. const showAnimation = ref(false);
  57. const articleStatusMap = {
  58. 0: "草稿",
  59. 1: "审核中",
  60. 2: "已发布",
  61. 3: "审核被拒绝",
  62. };
  63. // 监听外部 item 变化,同步到本地
  64. watch(
  65. () => props.item,
  66. (newItem) => {
  67. localItem.value = { ...newItem };
  68. },
  69. { deep: true }
  70. );
  71. // 跳转其它用户
  72. function toPersonal() {
  73. // 点自己的头像直接到我的tabbar页
  74. if (Number(localItem.value.userId) === Number(appStore.uid)) {
  75. return uni.switchTab({ url: '/pages/user/index' })
  76. }
  77. // 其它用户跳到 用户个人页
  78. uni.navigateTo({ url: `/pages/user/personal?id=${localItem.value.userId}` });
  79. }
  80. // 点赞
  81. async function handleLike() {
  82. try {
  83. const { code } = await setUserState({
  84. type: 0,
  85. bookId: localItem.value.id,
  86. });
  87. const wasLiked = localItem.value.likeMark;
  88. localItem.value.likeMark = !wasLiked;
  89. if (localItem.value.likeMark) {
  90. showAnimation.value = true;
  91. localItem.value.likeCount++;
  92. } else {
  93. showAnimation.value = false;
  94. localItem.value.likeCount--;
  95. }
  96. } catch (error) {
  97. console.error("handleLike", error);
  98. }
  99. }
  100. // 跳转详情
  101. const toDetail = (item) => {
  102. // 草稿和审核被拒绝跳到编辑页
  103. if (item?.status === 0 || item?.status === 3) {
  104. return uni.navigateTo(`/pages/article_create/edit?id=${item.id}`)
  105. }
  106. // console.log('点击详情', item)
  107. if (item.type === 2) {
  108. safeNavigateTo(`/pages/video/video_details?id=${item.id}&videoType=list`)
  109. } else {
  110. safeNavigateTo(`/pages/article_details/index?id=${item.id}`)
  111. }
  112. }
  113. </script>
  114. <style lang="scss" scoped>
  115. .article-card {
  116. width: 100%;
  117. background-color: #fff;
  118. border-radius: 12rpx;
  119. overflow: hidden;
  120. margin-bottom: 20rpx;
  121. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  122. .article-content {
  123. position: relative;
  124. .image-container {
  125. position: relative;
  126. width: 100%;
  127. .main-image {
  128. width: 100%;
  129. max-height: 400rpx;
  130. aspect-ratio: 4 / 3;
  131. object-fit: cover;
  132. display: block;
  133. position: relative;
  134. }
  135. .status-box {
  136. position: absolute;
  137. left: 0;
  138. top: 0;
  139. right: 0;
  140. bottom: 0;
  141. background-color: rgba(0, 0, 0, 0.4);
  142. display: flex;
  143. justify-content: center;
  144. align-items: center;
  145. z-index: 2;
  146. .item-status {
  147. color: #fff;
  148. display: flex;
  149. flex-direction: column;
  150. justify-content: center;
  151. align-items: center;
  152. }
  153. }
  154. }
  155. .content-box {
  156. padding: 16rpx;
  157. .title-container {
  158. margin-bottom: 8rpx;
  159. .title {
  160. font-size: 14px;
  161. color: #333;
  162. font-weight: bold;
  163. line-height: 1.4;
  164. }
  165. }
  166. }
  167. }
  168. .article-footer {
  169. display: flex;
  170. justify-content: space-between;
  171. align-items: center;
  172. padding: 16rpx;
  173. .author-detail {
  174. display: flex;
  175. align-items: center;
  176. .small-avatar {
  177. width: 40rpx;
  178. height: 40rpx;
  179. border-radius: 50%;
  180. margin-right: 8rpx;
  181. }
  182. .author-name {
  183. font-size: 12px;
  184. color: #666;
  185. white-space: nowrap;
  186. overflow: hidden;
  187. text-overflow: ellipsis;
  188. max-width: 120rpx;
  189. }
  190. }
  191. .interaction {
  192. display: flex;
  193. align-items: center;
  194. .like-count {
  195. font-size: 26rpx;
  196. color: #999;
  197. margin-left: 6rpx;
  198. }
  199. .unlike {}
  200. .liked {}
  201. }
  202. }
  203. }
  204. </style>