commodity.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <template>
  2. <view class="container" :style="appThemeStyle">
  3. <!-- 一级分类 -->
  4. <scroll-view class="cate-left" :scroll-y="true" @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. curIndex: -1,
  90. // 是否显示子分类
  91. showSubCate: false,
  92. // 二级分类:指针
  93. curIndex2: -1,
  94. // 商品列表
  95. goodsList: getEmptyPaginateObj(),
  96. // 上拉加载配置
  97. upOption: {
  98. // 首次自动执行
  99. auto: true,
  100. // 每页数据的数量; 默认10
  101. page: { size: pageSize },
  102. // 数量要大于3条才显示无更多数据
  103. noMoreSize: 3,
  104. // 返回顶部
  105. toTop: { right: 30, bottom: 48, zIndex: 9 }
  106. }
  107. }
  108. },
  109. computed: {
  110. // 二级分类列表
  111. subCateList() {
  112. if (this.list[this.curIndex] && this.list[this.curIndex].children) {
  113. return this.list[this.curIndex].children
  114. }
  115. return []
  116. }
  117. },
  118. methods: {
  119. /**
  120. * 上拉加载的回调 (页面初始化时也会执行一次)
  121. * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
  122. * @param {Object} page
  123. */
  124. upCallback(page) {
  125. const app = this
  126. // 设置列表数据
  127. app.getGoodsList(page.num)
  128. .then(list => {
  129. const curPageLen = list.data.length
  130. const totalSize = list.data.total
  131. app.mescroll.endBySize(curPageLen, totalSize)
  132. })
  133. .catch(() => app.mescroll.endErr())
  134. },
  135. /**
  136. * 获取商品列表
  137. * @param {Number} pageNo 页码
  138. */
  139. getGoodsList(pageNo = 1) {
  140. const app = this
  141. const categoryId = app.getCategoryId()
  142. return new Promise((resolve, reject) => {
  143. GoodsApi.list({ categoryId, page: pageNo }, { load: false })
  144. .then(result => {
  145. const newList = result.data.list
  146. app.goodsList.data = getMoreListData(newList, app.goodsList, pageNo)
  147. app.goodsList.last_page = newList.last_page
  148. resolve(newList)
  149. })
  150. .catch(reject)
  151. })
  152. },
  153. // 获取当前选择的分类ID
  154. getCategoryId() {
  155. const app = this
  156. if (app.curIndex2 > -1) {
  157. return app.subCateList[app.curIndex2].category_id
  158. }
  159. return app.curIndex > -1 ? app.list[app.curIndex].category_id : 0
  160. },
  161. // 一级分类:选中分类
  162. handleSelectNav(index) {
  163. this.curIndex = index
  164. this.onRefreshList()
  165. this.showSubCate = false
  166. this.curIndex2 = -1
  167. },
  168. // 二级分类:选中分类
  169. handleSelectSubCate(index) {
  170. this.curIndex2 = index
  171. this.showSubCate = false
  172. this.onRefreshList()
  173. },
  174. // 刷新列表数据
  175. onRefreshList() {
  176. this.goodsList = getEmptyPaginateObj()
  177. setTimeout(() => this.mescroll.resetUpScroll(), 120)
  178. },
  179. // 跳转至商品列表页
  180. onTargetGoods(goodsId) {
  181. this.$navTo('pages/goods/detail', { goodsId })
  182. },
  183. // 点击加入购物车
  184. handleAddCart(item) {
  185. this.$refs.AddCartPopup.handle(item)
  186. },
  187. // 更新购物车角标
  188. onUpdateCartTabBadge() {
  189. console.log('onUpdateCartTabBadge')
  190. setCartTabBadge()
  191. },
  192. // 切换子分类显示状态
  193. handleShowSubCate() {
  194. this.showSubCate = !this.showSubCate
  195. }
  196. }
  197. }
  198. </script>
  199. <style lang="scss" scoped>
  200. .container {
  201. padding-left: 173rpx;
  202. }
  203. // 分类内容
  204. .cate-content {
  205. z-index: 1;
  206. background: #fff;
  207. padding-top: 88rpx;
  208. min-height: 300rpx;
  209. }
  210. // 一级分类+二级分类 20
  211. .cate-left {
  212. position: fixed;
  213. top: calc(88rpx + var(--window-top));
  214. left: var(--window-left);
  215. bottom: var(--window-bottom);
  216. width: 173rpx;
  217. height: calc(100% - var(--window-bottom) - 88rpx);
  218. background: #f8f8f8;
  219. color: #444;
  220. }
  221. // 左侧一级分类
  222. .type-nav {
  223. position: relative;
  224. height: 90rpx;
  225. z-index: 10;
  226. display: block;
  227. font-size: 26rpx;
  228. display: flex;
  229. justify-content: center;
  230. align-items: center;
  231. &.selected {
  232. background: #fff;
  233. border-right: none;
  234. font-size: 28rpx;
  235. color: $main-bg
  236. }
  237. }
  238. // 商品列表
  239. .goods-list {
  240. background: #fff;
  241. position: relative;
  242. }
  243. .goods-item {
  244. padding: 28rpx 22rpx;
  245. display: flex;
  246. }
  247. .goods-item-left {
  248. position: relative;
  249. background: #fff;
  250. margin-right: 20rpx;
  251. .image {
  252. display: block;
  253. width: 180rpx;
  254. height: 180rpx;
  255. }
  256. }
  257. .goods-item-right {
  258. position: relative;
  259. flex: 1;
  260. .goods-name {
  261. display: block;
  262. width: 100%;
  263. min-height: 68rpx;
  264. font-size: 28rpx;
  265. line-height: 1.3;
  266. color: #333;
  267. }
  268. }
  269. .goods-item-desc {
  270. margin-top: 20rpx;
  271. .people {
  272. margin-right: 14rpx;
  273. }
  274. .desc-footer {
  275. width: 100%;
  276. display: flex;
  277. justify-content: space-between;
  278. align-items: center;
  279. position: absolute;
  280. right: 0rpx;
  281. bottom: 0rpx;
  282. min-height: 44rpx;
  283. .item-prices {
  284. padding-right: 6rpx;
  285. .price-x {
  286. margin-right: 14rpx;
  287. color: $main-bg;
  288. font-size: 28rpx;
  289. }
  290. .price-y {
  291. color: #999;
  292. text-decoration: line-through;
  293. font-size: 24rpx;
  294. }
  295. }
  296. }
  297. }
  298. // 子分类
  299. .sub-cate-list {
  300. background-color: #fff;
  301. width: 100%;
  302. z-index: 9;
  303. padding: 8rpx 40rpx 0 14rpx;
  304. overflow: hidden;
  305. position: sticky;
  306. top: calc(88rpx + var(--window-top));
  307. &.display-fold {
  308. height: 86rpx;
  309. }
  310. .nav-icon {
  311. position: absolute;
  312. right: 16rpx;
  313. top: 12rpx;
  314. font-size: 32rpx;
  315. }
  316. .sub-cate-item {
  317. float: left;
  318. background: #f8f8f8;
  319. padding: 10rpx 30rpx;
  320. margin-right: 22rpx;
  321. margin-bottom: 24rpx;
  322. font-size: 26rpx;
  323. border-radius: 14rpx;
  324. border: 1rpx solid #f8f8f8;
  325. &.selected {
  326. color: $main-bg;
  327. border: 1rpx solid $main-bg;
  328. }
  329. }
  330. }
  331. // 子分类遮罩层
  332. .mask {
  333. position: absolute;
  334. top: 0;
  335. bottom: 0;
  336. right: 0;
  337. left: 0;
  338. z-index: 8;
  339. background-color: rgba(0, 0, 0, 0.4);
  340. transition: all 0.3s ease-in-out 0s;
  341. }
  342. </style>