index.vue 7.1 KB

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