list.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <template>
  2. <view class="container" :style="appThemeStyle">
  3. <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ native: true }" @down="downCallback" :up="upOption"
  4. @up="upCallback">
  5. <!-- 页面头部 -->
  6. <view class="header">
  7. <view class="search">
  8. <search :tips="options.search ? options.search : '搜索商品'" @event="handleSearch" />
  9. </view>
  10. <!-- 切换列表显示方式 -->
  11. <view class="show-view" @click="handleShowView">
  12. <text class="iconfont icon-view-tile" v-if="showView"></text>
  13. <text class="iconfont icon-view-list" v-else></text>
  14. </view>
  15. </view>
  16. <!-- 排序标签 -->
  17. <view class="store-sort">
  18. <view class="sort-item" :class="{ active: sortType === 'all' }" @click="handleSortType('all')">
  19. <text>综合</text>
  20. </view>
  21. <view class="sort-item" :class="{ active: sortType === 'sales' }" @click="handleSortType('sales')">
  22. <text>销量</text>
  23. </view>
  24. <view class="sort-item sort-item-price" :class="{ active: sortType === 'price' }" @click="handleSortType('price')">
  25. <text>价格</text>
  26. <view class="price-arrow">
  27. <view class="icon up" :class="{ active: sortType === 'price' && !sortPrice }">
  28. <text class="iconfont icon-arrow-up"></text>
  29. </view>
  30. <view class="icon down" :class="{ active: sortType === 'price' && sortPrice }">
  31. <text class="iconfont icon-arrow-down"></text>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. <!-- 商品列表 -->
  37. <view class="goods-list clearfix" :class="['column-' + (showView ? '1' : '2')]">
  38. <view class="goods-item" v-for="(item, index) in list.data" :key="index" @click="onTargetDetail(item.goods_id)">
  39. <!-- 单列显示 -->
  40. <view v-if="showView" class="dis-flex">
  41. <!-- 商品图片 -->
  42. <view class="goods-item-left">
  43. <image class="image" :src="item.goods_image"></image>
  44. </view>
  45. <view class="goods-item-right">
  46. <!-- 商品名称 -->
  47. <view class="goods-name">
  48. <text class="twoline-hide">{{ item.goods_name }}</text>
  49. </view>
  50. <view class="goods-item-desc">
  51. <!-- 商品卖点 -->
  52. <view class="desc-selling-point dis-flex">
  53. <text class="oneline-hide">{{ item.selling_point }}</text>
  54. </view>
  55. <!-- 商品销量 -->
  56. <view class="desc-goods-sales dis-flex">
  57. <text>已售{{ item.goods_sales }}件</text>
  58. </view>
  59. <!-- 商品价格 -->
  60. <view class="desc-footer">
  61. <text class="price-x">¥{{ item.goods_price_min }}</text>
  62. <text class="price-y col-9" v-if="item.line_price_min > 0">¥{{ item.line_price_min }}</text>
  63. </view>
  64. </view>
  65. </view>
  66. </view>
  67. <!-- 多列显示 -->
  68. <view v-else class="">
  69. <!-- 商品图片 -->
  70. <view class="goods-image">
  71. <image class="image" mode="aspectFill" :src="item.goods_image"></image>
  72. </view>
  73. <view class="detail">
  74. <!-- 商品名称 -->
  75. <view class="goods-name">
  76. <text class="twoline-hide">{{ item.goods_name }}</text>
  77. </view>
  78. <!-- 商品价格 -->
  79. <view class="detail-price oneline-hide">
  80. <text class="goods-price f-30 col-m">¥{{ item.goods_price_min }}</text>
  81. <text v-if="item.line_price_min > 0" class="line-price col-9 f-24">¥{{ item.line_price_min }}</text>
  82. </view>
  83. </view>
  84. </view>
  85. </view>
  86. </view>
  87. </mescroll-body>
  88. </view>
  89. </template>
  90. <script>
  91. import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins'
  92. import * as GoodsApi from '@/api/goods'
  93. import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
  94. import Search from '@/components/search'
  95. const pageSize = 15
  96. const showViewKey = 'GoodsList-ShowView';
  97. export default {
  98. components: {
  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.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. console.log(app.options)
  157. const param = {
  158. sortType: app.sortType,
  159. sortPrice: Number(app.sortPrice),
  160. categoryId: app.options.categoryId || 0,
  161. goodsName: app.options.search || '',
  162. page: pageNo
  163. }
  164. return new Promise((resolve, reject) => {
  165. GoodsApi.list(param)
  166. .then(result => {
  167. // 合并新数据
  168. const newList = result.data.list
  169. app.list.data = getMoreListData(newList, app.list, pageNo)
  170. resolve(newList)
  171. })
  172. .catch(reject)
  173. })
  174. },
  175. // 切换排序方式
  176. handleSortType(newSortType) {
  177. const app = this
  178. const newSortPrice = newSortType === 'price' ? !app.sortPrice : true
  179. app.sortType = newSortType
  180. app.sortPrice = newSortPrice
  181. // 刷新列表数据
  182. app.list = getEmptyPaginateObj()
  183. app.mescroll.resetUpScroll()
  184. },
  185. // 切换列表显示方式
  186. handleShowView() {
  187. const app = this
  188. app.showView = !app.showView
  189. uni.setStorageSync(showViewKey, app.showView)
  190. },
  191. // 跳转商品详情页
  192. onTargetDetail(goodsId) {
  193. this.$navTo('pages/goods/detail', { goodsId })
  194. },
  195. /**
  196. * 商品搜索
  197. */
  198. handleSearch() {
  199. const searchPageUrl = 'pages/search/index'
  200. // 判断来源页面
  201. let pages = getCurrentPages()
  202. if (pages.length > 1 &&
  203. pages[pages.length - 2].route === searchPageUrl) {
  204. uni.navigateBack()
  205. return
  206. }
  207. // 跳转到商品搜索页
  208. this.$navTo(searchPageUrl)
  209. },
  210. },
  211. /**
  212. * 设置分享内容
  213. */
  214. onShareAppMessage() {
  215. // 构建分享参数
  216. const app = this
  217. return {
  218. title: "商品列表",
  219. path: "/pages/goods/list?" + this.$getShareUrlParams(app.options)
  220. }
  221. },
  222. /**
  223. * 分享到朋友圈
  224. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  225. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  226. */
  227. onShareTimeline() {
  228. // 构建分享参数
  229. const app = this
  230. return {
  231. title: "商品列表",
  232. path: "/pages/goods/list?" + this.$getShareUrlParams(app.options)
  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. font-size: 36rpx;
  252. color: #505050;
  253. display: flex;
  254. align-items: center;
  255. }
  256. }
  257. // 排序组件
  258. .store-sort {
  259. position: sticky;
  260. top: var(--window-top);
  261. display: flex;
  262. padding: 20rpx 0;
  263. font-size: 28rpx;
  264. background: #fff;
  265. color: #000;
  266. z-index: 99;
  267. .sort-item {
  268. flex-basis: 33.3333%;
  269. display: flex;
  270. justify-content: center;
  271. align-items: center;
  272. height: 50rpx;
  273. &.active {
  274. color: $main-bg;
  275. }
  276. }
  277. .sort-item-price .price-arrow {
  278. margin-left: 20rpx;
  279. font-size: 24rpx;
  280. color: #000;
  281. .icon {
  282. &.active {
  283. color: $main-bg;
  284. }
  285. &.up {
  286. margin-bottom: -16rpx;
  287. }
  288. &.down {
  289. margin-top: -16rpx;
  290. }
  291. }
  292. }
  293. }
  294. // 商品列表
  295. .goods-list {
  296. padding: 4rpx;
  297. box-sizing: border-box;
  298. }
  299. // 单列显示
  300. .goods-list.column-1 {
  301. .goods-item {
  302. width: 100%;
  303. height: 280rpx;
  304. margin-bottom: 12rpx;
  305. padding: 20rpx;
  306. box-sizing: border-box;
  307. background: #fff;
  308. line-height: 1.6;
  309. &:last-child {
  310. margin-bottom: 0;
  311. }
  312. }
  313. .goods-item-left {
  314. display: flex;
  315. width: 300rpx;
  316. background: #fff;
  317. align-items: center;
  318. .image {
  319. display: block;
  320. width: 240rpx;
  321. height: 240rpx;
  322. }
  323. }
  324. .goods-item-right {
  325. position: relative;
  326. flex: 1;
  327. .goods-name {
  328. margin-top: 10rpx;
  329. white-space: normal;
  330. color: #484848;
  331. font-size: 26rpx;
  332. // min-height: 68rpx;
  333. // line-height: 1.3;
  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: $main-bg;
  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. white-space: normal;
  397. color: #484848;
  398. font-size: 26rpx;
  399. margin-bottom: 4rpx;
  400. // min-height: 68rpx;
  401. // line-height: 1.3;
  402. }
  403. .detail-price {
  404. .goods-price {
  405. margin-right: 8rpx;
  406. }
  407. .line-price {
  408. text-decoration: line-through;
  409. }
  410. }
  411. }
  412. }
  413. </style>