index.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. const App = getApp()
  66. export default {
  67. components: {
  68. Search,
  69. Empty
  70. },
  71. data() {
  72. return {
  73. // 列表高度
  74. scrollHeight: 0,
  75. // 一级分类:指针
  76. curIndex: 0,
  77. // 内容区竖向滚动条位置
  78. scrollTop: 0,
  79. // 分类列表
  80. list: [],
  81. // 分类模板设置
  82. templet: {},
  83. // 正在加载中
  84. isLoading: true
  85. }
  86. },
  87. /**
  88. * 生命周期函数--监听页面加载
  89. */
  90. onLoad() {
  91. const app = this
  92. // 设置分类列表高度
  93. app.setListHeight()
  94. },
  95. /**
  96. * 生命周期函数--监听页面显示
  97. */
  98. onShow() {
  99. const app = this
  100. // 获取页面数据
  101. app.getPageData()
  102. // 更新购物车角标
  103. setCartTabBadge()
  104. },
  105. methods: {
  106. /**
  107. * 获取页面数据
  108. */
  109. getPageData() {
  110. const app = this
  111. app.isLoading = true
  112. Promise.all([
  113. // 获取分类模板设置
  114. // 优化建议: 可以将此处的false改为true 启用缓存
  115. SettingModel.data(false),
  116. // 获取分类列表
  117. CategoryApi.list()
  118. ])
  119. .then(result => {
  120. // 初始化分类模板设置
  121. app.initTemplet(result[0])
  122. // 初始化分类列表数据
  123. app.initCategory(result[1])
  124. })
  125. .finally(() => app.isLoading = false)
  126. },
  127. /**
  128. * 初始化分类模板设置
  129. * @param {Object} result
  130. */
  131. initTemplet(setting) {
  132. this.templet = setting[SettingKeyEnum.PAGE_CATEGORY_TEMPLATE.value]
  133. },
  134. /**
  135. * 初始化分类列表数据
  136. * @param {Object} result
  137. */
  138. initCategory(result) {
  139. const app = this
  140. const data = result.data
  141. app.list = data.list
  142. },
  143. /**
  144. * 设置分类列表高度
  145. */
  146. setListHeight() {
  147. const app = this
  148. uni.getSystemInfo({
  149. success(res) {
  150. app.scrollHeight = res.windowHeight - 47
  151. }
  152. })
  153. },
  154. // 一级分类:选中分类
  155. handleSelectNav(index) {
  156. const app = this
  157. app.curIndex = index
  158. app.scrollTop = 0
  159. },
  160. // 跳转至商品列表页
  161. onTargetGoodsList(categoryId) {
  162. this.$navTo('pages/goods/list', { categoryId })
  163. }
  164. },
  165. /**
  166. * 设置分享内容
  167. */
  168. onShareAppMessage() {
  169. const app = this
  170. return {
  171. title: _this.templet.shareTitle,
  172. path: '/pages/category/index?' + app.$getShareUrlParams()
  173. }
  174. },
  175. /**
  176. * 分享到朋友圈
  177. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  178. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  179. */
  180. onShareTimeline() {
  181. const app = this
  182. return {
  183. title: _this.templet.shareTitle,
  184. path: '/pages/category/index?' + app.$getShareUrlParams()
  185. }
  186. }
  187. }
  188. </script>
  189. <style>
  190. page {
  191. background: #fff;
  192. }
  193. </style>
  194. <style lang="scss" scoped>
  195. .cate-content {
  196. background: #fff;
  197. }
  198. .cate-wrapper {
  199. padding: 0 20rpx 20rpx 20rpx;
  200. box-sizing: border-box;
  201. }
  202. /* 一级分类(大图) 10 */
  203. .cate_style__10 .cate-item {
  204. margin-bottom: 18rpx;
  205. }
  206. .cate_style__10 .cate-item:last-child {
  207. margin-bottom: 0;
  208. }
  209. .cate_style__10 .cate-item image {
  210. display: block;
  211. width: 100%;
  212. height: auto;
  213. }
  214. /* 一级分类(小图) 11 */
  215. .cate_style__11 .cate-item {
  216. float: left;
  217. padding: 25rpx;
  218. width: 33.3333%;
  219. text-align: center;
  220. box-sizing: border-box;
  221. }
  222. .cate_style__11 .cate-item image {
  223. /* height: 208.656rpx; */
  224. width: 100%;
  225. display: block;
  226. }
  227. .cate_style__11 .cate-item image {
  228. display: block;
  229. width: 100%;
  230. margin-bottom: 10rpx;
  231. }
  232. .cate_style__11 .cate-item text {
  233. display: block;
  234. color: #555;
  235. }
  236. /* 二级分类 20 */
  237. .cate-content {
  238. width: 100%;
  239. }
  240. .cate-left {
  241. flex-direction: column;
  242. display: flex;
  243. width: 200rpx;
  244. color: #444;
  245. height: 100%;
  246. background: #f8f8f8;
  247. }
  248. .cate-right {
  249. display: flex;
  250. flex-direction: column;
  251. width: 550rpx;
  252. height: 100%;
  253. overflow: hidden;
  254. }
  255. .cate-right-cont {
  256. width: 100%;
  257. display: flex;
  258. flex-flow: row wrap;
  259. align-content: flex-start;
  260. padding-top: 26rpx;
  261. }
  262. .type-nav {
  263. position: relative;
  264. height: 90rpx;
  265. line-height: 90rpx;
  266. text-align: center;
  267. z-index: 10;
  268. display: block;
  269. font-size: 26rpx;
  270. }
  271. .type-nav.selected {
  272. background: #fff;
  273. color: #fa2209;
  274. border-right: none;
  275. font-size: 28rpx;
  276. }
  277. .cate-cont-box {
  278. margin-bottom: 30rpx;
  279. padding-bottom: 10rpx;
  280. background: #fff;
  281. overflow: hidden;
  282. }
  283. .cate-cont-box .cate-img {
  284. padding: 13rpx 10rpx 4rpx 10rpx;
  285. }
  286. .cate-cont-box .cate-img image {
  287. width: 100%;
  288. height: 150rpx;
  289. border-radius: 10rpx;
  290. }
  291. .cate-cont-box text {
  292. text-align: center;
  293. display: block;
  294. font-size: 26rpx;
  295. padding-bottom: 14rpx;
  296. color: #444;
  297. padding: 0 15rpx 30rpx 15rpx;
  298. }
  299. .cate-two-box {
  300. width: 100%;
  301. padding: 0 10px;
  302. }
  303. </style>