category.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // pages/category/category.js
  2. var http = require("../../utils/http.js");
  3. var util = require('../../utils/util.js');
  4. var config = require("../../utils/config.js");
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. selIndex: 0,
  11. categoryList: [],
  12. categoryImg: '',
  13. subCategoryList: [],
  14. pageTopHeight: wx.getWindowInfo().statusBarHeight,
  15. hotList: [],
  16. topCurrentIndex: 0,
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. this.getRecommended()
  23. this.get_neighborShop()
  24. // this.get_categoryInfo()
  25. },
  26. /**
  27. * 获取距离最近店铺id
  28. * @param {*} e
  29. */
  30. get_neighborShop: function () {
  31. var params = {
  32. url: "/shop/neighborShop",
  33. method: "GET",
  34. data: {
  35. lat: wx.getStorageSync('LATITUDE'),
  36. lon: wx.getStorageSync('LONGITUDE'),
  37. platform:1
  38. },
  39. callBack: (res) => {
  40. // this.get_categoryInfo(res)
  41. if(typeof res == 'number'){
  42. this.get_categoryInfo(res)
  43. }else{
  44. this.get_categoryInfo(res.shopId)
  45. }
  46. }
  47. };
  48. http.request(params);
  49. },
  50. get_categoryInfo: function (shopid) {
  51. var ths = this;
  52. //加载分类列表
  53. var params = {
  54. url: "/category/categoryInfo",
  55. method: "GET",
  56. data: {
  57. shopId:shopid
  58. },
  59. callBack: function (res) {
  60. let categoryName = ''
  61. res.forEach(e => {
  62. categoryName = e.categoryName.split('>')
  63. e.categoryName = categoryName[0]
  64. })
  65. ths.setData({
  66. categoryList: res,
  67. });
  68. if (!getApp().globalData.categoryId) {
  69. ths.getProdList(res[0].categoryId)
  70. ths.setData({
  71. categoryImg: res[0].pic,
  72. })
  73. }
  74. }
  75. };
  76. http.request(params);
  77. },
  78. /**
  79. * 选择定位后更新列表
  80. */
  81. go_update() {
  82. this.get_neighborShop()
  83. },
  84. /**
  85. * 生命周期函数--监听页面初次渲染完成
  86. */
  87. onReady: function () {
  88. },
  89. /**
  90. * 生命周期函数--监听页面显示
  91. */
  92. onShow: function () {
  93. if (getApp().globalData.categoryId) {
  94. let categoryId = getApp().globalData.categoryId
  95. let index = getApp().globalData.index
  96. let pic = getApp().globalData.pic
  97. this.getProdList(categoryId);
  98. this.setData({
  99. categoryImg: pic,
  100. selIndex: index
  101. });
  102. }else{
  103. this.get_neighborShop()
  104. }
  105. },
  106. /**
  107. * 生命周期函数--监听页面隐藏
  108. */
  109. onHide: function () {},
  110. /**
  111. * 生命周期函数--监听页面卸载
  112. */
  113. onUnload: function () {
  114. },
  115. /**
  116. * 页面相关事件处理函数--监听用户下拉动作
  117. */
  118. onPullDownRefresh: function () {
  119. },
  120. /**
  121. * 页面上拉触底事件的处理函数
  122. */
  123. onReachBottom: function () {
  124. },
  125. /**
  126. * 用户点击右上角分享
  127. */
  128. onShareAppMessage: function () {
  129. },
  130. /**
  131. * 分类点击事件,获取子分类
  132. */
  133. onMenuTab: function (e) {
  134. var id = e.currentTarget.dataset.id;
  135. var index = e.currentTarget.dataset.index;
  136. // this.getProdList(id);
  137. this.getProdList(this.data.categoryList[index].categoryId);
  138. this.setData({
  139. categoryImg: this.data.categoryList[index].pic,
  140. selIndex: index
  141. });
  142. getApp().globalData.categoryId = ''
  143. getApp().globalData.index = ''
  144. getApp().globalData.pic = ''
  145. },
  146. /**
  147. * 推荐词,type:1关键词,2热门搜索词,3推荐搜索词
  148. */
  149. getRecommended: function () {
  150. var params = {
  151. url: "/keyword/list",
  152. method: "GET",
  153. data: {
  154. type: 2
  155. },
  156. callBack: (res) => {
  157. let reslut = res.filter(item => item.status !== 2) //过滤下线词
  158. let timeReslut = reslut.filter(e => util.dateToTimestamp(e.effectiveTime) < new Date().getTime()) //过滤未生效词
  159. this.setData({
  160. hotList: timeReslut
  161. })
  162. }
  163. };
  164. http.request(params);
  165. },
  166. /**
  167. * 推荐词搜索
  168. */
  169. topSwiperChange: function (e) {
  170. this.setData({
  171. topCurrentIndex: e.detail.current // 获取当前滚动到的swiper-item的索引并更新到data中
  172. })
  173. },
  174. topHotSearch: function () {
  175. const topname = this.data.hotList[this.data.topCurrentIndex].name
  176. wx.navigateTo({
  177. url: '/pages/search-prod-show/search-prod-show?prodName=' + topname,
  178. })
  179. },
  180. // 跳转搜索页
  181. toSearchPage: function () {
  182. wx.navigateTo({
  183. url: '/pages/search-page/search-page',
  184. })
  185. },
  186. getProdList(categoryId) {
  187. this.setData({
  188. parentId: categoryId
  189. })
  190. //加载子分类列表
  191. var params = {
  192. url: "/category/categoryInfo",
  193. method: "GET",
  194. data: {
  195. parentId: categoryId
  196. },
  197. callBack: (res) => {
  198. let str='https://zswl-shop.oss-cn-chengdu.aliyuncs.com'
  199. res.forEach(item=>{
  200. item.categories.forEach(e=>{
  201. if(e.pic!=null){
  202. if(!e.pic.includes(str)){
  203. e.pic='https://zswl-shop.oss-cn-chengdu.aliyuncs.com/'+e.pic
  204. }
  205. }
  206. })
  207. })
  208. this.setData({
  209. subCategoryList: res,
  210. });
  211. }
  212. };
  213. http.request(params);
  214. },
  215. /**
  216. * 跳转到定位页面
  217. */
  218. toLocationPage: function () {
  219. wx.navigateTo({
  220. url: '/pages/locationAdd/locationAdd',
  221. })
  222. },
  223. // 跳转子分类商品页面
  224. toCatePage: function (e) {
  225. console.log(e);
  226. const {
  227. type,
  228. parentid,
  229. categoryid,
  230. subcateindex
  231. } = e.currentTarget.dataset
  232. wx.navigateTo({
  233. url: `/pages/sub-category/sub-category?parentId=${parentid}&categoryId=${type=='all'?this.data.subCategoryList[subcateindex].categoryId:categoryid}`,
  234. })
  235. },
  236. toCategoryPage: function () {
  237. wx.navigateTo({
  238. url: `/pages/sub-category/sub-category?categoryId=${this.data.parentId}`,
  239. })
  240. }
  241. })