index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view class="container">
  3. <!-- 搜索框 -->
  4. <search class="search" tips="搜索商品" @event="$navTo('pages/search/index')" />
  5. <!-- 一级分类 -->
  6. <primary v-if="setting.style == PageCategoryStyleEnum.ONE_LEVEL_BIG.value || setting.style == PageCategoryStyleEnum.ONE_LEVEL_SMALL.value"
  7. :display="setting.style" :list="list" />
  8. <!-- 二级分类 -->
  9. <secondary v-if="setting.style == PageCategoryStyleEnum.TWO_LEVEL.value" :list="list" />
  10. <!-- 分类+商品 -->
  11. <commodity v-if="setting.style == PageCategoryStyleEnum.COMMODITY.value" ref="mescrollItem" :list="list" :setting="setting" />
  12. </view>
  13. </template>
  14. <script>
  15. import MescrollCompMixin from '@/components/mescroll-uni/mixins/mescroll-comp'
  16. import { setCartTabBadge } from '@/core/app'
  17. import SettingKeyEnum from '@/common/enum/setting/Key'
  18. import { PageCategoryStyleEnum } from '@/common/enum/store/page/category'
  19. import SettingModel from '@/common/model/Setting'
  20. import * as CategoryApi from '@/api/category'
  21. import Empty from '@/components/empty'
  22. import Search from '@/components/search'
  23. import Primary from './components/primary'
  24. import Secondary from './components/secondary'
  25. import Commodity from './components/commodity'
  26. // 最后一次刷新时间
  27. let lastRefreshTime;
  28. export default {
  29. components: {
  30. Search,
  31. Empty,
  32. Primary,
  33. Secondary,
  34. Commodity
  35. },
  36. mixins: [MescrollCompMixin],
  37. data() {
  38. return {
  39. // 枚举类
  40. PageCategoryStyleEnum,
  41. // 分类列表
  42. list: [],
  43. // 分类模板设置
  44. setting: {},
  45. // 正在加载中
  46. isLoading: true
  47. }
  48. },
  49. /**
  50. * 生命周期函数--监听页面加载
  51. */
  52. onLoad() {
  53. // 加载页面数据
  54. this.onRefreshPage()
  55. },
  56. /**
  57. * 生命周期函数--监听页面显示
  58. */
  59. onShow() {
  60. // 每间隔5分钟自动刷新一次页面数据
  61. const curTime = new Date().getTime()
  62. if ((curTime - lastRefreshTime) > 5 * 60 * 1000) {
  63. this.onRefreshPage()
  64. }
  65. },
  66. methods: {
  67. // 刷新页面
  68. onRefreshPage() {
  69. // 记录刷新时间
  70. lastRefreshTime = new Date().getTime()
  71. // 获取页面数据
  72. this.getPageData()
  73. // 更新购物车角标
  74. setCartTabBadge()
  75. },
  76. // 获取页面数据
  77. getPageData() {
  78. const app = this
  79. app.isLoading = true
  80. Promise.all([
  81. // 获取分类模板设置
  82. // 优化建议: 可以将此处的false改为true 启用缓存
  83. SettingModel.data(false),
  84. // 获取分类列表
  85. CategoryApi.list()
  86. ])
  87. .then(result => {
  88. // 初始化分类模板设置
  89. app.initSetting(result[0])
  90. // 初始化分类列表数据
  91. app.initCategory(result[1])
  92. })
  93. .finally(() => app.isLoading = false)
  94. },
  95. /**
  96. * 初始化分类模板设置
  97. * @param {Object} result
  98. */
  99. initSetting(setting) {
  100. this.setting = setting[SettingKeyEnum.PAGE_CATEGORY_TEMPLATE.value]
  101. },
  102. /**
  103. * 初始化分类列表数据
  104. * @param {Object} result
  105. */
  106. initCategory(result) {
  107. this.list = result.data.list
  108. },
  109. },
  110. /**
  111. * 设置分享内容
  112. */
  113. onShareAppMessage() {
  114. const app = this
  115. return {
  116. title: _this.templet.shareTitle,
  117. path: '/pages/category/index?' + app.$getShareUrlParams()
  118. }
  119. },
  120. /**
  121. * 分享到朋友圈
  122. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  123. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  124. */
  125. onShareTimeline() {
  126. const app = this
  127. return {
  128. title: _this.templet.shareTitle,
  129. path: '/pages/category/index?' + app.$getShareUrlParams()
  130. }
  131. }
  132. }
  133. </script>
  134. <style>
  135. page {
  136. background: #fff;
  137. }
  138. </style>
  139. <style lang="scss" scoped>
  140. // 搜索框
  141. .search {
  142. position: fixed;
  143. top: var(--window-top);
  144. left: var(--window-left);
  145. right: var(--window-right);
  146. z-index: 9;
  147. padding-bottom: 20rpx;
  148. }
  149. </style>