search.vue 9.8 KB

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