search.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. 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. let that = this;
  75. this.setData({
  76. historyKeyword: []
  77. })
  78. util.request(api.SearchClearHistory)
  79. .then(function (res) {
  80. console.log('清除成功');
  81. });
  82. },
  83. getGoodsList: function () {
  84. let that = this;
  85. util.request(api.SearchResult + '/' + that.data.keyword).then(function (res) {
  86. if (res.errno === 0) {
  87. that.setData({
  88. searchStatus: true,
  89. categoryFilter: false,
  90. goodsList: res.data,
  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; //checked?
  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. })