search.js 4.4 KB

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