search.vue 11 KB

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