list.vue 10 KB

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