catalog.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. that.setData({
  47. categoryList: res.data.categoryList,
  48. currentCategory: res.data.currentCategory,
  49. currentSubCategoryList: res.data.currentSubCategory
  50. });
  51. that.getCategoryGoods();
  52. wx.hideLoading();
  53. });
  54. util.request(api.GoodsCount).then(function (res) {
  55. that.setData({
  56. goodsCount: res.data
  57. });
  58. });
  59. },
  60. getCategoryGoods(){//获取一级类目下所有商品
  61. let that=this;
  62. util.request(api.GoodsL1Category,{
  63. id:that.data.currentCategory.name=='热销商品'?'-1':that.data.currentCategory.id,
  64. shopId:that.data.addressInfo.shopId,
  65. isHot:that.data.currentCategory.name=='热销商品'?true:false
  66. }).then(res=>{
  67. console.info(res.data)
  68. that.setData({
  69. goodsList:res.data
  70. })
  71. })
  72. },
  73. getCurrentCategory: function (id) {
  74. let that = this;
  75. util.request(api.CatalogCurrent, {
  76. id:id,
  77. })
  78. .then(function (res) {
  79. that.setData({
  80. currentCategory: res.data.currentCategory,
  81. currentSubCategoryList: res.data.currentSubCategory,
  82. });
  83. that.getCategoryGoods();
  84. });
  85. },
  86. onReady: function () {
  87. // 页面渲染完成
  88. if (app.globalData.hasLogin) {
  89. this.setData({
  90. hasLogin: true
  91. });
  92. }
  93. },
  94. onShow: function () {
  95. // 页面显示
  96. //显示购物车角标
  97. //user.cartshow();
  98. //确定业务城市
  99. console.info(app.globalData.city)
  100. var addressInfo=wx.getStorageSync('addressInfo');
  101. if(addressInfo){
  102. this.setData({
  103. addressInfo:addressInfo
  104. })
  105. }
  106. },
  107. goLogin: function (e) {
  108. if (e.detail.userInfo == null) {
  109. app.globalData.hasLogin = false;
  110. util.showErrorToast('微信授权失败');
  111. return;
  112. }
  113. app.globalData.hasLogin = false;
  114. wx.setStorageSync('userInfo', null);
  115. user.loginByWeixin(e.detail.userInfo).then(res => {
  116. //跳转注册页
  117. wx.navigateTo({
  118. url: "/pages/auth/register/register"
  119. });
  120. }).catch((err) => {
  121. app.globalData.hasLogin = false;
  122. });
  123. },
  124. onHide: function () {
  125. // 页面隐藏
  126. },
  127. onUnload: function () {
  128. // 页面关闭
  129. },
  130. switchCate: function (event) {
  131. var that = this;
  132. var currentTarget = event.currentTarget;
  133. if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
  134. return false;
  135. }
  136. this.getCurrentCategory(event.currentTarget.dataset.id);
  137. },
  138. goinviteFriends(e) {
  139. console.log(app.globalData.hasLogin);
  140. //跳转到邀请页面
  141. if (app.globalData.hasLogin) {
  142. wx.navigateTo({
  143. url: '/pages/activity/invitefriends/inviteFriends'
  144. })
  145. } else {
  146. wx.navigateTo({
  147. url: "/pages/auth/login/login"
  148. });
  149. };
  150. },
  151. })