catalog.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. var util = require('../../../utils/util.js');
  2. var mall = require('../../../api/mall.js');
  3. const user = require('../../../utils/user.js');
  4. // const area = require('../../../utils/area.js');
  5. //获取应用实例
  6. const app = getApp();
  7. Page({
  8. data: {
  9. categoryList: [],
  10. currentCategory: {},
  11. currentSubCategoryList: {},
  12. scrollLeft: 0,
  13. scrollTop: 0,
  14. goodsList:[],
  15. goodsCount: 0,
  16. scrollHeight: 0,
  17. hasLogin: true,
  18. city:'',//当前业务城市
  19. citys:[],
  20. cityIndex:0,
  21. addressInfo:{},
  22. },
  23. onLoad: function (options) {
  24. this.getCatalog();
  25. },
  26. onPullDownRefresh() {
  27. wx.showNavigationBarLoading() //在标题栏中显示加载
  28. this.getCatalog();
  29. wx.hideNavigationBarLoading() //完成停止加载
  30. wx.stopPullDownRefresh() //停止下拉刷新
  31. },
  32. listenerCitySelected(e){
  33. this.setData({
  34. cityIndex:e.detail.value,
  35. city:this.data.citys[e.detail.value]
  36. })
  37. app.globalData.city=this.data.city;
  38. },
  39. getCatalog: function () {
  40. let that = this;
  41. wx.showLoading({
  42. title: '加载中...',
  43. });
  44. util.request(mall.CatalogList).then(function (res) {
  45. //1036763 写死值 如果地址不是苏州市 隐藏活动专享
  46. //20220223关闭所有活动合作专区
  47. //console.info(res.data.categoryList)
  48. var list=res.data.categoryList;
  49. // if(that.data.addressInfo.city&&that.data.addressInfo.city!="苏州市"){
  50. var categorys=[];
  51. res.data.categoryList.forEach(item=>{
  52. if(item.id!=1036763)
  53. categorys.push(item);
  54. });
  55. list=categorys;
  56. // }
  57. that.setData({
  58. categoryList:list,
  59. currentCategory: res.data.currentCategory,
  60. currentSubCategoryList: res.data.currentSubCategory
  61. });
  62. that.getCategoryGoods();
  63. wx.hideLoading();
  64. }).catch((err)=>{
  65. wx.hideLoading();
  66. console.log(err);
  67. });
  68. util.request(mall.GoodsCount).then(function (res) {
  69. that.setData({
  70. goodsCount: res.data
  71. });
  72. });
  73. },
  74. getCategoryGoods(){//获取一级类目下所有商品
  75. let that=this;
  76. util.request(mall.GoodsL1Category,{
  77. id:that.data.currentCategory.name=='热销商品'?'-1':that.data.currentCategory.id,
  78. // shopId:that.data.addressInfo.shopId,
  79. // isHot:that.data.currentCategory.name=='热销商品'?true:false
  80. }).then(res=>{
  81. console.info(res.data)
  82. that.setData({
  83. goodsList:res.data
  84. })
  85. })
  86. },
  87. getCurrentCategory: function (id) {
  88. let that = this;
  89. util.request(mall.CatalogCurrent, {
  90. id:id,
  91. })
  92. .then(function (res) {
  93. that.setData({
  94. currentCategory: res.data.currentCategory,
  95. currentSubCategoryList: res.data.currentSubCategory,
  96. });
  97. that.getCategoryGoods();
  98. });
  99. },
  100. onReady: function () {
  101. // 页面渲染完成
  102. if (app.globalData.hasLogin) {
  103. this.setData({
  104. hasLogin: true
  105. });
  106. }
  107. },
  108. onShow: function () {
  109. // 页面显示
  110. //显示购物车角标
  111. //user.cartshow();
  112. //确定业务城市
  113. var addressInfo=wx.getStorageSync('addressInfo');
  114. if(addressInfo){
  115. this.setData({
  116. addressInfo:addressInfo
  117. })
  118. }else{
  119. //手机性能慢,可能首页接口没初始化完毕
  120. let that=this;
  121. setTimeout(() => {
  122. var addressInfo=wx.getStorageSync('addressInfo');
  123. if(addressInfo){
  124. that.setData({
  125. addressInfo:addressInfo
  126. })
  127. }
  128. }, 500);
  129. }
  130. },
  131. goLogin: function (e) {
  132. if (e.detail.userInfo == null) {
  133. app.globalData.hasLogin = false;
  134. util.showErrorToast('微信授权失败');
  135. return;
  136. }
  137. app.globalData.hasLogin = false;
  138. wx.setStorageSync('userInfo', null);
  139. user.loginByWeixin(e.detail.userInfo).then(res => {
  140. //跳转注册页
  141. wx.navigateTo({
  142. url: "/pages/auth/register/register"
  143. });
  144. }).catch((err) => {
  145. app.globalData.hasLogin = false;
  146. });
  147. },
  148. onHide: function () {
  149. // 页面隐藏
  150. },
  151. onUnload: function () {
  152. // 页面关闭
  153. },
  154. switchCate: function (event) {
  155. var that = this;
  156. var currentTarget = event.currentTarget;
  157. if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
  158. return false;
  159. }
  160. this.getCurrentCategory(event.currentTarget.dataset.id);
  161. },
  162. goinviteFriends(e) {
  163. console.log(app.globalData.hasLogin);
  164. //跳转到邀请页面
  165. if (app.globalData.hasLogin) {
  166. wx.navigateTo({
  167. url: '/pages/activity/invitefriends/inviteFriends'
  168. })
  169. } else {
  170. wx.navigateTo({
  171. url: "/pages/auth/login/login"
  172. });
  173. };
  174. },
  175. })