search.vue 9.9 KB

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