commodity.vue 11 KB

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