search.js 4.2 KB

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