activity.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var api = require('../../config/api.js');
  2. var util = require('../../utils/util.js');
  3. var app = getApp();
  4. Page({
  5. data: {
  6. couponList: [],
  7. referrer: 0,
  8. sourceKey: '',
  9. scrollTop: 0
  10. },
  11. onLoad: function (options) {
  12. let that = this;
  13. // 页面初始化 options为页面跳转所带来的参数
  14. if (options.referrer) {
  15. that.setData({
  16. referrer: parseInt(options.referrer),
  17. sourceKey: options.sourceKey
  18. });
  19. }
  20. let code = null;
  21. if (null == wx.getStorageInfoSync('userInfo')) {
  22. util.login().then((res) => {
  23. code = res.code;
  24. return util.getUserInfo();
  25. }).then((userInfo) => {
  26. //登录远程服务器,不用
  27. wx.request({
  28. url: api.AuthLoginByWeixin,
  29. data: {
  30. code: code, userInfo: userInfo
  31. },
  32. method: 'POST',
  33. header: {
  34. 'Content-Type': 'application/json'
  35. },
  36. success: function (res) {
  37. if (res.errno === 0) {
  38. //存储用户信息
  39. if (res.data.userInfo) {
  40. wx.setStorageSync('userInfo', res.data.userInfo);
  41. }
  42. wx.setStorageSync('token', res.data.token);
  43. wx.setStorageSync('userId', res.data.userId);
  44. that.getCouponList();
  45. }
  46. },
  47. fail: function (err) {
  48. reject(err)
  49. console.log("failed")
  50. }
  51. })
  52. })
  53. } else {
  54. that.getCouponList();
  55. }
  56. },
  57. onReady: function () {
  58. },
  59. onShow: function () {
  60. },
  61. onHide: function () {
  62. // 页面隐藏
  63. },
  64. onUnload: function () {
  65. // 页面关闭
  66. },
  67. getCouponList() {
  68. let that = this;
  69. util.request(api.CouponTransActivit).then(function (res) {
  70. if (res.errno === 0) {
  71. that.setData({
  72. couponList: res.data
  73. });
  74. } else if (res.errno === 2) {
  75. util.showErrorToast(res.errmsg)
  76. that.setData({
  77. couponList: res.data
  78. });
  79. }
  80. });
  81. }
  82. })