1
0

archives.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <template>
  2. <view class="app-page">
  3. <view class="e-fixed">
  4. <tm-tabs color="light-blue" v-model="tab.activeIndex" :list="tab.list" align="center"
  5. @change="fnOnTabChange"></tm-tabs>
  6. </view>
  7. <!-- 占位区域 -->
  8. <view style="width: 100vw;height: 90rpx;"></view>
  9. <!-- 骨架屏:加载区域 -->
  10. <view v-if="loading != 'success'" class="loading-wrap">
  11. <tm-skeleton model="listAvatr"></tm-skeleton>
  12. <tm-skeleton model="listAvatr"></tm-skeleton>
  13. <tm-skeleton model="listAvatr"></tm-skeleton>
  14. </view>
  15. <!-- 加载完成区域 -->
  16. <block v-else>
  17. <view v-if="dataList.length == 0" class="list-empty flex flex-center">
  18. <tm-empty icon="icon-shiliangzhinengduixiang-" label="暂无归档的文章"></tm-empty>
  19. </view>
  20. <view v-else class="e-timeline tm-timeline mt-24">
  21. <block v-for="(item, index) in dataList" :key="index">
  22. <view class="tm-timeline-item tm-timeline-item--leftDir">
  23. <view style="width: 160rpx;">
  24. <view :style="{ width: '24rpx', height: '24rpx' }" :class="[black_tmeme ? 'bk' : '']"
  25. class="flex-center rounded tm-timeline-jidian border-white-a-2 grey-lighten-2 light-blue shadow-primary-4">
  26. </view>
  27. <view :style="{ marginTop: '-24rpx' }"
  28. :class="[index !== dataList.length - 1 ? 'tm-timeline-item-boder' : '', black_tmeme ? 'bk' : '']"
  29. class="grey-lighten-2"></view>
  30. </view>
  31. <view class="tm-timeline-item-content relative">
  32. <view class="tm-timeline-item-left">
  33. <view class="flex time text-weight-b mb-24">
  34. <text>{{ item.year }}年</text>
  35. <text v-if="tab.activeIndex == 0">{{ item.month }}月</text>
  36. <view class="text-size-s text-grey-darken-1 ml-12">
  37. (共 {{ item.posts.length }} 篇文章)
  38. </view>
  39. </view>
  40. <block v-if="item.posts.length != 0">
  41. <block v-for="(post, postIndex) in item.posts" :key="post.metadata.name">
  42. <tm-translate animation-name="fadeUp" :wait="calcAniWait(postIndex)">
  43. <view class="flex post shadow-3 pa-24 mb-24"
  44. :class="[globalAppSettings.layout.cardType]"
  45. @click="fnToArticleDetail(post)">
  46. <image class="post-thumbnail"
  47. :src="$utils.checkThumbnailUrl(post.spec.cover)" mode="aspectFill">
  48. </image>
  49. <view class="post-info pl-20">
  50. <view class="post-info_title text-overflow">{{ post.spec.title }}
  51. </view>
  52. <view
  53. class="post-info_summary text-overflow-2 mt-12 text-size-s text-grey-darken-1">
  54. {{ post.status.excerpt }}
  55. </view>
  56. <view class="post-info_time mt-12 text-size-s text-grey-darken-1">
  57. <text class="iconfont icon-clock text-size-s mr-6"></text>
  58. <text class="time-label">发布时间:</text>
  59. {{ {d: post.spec.publishTime, f: 'yyyy年MM月dd日 星期w'} | formatTime }}
  60. </view>
  61. </view>
  62. </view>
  63. </tm-translate>
  64. </block>
  65. </block>
  66. <view v-else class="post-empty text-size-m text-grey-darken-1">该日期下暂无归档文章!</view>
  67. </view>
  68. </view>
  69. </view>
  70. </block>
  71. </view>
  72. <view class="load-text mt-12">{{ loadMoreText }}</view>
  73. <!-- 返回顶部 -->
  74. <tm-flotbutton @click="fnToTopPage" size="m" color="bg-gradient-light-blue-accent"
  75. icon="icon-angle-up"></tm-flotbutton>
  76. </block>
  77. </view>
  78. </template>
  79. <script>
  80. import tmSkeleton from '@/tm-vuetify/components/tm-skeleton/tm-skeleton.vue';
  81. import tmTranslate from '@/tm-vuetify/components/tm-translate/tm-translate.vue';
  82. import tmFlotbutton from '@/tm-vuetify/components/tm-flotbutton/tm-flotbutton.vue';
  83. import tmTabs from '@/tm-vuetify/components/tm-tabs/tm-tabs.vue';
  84. import tmEmpty from '@/tm-vuetify/components/tm-empty/tm-empty.vue';
  85. import qs from 'qs'
  86. export default {
  87. components: {
  88. tmSkeleton,
  89. tmTranslate,
  90. tmFlotbutton,
  91. tmTabs,
  92. tmEmpty
  93. },
  94. data() {
  95. return {
  96. loading: 'loading',
  97. tab: {
  98. activeIndex: 0,
  99. list: ['按月份查看', '按年份查看']
  100. },
  101. queryParams: {
  102. size: 10,
  103. page: 1
  104. },
  105. result: {},
  106. cacheDataList: [], // 所有请求的缓存数据
  107. dataList: [], // 显示的数据
  108. isLoadMore: false,
  109. loadMoreText: "加载中..."
  110. };
  111. },
  112. computed: {
  113. black_tmeme: function() {
  114. return this.$tm.vx.state().tmVuetify.black;
  115. },
  116. color_tmeme: function() {
  117. return this.$tm.vx.state().tmVuetify.color;
  118. }
  119. },
  120. created() {
  121. this.fnGetData();
  122. },
  123. onPullDownRefresh() {
  124. this.isLoadMore = false;
  125. this.queryParams.page = 1;
  126. this.fnGetData();
  127. },
  128. onReachBottom(e) {
  129. if (this.result.hasNext) {
  130. this.queryParams.page += 1;
  131. this.isLoadMore = true;
  132. this.fnGetData();
  133. } else {
  134. uni.showToast({
  135. icon: 'none',
  136. title: '没有更多数据了'
  137. });
  138. }
  139. },
  140. methods: {
  141. fnOnTabChange(index) {
  142. this.fnResetSetAniWaitIndex();
  143. this.queryParams.page = 0;
  144. this.dataList = this.handleGetShowDataList(this.handleGetPosts(this.cacheDataList))
  145. },
  146. fnGetData() {
  147. if (this.isLoadMore) {
  148. uni.showLoading({
  149. title: "加载中..."
  150. })
  151. } else {
  152. this.loading = 'loading';
  153. this.loadMoreText = "加载中...";
  154. }
  155. const paramsStr = qs.stringify(this.queryParams, {
  156. allowDots: true,
  157. encodeValuesOnly: true,
  158. skipNulls: true,
  159. encode: true,
  160. arrayFormat: 'repeat'
  161. })
  162. uni.request({
  163. url: this.$baseApiUrl + '/apis/api.content.halo.run/v1alpha1/posts?' + paramsStr,
  164. method: 'GET',
  165. success: (res) => {
  166. const data = res.data;
  167. this.result = data;
  168. const posts = this.handleGetPosts(data.items)
  169. const showDataList = this.handleGetShowDataList(posts)
  170. if (this.isLoadMore) {
  171. this.cacheDataList = this.cacheDataList.concat(data.items);
  172. this.handleMergeDataList2(showDataList)
  173. } else {
  174. this.dataList = []
  175. this.cacheDataList = data.items;
  176. this.dataList = showDataList
  177. }
  178. this.loading = 'success';
  179. this.loadMoreText = data.hasNext ? '上拉加载更多' : '呜呜,没有更多数据啦~';
  180. uni.hideLoading();
  181. uni.stopPullDownRefresh();
  182. },
  183. fail: (err) => {
  184. this.loading = 'error';
  185. this.loadMoreText = '加载失败,请下拉刷新!';
  186. uni.$tm.toast(err.message || '数据加载失败!');
  187. uni.hideLoading();
  188. uni.stopPullDownRefresh();
  189. }
  190. })
  191. },
  192. // 处理数据分类
  193. handleGetPosts(dataList) {
  194. const posts = {}
  195. const postLabelYearKey = "content.halo.run/archive-year"
  196. const postLabelMonthKey = "content.halo.run/archive-month"
  197. dataList.forEach(item => {
  198. let postItemKey = ""
  199. if (this.tab.activeIndex == 0) {
  200. postItemKey =
  201. `${item.metadata.labels[postLabelYearKey]}-${item.metadata.labels[postLabelMonthKey]}`
  202. } else {
  203. postItemKey = `${item.metadata.labels[postLabelYearKey]}`
  204. }
  205. if (posts[postItemKey]) {
  206. posts[postItemKey].push(item)
  207. } else {
  208. posts[postItemKey] = [item]
  209. }
  210. })
  211. return posts;
  212. },
  213. // 根据分类的数据,处理成显示的数据
  214. handleGetShowDataList(posts) {
  215. const dataListResult = []
  216. Object.keys(posts).forEach((key) => {
  217. const postData = {
  218. sort: 0,
  219. key: key,
  220. year: key,
  221. month: "",
  222. posts: posts[key]
  223. }
  224. if (this.tab.activeIndex == 0) {
  225. const splitDate = key.split("-")
  226. postData.year = splitDate[0]
  227. postData.month = splitDate[1]
  228. postData.sort = Number(key.replace("-", ""))
  229. } else {
  230. postData.sort = Number(key)
  231. }
  232. dataListResult.push(postData)
  233. })
  234. if (this.tab.activeIndex == 1) {
  235. dataListResult.sort((a, b) => {
  236. return Number(b.year) - Number(a.year)
  237. })
  238. }
  239. return dataListResult;
  240. },
  241. handleMergeDataList(list1, list2) {
  242. // 将list1转换为以key为键的对象
  243. let merged = list1.reduce((acc, item) => {
  244. acc[item.key] = {
  245. ...item
  246. };
  247. return acc;
  248. }, {});
  249. // 遍历list2,合并posts数组或添加新对象
  250. list2.forEach(item => {
  251. if (merged[item.key]) {
  252. // 如果key已存在,合并posts数组
  253. merged[item.key].posts = [...merged[item.key].posts, ...item.posts];
  254. } else {
  255. // 如果key不存在,添加新对象
  256. merged[item.key] = {
  257. ...item
  258. };
  259. }
  260. });
  261. // 将对象转换回数组
  262. return Object.values(merged);
  263. },
  264. handleMergeDataList2(list) {
  265. const tempList = [...list]
  266. this.dataList.forEach((item, index) => {
  267. const find = this.dataList.find(x => x.key == item.key)
  268. if (find) {
  269. item.posts.forEach(post => {
  270. if (!find.posts.some(x => x.metadata.name == post.metadata.name)) {
  271. find.posts.push(post)
  272. }
  273. })
  274. tempList.splice(index, 1)
  275. }
  276. })
  277. tempList.forEach(post => {
  278. this.dataList.push(post)
  279. })
  280. console.log("this.dataList", this.dataList)
  281. },
  282. fnToArticleDetail(article) {
  283. uni.navigateTo({
  284. url: '/pagesA/article-detail/article-detail?name=' + article.metadata.name,
  285. animationType: 'slide-in-right'
  286. });
  287. }
  288. }
  289. };
  290. </script>
  291. <style lang="scss" scoped>
  292. .app-page {
  293. width: 100vw;
  294. min-height: 100vh;
  295. display: flex;
  296. flex-direction: column;
  297. background-color: #fafafd;
  298. }
  299. .loading-wrap {
  300. padding: 24rpx;
  301. }
  302. .list-empty {
  303. width: 100vw;
  304. height: 100vh;
  305. }
  306. .statistics {
  307. background-color: #ffffff;
  308. }
  309. .e-timeline {
  310. ::v-deep {
  311. .tm-timeline-item>view:first-child {
  312. width: 110rpx !important;
  313. }
  314. .tm-timeline-item-left {
  315. max-width: 580rpx !important;
  316. width: 100% !important;
  317. }
  318. }
  319. }
  320. .tm-timeline {
  321. .tm-timeline-item {
  322. .tm-timeline-item-left,
  323. .tm-timeline-item-right {
  324. width: 200rpx;
  325. flex-shrink: 0;
  326. }
  327. .tm-timeline-item-content {
  328. display: flex;
  329. justify-content: center;
  330. align-items: flex-start;
  331. align-content: flex-start;
  332. }
  333. .tm-timeline-jidian {
  334. margin: auto;
  335. }
  336. &.tm-timeline-item--leftDir {
  337. display: flex;
  338. flex-flow: row;
  339. &.endright {
  340. justify-content: flex-end;
  341. }
  342. .tm-timeline-item-left,
  343. .tm-timeline-item-right {
  344. width: auto;
  345. max-width: 400rpx;
  346. }
  347. .tm-timeline-item-boder {
  348. height: 100%;
  349. width: 1px;
  350. margin: auto;
  351. }
  352. .tm-timeline-jidian {
  353. position: relative;
  354. margin: auto;
  355. z-index: 2;
  356. }
  357. .tm-timeline-item-content {
  358. display: flex;
  359. justify-content: flex-start;
  360. align-items: flex-start;
  361. align-content: flex-start;
  362. }
  363. }
  364. }
  365. }
  366. .post {
  367. width: 560rpx;
  368. border-radius: 12rpx;
  369. background-color: #fff;
  370. &.lr_image_text {}
  371. &.lr_text_image {
  372. .post-thumbnail {
  373. order: 2;
  374. }
  375. .post-info {
  376. order: 1;
  377. padding-left: 0;
  378. padding-right: 24rpx;
  379. }
  380. }
  381. &.tb_image_text {
  382. flex-direction: column;
  383. .post-thumbnail {
  384. width: 100%;
  385. height: 220rpx;
  386. }
  387. .post-info {
  388. width: 100%;
  389. padding-left: 0;
  390. &_title {
  391. margin-top: 12rpx;
  392. }
  393. &_time {
  394. .iconfont {
  395. display: none;
  396. }
  397. .time-label {
  398. display: inline-block;
  399. }
  400. }
  401. }
  402. }
  403. &.tb_text_image {
  404. flex-direction: column;
  405. .post-thumbnail {
  406. order: 2;
  407. width: 100%;
  408. height: 220rpx;
  409. margin-top: 12rpx;
  410. }
  411. .post-info {
  412. order: 1;
  413. width: 100%;
  414. padding-left: 0;
  415. &_time {
  416. .iconfont {
  417. display: none;
  418. }
  419. .time-label {
  420. display: inline-block;
  421. }
  422. }
  423. }
  424. }
  425. &.only_text {
  426. .post-info {
  427. padding: 6rpx;
  428. &_time {
  429. margin-top: 20rpx;
  430. .iconfont {
  431. display: none;
  432. }
  433. .time-label {
  434. display: inline-block;
  435. }
  436. }
  437. }
  438. .post-thumbnail {
  439. display: none;
  440. }
  441. }
  442. }
  443. .post-thumbnail {
  444. border-radius: 6rpx;
  445. width: 200rpx;
  446. height: 170rpx;
  447. }
  448. .post-info {
  449. width: 0;
  450. flex-grow: 1;
  451. &_title {
  452. color: #303133;
  453. font-size: 28rpx;
  454. font-weight: bold;
  455. }
  456. &_summary {
  457. display: -webkit-box;
  458. line-height: 1.6;
  459. }
  460. &_time {
  461. .time-label {
  462. display: none;
  463. }
  464. }
  465. }
  466. .time {
  467. align-items: center;
  468. }
  469. </style>