search.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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, {})
  79. .then(function (res) {
  80. });
  81. },
  82. getGoodsList: function () {
  83. let that = this;
  84. 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) {
  85. if (res.errno === 0) {
  86. that.setData({
  87. searchStatus: true,
  88. categoryFilter: false,
  89. goodsList: res.data.data,
  90. filterCategory: res.data.filterCategory,
  91. page: res.data.currentPage,
  92. size: res.data.numsPerPage
  93. });
  94. }
  95. //重新获取关键词
  96. that.getSearchKeyword();
  97. });
  98. },
  99. onKeywordTap: function (event) {
  100. this.getSearchResult(event.target.dataset.keyword);
  101. },
  102. getSearchResult(keyword) {
  103. this.setData({
  104. keyword: keyword,
  105. page: 1,
  106. categoryId: 0,
  107. goodsList: []
  108. });
  109. this.getGoodsList();
  110. },
  111. openSortFilter: function (event) {
  112. let currentId = event.currentTarget.id;
  113. switch (currentId) {
  114. case 'categoryFilter':
  115. this.setData({
  116. 'categoryFilter': !this.data.categoryFilter,
  117. 'currentSortOrder': 'asc'
  118. });
  119. break;
  120. case 'priceSort':
  121. let tmpSortOrder = 'asc';
  122. if (this.data.currentSortOrder == 'asc') {
  123. tmpSortOrder = 'desc';
  124. }
  125. this.setData({
  126. 'currentSortType': 'price',
  127. 'currentSortOrder': tmpSortOrder,
  128. 'categoryFilter': false
  129. });
  130. this.getGoodsList();
  131. break;
  132. default:
  133. //综合排序
  134. this.setData({
  135. 'currentSortType': 'default',
  136. 'currentSortOrder': 'desc',
  137. 'categoryFilter': false
  138. });
  139. this.getGoodsList();
  140. }
  141. },
  142. selectCategory: function (event) {
  143. let currentIndex = event.target.dataset.categoryIndex;
  144. let filterCategory = this.data.filterCategory;
  145. let currentCategory = null;
  146. for (let key in filterCategory) {
  147. if (key == currentIndex) {
  148. filterCategory[key].selected = true;
  149. currentCategory = filterCategory[key];
  150. } else {
  151. filterCategory[key].selected = false;
  152. }
  153. }
  154. this.setData({
  155. 'filterCategory': filterCategory,
  156. 'categoryFilter': false,
  157. categoryId: currentCategory.id,
  158. page: 1,
  159. goodsList: []
  160. });
  161. this.getGoodsList();
  162. },
  163. onKeywordConfirm(event) {
  164. this.getSearchResult(event.detail.value);
  165. }
  166. })