index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ use: false }" :up="upOption"
  3. @up="upCallback">
  4. <!-- tab栏 -->
  5. <u-tabs :list="tabList" :is-scroll="true" :current="curTab" active-color="#fd4a5f" :duration="0.2"
  6. @change="onChangeTab" />
  7. <!-- 文章列表 -->
  8. <view class="article-list">
  9. <view class="article-item" :class="[`show-type__${item.show_type}`]" v-for="(item, index) in articleList.data"
  10. :key="index" @click="onTargetDetail(item.article_id)">
  11. <!-- 小图模式 -->
  12. <block v-if="item.show_type == 10">
  13. <view class="article-item__left flex-box">
  14. <view class="article-item__title">
  15. <text class="twoline-hide">{{ item.title }}</text>
  16. </view>
  17. <view class="article-item__footer m-top10">
  18. <text class="article-views f-24 col-8">{{ item.show_views }}次浏览</text>
  19. </view>
  20. </view>
  21. <view class="article-item__image">
  22. <image class="image" mode="widthFix" :src="item.image_url"></image>
  23. </view>
  24. </block>
  25. <!-- 大图模式 -->
  26. <block v-if="item.show_type == 20">
  27. <view class="article-item__title">
  28. <text class="twoline-hide">{{ item.title }}</text>
  29. </view>
  30. <view class="article-item__image m-top20">
  31. <image class="image" mode="widthFix" :src="item.image_url"></image>
  32. </view>
  33. <view class="article-item__footer m-top10">
  34. <text class="article-views f-24 col-8">{{ item.show_views }}次浏览</text>
  35. </view>
  36. </block>
  37. </view>
  38. </view>
  39. </mescroll-body>
  40. </template>
  41. <script>
  42. import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
  43. import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
  44. import * as ArticleApi from '@/api/article'
  45. import * as CategoryApi from '@/api/article/category'
  46. import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
  47. const pageSize = 15
  48. export default {
  49. components: {
  50. MescrollBody
  51. },
  52. mixins: [MescrollMixin],
  53. data() {
  54. return {
  55. // 选项卡列表
  56. tabList: [],
  57. // 当前选项
  58. curTab: 0,
  59. // 文章列表
  60. articleList: getEmptyPaginateObj(),
  61. // 上拉加载配置
  62. upOption: {
  63. // 首次自动执行
  64. auto: true,
  65. // 每页数据的数量; 默认10
  66. page: { size: pageSize },
  67. // 数量要大于3条才显示无更多数据
  68. noMoreSize: 3,
  69. }
  70. }
  71. },
  72. /**
  73. * 生命周期函数--监听页面加载
  74. */
  75. onLoad(options) {
  76. const app = this
  77. // 获取文章分类数据
  78. app.getCategoryList(options.categoryId)
  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(categoryId) {
  99. CategoryApi.list().then(result => {
  100. this.setTabList(result.data.list, categoryId)
  101. })
  102. },
  103. // 设置选项卡数据
  104. setTabList(categoryList, categoryId) {
  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 (categoryId > 0) {
  111. const findIndex = app.tabList.findIndex(item => item.value == 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.getTabValue(), 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. // 刷新订单列表
  137. this.onRefreshList()
  138. },
  139. // 获取当前标签项的值
  140. getTabValue() {
  141. const app = this
  142. return app.tabList.length ? app.tabList[app.curTab].value : 0
  143. },
  144. // 刷新列表数据
  145. onRefreshList() {
  146. this.articleList = getEmptyPaginateObj()
  147. setTimeout(() => this.mescroll.resetUpScroll(), 120)
  148. },
  149. // 跳转文章详情页
  150. onTargetDetail(articleId) {
  151. this.$navTo('pages/article/detail', { articleId })
  152. }
  153. },
  154. /**
  155. * 分享当前页面
  156. */
  157. onShareAppMessage() {
  158. return {
  159. title: '文章首页',
  160. path: "/pages/article/index?" + this.$getShareUrlParams()
  161. }
  162. },
  163. /**
  164. * 分享到朋友圈
  165. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  166. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  167. */
  168. onShareTimeline() {
  169. return {
  170. title: '文章首页',
  171. path: "/pages/article/index?" + this.$getShareUrlParams()
  172. }
  173. }
  174. }
  175. </script>
  176. <style lang="scss" scoped>
  177. .container {
  178. min-height: 100vh;
  179. }
  180. // 文章列表
  181. .article-list {
  182. padding-top: 20rpx;
  183. line-height: 1;
  184. background: #f7f7f7;
  185. }
  186. .article-item {
  187. margin-bottom: 20rpx;
  188. padding: 30rpx;
  189. background: #fff;
  190. &:last-child {
  191. margin-bottom: 0;
  192. }
  193. .article-item__title {
  194. max-height: 74rpx;
  195. font-size: 28rpx;
  196. line-height: 38rpx;
  197. color: #333;
  198. }
  199. .article-item__image .image {
  200. display: block;
  201. }
  202. }
  203. // 小图模式
  204. .show-type__10 {
  205. display: flex;
  206. .article-item__left {
  207. padding-right: 20rpx;
  208. }
  209. .article-item__title {
  210. // min-height: 72rpx;
  211. }
  212. .article-item__image .image {
  213. width: 240rpx;
  214. }
  215. }
  216. // 大图模式
  217. .show-type__20 .article-item__image .image {
  218. width: 100%;
  219. }
  220. </style>