list.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <template>
  2. <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ native: true }" @down="downCallback" :up="upOption" @up="upCallback">
  3. <!-- 页面头部 -->
  4. <view class="header">
  5. <search class="search" :tips="options.search ? options.search : '搜索商品'" @event="handleSearch" />
  6. <!-- 切换列表显示方式 -->
  7. <view class="show-view" @click="handleShowView">
  8. <text class="iconfont icon-view-tile" v-if="showView"></text>
  9. <text class="iconfont icon-view-list" v-else></text>
  10. </view>
  11. </view>
  12. <!-- 排序标签 -->
  13. <view class="store-sort">
  14. <view class="sort-item" :class="{ active: sortType === 'all' }" @click="handleSortType('all')">
  15. <text>综合</text>
  16. </view>
  17. <view class="sort-item" :class="{ active: sortType === 'sales' }" @click="handleSortType('sales')">
  18. <text>销量</text>
  19. </view>
  20. <view class="sort-item sort-item-price" :class="{ active: sortType === 'price' }" @click="handleSortType('price')">
  21. <text>价格</text>
  22. <view class="price-arrow">
  23. <view class="icon up" :class="{ active: sortType === 'price' && !sortPrice }">
  24. <text class="iconfont icon-arrow-up"></text>
  25. </view>
  26. <view class="icon down" :class="{ active: sortType === 'price' && sortPrice }">
  27. <text class="iconfont icon-arrow-down"></text>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. <!-- 商品列表 -->
  33. <view class="goods-list clearfix" :class="['column-' + (showView ? '1' : '2')]">
  34. <view class="goods-item" v-for="(item, index) in list.data" :key="index" @click="onTargetDetail(item.goods_id)">
  35. <!-- 单列显示 -->
  36. <view v-if="showView" class="dis-flex">
  37. <!-- 商品图片 -->
  38. <view class="goods-item_left">
  39. <image class="image" :src="item.goods_image"></image>
  40. </view>
  41. <view class="goods-item_right">
  42. <!-- 商品名称 -->
  43. <view class="goods-name">
  44. <text class="twoline-hide">{{ item.goods_name }}</text>
  45. </view>
  46. <view class="goods-item_desc">
  47. <!-- 商品卖点 -->
  48. <view class="desc-selling_point dis-flex">
  49. <text class="oneline-hide">{{ item.selling_point }}</text>
  50. </view>
  51. <!-- 商品销量 -->
  52. <view class="desc-goods_sales dis-flex">
  53. <text>已售{{ item.goods_sales }}件</text>
  54. </view>
  55. <!-- 商品价格 -->
  56. <view class="desc_footer">
  57. <text class="price_x">¥{{ item.goods_price_min }}</text>
  58. <text class="price_y col-9" v-if="item.line_price_min > 0">¥{{ item.line_price_min }}</text>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <!-- 多列显示 -->
  64. <view v-else class="">
  65. <!-- 商品图片 -->
  66. <view class="goods-image">
  67. <image class="image" mode="aspectFill" :src="item.goods_image"></image>
  68. </view>
  69. <view class="detail">
  70. <!-- 商品标题 -->
  71. <view class="goods-name">
  72. <text class="twoline-hide">{{ item.goods_name }}</text>
  73. </view>
  74. <!-- 商品价格 -->
  75. <view class="detail-price oneline-hide">
  76. <text class="goods-price f-30 col-m">¥{{ item.goods_price_min }}</text>
  77. <text v-if="item.line_price_min > 0" class="line-price col-9 f-24">¥{{ item.line_price_min }}</text>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </view>
  83. </mescroll-body>
  84. </template>
  85. <script>
  86. import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
  87. import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
  88. import * as GoodsApi from '@/api/goods'
  89. import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
  90. import Search from '@/components/search'
  91. const pageSize = 15
  92. const showViewKey = 'GoodsList-ShowView';
  93. export default {
  94. components: {
  95. MescrollBody,
  96. Search
  97. },
  98. mixins: [MescrollMixin],
  99. data() {
  100. return {
  101. showView: false, // 列表显示方式 (true列表、false平铺)
  102. sortType: 'all', // 排序类型
  103. sortPrice: false, // 价格排序 (true高到低 false低到高)
  104. options: {}, // 当前页面参数
  105. list: getEmptyPaginateObj(), // 商品列表数据
  106. // 上拉加载配置
  107. upOption: {
  108. // 首次自动执行
  109. auto: true,
  110. // 每页数据的数量; 默认10
  111. page: { size: pageSize },
  112. // 数量要大于4条才显示无更多数据
  113. noMoreSize: 4,
  114. }
  115. }
  116. },
  117. /**
  118. * 生命周期函数--监听页面加载
  119. */
  120. onLoad(options) {
  121. // 记录options
  122. this.options = options
  123. // 设置默认列表显示方式
  124. this.setShowView()
  125. },
  126. methods: {
  127. /**
  128. * 上拉加载的回调 (页面初始化时也会执行一次)
  129. * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
  130. * @param {Object} page
  131. */
  132. upCallback(page) {
  133. const app = this
  134. // 设置列表数据
  135. app.getGoodsList(page.num)
  136. .then(list => {
  137. const curPageLen = list.data.length
  138. const totalSize = list.data.total
  139. app.mescroll.endBySize(curPageLen, totalSize)
  140. })
  141. .catch(() => app.mescroll.endErr())
  142. },
  143. // 设置默认列表显示方式
  144. setShowView() {
  145. this.showView = uni.getStorageSync(showViewKey) || false
  146. },
  147. /**
  148. * 获取商品列表
  149. * @param {number} pageNo 页码
  150. */
  151. getGoodsList(pageNo = 1) {
  152. const app = this
  153. console.log(app.options)
  154. const param = {
  155. sortType: app.sortType,
  156. sortPrice: Number(app.sortPrice),
  157. categoryId: app.options.categoryId || 0,
  158. goodsName: app.options.search || '',
  159. page: pageNo
  160. }
  161. return new Promise((resolve, reject) => {
  162. GoodsApi.list(param)
  163. .then(result => {
  164. // 合并新数据
  165. const newList = result.data.list
  166. app.list.data = getMoreListData(newList, app.list, pageNo)
  167. resolve(newList)
  168. })
  169. .catch(reject)
  170. })
  171. },
  172. // 切换排序方式
  173. handleSortType(newSortType) {
  174. const app = this
  175. const newSortPrice = newSortType === 'price' ? !app.sortPrice : true
  176. app.sortType = newSortType
  177. app.sortPrice = newSortPrice
  178. // 刷新列表数据
  179. app.list = getEmptyPaginateObj()
  180. app.mescroll.resetUpScroll()
  181. },
  182. // 切换列表显示方式
  183. handleShowView() {
  184. const app = this
  185. app.showView = !app.showView
  186. uni.setStorageSync(showViewKey, app.showView)
  187. },
  188. // 跳转商品详情页
  189. onTargetDetail(goodsId) {
  190. this.$navTo('pages/goods/detail', { goodsId })
  191. },
  192. /**
  193. * 商品搜索
  194. */
  195. handleSearch() {
  196. const searchPageUrl = 'pages/search/index'
  197. // 判断来源页面
  198. let pages = getCurrentPages()
  199. if (pages.length > 1 &&
  200. pages[pages.length - 2].route === searchPageUrl) {
  201. uni.navigateBack()
  202. return
  203. }
  204. // 跳转到商品搜索页
  205. this.$navTo(searchPageUrl)
  206. }
  207. },
  208. /**
  209. * 设置分享内容
  210. */
  211. onShareAppMessage() {
  212. // 构建分享参数
  213. return {
  214. title: "全部分类",
  215. path: "/pages/category/index?" + this.$getShareUrlParams()
  216. }
  217. },
  218. /**
  219. * 分享到朋友圈
  220. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  221. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  222. */
  223. onShareTimeline() {
  224. // 构建分享参数
  225. return {
  226. title: "全部分类",
  227. path: "/pages/category/index?" + this.$getShareUrlParams()
  228. }
  229. }
  230. }
  231. </script>
  232. <style lang="scss" scoped>
  233. // 页面头部
  234. .header {
  235. display: flex;
  236. align-items: center;
  237. background-color: #fff;
  238. // 搜索框
  239. .search {
  240. flex: 1;
  241. }
  242. // 切换显示方式
  243. .show-view {
  244. width: 60rpx;
  245. height: 60rpx;
  246. line-height: 60rpx;
  247. font-size: 36rpx;
  248. color: #505050;
  249. }
  250. }
  251. // 排序组件
  252. .store-sort {
  253. position: sticky;
  254. top: var(--window-top);
  255. display: flex;
  256. padding: 20rpx 0;
  257. font-size: 28rpx;
  258. background: #fff;
  259. color: #000;
  260. z-index: 99;
  261. .sort-item {
  262. flex-basis: 33.3333%;
  263. display: flex;
  264. justify-content: center;
  265. align-items: center;
  266. height: 50rpx;
  267. &.active {
  268. color: #e49a3d;
  269. }
  270. }
  271. .sort-item-price .price-arrow {
  272. margin-left: 20rpx;
  273. font-size: 24rpx;
  274. color: #000;
  275. .icon {
  276. &.active {
  277. color: #e49a3d;
  278. }
  279. &.up {
  280. margin-bottom: -16rpx;
  281. }
  282. &.down {
  283. margin-top: -16rpx;
  284. }
  285. }
  286. }
  287. }
  288. // 商品列表
  289. .goods-list {
  290. padding: 4rpx;
  291. box-sizing: border-box;
  292. }
  293. // 单列显示
  294. .goods-list.column-1 {
  295. .goods-item {
  296. width: 100%;
  297. height: 280rpx;
  298. margin-bottom: 12rpx;
  299. padding: 20rpx;
  300. box-sizing: border-box;
  301. background: #fff;
  302. line-height: 1.6;
  303. &:last-child {
  304. margin-bottom: 0;
  305. }
  306. }
  307. .goods-item_left {
  308. display: flex;
  309. width: 300rpx;
  310. background: #fff;
  311. align-items: center;
  312. .image {
  313. display: block;
  314. width: 240rpx;
  315. height: 240rpx;
  316. }
  317. }
  318. .goods-item_right {
  319. position: relative;
  320. // width: 450rpx;
  321. flex: 1;
  322. .goods-name {
  323. margin-top: 10rpx;
  324. min-height: 68rpx;
  325. line-height: 1.3;
  326. white-space: normal;
  327. color: #484848;
  328. font-size: 26rpx;
  329. }
  330. }
  331. .goods-item_desc {
  332. margin-top: 8rpx;
  333. }
  334. .desc-selling_point {
  335. width: 400rpx;
  336. font-size: 24rpx;
  337. color: #e49a3d;
  338. }
  339. .desc-goods_sales {
  340. color: #999;
  341. font-size: 24rpx;
  342. }
  343. .desc_footer {
  344. font-size: 24rpx;
  345. .price_x {
  346. margin-right: 16rpx;
  347. color: #f03c3c;
  348. font-size: 30rpx;
  349. }
  350. .price_y {
  351. text-decoration: line-through;
  352. }
  353. }
  354. }
  355. // 平铺显示
  356. .goods-list.column-2 {
  357. .goods-item {
  358. width: 50%;
  359. }
  360. }
  361. .goods-item {
  362. float: left;
  363. box-sizing: border-box;
  364. padding: 6rpx;
  365. .goods-image {
  366. position: relative;
  367. width: 100%;
  368. height: 0;
  369. padding-bottom: 100%;
  370. overflow: hidden;
  371. background: #fff;
  372. &:after {
  373. content: '';
  374. display: block;
  375. margin-top: 100%;
  376. }
  377. .image {
  378. position: absolute;
  379. width: 100%;
  380. height: 100%;
  381. top: 0;
  382. left: 0;
  383. -o-object-fit: cover;
  384. object-fit: cover;
  385. }
  386. }
  387. .detail {
  388. padding: 8rpx;
  389. background: #fff;
  390. .goods-name {
  391. min-height: 68rpx;
  392. line-height: 32rpx;
  393. white-space: normal;
  394. color: #484848;
  395. font-size: 26rpx;
  396. }
  397. .detail-price {
  398. .goods-price {
  399. margin-right: 8rpx;
  400. }
  401. .line-price {
  402. text-decoration: line-through;
  403. }
  404. }
  405. }
  406. }
  407. </style>