index.js 2.4 KB

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