index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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"
  15. :setting="setting" />
  16. </view>
  17. </template>
  18. <script>
  19. import MescrollCompMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-comp'
  20. import {
  21. setCartTabBadge
  22. } from '@/core/app'
  23. import SettingKeyEnum from '@/common/enum/setting/Key'
  24. import {
  25. PageCategoryStyleEnum
  26. } from '@/common/enum/store/page/category'
  27. import SettingModel from '@/common/model/Setting'
  28. import * as CategoryApi from '@/api/category'
  29. import Search from '@/components/search'
  30. import Primary from './components/primary'
  31. import Secondary from './components/secondary'
  32. import Commodity from './components/commodity'
  33. // 最后一次刷新时间
  34. let lastRefreshTime;
  35. export default {
  36. components: {
  37. Search,
  38. Primary,
  39. Secondary,
  40. Commodity
  41. },
  42. mixins: [MescrollCompMixin],
  43. data() {
  44. return {
  45. // 枚举类
  46. PageCategoryStyleEnum,
  47. // 分类列表
  48. list: [],
  49. // 分类模板设置
  50. setting: {},
  51. // 正在加载中
  52. isLoading: true
  53. }
  54. },
  55. /**
  56. * 生命周期函数--监听页面加载
  57. */
  58. onLoad() {
  59. // 加载页面数据
  60. this.onRefreshPage()
  61. },
  62. /**
  63. * 生命周期函数--监听页面显示
  64. */
  65. onShow() {
  66. // 每间隔5分钟自动刷新一次页面数据
  67. const curTime = new Date().getTime()
  68. if ((curTime - lastRefreshTime) > 5 * 60 * 1000) {
  69. this.onRefreshPage()
  70. }
  71. },
  72. methods: {
  73. // 刷新页面
  74. onRefreshPage() {
  75. // 记录刷新时间
  76. lastRefreshTime = new Date().getTime()
  77. // 获取页面数据
  78. this.getPageData()
  79. // 更新购物车角标
  80. setCartTabBadge()
  81. },
  82. // 获取页面数据
  83. getPageData() {
  84. console.log('获取分类页面数据', )
  85. const app = this
  86. app.isLoading = true
  87. const result = [{
  88. "category_id": 10001,
  89. "name": "手机数码",
  90. "parent_id": 0,
  91. "image_id": 10185,
  92. "status": 1,
  93. "sort": 50,
  94. "children": [{
  95. "category_id": 10010,
  96. "name": "小米",
  97. "parent_id": 10001,
  98. "image_id": 10001,
  99. "status": 1,
  100. "sort": 90,
  101. "image": {
  102. "file_id": 10001,
  103. "file_type": 10,
  104. "preview_url": "https:\/\/static.yoshop.xany6.com\/201807171021031720f9679.png",
  105. "external_url": "https:\/\/static.yoshop.xany6.com\/201807171021031720f9679.png"
  106. }
  107. },
  108. {
  109. "category_id": 10011,
  110. "name": "华为",
  111. "parent_id": 10001,
  112. "image_id": 10002,
  113. "status": 1,
  114. "sort": 92,
  115. "image": {
  116. "file_id": 10002,
  117. "file_type": 10,
  118. "preview_url": "https:\/\/static.yoshop.xany6.com\/20180717102129b4e214214.jpg",
  119. "external_url": "https:\/\/static.yoshop.xany6.com\/20180717102129b4e214214.jpg"
  120. }
  121. },
  122. {
  123. "category_id": 10013,
  124. "name": "VIVO",
  125. "parent_id": 10001,
  126. "image_id": 10004,
  127. "status": 1,
  128. "sort": 94,
  129. "image": {
  130. "file_id": 10004,
  131. "file_type": 10,
  132. "preview_url": "https:\/\/static.yoshop.xany6.com\/20180717103633c69c94138.png",
  133. "external_url": "https:\/\/static.yoshop.xany6.com\/20180717103633c69c94138.png"
  134. }
  135. },
  136. {
  137. "category_id": 10012,
  138. "name": "iphone",
  139. "parent_id": 10001,
  140. "image_id": 10003,
  141. "status": 1,
  142. "sort": 96,
  143. "image": {
  144. "file_id": 10003,
  145. "file_type": 10,
  146. "preview_url": "https:\/\/static.yoshop.xany6.com\/20180717103600edf9f5038.jpg",
  147. "external_url": "https:\/\/static.yoshop.xany6.com\/20180717103600edf9f5038.jpg"
  148. }
  149. },
  150. {
  151. "category_id": 10016,
  152. "name": "三星",
  153. "parent_id": 10001,
  154. "image_id": 10007,
  155. "status": 1,
  156. "sort": 100,
  157. "image": {
  158. "file_id": 10007,
  159. "file_type": 10,
  160. "preview_url": "https:\/\/static.yoshop.xany6.com\/20180717103817b50602761.png",
  161. "external_url": "https:\/\/static.yoshop.xany6.com\/20180717103817b50602761.png"
  162. }
  163. },
  164. {
  165. "category_id": 10015,
  166. "name": "魅族",
  167. "parent_id": 10001,
  168. "image_id": 10006,
  169. "status": 1,
  170. "sort": 100,
  171. "image": {
  172. "file_id": 10006,
  173. "file_type": 10,
  174. "preview_url": "https:\/\/static.yoshop.xany6.com\/201807171037574f1163513.jpg",
  175. "external_url": "https:\/\/static.yoshop.xany6.com\/201807171037574f1163513.jpg"
  176. }
  177. },
  178. {
  179. "category_id": 10014,
  180. "name": "OPPO",
  181. "parent_id": 10001,
  182. "image_id": 10005,
  183. "status": 1,
  184. "sort": 100,
  185. "image": {
  186. "file_id": 10005,
  187. "file_type": 10,
  188. "preview_url": "https:\/\/static.yoshop.xany6.com\/2018071710372269a633020.png",
  189. "external_url": "https:\/\/static.yoshop.xany6.com\/2018071710372269a633020.png"
  190. }
  191. },
  192. {
  193. "category_id": 10017,
  194. "name": "女性手机",
  195. "parent_id": 10001,
  196. "image_id": 10008,
  197. "status": 1,
  198. "sort": 105,
  199. "image": {
  200. "file_id": 10008,
  201. "file_type": 10,
  202. "preview_url": "https:\/\/static.yoshop.xany6.com\/20180717103858579b29364.jpg",
  203. "external_url": "https:\/\/static.yoshop.xany6.com\/20180717103858579b29364.jpg"
  204. }
  205. }
  206. ],
  207. "image": {
  208. "file_id": 10185,
  209. "file_type": 10,
  210. "preview_url": "https:\/\/static.yoshop.xany6.com\/201809281707400fb788198.png",
  211. "external_url": "https:\/\/static.yoshop.xany6.com\/201809281707400fb788198.png"
  212. }
  213. },
  214. ]
  215. app.initSetting()
  216. app.initCategory(result)
  217. // Promise.all([
  218. // // 获取分类模板设置
  219. // // 优化建议: 可以将此处的false改为true 启用缓存
  220. // SettingModel.data(false),
  221. // // 获取分类列表
  222. // CategoryApi.list()
  223. // ])
  224. // .then(result => {
  225. // // 初始化分类模板设置
  226. // app.initSetting(result[0])
  227. // // 初始化分类列表数据
  228. // app.initCategory(result[1])
  229. // })
  230. // .finally(() => app.isLoading = false)
  231. },
  232. /**
  233. * 初始化分类模板设置
  234. * @param {Object} result
  235. */
  236. initSetting(setting) {
  237. this.setting = {
  238. "style": 20,
  239. "shareTitle": "全部分类",
  240. "showAddCart": true,
  241. "cartStyle": 1
  242. }
  243. },
  244. /**
  245. * 初始化分类列表数据
  246. * @param {Object} result
  247. */
  248. initCategory(result) {
  249. this.list = result
  250. },
  251. },
  252. /**
  253. * 设置分享内容
  254. */
  255. onShareAppMessage() {
  256. const app = this
  257. return {
  258. title: _this.templet.shareTitle,
  259. path: '/pages/category/index?' + app.$getShareUrlParams()
  260. }
  261. },
  262. /**
  263. * 分享到朋友圈
  264. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  265. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  266. */
  267. onShareTimeline() {
  268. const app = this
  269. return {
  270. title: _this.templet.shareTitle,
  271. path: '/pages/category/index?' + app.$getShareUrlParams()
  272. }
  273. }
  274. }
  275. </script>
  276. <style>
  277. page {
  278. background: #fff;
  279. }
  280. </style>
  281. <style lang="scss" scoped>
  282. // 搜索框
  283. .search {
  284. position: fixed;
  285. top: var(--window-top);
  286. left: var(--window-left);
  287. right: var(--window-right);
  288. z-index: 9;
  289. }
  290. </style>