listSearch.vue 10 KB

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