index.js 3.5 KB

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