search.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <template>
  2. <view>
  3. <scroll-view class="container" style="height: 100%">
  4. <view class="search-header">
  5. <view class="input-box">
  6. <van-icon name="search" />
  7. <input
  8. name="input"
  9. class="keywrod"
  10. :focus="true"
  11. :value="keyword"
  12. confirm-type="search"
  13. @input="inputChange"
  14. @focus="inputFocus"
  15. @confirm="onKeywordConfirm"
  16. :placeholder="defaultKeyword.keyword"
  17. />
  18. <van-icon class="del" v-if="keyword" @tap.native="clearKeyword" name="cross" />
  19. </view>
  20. <view class="right" @tap="closeSearch">取消</view>
  21. </view>
  22. <view class="no-search" v-if="!searchStatus">
  23. <view class="search-keywords search-history" v-if="!keyword && historyKeyword.length">
  24. <view class="h">
  25. <text class="title">历史记录</text>
  26. <van-icon @tap.native="clearHistory" name="delete" class="icon" />
  27. </view>
  28. <view class="b">
  29. <view class="item" @tap="onKeywordTap" :data-keyword="item.keyword" hover-class="navigator-hover" v-for="(item, index) in historyKeyword" :key="index">
  30. {{ item.keyword }}
  31. </view>
  32. </view>
  33. </view>
  34. <view class="search-keywords search-hot" v-if="!keyword && hotKeyword.length">
  35. <view class="h">
  36. <text class="title">热门搜索</text>
  37. </view>
  38. <view class="b">
  39. <view
  40. :class="'item ' + (item.is_hot === 1 ? 'active' : '')"
  41. hover-class="navigator-hover"
  42. @tap="onKeywordTap"
  43. :data-keyword="item.keyword"
  44. v-for="(item, index) in hotKeyword"
  45. :key="index"
  46. >
  47. {{ item.keyword }}
  48. </view>
  49. </view>
  50. </view>
  51. <view class="shelper-list" v-if="keyword">
  52. <view class="item" hover-class="navigator-hover" @tap="onKeywordTap" :data-keyword="item" v-for="(item, index) in helpKeyword" :key="index">{{ item }}</view>
  53. </view>
  54. </view>
  55. <view class="search-result" v-if="searchStatus && goodsList.length">
  56. <view class="sort">
  57. <view class="sort-box">
  58. <view :class="'item ' + (currentSortType == 'default' ? 'active' : '')" @tap="openSortFilter" id="defaultSort">
  59. <text class="txt">综合</text>
  60. </view>
  61. <view :class="'item ' + (currentSortType == 'price' ? 'active' : '')" @tap="openSortFilter" id="priceSort">
  62. <text class="txt">价格</text>
  63. <van-icon name="arrow-up" v-if="currentSortType == 'price' && currentSortOrder == 'asc'" />
  64. <van-icon name="arrow-down" v-else-if="currentSortType == 'price' && currentSortOrder == 'desc'" />
  65. </view>
  66. <view :class="'item ' + (currentSortType == 'category' ? 'active' : '')" @tap="openSortFilter" id="categoryFilter">
  67. <text class="txt">分类</text>
  68. </view>
  69. </view>
  70. <view class="sort-box-category" v-if="categoryFilter">
  71. <view
  72. :class="'item ' + (item.checked ? 'active' : '')"
  73. :data-category-index="index"
  74. @tap="selectCategory"
  75. v-for="(item, index) in filterCategory"
  76. :key="index"
  77. >
  78. {{ item.name }}
  79. </view>
  80. </view>
  81. </view>
  82. <view class="cate-item">
  83. <view class="b">
  84. <navigator
  85. :class="'item ' + ((iindex + 1) % 2 == 0 ? 'item-b' : '')"
  86. :url="'/pages/goods/goods?id=' + iitem.id"
  87. v-for="(iitem, iindex) in goodsList"
  88. :key="iindex"
  89. >
  90. <image class="img" :src="iitem.picUrl" background-size="cover"></image>
  91. <text class="name">{{ iitem.name }}</text>
  92. <!-- <view class="price">
  93. <view class="retailPrice">¥{{iitem.retailPrice}}</view>
  94. <view class="memberPrice">¥{{iitem.memberPrice}}</view>
  95. <view class="member-price-tag">会员价</view>
  96. </view> -->
  97. <!-- <view class="price"><text>¥{{iitem.retailPrice}}</text>
  98. <text class="counterPrice sub-color">¥{{iitem.counterPrice}}</text>
  99. </view> -->
  100. </navigator>
  101. </view>
  102. </view>
  103. </view>
  104. <view class="search-result-empty" v-if="!goodsList.length && searchStatus">
  105. <text class="text">您寻找的商品还未上架</text>
  106. </view>
  107. </scroll-view>
  108. </view>
  109. </template>
  110. <script>
  111. var util = require('../../../utils/util.js');
  112. var api = require('../../../config/api.js');
  113. var app = getApp();
  114. export default {
  115. data() {
  116. return {
  117. keywrod: '',
  118. searchStatus: false,
  119. goodsList: [],
  120. helpKeyword: [],
  121. historyKeyword: [],
  122. categoryFilter: false,
  123. currentSort: 'name',
  124. currentSortType: 'default',
  125. currentSortOrder: 'desc',
  126. filterCategory: [],
  127. defaultKeyword: {
  128. keyword: ''
  129. },
  130. hotKeyword: [],
  131. page: 1,
  132. limit: 20,
  133. categoryId: 0,
  134. addressInfo: {},
  135. keyword: '',
  136. iindex: 0,
  137. iitem: {
  138. id: '',
  139. picUrl: '',
  140. name: ''
  141. }
  142. };
  143. },
  144. onLoad: function () {
  145. var addressInfo = uni.getStorageSync('addressInfo');
  146. if (addressInfo) {
  147. this.setData({
  148. addressInfo: addressInfo
  149. });
  150. }
  151. this.getSearchKeyword();
  152. },
  153. methods: {
  154. //事件处理函数
  155. closeSearch: function () {
  156. uni.navigateBack();
  157. },
  158. clearKeyword: function () {
  159. this.setData({
  160. keyword: '',
  161. searchStatus: false
  162. });
  163. },
  164. getSearchKeyword() {
  165. let that = this;
  166. util.request(api.SearchIndex).then(function (res) {
  167. if (res.errno === 0) {
  168. that.setData({
  169. historyKeyword: res.data.historyKeywordList,
  170. defaultKeyword: res.data.defaultKeyword,
  171. hotKeyword: res.data.hotKeywordList
  172. });
  173. }
  174. });
  175. },
  176. inputChange: function (e) {
  177. this.setData({
  178. keyword: e.detail.value,
  179. searchStatus: false
  180. });
  181. if (e.detail.value) {
  182. this.getHelpKeyword();
  183. }
  184. },
  185. getHelpKeyword: function () {
  186. let that = this;
  187. util.request(api.SearchHelper, {
  188. keyword: that.keyword
  189. }).then(function (res) {
  190. if (res.errno === 0) {
  191. that.setData({
  192. helpKeyword: res.data
  193. });
  194. }
  195. });
  196. },
  197. inputFocus: function () {
  198. this.setData({
  199. searchStatus: false,
  200. goodsList: []
  201. });
  202. if (this.keyword) {
  203. this.getHelpKeyword();
  204. }
  205. },
  206. clearHistory: function () {
  207. this.setData({
  208. historyKeyword: []
  209. });
  210. util.request(api.SearchClearHistory, {}, 'POST').then(function (res) {
  211. console.log('清除成功');
  212. });
  213. },
  214. getGoodsList: function () {
  215. let that = this;
  216. util.request(api.GoodsList, {
  217. keyword: that.keyword,
  218. page: that.page,
  219. limit: that.limit,
  220. sort: that.currentSort,
  221. order: that.currentSortOrder,
  222. categoryId: that.categoryId,
  223. city: that.addressInfo.city,
  224. shopId: that.addressInfo.shopId
  225. }).then(function (res) {
  226. if (res.errno === 0) {
  227. that.setData({
  228. searchStatus: true,
  229. categoryFilter: false,
  230. goodsList: res.data.list,
  231. filterCategory: res.data.filterCategoryList
  232. });
  233. }
  234. //重新获取关键词
  235. that.getSearchKeyword();
  236. });
  237. },
  238. onKeywordTap: function (event) {
  239. this.getSearchResult(event.target.dataset.keyword);
  240. },
  241. getSearchResult(keyword) {
  242. if (keyword === '') {
  243. keyword = this.defaultKeyword.keyword;
  244. }
  245. this.setData({
  246. keyword: keyword,
  247. page: 1,
  248. categoryId: 0,
  249. goodsList: []
  250. });
  251. this.getGoodsList();
  252. },
  253. openSortFilter: function (event) {
  254. let currentId = event.currentTarget.id;
  255. switch (currentId) {
  256. case 'categoryFilter':
  257. this.setData({
  258. categoryFilter: !this.categoryFilter,
  259. currentSortType: 'category',
  260. currentSort: 'add_time',
  261. currentSortOrder: 'desc'
  262. });
  263. break;
  264. case 'priceSort':
  265. let tmpSortOrder = 'asc';
  266. if (this.currentSortOrder == 'asc') {
  267. tmpSortOrder = 'desc';
  268. }
  269. this.setData({
  270. currentSortType: 'price',
  271. currentSort: 'retail_price',
  272. currentSortOrder: tmpSortOrder,
  273. categoryFilter: false
  274. });
  275. this.getGoodsList();
  276. break;
  277. default:
  278. //综合排序
  279. this.setData({
  280. currentSortType: 'default',
  281. currentSort: 'name',
  282. currentSortOrder: 'desc',
  283. categoryFilter: false,
  284. categoryId: 0
  285. });
  286. this.getGoodsList();
  287. }
  288. },
  289. selectCategory: function (event) {
  290. let currentIndex = event.target.dataset.categoryIndex;
  291. let filterCategory = this.filterCategory;
  292. let currentCategory = null;
  293. for (let key in filterCategory) {
  294. if (key == currentIndex) {
  295. filterCategory[key].selected = true;
  296. currentCategory = filterCategory[key];
  297. } else {
  298. filterCategory[key].selected = false;
  299. }
  300. }
  301. this.setData({
  302. filterCategory: filterCategory,
  303. categoryFilter: false,
  304. categoryId: currentCategory.id,
  305. page: 1,
  306. goodsList: []
  307. });
  308. this.getGoodsList();
  309. },
  310. onKeywordConfirm(event) {
  311. this.getSearchResult(event.detail.value);
  312. }
  313. }
  314. };
  315. </script>
  316. <style>
  317. page {
  318. min-height: 100%;
  319. background-color: #f4f4f4;
  320. }
  321. .container {
  322. min-height: 100%;
  323. background-color: #f4f4f4;
  324. }
  325. .search-header {
  326. position: fixed;
  327. top: 0;
  328. width: 750rpx;
  329. height: 91rpx;
  330. display: flex;
  331. background: #fff;
  332. border-bottom: 1px solid rgba(0, 0, 0, 0.15);
  333. padding: 0 31.25rpx;
  334. font-size: 29rpx;
  335. color: #333;
  336. }
  337. .search-header .van-icon-search {
  338. line-height: 59rpx;
  339. }
  340. .search-header .input-box {
  341. position: relative;
  342. margin-top: 16rpx;
  343. float: left;
  344. width: 0;
  345. flex: 1;
  346. height: 59rpx;
  347. line-height: 59rpx;
  348. padding: 0 20rpx;
  349. background: #f4f4f4;
  350. }
  351. .search-header .icon {
  352. position: absolute;
  353. top: 14rpx;
  354. left: 20rpx;
  355. width: 31rpx;
  356. height: 31rpx;
  357. }
  358. .search-header .del {
  359. position: absolute;
  360. top: 3rpx;
  361. right: 10rpx;
  362. width: 53rpx;
  363. height: 53rpx;
  364. z-index: 10;
  365. }
  366. .search-header .keywrod {
  367. position: absolute;
  368. top: 0;
  369. left: 40rpx;
  370. width: 506rpx;
  371. height: 59rpx;
  372. padding-left: 30rpx;
  373. }
  374. .search-header .right {
  375. margin-top: 24rpx;
  376. margin-left: 31rpx;
  377. margin-right: 6rpx;
  378. width: 58rpx;
  379. height: 43rpx;
  380. line-height: 43rpx;
  381. float: right;
  382. }
  383. .no-search {
  384. height: auto;
  385. overflow: hidden;
  386. margin-top: 91rpx;
  387. }
  388. .search-keywords {
  389. background: #fff;
  390. width: 750rpx;
  391. height: auto;
  392. overflow: hidden;
  393. margin-bottom: 20rpx;
  394. }
  395. .search-keywords .h {
  396. padding: 0 31.25rpx;
  397. height: 93rpx;
  398. line-height: 93rpx;
  399. width: 100%;
  400. color: #999;
  401. font-size: 29rpx;
  402. }
  403. .search-keywords .title {
  404. display: block;
  405. width: 120rpx;
  406. float: left;
  407. }
  408. .search-keywords .icon {
  409. margin-top: 19rpx;
  410. float: right;
  411. display: block;
  412. margin-left: 511rpx;
  413. height: 55rpx;
  414. width: 55rpx;
  415. }
  416. .search-keywords .b {
  417. width: 750rpx;
  418. height: auto;
  419. overflow: hidden;
  420. padding-left: 31.25rpx;
  421. }
  422. .search-keywords .item {
  423. display: inline-block;
  424. width: auto;
  425. height: 48rpx;
  426. line-height: 48rpx;
  427. padding: 0 15rpx;
  428. border: 1px solid #999;
  429. margin: 0 31.25rpx 31.25rpx 0;
  430. font-size: 24rpx;
  431. color: #333;
  432. }
  433. .search-keywords .item.active {
  434. color: #b4282d;
  435. border: 1px solid #b4282d;
  436. }
  437. .shelper-list {
  438. width: 750rpx;
  439. height: auto;
  440. overflow: hidden;
  441. background: #fff;
  442. padding: 0 31.25rpx;
  443. }
  444. .shelper-list .item {
  445. height: 93rpx;
  446. width: 687.5rpx;
  447. line-height: 93rpx;
  448. font-size: 24rpx;
  449. color: #333;
  450. border-bottom: 1px solid #f4f4f4;
  451. }
  452. .sort {
  453. position: fixed;
  454. top: 91rpx;
  455. background: #fff;
  456. width: 100%;
  457. height: 78rpx;
  458. }
  459. .sort-box {
  460. background: #fff;
  461. width: 100%;
  462. height: 78rpx;
  463. overflow: hidden;
  464. padding: 0 30rpx;
  465. display: flex;
  466. border-bottom: 1px solid #d9d9d9;
  467. }
  468. .sort-box .item {
  469. height: 78rpx;
  470. line-height: 78rpx;
  471. text-align: center;
  472. flex: 1;
  473. color: #333;
  474. font-size: 30rpx;
  475. }
  476. .sort-box .item .txt {
  477. color: #333;
  478. }
  479. .sort-box .item.active .txt {
  480. color: #b4282d;
  481. }
  482. .sort-box .item .van-icon {
  483. margin-left: 6rpx;
  484. }
  485. .sort-box-category {
  486. background: #fff;
  487. width: 100%;
  488. height: auto;
  489. overflow: hidden;
  490. padding: 40rpx 40rpx 0 0;
  491. border-bottom: 1px solid #d9d9d9;
  492. }
  493. .sort-box-category .item {
  494. height: 54rpx;
  495. line-height: 54rpx;
  496. text-align: center;
  497. float: left;
  498. padding: 0 16rpx;
  499. margin: 0 0 40rpx 40rpx;
  500. border: 1px solid #666;
  501. color: #333;
  502. font-size: 24rpx;
  503. }
  504. .sort-box-category .item.active {
  505. color: #b4282d;
  506. border: 1px solid #b4282d;
  507. }
  508. .cate-item {
  509. margin-top: 175rpx;
  510. height: auto;
  511. overflow: hidden;
  512. }
  513. .cate-item .h {
  514. height: 145rpx;
  515. width: 750rpx;
  516. display: flex;
  517. flex-direction: column;
  518. align-items: center;
  519. justify-content: center;
  520. }
  521. .cate-item .h .name {
  522. display: block;
  523. height: 35rpx;
  524. margin-bottom: 18rpx;
  525. font-size: 30rpx;
  526. color: #333;
  527. }
  528. .cate-item .h .desc {
  529. display: block;
  530. height: 24rpx;
  531. font-size: 24rpx;
  532. color: #999;
  533. }
  534. .cate-item .b {
  535. width: 750rpx;
  536. padding: 0 6.25rpx;
  537. height: auto;
  538. overflow: hidden;
  539. }
  540. .cate-item .list-filter {
  541. height: 80rpx;
  542. width: 100%;
  543. background: #fff;
  544. margin-bottom: 6.25rpx;
  545. }
  546. .cate-item .b .item {
  547. float: left;
  548. background: #fff;
  549. width: 365rpx;
  550. margin-bottom: 6.25rpx;
  551. padding-bottom: 33.333rpx;
  552. height: auto;
  553. overflow: hidden;
  554. text-align: center;
  555. }
  556. .cate-item .b .item-b {
  557. margin-left: 6.25rpx;
  558. }
  559. .cate-item .item .img {
  560. width: 302rpx;
  561. height: 302rpx;
  562. }
  563. .cate-item .item .name {
  564. display: block;
  565. width: 365.625rpx;
  566. height: 35rpx;
  567. margin: 11.5rpx 0 22rpx 0;
  568. text-align: center;
  569. overflow: hidden;
  570. padding: 0 20rpx;
  571. font-size: 30rpx;
  572. color: #333;
  573. }
  574. .cate-item .item .price {
  575. height: 40rpx;
  576. line-height: 40rpx;
  577. text-align: center;
  578. font-size: 30rpx;
  579. color: #b4282d;
  580. }
  581. .search-result-empty {
  582. width: 100%;
  583. height: 100%;
  584. padding-top: 600rpx;
  585. }
  586. .search-result-empty .text {
  587. display: block;
  588. width: 100%;
  589. height: 40rpx;
  590. font-size: 28rpx;
  591. text-align: center;
  592. color: #999;
  593. }
  594. .cate-item .item .price {
  595. height: 40rpx;
  596. justify-content: center;
  597. display: flex;
  598. margin-top: 20rpx;
  599. align-items: flex-end;
  600. }
  601. .counterPrice {
  602. text-decoration: line-through;
  603. font-size: 24rpx;
  604. margin: 0 0 0 20rpx;
  605. }
  606. .retailPrice {
  607. font-size: 30rpx;
  608. height: 30rpx;
  609. line-height: 30rpx;
  610. }
  611. .memberPrice {
  612. font-size: 22rpx;
  613. color: #dd483e;
  614. margin: 0 10rpx 0 20rpx;
  615. }
  616. .member-price-tag {
  617. width: 88rpx;
  618. height: 26rpx;
  619. line-height: 26rpx;
  620. background: url('https://jzmall.lifejingzhi.com/file/jzmall/weixin/member/member-price-bg.png') no-repeat center center;
  621. background-size: 100% 100%;
  622. color: #5e3a11;
  623. font-size: 16rpx;
  624. padding: 0 8rpx 0 0;
  625. text-align: right;
  626. margin: 0 0 6rpx 0;
  627. }
  628. </style>