commodity.vue 10 KB

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