search.vue 10 KB

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