search.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. filterCategory: [],
  14. defaultKeyword: {},
  15. hotKeyword: [],
  16. page: 1,
  17. size: 20,
  18. currentSortType: 'id',
  19. currentSortOrder: 'desc',
  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. back() {
  36. wx.navigateBack({
  37. delta: 0,
  38. })
  39. },
  40. getSearchKeyword() {
  41. let that = this;
  42. util.request(api.SearchIndex).then(function (res) {
  43. if (res.errno === 0) {
  44. that.setData({
  45. historyKeyword: res.data.historyKeywordList,
  46. defaultKeyword: res.data.defaultKeyword,
  47. hotKeyword: res.data.hotKeywordList
  48. });
  49. }
  50. });
  51. },
  52. inputChange: function (e) {
  53. this.setData({
  54. keyword: e.detail.value,
  55. searchStatus: false
  56. });
  57. this.getHelpKeyword();
  58. },
  59. getHelpKeyword: function () {
  60. let that = this;
  61. util.request(api.SearchHelper, { keyword: that.data.keyword }).then(function (res) {
  62. if (res.errno === 0) {
  63. that.setData({
  64. helpKeyword: res.data
  65. });
  66. }
  67. });
  68. this.getGoodsList()
  69. },
  70. inputFocus: function () {
  71. this.setData({
  72. searchStatus: false,
  73. goodsList: []
  74. });
  75. if (this.data.keyword) {
  76. this.getHelpKeyword();
  77. }
  78. },
  79. clearHistory: function () {
  80. this.setData({
  81. historyKeyword: []
  82. })
  83. util.request(api.SearchClearHistory, {})
  84. .then(function (res) {
  85. });
  86. },
  87. getGoodsList: function () {
  88. let that = this;
  89. 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) {
  90. if (res.errno === 0) {
  91. const filterCategory = res.data.filterCategory
  92. for (let i = 0; i < filterCategory.length; i++) {
  93. if (filterCategory[i].checked) {
  94. for (let j = 0; j < i; j++) {
  95. filterCategory[j].checked = false
  96. }
  97. }
  98. }
  99. console.log(filterCategory)
  100. that.setData({
  101. searchStatus: true,
  102. categoryFilter: false,
  103. goodsList: res.data.goodsList,
  104. filterCategory: res.data.filterCategory,
  105. page: res.data.currentPage,
  106. size: res.data.numsPerPage
  107. });
  108. }
  109. //重新获取关键词
  110. that.getSearchKeyword();
  111. });
  112. },
  113. onKeywordTap: function (event) {
  114. this.getSearchResult(event.target.dataset.keyword);
  115. },
  116. gotoSearch:function(){
  117. this.getSearchResult(this.data.keyword);
  118. },
  119. getSearchResult(keyword) {
  120. this.setData({
  121. keyword: keyword,
  122. page: 1,
  123. categoryId: 0,
  124. goodsList: []
  125. });
  126. this.getGoodsList();
  127. },
  128. openSortFilter: function (event) {
  129. let currentId = event.currentTarget.id;
  130. switch (currentId) {
  131. case 'categoryFilter':
  132. this.setData({
  133. 'categoryFilter': !this.data.categoryFilter
  134. // 'currentSortOrder': 'asc'
  135. });
  136. break;
  137. case 'priceSort':
  138. let tmpSortOrder = 'asc';
  139. if (this.data.currentSortOrder == 'asc') {
  140. tmpSortOrder = 'desc';
  141. }
  142. this.setData({
  143. 'currentSortType': 'price',
  144. 'currentSortOrder': tmpSortOrder,
  145. 'categoryFilter': false
  146. });
  147. this.getGoodsList();
  148. break;
  149. default:
  150. //综合排序
  151. this.setData({
  152. 'currentSortType': 'default',
  153. 'currentSortOrder': 'desc',
  154. 'categoryFilter': false
  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. })