index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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" :current="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. // 文章列表
  56. articleList: getEmptyPaginateObj(),
  57. // 上拉加载配置
  58. upOption: {
  59. // 首次自动执行
  60. auto: true,
  61. // 每页数据的数量; 默认10
  62. page: { size: pageSize },
  63. // 数量要大于3条才显示无更多数据
  64. noMoreSize: 3,
  65. }
  66. }
  67. },
  68. /**
  69. * 生命周期函数--监听页面加载
  70. */
  71. onLoad(options) {
  72. const app = this
  73. // 获取文章分类数据
  74. app.getCategoryList(options.categoryId)
  75. },
  76. methods: {
  77. /**
  78. * 上拉加载的回调 (页面初始化时也会执行一次)
  79. * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
  80. * @param {Object} page
  81. */
  82. upCallback(page) {
  83. const app = this
  84. // 设置列表数据
  85. app.getArticleList(page.num)
  86. .then(list => {
  87. const curPageLen = list.data.length
  88. const totalSize = list.data.total
  89. app.mescroll.endBySize(curPageLen, totalSize)
  90. })
  91. .catch(() => app.mescroll.endErr())
  92. },
  93. // 获取文章分类数据
  94. getCategoryList(categoryId) {
  95. CategoryApi.list().then(result => {
  96. this.setTabList(result.data.list, categoryId)
  97. })
  98. },
  99. // 设置选项卡数据
  100. setTabList(categoryList, categoryId) {
  101. const app = this
  102. app.tabList = [{ value: 0, name: '全部' }]
  103. categoryList.forEach(item => {
  104. app.tabList.push({ value: item.category_id, name: item.name })
  105. })
  106. if (categoryId > 0) {
  107. const findIndex = app.tabList.findIndex(item => item.value == categoryId)
  108. app.curTab = findIndex > -1 ? findIndex : 0
  109. }
  110. },
  111. /**
  112. * 获取文章列表
  113. * @param {Number} pageNo 页码
  114. */
  115. getArticleList(pageNo = 1) {
  116. const app = this
  117. return new Promise((resolve, reject) => {
  118. ArticleApi.list({ categoryId: app.getTabValue(), page: pageNo }, { load: false })
  119. .then(result => {
  120. // 合并新数据
  121. const newList = result.data.list
  122. app.articleList.data = getMoreListData(newList, app.articleList, pageNo)
  123. resolve(newList)
  124. })
  125. .catch(reject)
  126. })
  127. },
  128. // 切换标签项
  129. onChangeTab(index) {
  130. // 设置当前选中的标签
  131. this.curTab = index
  132. // 刷新订单列表
  133. this.onRefreshList()
  134. },
  135. // 获取当前标签项的值
  136. getTabValue() {
  137. const app = this
  138. return app.tabList.length ? app.tabList[app.curTab].value : 0
  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>