Bladeren bron

fix 修复搜索历史不能获取和清除bug

tumobi 7 jaren geleden
bovenliggende
commit
c44a211bd9
3 gewijzigde bestanden met toevoegingen van 39 en 34 verwijderingen
  1. 2 1
      config/api.js
  2. 34 30
      pages/search/search.js
  3. 3 3
      pages/search/search.wxml

+ 2 - 1
config/api.js

@@ -1,4 +1,4 @@
-var NewApiRootUrl = 'http://127.0.0.1:8360/api/';
+var NewApiRootUrl = 'http://192.168.31.233:8360/api/';
 
 module.exports = {
     IndexUrl: NewApiRootUrl + 'index/index', //首页数据接口
@@ -40,6 +40,7 @@ module.exports = {
     SearchIndex: NewApiRootUrl + 'search/index',  //搜索页面数据
     SearchResult: NewApiRootUrl + 'search/result',  //搜索数据
     SearchHelper: NewApiRootUrl + 'search/helper',  //搜索帮助
+    SearchClearHistory: NewApiRootUrl + 'search/clearhistory',  //搜索帮助
 
     AddressList: NewApiRootUrl + 'address/list',  //收货地址列表
     AddressDetail: NewApiRootUrl + 'address/detail',  //收货地址详情

+ 34 - 30
pages/search/search.js

@@ -22,10 +22,10 @@ Page({
     categoryId: 0
   },
   //事件处理函数
-  closeSearch: function() {
+  closeSearch: function () {
     wx.navigateBack()
   },
-  clearKeyword: function(){
+  clearKeyword: function () {
     this.setData({
       keyword: '',
       searchStatus: false
@@ -33,6 +33,10 @@ Page({
   },
   onLoad: function () {
 
+    this.getSearchKeyword();
+  },
+
+  getSearchKeyword() {
     let that = this;
     util.request(api.SearchIndex).then(function (res) {
       if (res.errno === 0) {
@@ -44,15 +48,16 @@ Page({
       }
     });
   },
-  inputChange: function(e){
-    
+
+  inputChange: function (e) {
+
     this.setData({
       keyword: e.detail.value,
       searchStatus: false
     });
     this.getHelpKeyword();
   },
-  getHelpKeyword: function(){
+  getHelpKeyword: function () {
     let that = this;
     util.request(api.SearchHelper, { keyword: that.data.keyword }).then(function (res) {
       if (res.errno === 0) {
@@ -62,27 +67,27 @@ Page({
       }
     });
   },
-  inputFocus: function(){
+  inputFocus: function () {
     this.setData({
       searchStatus: false,
       goodsList: []
     });
 
-    if(this.data.keyword){
+    if (this.data.keyword) {
       this.getHelpKeyword();
     }
   },
-  clearHistory: function(){
+  clearHistory: function () {
     this.setData({
       historyKeyword: []
     })
 
-    util.request(api.ApiRootUrl + 'api/goods/search/history/clear', {keyword: '一'}, 'POST')
-    .then(function(res){
+    util.request(api.SearchClearHistory, {}, 'POST')
+      .then(function (res) {
         console.log('清除成功');
-    });
+      });
   },
-  getGoodsList: function(){
+  getGoodsList: function () {
     let that = this;
     util.request(api.GoodsList, { keyword: that.data.keyword, page: that.data.page, size: that.data.size, sort: that.data.currentSortType, order: that.data.currentSortOrder, categoryId: that.data.categoryId }).then(function (res) {
       if (res.errno === 0) {
@@ -95,19 +100,25 @@ Page({
           size: res.data.numsPerPage
         });
       }
+
+      //重新获取关键词
+      that.getSearchKeyword();
     });
   },
-  search: function(event){
+  onKeywordTap: function (event) {
 
+    this.getSearchResult(event.target.dataset.keyword);
+
+  },
+  getSearchResult(keyword) {
     this.setData({
-      keyword: event.target.dataset.keyword,
+      keyword: keyword,
       page: 1,
       categoryId: 0,
-      goodsList:[]
+      goodsList: []
     });
 
     this.getGoodsList();
-    
   },
   openSortFilter: function (event) {
     let currentId = event.currentTarget.id;
@@ -141,15 +152,15 @@ Page({
         this.getGoodsList();
     }
   },
-  selectCategory: function(event){
+  selectCategory: function (event) {
     let currentIndex = event.target.dataset.categoryIndex;
-    let filterCategory = this.data.filterCategory;    
+    let filterCategory = this.data.filterCategory;
     let currentCategory = null;
-    for (let key in filterCategory){
-      if(key == currentIndex){
+    for (let key in filterCategory) {
+      if (key == currentIndex) {
         filterCategory[key].selected = true;
         currentCategory = filterCategory[key];
-      }else{
+      } else {
         filterCategory[key].selected = false;
       }
     }
@@ -162,14 +173,7 @@ Page({
     });
     this.getGoodsList();
   },
-  onKeywordConfirm(event){
-    this.setData({
-      keyword: event.detail.value,
-      page: 1,
-      categoryId: 0,
-      goodsList: []
-    });
-
-    this.getGoodsList();
+  onKeywordConfirm(event) {
+    this.getSearchResult(event.detail.value);
   }
 })

+ 3 - 3
pages/search/search.wxml

@@ -14,7 +14,7 @@
       <image class="icon" bindtap="clearHistory" src="http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/del1-93f0a4add4.png"></image>
     </view>
     <view class="b">
-      <view class="item" bindtap="search" data-keyword="{{item}}" wx:for="{{historyKeyword}}" hover-class="navigator-hover">{{item}}</view>
+      <view class="item" bindtap="onKeywordTap" data-keyword="{{item}}" wx:for="{{historyKeyword}}" hover-class="navigator-hover">{{item}}</view>
     </view>
   </view>
   <view class="serach-keywords search-hot" wx:if="{{!keyword}}">
@@ -22,11 +22,11 @@
       <text class="title">热门搜索</text>
     </view>
     <view class="b">
-      <view class="item {{item.is_hot === 1 ? 'active' : ''}}" hover-class="navigator-hover" bindtap="search" data-keyword="{{item.keyword}}" wx:for="{{hotKeyword}}">{{item.keyword}}</view>
+      <view class="item {{item.is_hot === 1 ? 'active' : ''}}" hover-class="navigator-hover" bindtap="onKeywordTap" data-keyword="{{item.keyword}}" wx:for="{{hotKeyword}}">{{item.keyword}}</view>
     </view>
   </view>
   <view class="shelper-list" wx:if="{{keyword}}">
-    <view class="item" hover-class="navigator-hover" wx:for="{{helpKeyword}}" bindtap="search" data-keyword="{{item}}">{{item}}</view>
+    <view class="item" hover-class="navigator-hover" wx:for="{{helpKeyword}}" bindtap="onKeywordTap" data-keyword="{{item}}">{{item}}</view>
   </view>
   </view>