index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <view class="container">
  3. <!-- 搜索框 -->
  4. <search tips="搜索商品" @event="$navTo('pages/search/index')" />
  5. <!-- 分类样式:一级分类(大图) 10 -->
  6. <view class="cate-content" v-if="templet.style == 10 && list.length > 0">
  7. <view class="cate-wrapper cate_style__10">
  8. <scroll-view :scroll-y="true" :style="{ height: `${scrollHeight - 10}px` }">
  9. <view class="cate-item" v-for="(item, index) in list" :key="index"
  10. @click="onTargetGoodsList(item.category_id)">
  11. <image v-if="item.image" mode="widthFix" :src="item.image.preview_url"></image>
  12. </view>
  13. </scroll-view>
  14. </view>
  15. </view>
  16. <!-- 分类样式:一级分类(小图) 11 -->
  17. <view class="cate-content" v-else-if="templet.style == 11 && list.length > 0">
  18. <view class="cate-wrapper cate_style__11">
  19. <scroll-view class="clear" :scroll-y="true" :style="{ height: `${scrollHeight - 10}px` }">
  20. <view class="cate-item" v-for="(item, index) in list" :key="index"
  21. @click="onTargetGoodsList(item.category_id)">
  22. <image v-if="item.image" mode="widthFix" :src="item.image.preview_url"></image>
  23. <text class="f-26">{{ item.name }}</text>
  24. </view>
  25. </scroll-view>
  26. </view>
  27. </view>
  28. <!-- 分类样式:二级分类 20 -->
  29. <view class="cate-content dis-flex" v-else-if="templet.style == 20 && list.length > 0">
  30. <!-- 左侧 一级分类 -->
  31. <scroll-view class="cate-left f-28" :scroll-y="true" :style="{ height: `${scrollHeight}px` }">
  32. <text class="type-nav" :class="{ selected: curIndex == index }" v-for="(item, index) in list" :key="index"
  33. @click="handleSelectNav(index)">{{ item.name }}</text>
  34. </scroll-view>
  35. <!-- 右侧 二级分类 -->
  36. <scroll-view class="cate-right b-f" :scroll-top="scrollTop" :scroll-y="true"
  37. :style="{ height: `${scrollHeight}px` }">
  38. <view v-if="list[curIndex]">
  39. <view class="cate-right-cont">
  40. <view class="cate-two-box">
  41. <view class="cate-cont-box">
  42. <view class="flex-three" v-for="(item, idx) in list[curIndex].children" :key="idx"
  43. @click="onTargetGoodsList(item.category_id)">
  44. <view class="cate-img">
  45. <image v-if="item.image" :src="item.image.preview_url"></image>
  46. </view>
  47. <text>{{ item.name }}</text>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </scroll-view>
  54. </view>
  55. <empty v-if="!list.length" :isLoading="isLoading" />
  56. </view>
  57. </template>
  58. <script>
  59. import { setCartTabBadge } from '@/utils/app'
  60. import SettingKeyEnum from '@/common/enum/setting/Key'
  61. import SettingModel from '@/common/model/Setting'
  62. import * as CategoryApi from '@/api/category'
  63. import Search from '@/components/search'
  64. import Empty from '@/components/empty'
  65. // 最后一次刷新时间
  66. let lastRefreshTime;
  67. export default {
  68. components: {
  69. Search,
  70. Empty
  71. },
  72. data() {
  73. return {
  74. // 列表高度
  75. scrollHeight: 0,
  76. // 一级分类:指针
  77. curIndex: 0,
  78. // 内容区竖向滚动条位置
  79. scrollTop: 0,
  80. // 分类列表
  81. list: [],
  82. // 分类模板设置
  83. templet: {},
  84. // 正在加载中
  85. isLoading: true
  86. }
  87. },
  88. /**
  89. * 生命周期函数--监听页面加载
  90. */
  91. onLoad() {
  92. const app = this
  93. // 设置分类列表高度
  94. app.setListHeight()
  95. // 加载页面数据
  96. app.onRefreshPage()
  97. },
  98. /**
  99. * 生命周期函数--监听页面显示
  100. */
  101. onShow() {
  102. // 每间隔5分钟自动刷新一次页面数据
  103. const curTime = new Date().getTime()
  104. if ((curTime - lastRefreshTime) > 5 * 60 * 1000) {
  105. this.onRefreshPage()
  106. }
  107. },
  108. methods: {
  109. // 刷新页面
  110. onRefreshPage() {
  111. // 记录刷新时间
  112. lastRefreshTime = new Date().getTime()
  113. // 获取页面数据
  114. this.getPageData()
  115. // 更新购物车角标
  116. setCartTabBadge()
  117. },
  118. /**
  119. * 获取页面数据
  120. */
  121. getPageData() {
  122. const app = this
  123. app.isLoading = true
  124. Promise.all([
  125. // 获取分类模板设置
  126. // 优化建议: 可以将此处的false改为true 启用缓存
  127. SettingModel.data(false),
  128. // 获取分类列表
  129. CategoryApi.list()
  130. ])
  131. .then(result => {
  132. // 初始化分类模板设置
  133. app.initTemplet(result[0])
  134. // 初始化分类列表数据
  135. app.initCategory(result[1])
  136. })
  137. .finally(() => app.isLoading = false)
  138. },
  139. /**
  140. * 初始化分类模板设置
  141. * @param {Object} result
  142. */
  143. initTemplet(setting) {
  144. this.templet = setting[SettingKeyEnum.PAGE_CATEGORY_TEMPLATE.value]
  145. },
  146. /**
  147. * 初始化分类列表数据
  148. * @param {Object} result
  149. */
  150. initCategory(result) {
  151. const app = this
  152. const data = result.data
  153. app.list = data.list
  154. },
  155. /**
  156. * 设置分类列表高度
  157. */
  158. setListHeight() {
  159. const app = this
  160. uni.getSystemInfo({
  161. success(res) {
  162. app.scrollHeight = res.windowHeight - 47
  163. }
  164. })
  165. },
  166. // 一级分类:选中分类
  167. handleSelectNav(index) {
  168. const app = this
  169. app.curIndex = index
  170. app.scrollTop = 0
  171. },
  172. // 跳转至商品列表页
  173. onTargetGoodsList(categoryId) {
  174. this.$navTo('pages/goods/list', { categoryId })
  175. }
  176. },
  177. /**
  178. * 设置分享内容
  179. */
  180. onShareAppMessage() {
  181. const app = this
  182. return {
  183. title: _this.templet.shareTitle,
  184. path: '/pages/category/index?' + app.$getShareUrlParams()
  185. }
  186. },
  187. /**
  188. * 分享到朋友圈
  189. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  190. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  191. */
  192. onShareTimeline() {
  193. const app = this
  194. return {
  195. title: _this.templet.shareTitle,
  196. path: '/pages/category/index?' + app.$getShareUrlParams()
  197. }
  198. }
  199. }
  200. </script>
  201. <style>
  202. page {
  203. background: #fff;
  204. }
  205. </style>
  206. <style lang="scss" scoped>
  207. .cate-content {
  208. background: #fff;
  209. }
  210. .cate-wrapper {
  211. padding: 0 20rpx 20rpx 20rpx;
  212. box-sizing: border-box;
  213. }
  214. /* 一级分类(大图) 10 */
  215. .cate_style__10 .cate-item {
  216. margin-bottom: 18rpx;
  217. }
  218. .cate_style__10 .cate-item:last-child {
  219. margin-bottom: 0;
  220. }
  221. .cate_style__10 .cate-item image {
  222. display: block;
  223. width: 100%;
  224. height: auto;
  225. }
  226. /* 一级分类(小图) 11 */
  227. .cate_style__11 .cate-item {
  228. float: left;
  229. padding: 25rpx;
  230. width: 33.3333%;
  231. text-align: center;
  232. box-sizing: border-box;
  233. }
  234. .cate_style__11 .cate-item image {
  235. /* height: 208.656rpx; */
  236. width: 100%;
  237. display: block;
  238. }
  239. .cate_style__11 .cate-item image {
  240. display: block;
  241. width: 100%;
  242. margin-bottom: 10rpx;
  243. }
  244. .cate_style__11 .cate-item text {
  245. display: block;
  246. color: #555;
  247. }
  248. /* 二级分类 20 */
  249. .cate-content {
  250. width: 100%;
  251. }
  252. .cate-left {
  253. flex-direction: column;
  254. display: flex;
  255. width: 200rpx;
  256. color: #444;
  257. height: 100%;
  258. background: #f8f8f8;
  259. }
  260. .cate-right {
  261. display: flex;
  262. flex-direction: column;
  263. width: 550rpx;
  264. height: 100%;
  265. overflow: hidden;
  266. }
  267. .cate-right-cont {
  268. width: 100%;
  269. display: flex;
  270. flex-flow: row wrap;
  271. align-content: flex-start;
  272. padding-top: 26rpx;
  273. }
  274. .type-nav {
  275. position: relative;
  276. height: 90rpx;
  277. line-height: 90rpx;
  278. text-align: center;
  279. z-index: 10;
  280. display: block;
  281. font-size: 26rpx;
  282. }
  283. .type-nav.selected {
  284. background: #fff;
  285. color: #fa2209;
  286. border-right: none;
  287. font-size: 28rpx;
  288. }
  289. .cate-cont-box {
  290. margin-bottom: 30rpx;
  291. padding-bottom: 10rpx;
  292. background: #fff;
  293. overflow: hidden;
  294. }
  295. .cate-cont-box .cate-img {
  296. padding: 13rpx 10rpx 4rpx 10rpx;
  297. }
  298. .cate-cont-box .cate-img image {
  299. width: 100%;
  300. height: 150rpx;
  301. border-radius: 10rpx;
  302. }
  303. .cate-cont-box text {
  304. text-align: center;
  305. display: block;
  306. font-size: 26rpx;
  307. padding-bottom: 14rpx;
  308. color: #444;
  309. padding: 0 15rpx 30rpx 15rpx;
  310. }
  311. .cate-two-box {
  312. width: 100%;
  313. padding: 0 10px;
  314. }
  315. </style>