1
0

search.js 5.2 KB

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