knowledgeCard.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <div class="knowledge-card">
  3. <div class="knowledge-header">
  4. <div class="knowledge-title">
  5. <div class="title-item" :class="{ active: activeKnowledgeId === 8 }" @click="handleKnowledgeClick(8)">
  6. 焦点话题
  7. </div>
  8. <div class="title-item" :class="{ active: activeKnowledgeId === 6 }" @click="handleKnowledgeClick(6)">
  9. 种植精华
  10. </div>
  11. <!-- <div class="title-item" :class="{ active: activeKnowledgeId === 7 }" @click="handleKnowledgeClick(7)">
  12. 实战操作
  13. </div> -->
  14. </div>
  15. <!-- <div class="more-text" @click="handleMoreClick">
  16. 更多<el-icon class="more-icon"><ArrowRight /></el-icon>
  17. </div> -->
  18. </div>
  19. <div class="knowledge-content focus-content" v-if="activeKnowledgeId === 8">
  20. <div
  21. class="focus-item"
  22. @click="handleBannerClick(item.id)"
  23. v-for="(item, index) in knowledgeList"
  24. :key="index"
  25. >
  26. <img class="thumb" :src="item.media?.[0]" alt="" />
  27. <div class="focus-content">
  28. <div class="title van-ellipsis">{{ item.title }}</div>
  29. <div class="focus-expert">
  30. <div class="expert-name">
  31. <el-avatar :size="22" :src="item.expertAvatar+resize_300" />
  32. {{ item.expertName || '专家' }}
  33. <span class="expert-text">发表了回复,点击查看</span>
  34. </div>
  35. <div class="date">{{ formatDate(item.createTime) }}</div>
  36. </div>
  37. <!-- <div class="focus-answer">
  38. <div class="answer-icon">
  39. </div>
  40. {{ item.summary }}
  41. </div> -->
  42. </div>
  43. </div>
  44. <div class="no-data" v-if="knowledgeList.length === 0">暂无数据</div>
  45. </div>
  46. <div class="knowledge-content" v-else>
  47. <div
  48. class="knowledge-item"
  49. @click="handleBannerClick(item.id)"
  50. v-for="(item, index) in knowledgeList"
  51. :key="index"
  52. >
  53. <img class="thumb" :src="item.media?.[0]" alt="" />
  54. <div class="card-body-left">
  55. <div class="title van-multi-ellipsis--l2">{{ item.title }}</div>
  56. <div class="date">{{ formatDate(item.createTime) }}</div>
  57. </div>
  58. </div>
  59. <div class="no-data" v-if="knowledgeList.length === 0">暂无数据</div>
  60. </div>
  61. </div>
  62. </template>
  63. <script setup>
  64. import { onMounted, ref, watch } from "vue";
  65. import { useRouter } from "vue-router";
  66. import { useStore } from "vuex";
  67. import { resize_300 } from "@/api/config";
  68. const router = useRouter();
  69. const store = useStore();
  70. const activeKnowledgeId = ref(8);
  71. // 按 topicId 缓存知识列表,避免每次切换都请求接口
  72. const knowledgeCache = ref({});
  73. const handleKnowledgeClick = (index) => {
  74. activeKnowledgeId.value = index;
  75. // 如果已经有缓存,直接用缓存数据
  76. if (knowledgeCache.value[index]) {
  77. knowledgeList.value = knowledgeCache.value[index];
  78. return;
  79. }
  80. // 没有缓存时再发起请求
  81. getKnowledgeList();
  82. };
  83. const handleBannerClick = (id) => {
  84. router.push(`/warning_detail?id=${id}&showImage=true`);
  85. };
  86. const handleMoreClick = () => {
  87. router.push(`/knowledge_list?topicId=${activeKnowledgeId.value}`);
  88. };
  89. const knowledgeList = ref([]);
  90. onMounted(() => {
  91. // 默认 topicId 首次进入时请求一次
  92. getKnowledgeList();
  93. });
  94. watch(
  95. () => store.state.home.gardenId,
  96. (newGardenId) => {
  97. if (newGardenId) {
  98. getKnowledgeList();
  99. }
  100. }
  101. );
  102. const formatDate = (dateString) => {
  103. if (!dateString) return "";
  104. // 将 2026-01-06T00:00:00 格式转换为 2026-01-06
  105. return dateString.split("T")[0];
  106. };
  107. const getKnowledgeList = async () => {
  108. try {
  109. let data = [];
  110. // 实战知识库:使用原接口
  111. const params = {
  112. page: 1,
  113. limit: 10,
  114. topicId: activeKnowledgeId.value,
  115. };
  116. const { data: result } = await VE_API.home.warningPageList(params);
  117. data = result || [];
  118. knowledgeList.value = data;
  119. // 按当前 topicId 缓存,后续切换直接读取
  120. knowledgeCache.value[activeKnowledgeId.value] = data;
  121. } catch (error) {
  122. console.error("获取知识列表失败:", error);
  123. knowledgeList.value = [];
  124. }
  125. };
  126. </script>
  127. <style lang="scss" scoped>
  128. .knowledge-card {
  129. padding: 0 10px;
  130. .knowledge-header {
  131. display: flex;
  132. align-items: center;
  133. justify-content: space-between;
  134. padding: 6px 0 2px 0;
  135. .knowledge-title {
  136. display: flex;
  137. align-items: center;
  138. gap: 10px;
  139. color: #bfbfbf;
  140. .title-item {
  141. cursor: pointer;
  142. font-size: 18px;
  143. font-family: "PangMenZhengDao";
  144. &.active {
  145. color: #2199f8;
  146. }
  147. }
  148. }
  149. .more-text {
  150. font-size: 13px;
  151. color: #4e5969;
  152. cursor: pointer;
  153. display: flex;
  154. align-items: center;
  155. }
  156. }
  157. .knowledge-content {
  158. padding: 8px 0;
  159. .knowledge-item {
  160. display: flex;
  161. gap: 12px;
  162. // padding: 12px 10px;
  163. // background: #ffffff;
  164. border-radius: 12px;
  165. align-items: center;
  166. height: 74px;
  167. box-sizing: border-box;
  168. .thumb {
  169. width: 112px;
  170. height: 74px;
  171. border-radius: 8px;
  172. object-fit: cover;
  173. flex: none;
  174. }
  175. .card-body-left {
  176. flex: 1;
  177. height: 95%;
  178. display: flex;
  179. flex-direction: column;
  180. justify-content: space-between;
  181. font-size: 14px;
  182. line-height: 22px;
  183. .date {
  184. font-size: 13px;
  185. color: #86909c;
  186. margin-top: 4px;
  187. }
  188. }
  189. }
  190. .knowledge-item + .knowledge-item {
  191. margin-top: 12px;
  192. }
  193. .item-content {
  194. font-size: 14px;
  195. color: #666666;
  196. }
  197. .focus-item {
  198. margin-bottom: 8px;
  199. border: 1px solid rgba(142, 142, 142, 0.2);
  200. border-radius: 12px;
  201. padding: 5px 10px;
  202. .thumb {
  203. width: 100%;
  204. height: 123px;
  205. border-radius: 5px;
  206. object-fit: cover;
  207. }
  208. .focus-content {
  209. .title {
  210. font-weight: 500;
  211. font-size: 16px;
  212. line-height: 22px;
  213. color: #1d2129;
  214. padding: 8px 0;
  215. }
  216. .focus-expert {
  217. padding: 0 5px;
  218. margin-bottom: 5px;
  219. display: flex;
  220. align-items: center;
  221. justify-content: space-between;
  222. font-size: 12px;
  223. color: #666666;
  224. height: 32px;
  225. border-radius: 5px;
  226. background: rgba(33, 153, 248, 0.05);
  227. .expert-name {
  228. display: flex;
  229. align-items: center;
  230. gap: 6px;
  231. .expert-text {
  232. padding-left: 2px;
  233. color: #2199F8;
  234. }
  235. }
  236. }
  237. .focus-answer {
  238. padding: 5px;
  239. background: rgba(33, 153, 248, 0.05);
  240. border-radius: 5px;
  241. font-size: 13px;
  242. line-height: 22px;
  243. color: #86909C;
  244. text-align: justify;
  245. display: flex;
  246. gap: 8px;
  247. .answer-icon {
  248. margin-top: 2px;
  249. flex: none;
  250. width: 18px;
  251. height: 18px;
  252. background: #2199f8;
  253. border-radius: 2px;
  254. font-size: 12px;
  255. color: #fff;
  256. text-align: center;
  257. line-height: 18px;
  258. }
  259. }
  260. }
  261. }
  262. }
  263. .no-data {
  264. font-size: 14px;
  265. color: #999999;
  266. text-align: center;
  267. margin-top: 20px;
  268. }
  269. }
  270. </style>