news.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view class="container">
  3. <!-- 分段器区域 -->
  4. <common-controlTag :tagList="tagList" rangekey="name" type="between" @change="handleChange"></common-controlTag>
  5. <!-- 列表区域 -->
  6. <view class="list">
  7. <!-- 每一条新闻区域 -->
  8. <view class="list_box" v-for="item in dataList" :key="item.id" @click="clickItem(item)">
  9. <image class="img" src="@/static/images/10.png" mode="aspectFill"></image>
  10. <view class="box_info">
  11. <view class="info_title">{{ item.theme }}</view>
  12. <view class="">{{ dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss') }}</view>
  13. </view>
  14. </view>
  15. </view>
  16. <!-- 没有数据时展示的页面 -->
  17. <noData v-if="!dataList.length"></noData>
  18. </view>
  19. </template>
  20. <script setup>
  21. import { onLoad, onReachBottom } from '@dcloudio/uni-app'
  22. import { ref } from 'vue'
  23. import { getCategoryNewss, getNewsByCategoryId } from '@/api/index.js'
  24. import dayjs from 'dayjs'
  25. // 分段器数组
  26. const tagList = ref([])
  27. // 分段器当前索引
  28. const currentIndex = ref(0)
  29. // 当前页
  30. const currentPage = ref(1)
  31. // 每页多少条
  32. const pageCount = ref(6)
  33. // 总条数
  34. const total = ref(0)
  35. // 列表数据
  36. const dataList = ref([])
  37. onLoad(() => {
  38. // 获取新闻分类列表数据
  39. getCategory()
  40. })
  41. // 页面触底触发的回调
  42. onReachBottom(() => {
  43. if (total.value > dataList.value.length) {
  44. currentPage.value++
  45. getData()
  46. } else {
  47. uni.showToast({
  48. title: '没有更多数据了~~',
  49. icon: 'none'
  50. })
  51. }
  52. })
  53. // 获取新闻分类列表数据
  54. const getCategory = async () => {
  55. const res = await getCategoryNewss()
  56. // console.log(res)
  57. tagList.value = res.data
  58. // 获取新闻分页数据
  59. getData()
  60. }
  61. // 获取新闻分页数据
  62. const getData = async () => {
  63. let data = {
  64. currentPage: currentPage.value,
  65. pageCount: pageCount.value,
  66. categoryId: tagList.value[currentIndex.value].id
  67. }
  68. const res = await getNewsByCategoryId(data)
  69. // console.log(res)
  70. dataList.value = [...dataList.value, ...res.data.list]
  71. total.value = res.data.totalCount
  72. }
  73. // 分段器切换时的回调
  74. const handleChange = (e) => {
  75. if (currentIndex.value != e) {
  76. currentIndex.value = e
  77. currentPage.value = 1
  78. dataList.value = []
  79. getData()
  80. }
  81. }
  82. // 点击每一条新闻的回调
  83. const clickItem = (item) => {
  84. // console.log(item)
  85. let info = encodeURIComponent(JSON.stringify(item))
  86. uni.navigateTo({
  87. url: `/pages/news_detail/news_detail?info=${info}`
  88. })
  89. }
  90. </script>
  91. <style lang="scss" scoped>
  92. .container {
  93. padding: 20rpx 18rpx;
  94. min-height: 100vh;
  95. .list {
  96. margin-top: 20rpx;
  97. .list_box {
  98. display: flex;
  99. padding: 30rpx;
  100. margin-bottom: 20rpx;
  101. width: 714rpx;
  102. height: 234rpx;
  103. border-radius: 28rpx;
  104. box-shadow: 0 4rpx 34rpx rgba(211, 211, 211, 0.32);
  105. .img {
  106. margin-right: 24rpx;
  107. width: 249rpx;
  108. height: 168rpx;
  109. }
  110. .box_info {
  111. display: flex;
  112. flex-direction: column;
  113. justify-content: space-between;
  114. flex: 1;
  115. font-size: 24rpx;
  116. color: #808080;
  117. .info_title {
  118. color: #000;
  119. font-size: 28rpx;
  120. font-weight: bold;
  121. display: -webkit-box;
  122. -webkit-box-orient: vertical;
  123. -webkit-line-clamp: 2;
  124. overflow: hidden;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. </style>