catalog.js 4.8 KB

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