search.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. var util = require('../../../utils/jmsUtil.js');
  2. var mall = require('../../../api/mall.js');
  3. var app = getApp()
  4. Page({
  5. data: {
  6. keywrod: '',
  7. searchStatus: false,
  8. goodsList: [],
  9. helpKeyword: [],
  10. historyKeyword: [],
  11. categoryFilter: false,
  12. currentSort: 'name',
  13. currentSortType: 'default',
  14. currentSortOrder: 'desc',
  15. filterCategory: [],
  16. defaultKeyword: {},
  17. hotKeyword: [],
  18. page: 1,
  19. limit: 20,
  20. categoryId: 0,
  21. addressInfo:{}
  22. },
  23. //事件处理函数
  24. closeSearch: function() {
  25. wx.navigateBack()
  26. },
  27. clearKeyword: function() {
  28. this.setData({
  29. keyword: '',
  30. searchStatus: false
  31. });
  32. },
  33. onLoad: function() {
  34. // var addressInfo=wx.getStorageSync('addressInfo');
  35. // if(addressInfo){
  36. // this.setData({
  37. // addressInfo:addressInfo
  38. // })
  39. // }
  40. this.getSearchKeyword();
  41. },
  42. getSearchKeyword() {
  43. let that = this;
  44. util.request(mall.SearchIndex).then(function(res) {
  45. if (res.errno === 0) {
  46. that.setData({
  47. historyKeyword: res.data.historyKeywordList,
  48. defaultKeyword: res.data.defaultKeyword,
  49. hotKeyword: res.data.hotKeywordList
  50. });
  51. }
  52. });
  53. },
  54. inputChange: function(e) {
  55. this.setData({
  56. keyword: e.detail.value,
  57. searchStatus: false
  58. });
  59. if (e.detail.value) {
  60. this.getHelpKeyword();
  61. }
  62. },
  63. getHelpKeyword: function() {
  64. let that = this;
  65. util.request(mall.SearchHelper, {
  66. keyword: that.data.keyword
  67. }).then(function(res) {
  68. if (res.errno === 0) {
  69. that.setData({
  70. helpKeyword: res.data
  71. });
  72. }
  73. });
  74. },
  75. inputFocus: function() {
  76. this.setData({
  77. searchStatus: false,
  78. goodsList: []
  79. });
  80. if (this.data.keyword) {
  81. this.getHelpKeyword();
  82. }
  83. },
  84. clearHistory: function() {
  85. this.setData({
  86. historyKeyword: []
  87. })
  88. util.request(mall.SearchClearHistory, {}, 'POST')
  89. .then(function(res) {
  90. console.log('清除成功');
  91. });
  92. },
  93. getGoodsList: function() {
  94. let that = this;
  95. util.request(mall.GoodsList, {
  96. commodityName: that.data.keyword,
  97. page: that.data.page,
  98. limit: that.data.limit,
  99. sort: that.data.currentSort,
  100. order: that.data.currentSortOrder,
  101. // categoryId: that.data.categoryId,
  102. // city:that.data.addressInfo.city,
  103. // shopId:that.data.addressInfo.shopId
  104. }).then(function(res) {
  105. if (res.errno === 0) {
  106. that.setData({
  107. searchStatus: true,
  108. categoryFilter: false,
  109. goodsList: res.data.list,
  110. filterCategory: res.data.filterCategoryList
  111. });
  112. }
  113. //重新获取关键词
  114. that.getSearchKeyword();
  115. });
  116. },
  117. onKeywordTap: function(event) {
  118. this.getSearchResult(event.target.dataset.keyword);
  119. },
  120. getSearchResult(keyword) {
  121. if (keyword === '') {
  122. keyword = this.data.defaultKeyword.keyword;
  123. }
  124. this.setData({
  125. keyword: keyword,
  126. page: 1,
  127. categoryId: 0,
  128. goodsList: []
  129. });
  130. this.getGoodsList();
  131. },
  132. openSortFilter: function(event) {
  133. let currentId = event.currentTarget.id;
  134. switch (currentId) {
  135. case 'categoryFilter':
  136. this.setData({
  137. categoryFilter: !this.data.categoryFilter,
  138. currentSortType: 'category',
  139. currentSort: 'add_time',
  140. currentSortOrder: 'desc'
  141. });
  142. break;
  143. case 'priceSort':
  144. let tmpSortOrder = 'asc';
  145. if (this.data.currentSortOrder == 'asc') {
  146. tmpSortOrder = 'desc';
  147. }
  148. this.setData({
  149. currentSortType: 'price',
  150. currentSort: 'retail_price',
  151. currentSortOrder: tmpSortOrder,
  152. categoryFilter: false
  153. });
  154. this.getGoodsList();
  155. break;
  156. default:
  157. //综合排序
  158. this.setData({
  159. currentSortType: 'default',
  160. currentSort: 'name',
  161. currentSortOrder: 'desc',
  162. categoryFilter: false,
  163. categoryId: 0,
  164. });
  165. this.getGoodsList();
  166. }
  167. },
  168. selectCategory: function(event) {
  169. let currentIndex = event.target.dataset.categoryIndex;
  170. let filterCategory = this.data.filterCategory;
  171. let currentCategory = null;
  172. for (let key in filterCategory) {
  173. if (key == currentIndex) {
  174. filterCategory[key].selected = true;
  175. currentCategory = filterCategory[key];
  176. } else {
  177. filterCategory[key].selected = false;
  178. }
  179. }
  180. this.setData({
  181. filterCategory: filterCategory,
  182. categoryFilter: false,
  183. categoryId: currentCategory.id,
  184. page: 1,
  185. goodsList: []
  186. });
  187. this.getGoodsList();
  188. },
  189. onKeywordConfirm(event) {
  190. this.getSearchResult(event.detail.value);
  191. }
  192. })