search.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. defaultKeyword: '输入关键字',
  12. hotKeyword: [],
  13. page: 1,
  14. size: 10,
  15. },
  16. //事件处理函数
  17. closeSearch: function() {
  18. wx.navigateBack()
  19. },
  20. clearKeyword: function() {
  21. this.setData({
  22. keyword: '',
  23. searchStatus: false
  24. });
  25. },
  26. onLoad: function() {
  27. this.getSearchKeyword();
  28. },
  29. getSearchKeyword() {
  30. let that = this;
  31. util.request(api.SearchIndex).then(function(res) {
  32. if (res.errno === 0) {
  33. that.setData({
  34. historyKeyword: res.data.historyKeywordList,
  35. hotKeyword: res.data.hotKeywordList
  36. });
  37. }
  38. });
  39. },
  40. inputChange: function(e) {
  41. this.setData({
  42. keyword: e.detail.value,
  43. searchStatus: false
  44. });
  45. this.getHelpKeyword();
  46. },
  47. //借用淘宝的输入辅助api
  48. getHelpKeyword: function() {
  49. let that = this;
  50. util.request('https://suggest.taobao.com/sug', {
  51. code: 'utf-8',
  52. q: that.data.keyword
  53. }).then(function(res) {
  54. that.setData({
  55. helpKeyword: res.result
  56. });
  57. });
  58. },
  59. inputFocus: function() {
  60. this.setData({
  61. searchStatus: false,
  62. goodsList: []
  63. });
  64. if (this.data.keyword) {
  65. this.getHelpKeyword();
  66. }
  67. },
  68. clearHistory: function() {
  69. let that = this;
  70. this.setData({
  71. historyKeyword: []
  72. })
  73. util.request(api.SearchClearHistory)
  74. .then(function(res) {
  75. console.log('清除成功');
  76. });
  77. },
  78. getGoodsList: function() {
  79. let that = this;
  80. util.request(api.SearchResult + '/' + that.data.keyword, {
  81. page: this.data.page,
  82. size: this.data.size
  83. }).then(function(res) {
  84. if (res.errno === 0) {
  85. that.setData({
  86. searchStatus: true,
  87. categoryFilter: false,
  88. goodsList: that.data.goodsList.concat(res.data),
  89. });
  90. }
  91. //重新获取关键词
  92. that.getSearchKeyword();
  93. });
  94. },
  95. onKeywordTap: function(event) {
  96. this.getSearchResult(event.target.dataset.keyword);
  97. },
  98. getSearchResult(keyword) {
  99. this.setData({
  100. keyword: keyword,
  101. page: 1,
  102. categoryId: 0,
  103. goodsList: []
  104. });
  105. this.getGoodsList();
  106. },
  107. onKeywordConfirm(event) {
  108. this.getSearchResult(event.detail.value);
  109. },
  110. onPullDownRefresh: function() {
  111. console.log("上拉刷新")
  112. this.onLoad()
  113. setTimeout(function callback() {
  114. wx.stopPullDownRefresh()
  115. }, 500)
  116. },
  117. onReachBottom: function() {
  118. console.log("拉到底")
  119. this.setData({
  120. page: this.data.page + 1
  121. })
  122. this.getGoodsList()
  123. },
  124. })