listSearch.vue 10 KB

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