listSearch.vue 9.1 KB

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