search.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. filterCategory: [],
  13. defaultKeyword: {},
  14. hotKeyword: [],
  15. page: 1,
  16. size: 1000,
  17. currentSortType: 'id',
  18. currentSortOrder: 'desc',
  19. categoryId: 0,
  20. showNavList: false,
  21. footCart: {}
  22. },
  23. toggleNav() {
  24. this.setData({
  25. showNavList: !this.data.showNavList
  26. })
  27. },
  28. switchNav(event) {
  29. let name = event.currentTarget.dataset.name;
  30. wx.switchTab({
  31. url: `/pages/${name}/${name}`,
  32. });
  33. },
  34. //事件处理函数
  35. closeSearch: function () {
  36. wx.navigateBack()
  37. },
  38. clearKeyword: function () {
  39. this.setData({
  40. keyword: '',
  41. searchStatus: false
  42. });
  43. },
  44. onLoad: function () {
  45. this.getSearchKeyword();
  46. this.getFootCart();
  47. },
  48. getFootCart: function () {
  49. let that = this;
  50. util.request(api.GetFootCart).then(function (res) {
  51. if (res.errno === 0) {
  52. that.setData({
  53. footCart: res.data,
  54. });
  55. }
  56. });
  57. },
  58. getSearchKeyword() {
  59. let that = this;
  60. util.request(api.SearchIndex).then(function (res) {
  61. if (res.errno === 0) {
  62. that.setData({
  63. historyKeyword: res.data.historyKeywordList,
  64. defaultKeyword: res.data.defaultKeyword,
  65. hotKeyword: res.data.hotKeywordList
  66. });
  67. }
  68. });
  69. },
  70. inputChange: function (e) {
  71. this.setData({
  72. keyword: e.detail.value,
  73. searchStatus: false
  74. });
  75. this.getHelpKeyword();
  76. },
  77. getHelpKeyword: function () {
  78. let that = this;
  79. util.request(api.SearchHelper, { keyword: that.data.keyword }).then(function (res) {
  80. if (res.errno === 0) {
  81. that.setData({
  82. helpKeyword: res.data
  83. });
  84. }
  85. });
  86. },
  87. inputFocus: function () {
  88. this.setData({
  89. searchStatus: false,
  90. goodsList: []
  91. });
  92. if (this.data.keyword) {
  93. this.getHelpKeyword();
  94. }
  95. },
  96. clearHistory: function () {
  97. this.setData({
  98. historyKeyword: []
  99. })
  100. util.request(api.SearchClearHistory, {}, 'POST')
  101. .then(function (res) {
  102. console.log('清除成功');
  103. });
  104. },
  105. getGoodsList: function () {
  106. let that = this;
  107. wx.showLoading({
  108. title: '加载中...',
  109. });
  110. 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,
  111. storeId:wx.getStorageSync("storeId")}).then(function (res) {
  112. if (res.errno === 0) {
  113. that.setData({
  114. searchStatus: true,
  115. categoryFilter: false,
  116. goodsList: res.data.data,
  117. filterCategory: res.data.filterCategory,
  118. page: res.data.currentPage,
  119. size: res.data.numsPerPage
  120. });
  121. wx.hideLoading();
  122. }
  123. //重新获取关键词
  124. that.getSearchKeyword();
  125. });
  126. },
  127. onKeywordTap: function (event) {
  128. this.getSearchResult(event.target.dataset.keyword);
  129. },
  130. getSearchResult(keyword) {
  131. this.setData({
  132. keyword: keyword,
  133. page: 1,
  134. categoryId: 0,
  135. goodsList: []
  136. });
  137. this.getGoodsList();
  138. },
  139. openSortFilter: function (event) {
  140. let currentId = event.currentTarget.id;
  141. switch (currentId) {
  142. case 'categoryFilter':
  143. this.setData({
  144. 'categoryFilter': !this.data.categoryFilter,
  145. 'currentSortOrder': 'asc'
  146. });
  147. break;
  148. case 'priceSort':
  149. let tmpSortOrder = 'asc';
  150. if (this.data.currentSortOrder == 'asc') {
  151. tmpSortOrder = 'desc';
  152. }
  153. this.setData({
  154. 'currentSortType': 'price',
  155. 'currentSortOrder': tmpSortOrder,
  156. 'categoryFilter': false
  157. });
  158. this.getGoodsList();
  159. break;
  160. default:
  161. //综合排序
  162. this.setData({
  163. 'currentSortType': 'default',
  164. 'currentSortOrder': 'desc',
  165. 'categoryFilter': false
  166. });
  167. this.getGoodsList();
  168. }
  169. },
  170. selectCategory: function (event) {
  171. let currentIndex = event.target.dataset.categoryIndex;
  172. let filterCategory = this.data.filterCategory;
  173. let currentCategory = null;
  174. for (let key in filterCategory) {
  175. if (key == currentIndex) {
  176. filterCategory[key].selected = true;
  177. currentCategory = filterCategory[key];
  178. } else {
  179. filterCategory[key].selected = false;
  180. }
  181. }
  182. this.setData({
  183. 'filterCategory': filterCategory,
  184. 'categoryFilter': false,
  185. categoryId: currentCategory.id,
  186. page: 1,
  187. goodsList: []
  188. });
  189. this.getGoodsList();
  190. },
  191. onKeywordConfirm(event) {
  192. this.getSearchResult(event.detail.value);
  193. }
  194. })