list.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <template>
  2. <view class="content">
  3. <!-- <view class="navbar" :style="{ position: headerPosition, top: headerTop }">
  4. <view class="nav-item" :class="{ current: filterIndex === 0 }" @click="tabClick(0)">综合排序</view>
  5. <view class="nav-item" :class="{ current: filterIndex === 1 }" @click="tabClick(1)">
  6. <text>销量优先</text>
  7. <view class="p-box">
  8. <text :class="{ active: numberOrder === 1 && filterIndex === 1 }" class="iconfont iconfold"></text>
  9. <text :class="{ active: numberOrder === 2 && filterIndex === 1 }" class="iconfont iconfold xia"></text>
  10. </view>
  11. </view>
  12. <view class="nav-item" :class="{ current: filterIndex === 2 }" @click="tabClick(2)">
  13. <text>价格</text>
  14. <view class="p-box">
  15. <text :class="{ active: priceOrder === 1 && filterIndex === 2 }" class="iconfont iconfold"></text>
  16. <text :class="{ active: priceOrder === 2 && filterIndex === 2 }" class="iconfont iconfold xia"></text>
  17. </view>
  18. </view>
  19. <text class="cate-item iconfont iconapps" @click="toggleCateMask('show')"></text>
  20. </view> -->
  21. <view class="priduct-main">
  22. <view v-for="(item, index) in goodsList" :key="index" class="priduct-item" @click="navToDetailPage(item)">
  23. <view class="priduct-main-image"><image :src="item.image" mode="aspectFill"></image></view>
  24. <text class="title clamp">{{ item.title }}</text>
  25. <view class="priduct-main-name clamp margin-c-20">{{ item.store_name }}</view>
  26. <view class="priduct-main-price">
  27. <view class="price" v-if="item.integral != 0">¥{{ item.price * 1 }} + {{ item.integral }}趣豆</view>
  28. <view class="price" v-else>¥{{ item.price }}</view>
  29. </view>
  30. </view>
  31. </view>
  32. <uni-load-more :status="loadingType"></uni-load-more>
  33. <!-- <view class="cate-mask" :class="cateMaskState === 0 ? 'none' : cateMaskState === 1 ? 'show' : ''" @click="toggleCateMask">
  34. <view class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
  35. <scroll-view scroll-y class="cate-list">
  36. <view v-for="item in cateList" :key="item.id">
  37. <view class="cate-item b-b two">{{ item.cate_name }}</view>
  38. <view v-for="tItem in item.children" :key="tItem.id" class="cate-item b-b" :class="{ active: tItem.id == cateId }" @click="changeCate(tItem)">
  39. {{ tItem.cate_name }}
  40. </view>
  41. </view>
  42. </scroll-view>
  43. </view>
  44. </view> -->
  45. </view>
  46. </template>
  47. <script>
  48. import { getProducts } from '@/api/product.js';
  49. import { getCategoryList } from '@/api/product.js';
  50. export default {
  51. data() {
  52. return {
  53. cateMaskState: 0, //分类面板展开状态
  54. headerPosition: 'fixed',
  55. headerTop: '0px',
  56. loadingType: 'more', //加载更多状态
  57. filterIndex: 0, //查询类型
  58. numberOrder: 0, //1 销量从低到高 2销量从高到低
  59. limit: 8, //每次加载数据条数
  60. page: 1, //当前页数
  61. cateId: 0, //已选三级分类id
  62. priceOrder: 0, //1 价格从低到高 2价格从高到低
  63. cateList: [], //分类列表
  64. goodsList: [] //商品列表
  65. };
  66. },
  67. onLoad(options) {
  68. // #ifdef H5
  69. // this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight + 'px';
  70. // #endif
  71. this.cateId = options.tid;
  72. // this.loadCateList(options.fid, options.sid);
  73. this.loadData();
  74. },
  75. onPageScroll(e) {
  76. //兼容iOS端下拉时顶部漂移
  77. if (e.scrollTop >= 0) {
  78. this.headerPosition = 'fixed';
  79. } else {
  80. this.headerPosition = 'absolute';
  81. }
  82. },
  83. //下拉刷新
  84. onPullDownRefresh() {
  85. this.loadData('refresh');
  86. },
  87. //监听页面是否滚动到底部加载更多
  88. onReachBottom() {
  89. this.loadData();
  90. },
  91. methods: {
  92. //加载分类
  93. async loadCateList(fid, sid) {
  94. let obj = this;
  95. getCategoryList({}).then(function(e) {
  96. console.log(e);
  97. e.data.forEach(function(e) {
  98. if (e.id == fid) {
  99. obj.cateList = e.children;
  100. return;
  101. }
  102. });
  103. console.log(obj.cateList);
  104. });
  105. },
  106. //加载商品 ,带下拉刷新和上滑加载
  107. async loadData(type = 'add', loading) {
  108. let obj = this;
  109. let data = {
  110. page: obj.page,
  111. limit: obj.limit,
  112. sid: obj.cateId //分类id
  113. };
  114. //没有更多直接返回
  115. if (type === 'add') {
  116. if (obj.loadingType === 'nomore') {
  117. return;
  118. }
  119. obj.loadingType = 'loading';
  120. } else {
  121. obj.loadingType = 'more';
  122. }
  123. if (type === 'refresh') {
  124. // 清空数组
  125. obj.goodsList = [];
  126. obj.page = 1;
  127. }
  128. if (this.filterIndex == 1) {
  129. console.log(obj.salesOrder);
  130. data.salesOrder = obj.numberOrder == 1 ? 'asc' : 'desc';
  131. }
  132. if (this.filterIndex == 2) {
  133. console.log(obj.priceOrder);
  134. data.priceOrder = obj.priceOrder == 1 ? 'asc' : 'desc';
  135. }
  136. getProducts(data).then(function(e) {
  137. console.log(e.data);
  138. obj.goodsList = obj.goodsList.concat(e.data);
  139. //判断是否还有下一页,有是more 没有是nomore
  140. if (obj.limit == e.data.length) {
  141. obj.page++;
  142. obj.loadingType = 'more';
  143. } else {
  144. obj.loadingType = 'nomore';
  145. }
  146. if (type === 'refresh') {
  147. if (loading == 1) {
  148. uni.hideLoading();
  149. } else {
  150. uni.stopPullDownRefresh();
  151. }
  152. }
  153. });
  154. },
  155. //筛选点击
  156. tabClick(index) {
  157. // 防止重复点击综合排序
  158. if (this.filterIndex === 0 && this.filterIndex === index) {
  159. return;
  160. }
  161. this.filterIndex = index;
  162. // 判断是否为销量优先
  163. if (index === 1) {
  164. this.numberOrder = this.numberOrder === 1 ? 2 : 1;
  165. }
  166. // 判断是否为价格优先
  167. if (index === 2) {
  168. this.priceOrder = this.priceOrder === 1 ? 2 : 1;
  169. }
  170. // 初始化页数
  171. this.page = 1;
  172. // 初始化数组
  173. uni.pageScrollTo({
  174. duration: 300,
  175. scrollTop: 0
  176. });
  177. this.loadData('refresh', 1);
  178. uni.showLoading({
  179. title: '正在加载'
  180. });
  181. },
  182. //显示分类面板
  183. toggleCateMask(type) {
  184. let timer = type === 'show' ? 10 : 300;
  185. let state = type === 'show' ? 1 : 0;
  186. this.cateMaskState = 2;
  187. setTimeout(() => {
  188. this.cateMaskState = state;
  189. }, timer);
  190. },
  191. //分类点击
  192. changeCate(item) {
  193. this.cateId = item.id;
  194. // 显示右侧分类
  195. this.toggleCateMask();
  196. // 滚轮返回顶部
  197. uni.pageScrollTo({
  198. duration: 300,
  199. scrollTop: 0
  200. });
  201. // 初始化查询页数
  202. this.page = 1;
  203. // 重新加载数据
  204. this.loadData('refresh', 1);
  205. uni.showLoading({
  206. title: '正在加载'
  207. });
  208. },
  209. //详情
  210. navToDetailPage(item) {
  211. let id = item.id;
  212. uni.navigateTo({
  213. url: `/pages/product/product?id=${id}`
  214. });
  215. },
  216. stopPrevent() {}
  217. }
  218. };
  219. </script>
  220. <style lang="scss">
  221. page,
  222. .content {
  223. background: $page-color-base;
  224. }
  225. .content {
  226. // padding-top: 96rpx;
  227. }
  228. .navbar {
  229. position: fixed;
  230. left: 0;
  231. top: var(--window-top);
  232. display: flex;
  233. width: 100%;
  234. height: 80rpx;
  235. background: #fff;
  236. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.06);
  237. z-index: 10;
  238. .nav-item {
  239. flex: 1;
  240. display: flex;
  241. justify-content: center;
  242. align-items: center;
  243. height: 100%;
  244. font-size: 30rpx;
  245. color: $font-color-dark;
  246. position: relative;
  247. &.current {
  248. color: $base-color;
  249. &:after {
  250. content: '';
  251. position: absolute;
  252. left: 50%;
  253. bottom: 0;
  254. transform: translateX(-50%);
  255. width: 120rpx;
  256. height: 0;
  257. border-bottom: 4rpx solid $base-color;
  258. }
  259. }
  260. }
  261. .p-box {
  262. display: flex;
  263. flex-direction: column;
  264. .iconfont {
  265. display: flex;
  266. align-items: center;
  267. justify-content: center;
  268. width: 30rpx;
  269. height: 14rpx;
  270. line-height: 1;
  271. margin-left: 4rpx;
  272. font-size: 26rpx;
  273. color: #888;
  274. &.active {
  275. color: $base-color;
  276. }
  277. }
  278. .xia {
  279. transform: scaleY(-1);
  280. }
  281. }
  282. .cate-item {
  283. display: flex;
  284. justify-content: center;
  285. align-items: center;
  286. height: 100%;
  287. width: 80rpx;
  288. position: relative;
  289. font-size: 44rpx;
  290. &:after {
  291. content: '';
  292. position: absolute;
  293. left: 0;
  294. top: 50%;
  295. transform: translateY(-50%);
  296. border-left: 1px solid #ddd;
  297. width: 0;
  298. height: 36rpx;
  299. }
  300. }
  301. }
  302. /* 分类 */
  303. .cate-mask {
  304. position: fixed;
  305. left: 0;
  306. top: var(--window-top);
  307. bottom: 0;
  308. width: 100%;
  309. background: rgba(0, 0, 0, 0);
  310. z-index: 95;
  311. transition: 0.3s;
  312. .cate-content {
  313. width: 630rpx;
  314. height: 100%;
  315. background: #fff;
  316. float: right;
  317. transform: translateX(100%);
  318. transition: 0.3s;
  319. }
  320. &.none {
  321. display: none;
  322. }
  323. &.show {
  324. background: rgba(0, 0, 0, 0.4);
  325. .cate-content {
  326. transform: translateX(0);
  327. }
  328. }
  329. }
  330. .cate-list {
  331. display: flex;
  332. flex-direction: column;
  333. height: 100%;
  334. .cate-item {
  335. display: flex;
  336. align-items: center;
  337. height: 90rpx;
  338. padding-left: 30rpx;
  339. font-size: 28rpx;
  340. color: #555;
  341. position: relative;
  342. }
  343. .two {
  344. height: 64rpx;
  345. color: #303133;
  346. font-size: 30rpx;
  347. background: #f8f8f8;
  348. }
  349. .active {
  350. color: $base-color;
  351. }
  352. }
  353. /* 商品列表 */
  354. .priduct-main {
  355. margin-top: 38rpx;
  356. width: 100%;
  357. display: flex;
  358. flex-wrap: wrap;
  359. padding: 0 32rpx;
  360. .priduct-item {
  361. width: 48%;
  362. background-color: #ffffff;
  363. border-radius: 12rpx;
  364. margin-bottom: 24rpx;
  365. &:nth-child(2n + 1) {
  366. margin-right: 24rpx;
  367. }
  368. .priduct-main-image {
  369. width: 100%;
  370. height: 330rpx;
  371. // background: red;
  372. border-radius: 3px;
  373. overflow: hidden;
  374. image {
  375. width: 100%;
  376. height: 100%;
  377. opacity: 1;
  378. border-radius: 12rpx 12rpx 0 0;
  379. }
  380. }
  381. .priduct-main-name {
  382. font-size: $font-base;
  383. color: $font-color-dark;
  384. font-weight: bold;
  385. line-height: 80rpx;
  386. }
  387. .priduct-main-price {
  388. display: flex;
  389. justify-content: space-between;
  390. padding: 0 16rpx 12rpx;
  391. .price {
  392. font-size: 36rpx;
  393. font-weight: bold;
  394. color: #fd3b39;
  395. }
  396. .cart-icon {
  397. image {
  398. width: 44rpx;
  399. height: 44rpx;
  400. }
  401. }
  402. }
  403. }
  404. }
  405. </style>