news.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 } 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. const getCategory = async () => {
  43. const res = await getCategoryNewss()
  44. // console.log(res)
  45. tagList.value = res.data
  46. // 获取新闻分页数据
  47. getData()
  48. }
  49. // 获取新闻分页数据
  50. const getData = async () => {
  51. let data = {
  52. currentPage: currentPage.value,
  53. pageCount: pageCount.value,
  54. categoryId: tagList.value[currentIndex.value].id
  55. }
  56. const res = await getNewsByCategoryId(data)
  57. // console.log(res)
  58. dataList.value = [...dataList.value, ...res.data.list]
  59. total.value = res.data.totalCount
  60. }
  61. // 分段器切换时的回调
  62. const handleChange = (e) => {
  63. if (currentIndex.value != e) {
  64. currentIndex.value = e
  65. currentPage.value = 1
  66. dataList.value = []
  67. getData()
  68. }
  69. }
  70. // 点击每一条新闻的回调
  71. const clickItem = (item) => {
  72. // console.log(item)
  73. let info = encodeURIComponent(JSON.stringify(item))
  74. uni.navigateTo({
  75. url: `/pages/news_detail/news_detail?info=${info}`
  76. })
  77. }
  78. </script>
  79. <style lang="scss" scoped>
  80. .container {
  81. padding: 20rpx 18rpx;
  82. min-height: 100vh;
  83. .list {
  84. margin-top: 20rpx;
  85. .list_box {
  86. display: flex;
  87. padding: 30rpx;
  88. margin-bottom: 20rpx;
  89. width: 714rpx;
  90. height: 234rpx;
  91. border-radius: 28rpx;
  92. box-shadow: 0 4rpx 34rpx rgba(211, 211, 211, 0.32);
  93. .img {
  94. margin-right: 24rpx;
  95. width: 249rpx;
  96. height: 168rpx;
  97. }
  98. .box_info {
  99. display: flex;
  100. flex-direction: column;
  101. justify-content: space-between;
  102. flex: 1;
  103. font-size: 24rpx;
  104. color: #808080;
  105. .info_title {
  106. color: #000;
  107. font-size: 28rpx;
  108. font-weight: bold;
  109. display: -webkit-box;
  110. -webkit-box-orient: vertical;
  111. -webkit-line-clamp: 2;
  112. overflow: hidden;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. </style>