commodity.vue 9.9 KB

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