123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- var util = require('../../../utils/jmsUtil.js');
- var mall = require('../../../api/mall.js');
- var app = getApp()
- Page({
- data: {
- keywrod: '',
- searchStatus: false,
- goodsList: [],
- helpKeyword: [],
- historyKeyword: [],
- categoryFilter: false,
- currentSort: 'name',
- currentSortType: 'default',
- currentSortOrder: 'desc',
- filterCategory: [],
- defaultKeyword: {},
- hotKeyword: [],
- page: 1,
- limit: 20,
- categoryId: 0,
- addressInfo:{}
- },
- //事件处理函数
- closeSearch: function() {
- wx.navigateBack()
- },
- clearKeyword: function() {
- this.setData({
- keyword: '',
- searchStatus: false
- });
- },
- onLoad: function() {
- // var addressInfo=wx.getStorageSync('addressInfo');
- // if(addressInfo){
- // this.setData({
- // addressInfo:addressInfo
- // })
- // }
- this.getSearchKeyword();
- },
- getSearchKeyword() {
- let that = this;
- util.request(mall.SearchIndex).then(function(res) {
- if (res.errno === 0) {
- that.setData({
- historyKeyword: res.data.historyKeywordList,
- defaultKeyword: res.data.defaultKeyword,
- hotKeyword: res.data.hotKeywordList
- });
- }
- });
- },
- inputChange: function(e) {
- this.setData({
- keyword: e.detail.value,
- searchStatus: false
- });
- if (e.detail.value) {
- this.getHelpKeyword();
- }
- },
- getHelpKeyword: function() {
- let that = this;
- util.request(mall.SearchHelper, {
- keyword: that.data.keyword
- }).then(function(res) {
- if (res.errno === 0) {
- that.setData({
- helpKeyword: res.data
- });
- }
- });
- },
- inputFocus: function() {
- this.setData({
- searchStatus: false,
- goodsList: []
- });
- if (this.data.keyword) {
- this.getHelpKeyword();
- }
- },
- clearHistory: function() {
- this.setData({
- historyKeyword: []
- })
- util.request(mall.SearchClearHistory, {}, 'POST')
- .then(function(res) {
- console.log('清除成功');
- });
- },
- getGoodsList: function() {
- let that = this;
- util.request(mall.GoodsList, {
- commodityName: that.data.keyword,
- page: that.data.page,
- limit: that.data.limit,
- sort: that.data.currentSort,
- order: that.data.currentSortOrder,
- // categoryId: that.data.categoryId,
- // city:that.data.addressInfo.city,
- // shopId:that.data.addressInfo.shopId
- }).then(function(res) {
- if (res.errno === 0) {
- that.setData({
- searchStatus: true,
- categoryFilter: false,
- goodsList: res.data.list,
- filterCategory: res.data.filterCategoryList
- });
- }
- //重新获取关键词
- that.getSearchKeyword();
- });
- },
- onKeywordTap: function(event) {
- this.getSearchResult(event.target.dataset.keyword);
- },
- getSearchResult(keyword) {
- if (keyword === '') {
- keyword = this.data.defaultKeyword.keyword;
- }
- this.setData({
- keyword: keyword,
- page: 1,
- categoryId: 0,
- goodsList: []
- });
- this.getGoodsList();
- },
- openSortFilter: function(event) {
- let currentId = event.currentTarget.id;
- switch (currentId) {
- case 'categoryFilter':
- this.setData({
- categoryFilter: !this.data.categoryFilter,
- currentSortType: 'category',
- currentSort: 'add_time',
- currentSortOrder: 'desc'
- });
- break;
- case 'priceSort':
- let tmpSortOrder = 'asc';
- if (this.data.currentSortOrder == 'asc') {
- tmpSortOrder = 'desc';
- }
- this.setData({
- currentSortType: 'price',
- currentSort: 'retail_price',
- currentSortOrder: tmpSortOrder,
- categoryFilter: false
- });
- this.getGoodsList();
- break;
- default:
- //综合排序
- this.setData({
- currentSortType: 'default',
- currentSort: 'name',
- currentSortOrder: 'desc',
- categoryFilter: false,
- categoryId: 0,
- });
- this.getGoodsList();
- }
- },
- selectCategory: function(event) {
- let currentIndex = event.target.dataset.categoryIndex;
- let filterCategory = this.data.filterCategory;
- let currentCategory = null;
- for (let key in filterCategory) {
- if (key == currentIndex) {
- filterCategory[key].selected = true;
- currentCategory = filterCategory[key];
- } else {
- filterCategory[key].selected = false;
- }
- }
- this.setData({
- filterCategory: filterCategory,
- categoryFilter: false,
- categoryId: currentCategory.id,
- page: 1,
- goodsList: []
- });
- this.getGoodsList();
- },
- onKeywordConfirm(event) {
- this.getSearchResult(event.detail.value);
- }
- })
|