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