catalog.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.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. //CatalogList
  41. let that = this;
  42. wx.showLoading({
  43. title: '加载中...',
  44. });
  45. util.request(api.CatalogList).then(function (res) {
  46. //1036763 写死值 如果地址为上海 隐藏活动专享
  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. });
  65. util.request(api.GoodsCount).then(function (res) {
  66. that.setData({
  67. goodsCount: res.data
  68. });
  69. });
  70. },
  71. getCategoryGoods(){//获取一级类目下所有商品
  72. let that=this;
  73. util.request(api.GoodsL1Category,{
  74. id:that.data.currentCategory.name=='热销商品'?'-1':that.data.currentCategory.id,
  75. shopId:that.data.addressInfo.shopId,
  76. isHot:that.data.currentCategory.name=='热销商品'?true:false
  77. }).then(res=>{
  78. console.info(res.data)
  79. that.setData({
  80. goodsList:res.data
  81. })
  82. })
  83. },
  84. getCurrentCategory: function (id) {
  85. let that = this;
  86. util.request(api.CatalogCurrent, {
  87. id:id,
  88. })
  89. .then(function (res) {
  90. that.setData({
  91. currentCategory: res.data.currentCategory,
  92. currentSubCategoryList: res.data.currentSubCategory,
  93. });
  94. that.getCategoryGoods();
  95. });
  96. },
  97. onReady: function () {
  98. // 页面渲染完成
  99. if (app.globalData.hasLogin) {
  100. this.setData({
  101. hasLogin: true
  102. });
  103. }
  104. },
  105. onShow: function () {
  106. // 页面显示
  107. //显示购物车角标
  108. //user.cartshow();
  109. //确定业务城市
  110. var addressInfo=wx.getStorageSync('addressInfo');
  111. if(addressInfo){
  112. this.setData({
  113. addressInfo:addressInfo
  114. })
  115. }
  116. },
  117. goLogin: function (e) {
  118. if (e.detail.userInfo == null) {
  119. app.globalData.hasLogin = false;
  120. util.showErrorToast('微信授权失败');
  121. return;
  122. }
  123. app.globalData.hasLogin = false;
  124. wx.setStorageSync('userInfo', null);
  125. user.loginByWeixin(e.detail.userInfo).then(res => {
  126. //跳转注册页
  127. wx.navigateTo({
  128. url: "/pages/auth/register/register"
  129. });
  130. }).catch((err) => {
  131. app.globalData.hasLogin = false;
  132. });
  133. },
  134. onHide: function () {
  135. // 页面隐藏
  136. },
  137. onUnload: function () {
  138. // 页面关闭
  139. },
  140. switchCate: function (event) {
  141. var that = this;
  142. var currentTarget = event.currentTarget;
  143. if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
  144. return false;
  145. }
  146. this.getCurrentCategory(event.currentTarget.dataset.id);
  147. },
  148. goinviteFriends(e) {
  149. console.log(app.globalData.hasLogin);
  150. //跳转到邀请页面
  151. if (app.globalData.hasLogin) {
  152. wx.navigateTo({
  153. url: '/pages/activity/invitefriends/inviteFriends'
  154. })
  155. } else {
  156. wx.navigateTo({
  157. url: "/pages/auth/login/login"
  158. });
  159. };
  160. },
  161. })