index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <template>
  2. <view class="container" :style="appThemeStyle">
  3. <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ use: false }" :up="upOption" @up="upCallback">
  4. <!-- tab栏 -->
  5. <u-tabs :list="tabList" :is-scroll="true" v-model="curTab" :active-color="appTheme.mainBg" :duration="0.2" @change="onChangeTab" />
  6. <!-- 文章列表 -->
  7. <view class="article-list">
  8. <view class="article-item" :class="[`show-type__${item.show_type}`]" v-for="(item, index) in articleList.data" :key="index"
  9. @click="onTargetDetail(item.article_id)">
  10. <!-- 小图模式 -->
  11. <block v-if="item.show_type == 10">
  12. <view class="article-item__left flex-box">
  13. <view class="article-item__title">
  14. <text class="twoline-hide">{{ item.title }}</text>
  15. </view>
  16. <view class="article-item__footer m-top10">
  17. <text class="article-views f-24 col-8">{{ item.show_views }}次浏览</text>
  18. </view>
  19. </view>
  20. <view class="article-item__image">
  21. <image class="image" mode="widthFix" :src="item.image_url"></image>
  22. </view>
  23. </block>
  24. <!-- 大图模式 -->
  25. <block v-if="item.show_type == 20">
  26. <view class="article-item__title">
  27. <text class="twoline-hide">{{ item.title }}</text>
  28. </view>
  29. <view class="article-item__image m-top20">
  30. <image class="image" mode="widthFix" :src="item.image_url"></image>
  31. </view>
  32. <view class="article-item__footer m-top10">
  33. <text class="article-views f-24 col-8">{{ item.show_views }}次浏览</text>
  34. </view>
  35. </block>
  36. </view>
  37. </view>
  38. </mescroll-body>
  39. </view>
  40. </template>
  41. <script>
  42. import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins'
  43. import * as ArticleApi from '@/api/article'
  44. import * as CategoryApi from '@/api/article/category'
  45. import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
  46. const pageSize = 15
  47. export default {
  48. mixins: [MescrollMixin],
  49. data() {
  50. return {
  51. // 选项卡列表
  52. tabList: [],
  53. // 当前选项
  54. curTab: 0,
  55. // 当前文章分类ID
  56. categoryId: 0,
  57. // 文章列表
  58. articleList: getEmptyPaginateObj(),
  59. // 上拉加载配置
  60. upOption: {
  61. // 首次自动执行
  62. auto: true,
  63. // 每页数据的数量; 默认10
  64. page: { size: pageSize },
  65. // 数量要大于3条才显示无更多数据
  66. noMoreSize: 3,
  67. }
  68. }
  69. },
  70. /**
  71. * 生命周期函数--监听页面加载
  72. */
  73. onLoad(options) {
  74. const app = this
  75. // 记录文章分类ID
  76. app.categoryId = options.categoryId || 0
  77. // 获取文章分类数据
  78. app.getCategoryList()
  79. },
  80. methods: {
  81. /**
  82. * 上拉加载的回调 (页面初始化时也会执行一次)
  83. * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
  84. * @param {Object} page
  85. */
  86. upCallback(page) {
  87. const app = this
  88. // 设置列表数据
  89. app.getArticleList(page.num)
  90. .then(list => {
  91. const curPageLen = list.data.length
  92. const totalSize = list.data.total
  93. app.mescroll.endBySize(curPageLen, totalSize)
  94. })
  95. .catch(() => app.mescroll.endErr())
  96. },
  97. // 获取文章分类数据
  98. getCategoryList() {
  99. CategoryApi.list().then(result => {
  100. this.setTabList(result.data.list)
  101. })
  102. },
  103. // 设置选项卡数据
  104. setTabList(categoryList) {
  105. const app = this
  106. app.tabList = [{ value: 0, name: '全部' }]
  107. categoryList.forEach(item => {
  108. app.tabList.push({ value: item.category_id, name: item.name })
  109. })
  110. if (app.categoryId > 0) {
  111. const findIndex = app.tabList.findIndex(item => item.value == app.categoryId)
  112. app.curTab = findIndex > -1 ? findIndex : 0
  113. }
  114. },
  115. /**
  116. * 获取文章列表
  117. * @param {Number} pageNo 页码
  118. */
  119. getArticleList(pageNo = 1) {
  120. const app = this
  121. return new Promise((resolve, reject) => {
  122. ArticleApi.list({ categoryId: app.categoryId, page: pageNo }, { load: false })
  123. .then(result => {
  124. // 合并新数据
  125. const newList = result.data.list
  126. app.articleList.data = getMoreListData(newList, app.articleList, pageNo)
  127. resolve(newList)
  128. })
  129. .catch(reject)
  130. })
  131. },
  132. // 切换标签项
  133. onChangeTab(index) {
  134. // 设置当前选中的标签
  135. this.curTab = index
  136. this.categoryId = this.tabList[index].value
  137. // 刷新订单列表
  138. this.onRefreshList()
  139. },
  140. // 刷新列表数据
  141. onRefreshList() {
  142. this.articleList = getEmptyPaginateObj()
  143. setTimeout(() => this.mescroll.resetUpScroll(), 120)
  144. },
  145. // 跳转文章详情页
  146. onTargetDetail(articleId) {
  147. this.$navTo('pages/article/detail', { articleId })
  148. },
  149. },
  150. /**
  151. * 分享当前页面
  152. */
  153. onShareAppMessage() {
  154. return {
  155. title: '文章首页',
  156. path: "/pages/article/index?" + this.$getShareUrlParams()
  157. }
  158. },
  159. /**
  160. * 分享到朋友圈
  161. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  162. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  163. */
  164. onShareTimeline() {
  165. return {
  166. title: '文章首页',
  167. path: "/pages/article/index?" + this.$getShareUrlParams()
  168. }
  169. },
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. .container {
  174. min-height: 100vh;
  175. }
  176. // 文章列表
  177. .article-list {
  178. padding-top: 20rpx;
  179. line-height: 1;
  180. background: #f7f7f7;
  181. }
  182. .article-item {
  183. margin-bottom: 20rpx;
  184. padding: 30rpx;
  185. background: #fff;
  186. &:last-child {
  187. margin-bottom: 0;
  188. }
  189. .article-item__title {
  190. max-height: 74rpx;
  191. font-size: 28rpx;
  192. line-height: 38rpx;
  193. color: #333;
  194. }
  195. .article-item__image .image {
  196. display: block;
  197. }
  198. }
  199. // 小图模式
  200. .show-type__10 {
  201. display: flex;
  202. .article-item__left {
  203. padding-right: 20rpx;
  204. }
  205. .article-item__title {
  206. // min-height: 72rpx;
  207. }
  208. .article-item__image .image {
  209. width: 240rpx;
  210. }
  211. }
  212. // 大图模式
  213. .show-type__20 .article-item__image .image {
  214. width: 100%;
  215. }
  216. </style>