search.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. var app = getApp()
  4. Page({
  5. data: {
  6. keywrod: '',
  7. searchStatus: false,
  8. goodsList: [],
  9. helpKeyword: [],
  10. historyKeyword: [],
  11. categoryFilter: false,
  12. currentSortType: 'default',
  13. currentSortOrder: '',
  14. filterCategory: [],
  15. defaultKeyword: {},
  16. hotKeyword: [],
  17. page: 1,
  18. size: 20,
  19. currentSortType: 'id',
  20. currentSortOrder: 'desc',
  21. categoryId: 0
  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. let that = this;
  35. util.request(api.SearchIndex).then(function (res) {
  36. if (res.errno === 0) {
  37. that.setData({
  38. historyKeyword: res.data.historyKeywordList,
  39. defaultKeyword: res.data.defaultKeyword,
  40. hotKeyword: res.data.hotKeywordList
  41. });
  42. }
  43. });
  44. },
  45. inputChange: function(e){
  46. this.setData({
  47. keyword: e.detail.value,
  48. searchStatus: false
  49. });
  50. this.getHelpKeyword();
  51. },
  52. getHelpKeyword: function(){
  53. let that = this;
  54. util.request(api.SearchHelper, { keyword: that.data.keyword }).then(function (res) {
  55. if (res.errno === 0) {
  56. that.setData({
  57. helpKeyword: res.data
  58. });
  59. }
  60. });
  61. },
  62. inputFocus: function(){
  63. this.setData({
  64. searchStatus: false,
  65. goodsList: []
  66. });
  67. if(this.data.keyword){
  68. this.getHelpKeyword();
  69. }
  70. },
  71. clearHistory: function(){
  72. this.setData({
  73. historyKeyword: []
  74. })
  75. util.request(api.ApiRootUrl + 'api/goods/search/history/clear', {keyword: '一'}, 'POST')
  76. .then(function(res){
  77. console.log('清除成功');
  78. });
  79. },
  80. getGoodsList: function(){
  81. let that = this;
  82. 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) {
  83. if (res.errno === 0) {
  84. that.setData({
  85. searchStatus: true,
  86. categoryFilter: false,
  87. goodsList: res.data.data,
  88. filterCategory: res.data.filterCategory,
  89. page: res.data.currentPage,
  90. size: res.data.numsPerPage
  91. });
  92. }
  93. });
  94. },
  95. search: function(event){
  96. this.setData({
  97. keyword: event.target.dataset.keyword,
  98. page: 1,
  99. categoryId: 0,
  100. goodsList:[]
  101. });
  102. this.getGoodsList();
  103. },
  104. openSortFilter: function (event) {
  105. let currentId = event.currentTarget.id;
  106. switch (currentId) {
  107. case 'categoryFilter':
  108. this.setData({
  109. 'categoryFilter': !this.data.categoryFilter,
  110. 'currentSortOrder': 'asc'
  111. });
  112. break;
  113. case 'priceSort':
  114. let tmpSortOrder = 'asc';
  115. if (this.data.currentSortOrder == 'asc') {
  116. tmpSortOrder = 'desc';
  117. }
  118. this.setData({
  119. 'currentSortType': 'price',
  120. 'currentSortOrder': tmpSortOrder,
  121. 'categoryFilter': false
  122. });
  123. this.getGoodsList();
  124. break;
  125. default:
  126. //综合排序
  127. this.setData({
  128. 'currentSortType': 'default',
  129. 'currentSortOrder': 'desc',
  130. 'categoryFilter': false
  131. });
  132. this.getGoodsList();
  133. }
  134. },
  135. selectCategory: function(event){
  136. let currentIndex = event.target.dataset.categoryIndex;
  137. let filterCategory = this.data.filterCategory;
  138. let currentCategory = null;
  139. for (let key in filterCategory){
  140. if(key == currentIndex){
  141. filterCategory[key].selected = true;
  142. currentCategory = filterCategory[key];
  143. }else{
  144. filterCategory[key].selected = false;
  145. }
  146. }
  147. this.setData({
  148. 'filterCategory': filterCategory,
  149. 'categoryFilter': false,
  150. categoryId: currentCategory.id,
  151. page: 1,
  152. goodsList: []
  153. });
  154. this.getGoodsList();
  155. },
  156. onKeywordConfirm(event){
  157. this.setData({
  158. keyword: event.detail.value,
  159. page: 1,
  160. categoryId: 0,
  161. goodsList: []
  162. });
  163. this.getGoodsList();
  164. }
  165. })