search.js 5.3 KB

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