search.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <template>
  2. <view class="content">
  3. <!-- 兼容小程序搜索 -->
  4. <!-- #ifdef MP || APP-PLUS -->
  5. <view class="vheight"></view>
  6. <view class="input-box flex">
  7. <view class=" input-content flex">
  8. <view class="iconfont iconsearch"></view>
  9. <view class="input"><input type="text" v-model="keyword" placeholder="请输入搜索内容" /></view>
  10. </view>
  11. <view class="input-button flex" @click="navTo"><text>搜索</text></view>
  12. </view>
  13. <!-- #endif -->
  14. <swiper :current="tabCurrentIndex" class="swiper-box " duration="300">
  15. <swiper-item class="search-hot">
  16. <view class="title"><text>热门搜索</text></view>
  17. <view class="hot-list">
  18. <view @click="clickHotText(ls)" class="list-item" :key="ind" v-for="(ls, ind) in list">
  19. <text>{{ ls }}</text>
  20. </view>
  21. </view>
  22. </swiper-item>
  23. <swiper-item class="search-hot position-relative">
  24. <view class="navbar">
  25. <view class="nav-item" @click="defaultSearch()">默认</view>
  26. <view class="nav-item" :class="{ current: searchType === 1 }" @click="sortTab(1)">
  27. <text>销量优先</text>
  28. <view class="p-box">
  29. <text :class="{ active: searchType === 1 && numberOrder === 1 }"
  30. class="iconfont iconfold"></text>
  31. <text :class="{ active: searchType === 1 && numberOrder === 2 }"
  32. class="iconfont iconfold xia"></text>
  33. </view>
  34. </view>
  35. <view class="nav-item" :class="{ current: searchType === 2 }" @click="sortTab(2)">
  36. <text>价格</text>
  37. <view class="p-box">
  38. <text :class="{ active: searchType === 2 && priceOrder === 1 }"
  39. class="iconfont iconfold"></text>
  40. <text :class="{ active: searchType === 2 && priceOrder === 2 }"
  41. class="iconfont iconfold xia"></text>
  42. </view>
  43. </view>
  44. <view class="nav-item" :class="{ current: newOrder == 1 }" @click="newGoodsTab()">新品</view>
  45. </view>
  46. <scroll-view scroll-y class="cate-list" @scrolltolower='getProducts'>
  47. <view class="guess-section">
  48. <view v-for="(item, index) in goodsList" :key="index" class="guess-item"
  49. @click="navToDetailPage(item)">
  50. <view class="image-wrapper">
  51. <image :src="item.image" mode="aspectFill"></image>
  52. </view>
  53. <text class="title clamp margin-c-20">{{ item.store_name }}</text>
  54. <view class="cmy-hr"></view>
  55. <view class="price margin-c-20 flex">
  56. <view>
  57. <text class="font-size-sm ">¥</text>
  58. {{ item.price }}
  59. </view>
  60. <view class="font-size-sm">
  61. <text class="font-color-gray">{{ item.sales }}人购买</text>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. <uni-load-more :status="loadingType"></uni-load-more>
  67. </scroll-view>
  68. </swiper-item>
  69. </swiper>
  70. </view>
  71. </template>
  72. <script>
  73. import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
  74. import {
  75. searchKeyword,
  76. getProducts
  77. } from '@/api/product.js';
  78. export default {
  79. components: {
  80. uniLoadMore
  81. },
  82. data() {
  83. return {
  84. arrlist: [], //热门关键词
  85. keyword: '', //关键字
  86. list: [], //搜索内容
  87. tabCurrentIndex: 0, //切换
  88. goodsList: [],
  89. limit: 6, //每次加载数据条数
  90. page: 1, //当前页数
  91. loadingType: 'more', //加载更多状态
  92. numberOrder: 1, //1 销量从低到高 2销量从高到低
  93. priceOrder: 1, //1 价格从低到高 2价格从高到低
  94. newOrder: 0, //0 不是新品 1是新品
  95. searchType: 0 //0为默认查询 1为销量 2 为价格
  96. };
  97. },
  98. // #ifndef MP
  99. //点击导航栏 buttons 时触发
  100. onNavigationBarButtonTap(e) {
  101. const index = e.index;
  102. if (index === 0) {
  103. this.navTo();
  104. }
  105. },
  106. // 点击键盘搜索事件
  107. onNavigationBarSearchInputConfirmed(e) {
  108. this.navTo();
  109. },
  110. // 搜索栏内容变化事件
  111. onNavigationBarSearchInputChanged(e) {
  112. this.keyword = e.text;
  113. },
  114. // #endif
  115. onLoad() {
  116. this.loadData();
  117. },
  118. //下拉刷新
  119. onPullDownRefresh() {
  120. this.page = 1
  121. this.getProducts('refresh');
  122. },
  123. methods: {
  124. // 加载商品
  125. async getProducts(type, loading) {
  126. let obj = this;
  127. // 判断是否为加载数据
  128. if (type !== 'refresh') {
  129. //没有更多数据直接跳出方法
  130. if (obj.loadingType === 'nomore') {
  131. return;
  132. } else {
  133. // 设置当前为数据载入中
  134. obj.loadingType = 'loading';
  135. }
  136. } else {
  137. //当重新加载数据时更新状态为可继续添加数据
  138. obj.loadingType = 'more';
  139. }
  140. let data = {
  141. page: obj.page,
  142. limit: obj.limit,
  143. news: obj.newOrder,
  144. keyword: this.keyword,
  145. store_region: 1
  146. };
  147. // 判断是否为销售数量排序
  148. if (this.searchType === 1) {
  149. data.salesOrder = obj.numberOrder === 1 ? 'asc' : 'desc';
  150. }
  151. // 判断是否为金额排序
  152. if (this.searchType === 2) {
  153. data.priceOrder = obj.priceOrder === 1 ? 'asc' : 'desc';
  154. }
  155. getProducts(data).then(e => {
  156. if (type === 'refresh') {
  157. obj.goodsList = [];
  158. }
  159. obj.goodsList = obj.goodsList.concat(e.data);
  160. //判断是否还有下一页,有是more 没有是nomore
  161. if (obj.limit == e.data.length) {
  162. obj.page++;
  163. obj.loadingType = 'more';
  164. } else {
  165. obj.loadingType = 'nomore';
  166. }
  167. // 判断是否为刷新数据
  168. if (type === 'refresh') {
  169. // 判断是否为点击搜索按钮跳转加载
  170. if (loading == 1) {
  171. uni.hideLoading();
  172. } else {
  173. uni.stopPullDownRefresh();
  174. }
  175. }
  176. });
  177. },
  178. // 点击关键词触发事件
  179. clickHotText(e) {
  180. this.keyword = e;
  181. this.navTo();
  182. },
  183. // 加载搜索关键字
  184. async loadData() {
  185. searchKeyword({})
  186. .then(e => {
  187. this.list = e.data;
  188. })
  189. .catch(e => {
  190. console.log(e);
  191. });
  192. },
  193. // 点击触发搜索事件
  194. navTo() {
  195. this.tabCurrentIndex = 1;
  196. this.infoData()
  197. },
  198. // 默认搜索
  199. defaultSearch() {
  200. // 初始化查询
  201. this.numberOrder = '';
  202. this.priceOrder = '';
  203. this.newOrder = 0;
  204. this.searchType = 0;
  205. this.infoData();
  206. },
  207. // 是否为新品
  208. newGoodsTab() {
  209. this.newOrder = this.newOrder === 1 ? 0 : 1;
  210. this.infoData();
  211. },
  212. // 排序
  213. sortTab(nub) {
  214. this.searchType = nub;
  215. if (this.searchType === 1) {
  216. this.numberOrder = this.numberOrder === 1 ? 2 : 1;
  217. }
  218. if (this.searchType === 2) {
  219. this.priceOrder = this.priceOrder === 1 ? 2 : 1;
  220. }
  221. this.infoData();
  222. },
  223. // 查询切换后初始化
  224. infoData() {
  225. // 初始化页数
  226. this.page = 1;
  227. // 初始化数组
  228. uni.pageScrollTo({
  229. duration: 300,
  230. scrollTop: 0
  231. });
  232. // 加载数据
  233. this.getProducts('refresh', 1);
  234. uni.showLoading({
  235. title: '正在加载'
  236. });
  237. },
  238. navToDetailPage(item) {
  239. //测试数据没有写id,用title代替
  240. let id = item.id;
  241. uni.navigateTo({
  242. url: '/pages/product/product?id=' + id
  243. });
  244. }
  245. }
  246. };
  247. </script>
  248. <style lang="scss">
  249. page,
  250. .content {
  251. height: 100%;
  252. background-color: $page-color-base;
  253. }
  254. /* #ifdef MP || APP-PLUS */
  255. .vheight {
  256. height: var(--status-bar-height);
  257. background-color: #FFFFFF;
  258. }
  259. .input-box {
  260. padding: 25rpx;
  261. background-color: #ffffff;
  262. height: 44px;
  263. .iconsearch {
  264. font-size: 50rpx;
  265. }
  266. .input-content {
  267. border-radius: 99rpx;
  268. flex-grow: 1;
  269. padding: 10rpx 30rpx;
  270. background-color: rgba(231, 231, 231, 0.7);
  271. .input {
  272. flex-grow: 1;
  273. input {
  274. font-size: $font-lg;
  275. }
  276. }
  277. }
  278. .input-button {
  279. padding-left: 20rpx;
  280. font-size: $font-lg;
  281. height: 100%;
  282. }
  283. }
  284. /* #endif */
  285. .swiper-box {
  286. /* #ifndef MP */
  287. height: 100%;
  288. /* #endif */
  289. /* #ifdef MP */
  290. height: calc(100% - 44px);
  291. /* #endif */
  292. .search-hot {
  293. padding: 25rpx;
  294. .title {
  295. font-size: $font-lg;
  296. color: $font-color-light;
  297. }
  298. .hot-list {
  299. display: flex;
  300. flex-wrap: wrap;
  301. margin-top: 30rpx;
  302. .list-item {
  303. padding: 10rpx 20rpx;
  304. border: 1px solid $border-color-dark;
  305. color: $font-color-dark;
  306. font-size: $font-base;
  307. margin-right: 20rpx;
  308. margin-bottom: 20rpx;
  309. }
  310. }
  311. }
  312. }
  313. // 订单
  314. %icon {
  315. margin-right: 10rpx;
  316. display: inline-block;
  317. padding: 2rpx 10rpx;
  318. border: 1rpx solid $color-yellow;
  319. color: $color-yellow;
  320. line-height: 1;
  321. font-size: $font-base;
  322. border-radius: 10rpx;
  323. }
  324. .guess-section {
  325. display: flex;
  326. flex-wrap: wrap;
  327. .guess-item {
  328. overflow: hidden;
  329. display: flex;
  330. flex-direction: column;
  331. width: 48%;
  332. margin-bottom: 4%;
  333. border-radius: $border-radius-sm;
  334. background-color: white;
  335. box-shadow: $box-shadow;
  336. &:nth-child(2n + 1) {
  337. margin-right: 4%;
  338. }
  339. }
  340. .image-wrapper {
  341. width: 100%;
  342. height: 330rpx;
  343. border-radius: 3px;
  344. overflow: hidden;
  345. image {
  346. width: 100%;
  347. height: 100%;
  348. opacity: 1;
  349. }
  350. }
  351. .title {
  352. font-size: $font-base;
  353. color: $font-color-dark;
  354. font-weight: bold;
  355. line-height: 80rpx;
  356. }
  357. .price {
  358. font-size: $font-lg;
  359. color: $font-color-base;
  360. font-weight: bold;
  361. line-height: 1;
  362. line-height: 80rpx;
  363. }
  364. .icon {
  365. @extend %icon;
  366. }
  367. .detail {
  368. line-height: 1;
  369. }
  370. .tip {
  371. color: white;
  372. background-color: $color-yellow;
  373. line-height: 1.5;
  374. font-size: $font-sm;
  375. padding-left: 20rpx;
  376. }
  377. }
  378. .navbar {
  379. position: absolute;
  380. top: 0;
  381. left: 0;
  382. display: flex;
  383. width: 100%;
  384. height: 40px;
  385. background: #fff;
  386. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.06);
  387. z-index: 10;
  388. .nav-item {
  389. flex: 1;
  390. display: flex;
  391. justify-content: center;
  392. align-items: center;
  393. height: 100%;
  394. font-size: 30rpx;
  395. color: $font-color-dark;
  396. position: relative;
  397. &.current {
  398. color: $base-color;
  399. &:after {
  400. content: '';
  401. position: absolute;
  402. left: 50%;
  403. bottom: 0;
  404. transform: translateX(-50%);
  405. width: 120rpx;
  406. height: 0;
  407. border-bottom: 4rpx solid $base-color;
  408. }
  409. }
  410. }
  411. .p-box {
  412. display: flex;
  413. flex-direction: column;
  414. .iconfont {
  415. display: flex;
  416. align-items: center;
  417. justify-content: center;
  418. width: 30rpx;
  419. height: 14rpx;
  420. line-height: 1;
  421. margin-left: 4rpx;
  422. font-size: 26rpx;
  423. color: #888;
  424. &.active {
  425. color: $base-color;
  426. }
  427. }
  428. .xia {
  429. transform: scaleY(-1);
  430. }
  431. }
  432. .cate-item {
  433. display: flex;
  434. justify-content: center;
  435. align-items: center;
  436. height: 100%;
  437. width: 80rpx;
  438. position: relative;
  439. font-size: 44rpx;
  440. &:after {
  441. content: '';
  442. position: absolute;
  443. left: 0;
  444. top: 50%;
  445. transform: translateY(-50%);
  446. border-left: 1px solid #ddd;
  447. width: 0;
  448. height: 36rpx;
  449. }
  450. }
  451. }
  452. .cate-list {
  453. height: 100%;
  454. padding-top: 40px;
  455. }
  456. </style>