index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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(){
  79. var that = this;
  80. app.isAuth('scope.userInfo', function(res){
  81. if(res){
  82. that.cashdialog.setErrMsg('');
  83. that.cashdialog.showDialog();
  84. }else{
  85. app.location('login/index');
  86. }
  87. });
  88. },
  89. /**
  90. * 跳转兑换页面
  91. */
  92. toCash: function(){
  93. var that = this;
  94. var cashCode = this.cashdialog.getCashCode();
  95. if (cashCode.length <= 0) {
  96. that.cashdialog.setErrMsg('请输入兑换码!');
  97. return ;
  98. }
  99. app.request('product.api.checkCode', {
  100. code: cashCode
  101. }, {
  102. success: function (data, res) {
  103. that.cashdialog.hideDialog();
  104. app.location('cash/index?code=' + cashCode);
  105. },
  106. fail: function (res) {
  107. that.cashdialog.setErrMsg(res.data.msg);
  108. }
  109. });
  110. },
  111. /**
  112. * 页面下拉事件的处理函数
  113. */
  114. onPullDownRefresh: function () {
  115. this.pg = 1;
  116. app.startPullDown(this);
  117. },
  118. /**
  119. * 页面上拉触底事件的处理函数
  120. */
  121. onReachBottom: function () {
  122. app.startPullDown(this);
  123. },
  124. /**
  125. * 用户点击右上角分享
  126. */
  127. onShareAppMessage: function (options) {
  128. //打开好友列表
  129. wx.showShareMenu({
  130. withShareTicket: true,
  131. success: function (res) {
  132. console.log('wx.showShareMenu success', res.data);
  133. },
  134. fail: function (res) {
  135. console.log('wx.showShareMenu fail', res.data);
  136. }
  137. })
  138. return {
  139. title: '',
  140. desc: '',
  141. path: '/template/home/index'
  142. }
  143. }
  144. })