index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const app = getApp();
  2. Page({
  3. cashdialog: null,
  4. pid: '',
  5. pg: 1,
  6. data: {
  7. isShowNull: false
  8. },
  9. onLoad: function (options) {
  10. //产品详情路径附带参数,用于来源分享的参数scene=encodeURIComponent(uid, product_id)
  11. if (options != null && typeof options.scene != 'undefined') {
  12. let str = decodeURIComponent(options.scene);
  13. let arr = str.split(',');
  14. let uid = arr[0];
  15. this.pid = arr[1];
  16. app.saveInviteUid(uid);
  17. }
  18. if (app.isLogin){
  19. this.getHomeData(this);
  20. }else{
  21. var that = this;
  22. wx.login({
  23. success: function (res) {
  24. if (res.code) {
  25. app.request("passport.applet.bind", {
  26. code: res.code
  27. }, {
  28. success: function (data, res) {
  29. that.getHomeData(that);
  30. }
  31. })
  32. }
  33. }
  34. })
  35. }
  36. },
  37. /**
  38. * 获取首页数据
  39. */
  40. getHomeData: function(that){
  41. if(that.pid != ''){
  42. app.location('view/index?id=' + that.pid);
  43. }
  44. if (that.pg == 1) {
  45. app.showLoading();
  46. }
  47. that.cashdialog = that.selectComponent("#cashdialog");
  48. app.set(that, 'loadMoreText', '正在加载');
  49. app.request('product.api.home', {
  50. pg: that.pg
  51. }, {
  52. success: function (data, res) {
  53. if (data.product.length > 0) {
  54. if (that.pg > 1) {
  55. app.setList(that, data.product, true);
  56. } else {
  57. app.setList(that, data.product);
  58. }
  59. that.pg++;
  60. app.set(that, 'isShowNull', false);
  61. } else {
  62. app.set(that, 'isShowNull', true);
  63. }
  64. app.hideLoading();
  65. }
  66. });
  67. },
  68. /**
  69. * 查看商品详情
  70. */
  71. view: function (event) {
  72. let id = app.getData(event).id;
  73. app.location('view/index?id=' + id);
  74. },
  75. /**
  76. * 礼品卡兑换
  77. */
  78. cash: function(e){
  79. if(app.isLogin()){
  80. this.cashdialog.setErrMsg('');
  81. this.cashdialog.showDialog();
  82. }else{
  83. app.location('login/index');
  84. }
  85. },
  86. /**
  87. * 跳转兑换页面
  88. */
  89. toCash: function(){
  90. var that = this;
  91. var cashCode = this.cashdialog.getCashCode();
  92. if (cashCode.length <= 0) {
  93. that.cashdialog.setErrMsg('请输入兑换码!');
  94. return ;
  95. }
  96. app.request('product.api.checkCode', {
  97. code: cashCode
  98. }, {
  99. success: function (data, res) {
  100. that.cashdialog.hideDialog();
  101. app.location('cash/index?code=' + cashCode);
  102. },
  103. fail: function (res) {
  104. that.cashdialog.setErrMsg(res.data.msg);
  105. }
  106. });
  107. },
  108. /**
  109. * 页面下拉事件的处理函数
  110. */
  111. onPullDownRefresh: function () {
  112. this.pg = 1;
  113. app.startPullDown(this);
  114. },
  115. /**
  116. * 页面上拉触底事件的处理函数
  117. */
  118. onReachBottom: function () {
  119. app.startPullDown(this);
  120. },
  121. })