index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const util = require('../../utils/util.js');
  2. const api = require('../../config/api.js');
  3. const user = require('../../utils/user.js');
  4. //获取应用实例
  5. const app = getApp();
  6. Page({
  7. data: {
  8. newGoods: [],
  9. hotGoods: [],
  10. topics: [],
  11. brands: [],
  12. groupons: [],
  13. floorGoods: [],
  14. banner: [],
  15. channel: [],
  16. coupon: [],
  17. goodsCount: 0
  18. },
  19. onShareAppMessage: function() {
  20. return {
  21. title: 'litemall小程序商场',
  22. desc: '开源微信小程序商城',
  23. path: '/pages/index/index'
  24. }
  25. },
  26. onPullDownRefresh() {
  27. wx.showNavigationBarLoading() //在标题栏中显示加载
  28. this.getIndexData();
  29. wx.hideNavigationBarLoading() //完成停止加载
  30. wx.stopPullDownRefresh() //停止下拉刷新
  31. },
  32. getIndexData: function() {
  33. let that = this;
  34. util.request(api.IndexUrl).then(function(res) {
  35. if (res.errno === 0) {
  36. that.setData({
  37. newGoods: res.data.newGoodsList,
  38. hotGoods: res.data.hotGoodsList,
  39. topics: res.data.topicList,
  40. brands: res.data.brandList,
  41. floorGoods: res.data.floorGoodsList,
  42. banner: res.data.banner,
  43. groupons: res.data.grouponList,
  44. channel: res.data.channel,
  45. coupon: res.data.couponList
  46. });
  47. }
  48. });
  49. util.request(api.GoodsCount).then(function (res) {
  50. that.setData({
  51. goodsCount: res.data
  52. });
  53. });
  54. },
  55. onLoad: function(options) {
  56. // 页面初始化 options为页面跳转所带来的参数
  57. if (options.scene) {
  58. //这个scene的值存在则证明首页的开启来源于朋友圈分享的图,同时可以通过获取到的goodId的值跳转导航到对应的详情页
  59. var scene = decodeURIComponent(options.scene);
  60. console.log("scene:" + scene);
  61. let info_arr = [];
  62. info_arr = scene.split(',');
  63. let _type = info_arr[0];
  64. let id = info_arr[1];
  65. if (_type == 'goods') {
  66. wx.navigateTo({
  67. url: '../goods/goods?id=' + id
  68. });
  69. } else if (_type == 'groupon') {
  70. wx.navigateTo({
  71. url: '../goods/goods?grouponId=' + id
  72. });
  73. } else {
  74. wx.navigateTo({
  75. url: '../index/index'
  76. });
  77. }
  78. }
  79. // 页面初始化 options为页面跳转所带来的参数
  80. if (options.grouponId) {
  81. //这个pageId的值存在则证明首页的开启来源于用户点击来首页,同时可以通过获取到的pageId的值跳转导航到对应的详情页
  82. wx.navigateTo({
  83. url: '../goods/goods?grouponId=' + options.grouponId
  84. });
  85. }
  86. // 页面初始化 options为页面跳转所带来的参数
  87. if (options.goodId) {
  88. //这个goodId的值存在则证明首页的开启来源于分享,同时可以通过获取到的goodId的值跳转导航到对应的详情页
  89. wx.navigateTo({
  90. url: '../goods/goods?id=' + options.goodId
  91. });
  92. }
  93. // 页面初始化 options为页面跳转所带来的参数
  94. if (options.orderId) {
  95. //这个orderId的值存在则证明首页的开启来源于订单模版通知,同时可以通过获取到的pageId的值跳转导航到对应的详情页
  96. wx.navigateTo({
  97. url: '../ucenter/orderDetail/orderDetail?id=' + options.orderId
  98. });
  99. }
  100. this.getIndexData();
  101. },
  102. onReady: function() {
  103. // 页面渲染完成
  104. },
  105. onShow: function() {
  106. // 页面显示
  107. },
  108. onHide: function() {
  109. // 页面隐藏
  110. },
  111. onUnload: function() {
  112. // 页面关闭
  113. },
  114. getCoupon(e) {
  115. let couponId = e.currentTarget.dataset.index
  116. util.request(api.CouponReceive, {
  117. couponId: couponId
  118. }, 'POST').then(res => {
  119. if (res.errno === 0) {
  120. wx.showToast({
  121. title: "领取成功"
  122. })
  123. }
  124. else{
  125. util.showErrorToast(res.errmsg);
  126. }
  127. })
  128. },
  129. })