search.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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: 10,
  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. // 'https://suggest.taobao.com/sug?code=utf-8&q=a'
  57. util.request('https://suggest.taobao.com/sug', {
  58. code: 'utf-8',
  59. q: that.data.keyword
  60. }).then(function(res) {
  61. that.setData({
  62. helpKeyword: res.result
  63. });
  64. });
  65. },
  66. inputFocus: function() {
  67. this.setData({
  68. searchStatus: false,
  69. goodsList: []
  70. });
  71. if (this.data.keyword) {
  72. this.getHelpKeyword();
  73. }
  74. },
  75. clearHistory: function() {
  76. let that = this;
  77. this.setData({
  78. historyKeyword: []
  79. })
  80. util.request(api.SearchClearHistory)
  81. .then(function(res) {
  82. console.log('清除成功');
  83. });
  84. },
  85. getGoodsList: function() {
  86. let that = this;
  87. util.request(api.SearchResult + '/' + that.data.keyword, {
  88. page: this.data.page,
  89. size: this.data.size
  90. }).then(function(res) {
  91. if (res.errno === 0) {
  92. that.setData({
  93. searchStatus: true,
  94. categoryFilter: false,
  95. goodsList: that.data.goodsList.concat(res.data),
  96. // page: res.data.currentPage,
  97. // size: res.data.numsPerPage
  98. });
  99. }
  100. //重新获取关键词
  101. that.getSearchKeyword();
  102. });
  103. },
  104. onKeywordTap: function(event) {
  105. this.getSearchResult(event.target.dataset.keyword);
  106. },
  107. getSearchResult(keyword) {
  108. this.setData({
  109. keyword: keyword,
  110. page: 1,
  111. categoryId: 0,
  112. goodsList: []
  113. });
  114. this.getGoodsList();
  115. },
  116. // openSortFilter: function (event) {
  117. // let currentId = event.currentTarget.id;
  118. // switch (currentId) {
  119. // case 'categoryFilter':
  120. // this.setData({
  121. // 'categoryFilter': !this.data.categoryFilter,
  122. // 'currentSortOrder': 'asc'
  123. // });
  124. // break;
  125. // case 'priceSort':
  126. // let tmpSortOrder = 'asc';
  127. // if (this.data.currentSortOrder == 'asc') {
  128. // tmpSortOrder = 'desc';
  129. // }
  130. // this.setData({
  131. // 'currentSortType': 'price',
  132. // 'currentSortOrder': tmpSortOrder,
  133. // 'categoryFilter': false
  134. // });
  135. // this.getGoodsList();
  136. // break;
  137. // default:
  138. // //综合排序
  139. // this.setData({
  140. // 'currentSortType': 'default',
  141. // 'currentSortOrder': 'desc',
  142. // 'categoryFilter': false
  143. // });
  144. // this.getGoodsList();
  145. // }
  146. // },
  147. // selectCategory: function (event) {
  148. // let currentIndex = event.target.dataset.categoryIndex;
  149. // let filterCategory = this.data.filterCategory;
  150. // let currentCategory = null;
  151. // for (let key in filterCategory) {
  152. // if (key == currentIndex) {
  153. // filterCategory[key].selected = true; //checked?
  154. // currentCategory = filterCategory[key];
  155. // } else {
  156. // filterCategory[key].selected = false;
  157. // }
  158. // }
  159. // this.setData({
  160. // 'filterCategory': filterCategory,
  161. // 'categoryFilter': false,
  162. // categoryId: currentCategory.id,
  163. // page: 1,
  164. // goodsList: []
  165. // });
  166. // this.getGoodsList();
  167. // },
  168. onKeywordConfirm(event) {
  169. this.getSearchResult(event.detail.value);
  170. },
  171. onPullDownRefresh: function() {
  172. console.log("上拉刷新")
  173. this.onLoad()
  174. setTimeout(function callback() {
  175. wx.stopPullDownRefresh()
  176. }, 500)
  177. },
  178. onReachBottom: function() {
  179. console.log("拉到底")
  180. this.setData({
  181. page: this.data.page + 1
  182. })
  183. this.getGoodsList()
  184. },
  185. })