index.vue 4.6 KB

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