search.js 4.3 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. 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', { code: 'utf-8', q: that.data.keyword }).then(function (res) {
  58. that.setData({
  59. helpKeyword: res.result
  60. });
  61. });
  62. },
  63. inputFocus: function () {
  64. this.setData({
  65. searchStatus: false,
  66. goodsList: []
  67. });
  68. if (this.data.keyword) {
  69. this.getHelpKeyword();
  70. }
  71. },
  72. clearHistory: function () {
  73. let that = this;
  74. this.setData({
  75. historyKeyword: []
  76. })
  77. util.request(api.SearchClearHistory)
  78. .then(function (res) {
  79. console.log('清除成功');
  80. });
  81. },
  82. getGoodsList: function () {
  83. let that = this;
  84. util.request(api.SearchResult + '/' + that.data.keyword).then(function (res) {
  85. if (res.errno === 0) {
  86. that.setData({
  87. searchStatus: true,
  88. categoryFilter: false,
  89. goodsList: res.data,
  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. getSearchResult(keyword) {
  102. this.setData({
  103. keyword: keyword,
  104. page: 1,
  105. categoryId: 0,
  106. goodsList: []
  107. });
  108. this.getGoodsList();
  109. },
  110. // openSortFilter: function (event) {
  111. // let currentId = event.currentTarget.id;
  112. // switch (currentId) {
  113. // case 'categoryFilter':
  114. // this.setData({
  115. // 'categoryFilter': !this.data.categoryFilter,
  116. // 'currentSortOrder': 'asc'
  117. // });
  118. // break;
  119. // case 'priceSort':
  120. // let tmpSortOrder = 'asc';
  121. // if (this.data.currentSortOrder == 'asc') {
  122. // tmpSortOrder = 'desc';
  123. // }
  124. // this.setData({
  125. // 'currentSortType': 'price',
  126. // 'currentSortOrder': tmpSortOrder,
  127. // 'categoryFilter': false
  128. // });
  129. // this.getGoodsList();
  130. // break;
  131. // default:
  132. // //综合排序
  133. // this.setData({
  134. // 'currentSortType': 'default',
  135. // 'currentSortOrder': 'desc',
  136. // 'categoryFilter': false
  137. // });
  138. // this.getGoodsList();
  139. // }
  140. // },
  141. // selectCategory: function (event) {
  142. // let currentIndex = event.target.dataset.categoryIndex;
  143. // let filterCategory = this.data.filterCategory;
  144. // let currentCategory = null;
  145. // for (let key in filterCategory) {
  146. // if (key == currentIndex) {
  147. // filterCategory[key].selected = true; //checked?
  148. // currentCategory = filterCategory[key];
  149. // } else {
  150. // filterCategory[key].selected = false;
  151. // }
  152. // }
  153. // this.setData({
  154. // 'filterCategory': filterCategory,
  155. // 'categoryFilter': false,
  156. // categoryId: currentCategory.id,
  157. // page: 1,
  158. // goodsList: []
  159. // });
  160. // this.getGoodsList();
  161. // },
  162. onKeywordConfirm(event) {
  163. this.getSearchResult(event.detail.value);
  164. }
  165. })