search.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <template>
  2. <scroll-view class="container" style="height: 100%;">
  3. <view class="search-header">
  4. <view class="input-box">
  5. <image class="icon" src="http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/search2-2fb94833aa.png"></image>
  6. <input name="input" class="keywrod" :focus="true" v-model="keyword" confirm-type="search" @input="inputChange"
  7. @focus="inputFocus" @confirm="onKeywordConfirm" :placeholder="defaultKeyword.keyword" />
  8. <image class="del" v-if="keyword" @tap="clearKeyword" src="http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/clearIpt-f71b83e3c2.png"></image>
  9. </view>
  10. <view class="right" @tap="closeSearch">取消</view>
  11. </view>
  12. <view class="no-search" v-if="!searchStatus">
  13. <view class="serach-keywords search-history" v-if="!keyword && historyKeyword.length">
  14. <view class="h">
  15. <text class="title">历史记录</text>
  16. <image class="icon" @tap="clearHistory" src="http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/del1-93f0a4add4.png"></image>
  17. </view>
  18. <view class="b">
  19. <view class="item" @tap="onKeywordTap" :data-keyword="item" v-for="(item, index) in historyKeyword" :key="index"
  20. hover-class="navigator-hover">{{item}}</view>
  21. </view>
  22. </view>
  23. <view class="serach-keywords search-hot" v-if="!keyword">
  24. <view class="h">
  25. <text class="title">热门搜索</text>
  26. </view>
  27. <view class="b">
  28. <view :class="'item '+(item.is_hot === 1 ? 'active' : '')" hover-class="navigator-hover" @tap="onKeywordTap"
  29. :data-keyword="item.keyword" v-for="(item, index) in hotKeyword" :key="index">{{item.keyword}}</view>
  30. </view>
  31. </view>
  32. <view class="shelper-list" v-if="keyword">
  33. <view class="item" hover-class="navigator-hover" v-for="(item, index) in helpKeyword" :key="index" @tap="onKeywordTap"
  34. :data-keyword="item">{{item}}</view>
  35. </view>
  36. </view>
  37. <view class="search-result" v-if="searchStatus && goodsList.length">
  38. <view class="sort">
  39. <view class="sort-box">
  40. <view :class="'item ' + (currentSortType == 'default' ? 'active' : '')" @tap="openSortFilter" id="defaultSort">
  41. <text class="txt">综合</text>
  42. </view>
  43. <view :class="'item by-price ' + (currentSortType == 'price' ? 'active' : '') + (currentSortOrder == 'asc' ? ' asc' : ' desc')"
  44. @tap="openSortFilter" id="priceSort">
  45. <text class="txt">价格</text>
  46. </view>
  47. <view :class="'item '+(currentSortType == 'category' ? 'active' : '')" @tap="openSortFilter" id="categoryFilter">
  48. <text class="txt">分类</text>
  49. </view>
  50. </view>
  51. <view class="sort-box-category" v-if="categoryFilter">
  52. <view :class="'item '+(item.checked ? 'active' : '')" v-for="(item, index) in filterCategory" :key="item.id"
  53. :data-category-index="index" @tap="selectCategory">{{item.name}}</view>
  54. </view>
  55. </view>
  56. <view class="cate-item">
  57. <view class="b">
  58. <navigator :class="'item ' + ((iindex + 1) % 2 == 0 ? 'item-b' : '')" :url="'/pages/goods/goods?id='+iitem.id"
  59. v-for="(iitem, iindex) in goodsList" :key="iidex">
  60. <image class="img" :src="iitem.list_pic_url" background-size="cover"></image>
  61. <text class="name">{{iitem.name}}</text>
  62. <text class="price">¥{{iitem.retail_price}}</text>
  63. </navigator>
  64. </view>
  65. </view>
  66. </view>
  67. <view class="search-result-empty" v-if="!goodsList.length && searchStatus">
  68. <image class="icon" src="http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/noSearchResult-7572a94f32.png"></image>
  69. <text class="text">您寻找的商品还未上架</text>
  70. </view>
  71. </scroll-view>
  72. </template>
  73. <script>
  74. const util = require("@/utils/util.js");
  75. const api = require('@/utils/api.js');
  76. export default {
  77. data() {
  78. return {
  79. keywrod: '',
  80. searchStatus: false,
  81. goodsList: [],
  82. helpKeyword: [],
  83. historyKeyword: [],
  84. categoryFilter: false,
  85. currentSortType: 'default',
  86. currentSortOrder: '',
  87. filterCategory: [],
  88. defaultKeyword: {},
  89. hotKeyword: [],
  90. page: 1,
  91. size: 20,
  92. currentSortType: 'id',
  93. currentSortOrder: 'desc',
  94. categoryId: 0
  95. }
  96. },
  97. methods: {
  98. closeSearch: function() {
  99. uni.navigateBack()
  100. },
  101. clearKeyword: function() {
  102. this.keyword = ''
  103. this.searchStatus = false
  104. },
  105. getSearchKeyword() {
  106. let that = this;
  107. util.request(api.SearchIndex).then(function(res) {
  108. if (res.errno === 0) {
  109. that.historyKeyword = res.data.historyKeywordList
  110. that.defaultKeyword = res.data.defaultKeyword
  111. that.hotKeyword = res.data.hotKeywordList
  112. }
  113. });
  114. },
  115. inputChange: function(e) {
  116. this.keyword = e.detail.value
  117. this.searchStatus = false
  118. this.getHelpKeyword();
  119. },
  120. getHelpKeyword: function() {
  121. let that = this;
  122. util.request(api.SearchHelper, {
  123. keyword: that.keyword
  124. }).then(function(res) {
  125. if (res.errno === 0) {
  126. that.helpKeyword = res.data;
  127. }
  128. });
  129. },
  130. inputFocus: function() {
  131. this.searchStatus = false
  132. this.goodsList = []
  133. if (this.keyword) {
  134. this.getHelpKeyword();
  135. }
  136. },
  137. clearHistory: function() {
  138. this.historyKeyword = []
  139. util.request(api.SearchClearHistory)
  140. },
  141. getGoodsList: function() {
  142. let that = this;
  143. util.request(api.GoodsList, {
  144. keyword: that.keyword,
  145. page: that.page,
  146. size: that.size,
  147. sort: that.currentSortType,
  148. order: that.currentSortOrder,
  149. categoryId: that.categoryId
  150. }).then(function(res) {
  151. if (res.errno === 0) {
  152. that.searchStatus = true
  153. that.categoryFilter = false
  154. that.goodsList = res.data.data
  155. that.filterCategory = res.data.filterCategory
  156. that.page = res.data.currentPage
  157. that.size = res.data.numsPerPage
  158. }
  159. //重新获取关键词
  160. that.getSearchKeyword();
  161. });
  162. },
  163. onKeywordTap: function(event) {
  164. this.getSearchResult(event.target.dataset.keyword);
  165. },
  166. getSearchResult(keyword) {
  167. this.keyword = keyword
  168. this.page = 1
  169. this.categoryId = 0
  170. this.goodsList = []
  171. this.getGoodsList();
  172. },
  173. openSortFilter: function(event) {
  174. let that = this;
  175. let currentId = event.currentTarget.id;
  176. switch (currentId) {
  177. case 'categoryFilter':
  178. that.categoryFilter = !that.categoryFilter
  179. that.currentSortOrder = 'asc'
  180. break;
  181. case 'priceSort':
  182. let tmpSortOrder = 'asc';
  183. if (that.currentSortOrder == 'asc') {
  184. tmpSortOrder = 'desc';
  185. }
  186. that.currentSortType = 'price'
  187. that.currentSortOrder = tmpSortOrder
  188. that.categoryFilter = false
  189. that.getGoodsList();
  190. break;
  191. default:
  192. //综合排序
  193. that.currentSortType = 'default'
  194. that.currentSortOrder = 'desc'
  195. that.categoryFilter = false
  196. that.getGoodsList();
  197. }
  198. },
  199. selectCategory: function(event) {
  200. let currentIndex = event.target.dataset.categoryIndex;
  201. let filterCategory = this.filterCategory;
  202. let currentCategory = null;
  203. for (let key in filterCategory) {
  204. if (key == currentIndex) {
  205. filterCategory[key].selected = true;
  206. currentCategory = filterCategory[key];
  207. } else {
  208. filterCategory[key].selected = false;
  209. }
  210. }
  211. this.filterCategory = filterCategory
  212. this.categoryFilter = false
  213. this.categoryId = currentCategory.id
  214. this.page = 1
  215. this.goodsList = []
  216. this.getGoodsList();
  217. },
  218. onKeywordConfirm(event) {
  219. this.getSearchResult(event.detail.value);
  220. }
  221. },
  222. onLoad: function() {
  223. this.getSearchKeyword();
  224. }
  225. }
  226. </script>
  227. <style lang="scss">
  228. page {
  229. min-height: 100%;
  230. background-color: #f4f4f4;
  231. }
  232. .container {
  233. min-height: 100%;
  234. background-color: #f4f4f4;
  235. }
  236. .search-header {
  237. position: fixed;
  238. top: 0;
  239. width: 750rpx;
  240. height: 91rpx;
  241. display: flex;
  242. background: #fff;
  243. border-bottom: 1px solid rgba(0, 0, 0, .15);
  244. padding: 0 31.25rpx;
  245. font-size: 29rpx;
  246. color: #333;
  247. }
  248. .search-header .input-box {
  249. position: relative;
  250. margin-top: 16rpx;
  251. float: left;
  252. width: 0;
  253. flex: 1;
  254. height: 59rpx;
  255. line-height: 59rpx;
  256. padding: 0 20rpx;
  257. background: #f4f4f4;
  258. }
  259. .search-header .icon {
  260. position: absolute;
  261. top: 14rpx;
  262. left: 20rpx;
  263. width: 31rpx;
  264. height: 31rpx;
  265. }
  266. .search-header .del {
  267. position: absolute;
  268. top: 3rpx;
  269. right: 10rpx;
  270. width: 53rpx;
  271. height: 53rpx;
  272. z-index: 10;
  273. }
  274. .search-header .keywrod {
  275. position: absolute;
  276. top: 0;
  277. left: 40rpx;
  278. width: 506rpx;
  279. height: 59rpx;
  280. padding-left: 30rpx;
  281. }
  282. .search-header .right {
  283. margin-top: 24rpx;
  284. margin-left: 31rpx;
  285. margin-right: 6rpx;
  286. width: 58rpx;
  287. height: 43rpx;
  288. line-height: 43rpx;
  289. float: right;
  290. }
  291. .no-search {
  292. height: auto;
  293. overflow: hidden;
  294. margin-top: 91rpx;
  295. }
  296. .serach-keywords {
  297. background: #fff;
  298. width: 750rpx;
  299. height: auto;
  300. overflow: hidden;
  301. margin-bottom: 20rpx;
  302. }
  303. .serach-keywords .h {
  304. padding: 0 31.25rpx;
  305. height: 93rpx;
  306. line-height: 93rpx;
  307. width: 100%;
  308. color: #999;
  309. font-size: 29rpx;
  310. }
  311. .serach-keywords .title {
  312. display: block;
  313. width: 120rpx;
  314. float: left;
  315. }
  316. .serach-keywords .icon {
  317. margin-top: 19rpx;
  318. float: right;
  319. display: block;
  320. margin-left: 511rpx;
  321. height: 55rpx;
  322. width: 55rpx;
  323. }
  324. .serach-keywords .b {
  325. width: 750rpx;
  326. height: auto;
  327. overflow: hidden;
  328. padding-left: 31.25rpx;
  329. }
  330. .serach-keywords .item {
  331. display: inline-block;
  332. width: auto;
  333. height: 48rpx;
  334. line-height: 48rpx;
  335. padding: 0 15rpx;
  336. border: 1px solid #999;
  337. margin: 0 31.25rpx 31.25rpx 0;
  338. font-size: 24rpx;
  339. color: #333;
  340. }
  341. .serach-keywords .item.active {
  342. color: #b4282d;
  343. border: 1px solid #b4282d;
  344. }
  345. .shelper-list {
  346. width: 750rpx;
  347. height: auto;
  348. overflow: hidden;
  349. background: #fff;
  350. padding: 0 31.25rpx;
  351. }
  352. .shelper-list .item {
  353. height: 93rpx;
  354. width: 687.5rpx;
  355. line-height: 93rpx;
  356. font-size: 24rpx;
  357. color: #333;
  358. border-bottom: 1px solid #f4f4f4;
  359. }
  360. .sort {
  361. position: fixed;
  362. top: 91rpx;
  363. background: #fff;
  364. width: 100%;
  365. height: 78rpx;
  366. }
  367. .sort-box {
  368. background: #fff;
  369. width: 100%;
  370. height: 78rpx;
  371. overflow: hidden;
  372. padding: 0 30rpx;
  373. display: flex;
  374. border-bottom: 1px solid #d9d9d9;
  375. }
  376. .sort-box .item {
  377. height: 78rpx;
  378. line-height: 78rpx;
  379. text-align: center;
  380. flex: 1;
  381. color: #333;
  382. font-size: 30rpx;
  383. }
  384. .sort-box .item .txt {
  385. display: block;
  386. width: 100%;
  387. height: 100%;
  388. color: #333;
  389. }
  390. .sort-box .item.active .txt {
  391. color: #b4282d;
  392. }
  393. .sort-box .item.by-price {
  394. background: url(//yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/no-3127092a69.png) 155rpx center no-repeat;
  395. background-size: 15rpx 21rpx;
  396. }
  397. .sort-box .item.by-price.active.asc {
  398. background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/up-636b92c0a5.png) 155rpx center no-repeat;
  399. background-size: 15rpx 21rpx;
  400. }
  401. .sort-box .item.by-price.active.desc {
  402. background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/down-95e035f3e5.png) 155rpx center no-repeat;
  403. background-size: 15rpx 21rpx;
  404. }
  405. .sort-box-category {
  406. background: #fff;
  407. width: 100%;
  408. height: auto;
  409. overflow: hidden;
  410. padding: 40rpx 40rpx 0 0;
  411. border-bottom: 1px solid #d9d9d9;
  412. }
  413. .sort-box-category .item {
  414. height: 54rpx;
  415. line-height: 54rpx;
  416. text-align: center;
  417. float: left;
  418. padding: 0 16rpx;
  419. margin: 0 0 40rpx 40rpx;
  420. border: 1px solid #666;
  421. color: #333;
  422. font-size: 24rpx;
  423. }
  424. .sort-box-category .item.active {
  425. color: #b4282d;
  426. border: 1px solid #b4282d;
  427. }
  428. .cate-item {
  429. margin-top: 175rpx;
  430. height: auto;
  431. overflow: hidden;
  432. }
  433. .cate-item .h {
  434. height: 145rpx;
  435. width: 750rpx;
  436. display: flex;
  437. flex-direction: column;
  438. align-items: center;
  439. justify-content: center;
  440. }
  441. .cate-item .h .name {
  442. display: block;
  443. height: 35rpx;
  444. margin-bottom: 18rpx;
  445. font-size: 30rpx;
  446. color: #333;
  447. }
  448. .cate-item .h .desc {
  449. display: block;
  450. height: 24rpx;
  451. font-size: 24rpx;
  452. color: #999;
  453. }
  454. .cate-item .b {
  455. width: 750rpx;
  456. padding: 0 6.25rpx;
  457. height: auto;
  458. overflow: hidden;
  459. }
  460. .cate-item .list-filter {
  461. height: 80rpx;
  462. width: 100%;
  463. background: #fff;
  464. margin-bottom: 6.25rpx;
  465. }
  466. .cate-item .b .item {
  467. float: left;
  468. background: #fff;
  469. width: 365rpx;
  470. margin-bottom: 6.25rpx;
  471. padding-bottom: 33.333rpx;
  472. height: auto;
  473. overflow: hidden;
  474. text-align: center;
  475. }
  476. .cate-item .b .item-b {
  477. margin-left: 6.25rpx;
  478. }
  479. .cate-item .item .img {
  480. width: 302rpx;
  481. height: 302rpx;
  482. }
  483. .cate-item .item .name {
  484. display: block;
  485. width: 365.625rpx;
  486. height: 35rpx;
  487. margin: 11.5rpx 0 22rpx 0;
  488. text-align: center;
  489. overflow: hidden;
  490. padding: 0 20rpx;
  491. font-size: 30rpx;
  492. color: #333;
  493. }
  494. .cate-item .item .price {
  495. display: block;
  496. width: 365.625rpx;
  497. height: 30rpx;
  498. text-align: center;
  499. font-size: 30rpx;
  500. color: #b4282d;
  501. }
  502. .search-result-empty {
  503. width: 100%;
  504. height: 100%;
  505. padding-top: 300rpx;
  506. }
  507. .search-result-empty .icon {
  508. margin: 0 auto;
  509. display: block;
  510. width: 240rpx;
  511. height: 240rpx;
  512. }
  513. .search-result-empty .text {
  514. display: block;
  515. width: 100%;
  516. height: 40rpx;
  517. font-size: 28rpx;
  518. text-align: center;
  519. color: #999;
  520. }
  521. </style>