commodity.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <view class="container" :style="appThemeStyle">
  3. <!-- 一级分类 -->
  4. <scroll-view class="cate-left" :scroll-y="true" :style="{ height: `${scrollHeight}px` }" @touchmove.stop.prevent>
  5. <text class="type-nav" :class="{ selected: curIndex == -1 }" @click="handleSelectNav(-1)">全部</text>
  6. <text class="type-nav" :class="{ selected: curIndex == index }" v-for="(item, index) in list" :key="index" @click="handleSelectNav(index)">{{ item.name }}</text>
  7. </scroll-view>
  8. <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ use: false }" :up="upOption" :bottombar="false" @up="upCallback">
  9. <view class="cate-content">
  10. <!-- 子分类 -->
  11. <view v-if="subCateList.length" class="sub-cate-list clearfix" :class="{ 'display-fold': !showSubCate }" @touchmove.stop.prevent>
  12. <view class="nav-icon" @click="handleShowSubCate">
  13. <text class="iconfont" :class="[ showSubCate ? 'icon-arrow-up' : 'icon-arrow-down' ]"></text>
  14. </view>
  15. <view class="sub-cate-item" :class="{ selected: curIndex2 == -1 }" @click="handleSelectSubCate(-1)">
  16. <text>全部</text>
  17. </view>
  18. <view class="sub-cate-item" v-for="(item, index) in subCateList" :key="index" :class="{ selected: curIndex2 == index }" @click="handleSelectSubCate(index)">
  19. <text>{{ item.name }}</text>
  20. </view>
  21. </view>
  22. <!-- 商品列表 -->
  23. <view class="goods-list">
  24. <view class="goods-item--container" v-for="(item, index) in goodsList.data" :key="index">
  25. <view class="goods-item" @click="onTargetGoods(item.goods_id)">
  26. <!-- 商品图片 -->
  27. <view class="goods-item-left">
  28. <image class="image" :src="item.goods_image"></image>
  29. </view>
  30. <view class="goods-item-right">
  31. <!-- 商品标题 -->
  32. <view class="goods-name">
  33. <text class="twoline-hide">{{ item.goods_name }}</text>
  34. </view>
  35. <!-- 商品信息 -->
  36. <view class="goods-item-desc">
  37. <view class="desc-footer">
  38. <view class="item-prices oneline-hide">
  39. <text class="price-x">¥{{ item.goods_price_min }}</text>
  40. <text v-if="item.line_price_min > 0" class="price-y">¥{{ item.line_price_min }}</text>
  41. </view>
  42. <add-cart-btn v-if="setting.showAddCart" :btnStyle="setting.cartStyle" @handleAddCart="handleAddCart(item)" />
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. <!-- 遮罩层 -->
  50. <view class="mask" v-show="showSubCate" @touchmove.stop.prevent @click="handleShowSubCate"></view>
  51. <!-- 加入购物车组件 -->
  52. <AddCartPopup ref="AddCartPopup" @addCart="onUpdateCartTabBadge" />
  53. </view>
  54. </mescroll-body>
  55. </view>
  56. </template>
  57. <script>
  58. import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins'
  59. import { getEmptyPaginateObj, getMoreListData, setCartTabBadge } from '@/core/app'
  60. import { PageCategoryStyleEnum } from '@/common/enum/store/page/category'
  61. import AddCartBtn from '@/components/add-cart-btn'
  62. import AddCartPopup from '@/components/add-cart-popup'
  63. import { rpx2px } from '@/utils/util'
  64. import * as GoodsApi from '@/api/goods'
  65. const pageSize = 15
  66. export default {
  67. components: {
  68. AddCartBtn,
  69. AddCartPopup
  70. },
  71. mixins: [MescrollMixin],
  72. props: {
  73. // 分类列表
  74. list: {
  75. type: Array,
  76. default: []
  77. },
  78. // 分类设置
  79. setting: {
  80. type: Object,
  81. default: () => {}
  82. },
  83. },
  84. data() {
  85. return {
  86. // 枚举类
  87. PageCategoryStyleEnum,
  88. // 列表高度
  89. scrollHeight: 0,
  90. // 一级分类:指针
  91. curIndex: -1,
  92. // 是否显示子分类
  93. showSubCate: false,
  94. // 二级分类:指针
  95. curIndex2: -1,
  96. // 商品列表
  97. goodsList: getEmptyPaginateObj(),
  98. // 上拉加载配置
  99. upOption: {
  100. // 首次自动执行
  101. auto: true,
  102. // 每页数据的数量; 默认10
  103. page: { size: pageSize },
  104. // 数量要大于3条才显示无更多数据
  105. noMoreSize: 3,
  106. // 返回顶部
  107. toTop: { right: 30, bottom: 48, zIndex: 9 }
  108. }
  109. }
  110. },
  111. created() {
  112. // 设置分类列表高度
  113. this.setListHeight()
  114. },
  115. computed: {
  116. // 二级分类列表
  117. subCateList() {
  118. if (this.list[this.curIndex] && this.list[this.curIndex].children) {
  119. return this.list[this.curIndex].children
  120. }
  121. return []
  122. }
  123. },
  124. methods: {
  125. /**
  126. * 上拉加载的回调 (页面初始化时也会执行一次)
  127. * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
  128. * @param {Object} page
  129. */
  130. upCallback(page) {
  131. const app = this
  132. // 设置列表数据
  133. app.getGoodsList(page.num)
  134. .then(list => {
  135. const curPageLen = list.data.length
  136. const totalSize = list.data.total
  137. app.mescroll.endBySize(curPageLen, totalSize)
  138. })
  139. .catch(() => app.mescroll.endErr())
  140. },
  141. /**
  142. * 获取商品列表
  143. * @param {Number} pageNo 页码
  144. */
  145. getGoodsList(pageNo = 1) {
  146. const app = this
  147. const categoryId = app.getCategoryId()
  148. return new Promise((resolve, reject) => {
  149. GoodsApi.list({ categoryId, page: pageNo }, { load: false })
  150. .then(result => {
  151. const newList = result.data.list
  152. app.goodsList.data = getMoreListData(newList, app.goodsList, pageNo)
  153. app.goodsList.last_page = newList.last_page
  154. resolve(newList)
  155. })
  156. .catch(reject)
  157. })
  158. },
  159. // 获取当前选择的分类ID
  160. getCategoryId() {
  161. const app = this
  162. if (app.curIndex2 > -1) {
  163. return app.subCateList[app.curIndex2].category_id
  164. }
  165. return app.curIndex > -1 ? app.list[app.curIndex].category_id : 0
  166. },
  167. // 设置列表内容的高度
  168. setListHeight() {
  169. const { windowHeight } = uni.getSystemInfoSync()
  170. this.scrollHeight = windowHeight - rpx2px(88)
  171. },
  172. // 一级分类:选中分类
  173. handleSelectNav(index) {
  174. this.curIndex = index
  175. this.onRefreshList()
  176. this.showSubCate = false
  177. this.curIndex2 = -1
  178. },
  179. // 二级分类:选中分类
  180. handleSelectSubCate(index) {
  181. this.curIndex2 = index
  182. this.showSubCate = false
  183. this.onRefreshList()
  184. },
  185. // 刷新列表数据
  186. onRefreshList() {
  187. this.goodsList = getEmptyPaginateObj()
  188. setTimeout(() => this.mescroll.resetUpScroll(), 120)
  189. },
  190. // 跳转至商品列表页
  191. onTargetGoods(goodsId) {
  192. this.$navTo('pages/goods/detail', { goodsId })
  193. },
  194. // 点击加入购物车
  195. handleAddCart(item) {
  196. this.$refs.AddCartPopup.handle(item)
  197. },
  198. // 更新购物车角标
  199. onUpdateCartTabBadge() {
  200. console.log('onUpdateCartTabBadge')
  201. setCartTabBadge()
  202. },
  203. // 切换子分类显示状态
  204. handleShowSubCate() {
  205. this.showSubCate = !this.showSubCate
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss" scoped>
  211. .container {
  212. padding-left: 173rpx;
  213. }
  214. // 分类内容
  215. .cate-content {
  216. z-index: 1;
  217. background: #fff;
  218. padding-top: 88rpx;
  219. min-height: 300rpx;
  220. }
  221. // 一级分类+二级分类 20
  222. .cate-left {
  223. position: fixed;
  224. top: calc(88rpx + var(--window-top));
  225. left: var(--window-left);
  226. bottom: var(--window-bottom);
  227. width: 173rpx;
  228. height: 100%;
  229. background: #f8f8f8;
  230. color: #444;
  231. }
  232. // 左侧一级分类
  233. .type-nav {
  234. position: relative;
  235. height: 90rpx;
  236. z-index: 10;
  237. display: block;
  238. font-size: 26rpx;
  239. display: flex;
  240. justify-content: center;
  241. align-items: center;
  242. &.selected {
  243. background: #fff;
  244. border-right: none;
  245. font-size: 28rpx;
  246. color: $main-bg
  247. }
  248. }
  249. // 商品列表
  250. .goods-list {
  251. background: #fff;
  252. position: relative;
  253. }
  254. .goods-item {
  255. padding: 28rpx 22rpx;
  256. display: flex;
  257. }
  258. .goods-item-left {
  259. position: relative;
  260. background: #fff;
  261. margin-right: 20rpx;
  262. .image {
  263. display: block;
  264. width: 180rpx;
  265. height: 180rpx;
  266. }
  267. }
  268. .goods-item-right {
  269. position: relative;
  270. flex: 1;
  271. .goods-name {
  272. display: block;
  273. width: 100%;
  274. min-height: 68rpx;
  275. font-size: 28rpx;
  276. line-height: 1.3;
  277. color: #333;
  278. }
  279. }
  280. .goods-item-desc {
  281. margin-top: 20rpx;
  282. .people {
  283. margin-right: 14rpx;
  284. }
  285. .desc-footer {
  286. width: 100%;
  287. display: flex;
  288. justify-content: space-between;
  289. align-items: center;
  290. position: absolute;
  291. right: 0rpx;
  292. bottom: 0rpx;
  293. min-height: 44rpx;
  294. .item-prices {
  295. padding-right: 6rpx;
  296. .price-x {
  297. margin-right: 14rpx;
  298. color: $main-bg;
  299. font-size: 28rpx;
  300. }
  301. .price-y {
  302. color: #999;
  303. text-decoration: line-through;
  304. font-size: 24rpx;
  305. }
  306. }
  307. }
  308. }
  309. // 子分类
  310. .sub-cate-list {
  311. background-color: #fff;
  312. width: 100%;
  313. z-index: 9;
  314. padding: 8rpx 40rpx 0 14rpx;
  315. overflow: hidden;
  316. position: sticky;
  317. top: calc(88rpx + var(--window-top));
  318. &.display-fold {
  319. height: 86rpx;
  320. }
  321. .nav-icon {
  322. position: absolute;
  323. right: 16rpx;
  324. top: 12rpx;
  325. font-size: 32rpx;
  326. }
  327. .sub-cate-item {
  328. float: left;
  329. background: #f8f8f8;
  330. padding: 10rpx 30rpx;
  331. margin-right: 22rpx;
  332. margin-bottom: 24rpx;
  333. font-size: 26rpx;
  334. border-radius: 14rpx;
  335. border: 1rpx solid #f8f8f8;
  336. &.selected {
  337. color: $main-bg;
  338. border: 1rpx solid $main-bg;
  339. }
  340. }
  341. }
  342. // 子分类遮罩层
  343. .mask {
  344. position: absolute;
  345. top: 0;
  346. bottom: 0;
  347. right: 0;
  348. left: 0;
  349. z-index: 8;
  350. background-color: rgba(0, 0, 0, 0.4);
  351. transition: all 0.3s ease-in-out 0s;
  352. }
  353. </style>