news.vue 2.8 KB

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