auth.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // pages/auth/auth.js
  2. var app = getApp();
  3. var api = require('../../config/api.js');
  4. var util = require('../../utils/util.js');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. userInfo: app.globalData.userInfo,
  11. hasUserInfo: false,
  12. canIUse: wx.canIUse('button.open-type.getUserInfo')
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function(options) {
  18. },
  19. startLogin: function(e) {
  20. console.log(e);
  21. this.login(e.detail).then((userInfo) => {
  22. this.setData({
  23. userInfo: userInfo,
  24. hasUserInfo: true
  25. })
  26. });
  27. },
  28. login: function (detail) {
  29. let code = null;
  30. return new Promise(function (resolve, reject) {
  31. return util.login().then((res) => {
  32. code = res.code;
  33. }).then(() => {
  34. //登录远程服务器
  35. util.request(api.AuthLoginByWeixin, {
  36. code: code,
  37. detail: detail,
  38. firstLogin: true
  39. }, 'POST').then(res => {
  40. if (res.errno === 0) {
  41. //存储用户信息
  42. wx.setStorageSync('userInfo', res.data.userInfo);
  43. wx.setStorageSync('token', res.data.token);
  44. //反应到当前登录
  45. app.globalData.userInfo = res.data.userInfo;
  46. app.globalData.token = res.data.token;
  47. resolve(res.data.userInfo);
  48. } else {
  49. reject(res.data.userInfo);
  50. }
  51. }).catch((err) => { //request
  52. reject(err);
  53. });
  54. }).catch((err) => { //login
  55. reject(err);
  56. })
  57. });
  58. },
  59. goback: function () {
  60. wx.navigateBack({
  61. delta: 1
  62. })
  63. },
  64. /**
  65. * 生命周期函数--监听页面初次渲染完成
  66. */
  67. onReady: function() {
  68. },
  69. /**
  70. * 生命周期函数--监听页面显示
  71. */
  72. onShow: function() {
  73. },
  74. /**
  75. * 生命周期函数--监听页面隐藏
  76. */
  77. onHide: function() {
  78. },
  79. /**
  80. * 生命周期函数--监听页面卸载
  81. */
  82. onUnload: function() {
  83. },
  84. /**
  85. * 页面相关事件处理函数--监听用户下拉动作
  86. */
  87. onPullDownRefresh: function() {
  88. },
  89. /**
  90. * 页面上拉触底事件的处理函数
  91. */
  92. onReachBottom: function() {
  93. },
  94. /**
  95. * 用户点击右上角分享
  96. */
  97. onShareAppMessage: function() {
  98. }
  99. })