auth.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }, 'POST').then(res => {
  39. if (res.errno === 0) {
  40. //存储用户信息
  41. wx.setStorageSync('userInfo', res.data.userInfo);
  42. wx.setStorageSync('token', res.data.token);
  43. //反应到当前登录
  44. app.globalData.userInfo = res.data.userInfo;
  45. app.globalData.token = res.data.token;
  46. resolve(res.data.userInfo);
  47. } else {
  48. reject(res.data.userInfo);
  49. }
  50. }).catch((err) => { //request
  51. reject(err);
  52. });
  53. }).catch((err) => { //login
  54. reject(err);
  55. })
  56. });
  57. },
  58. goback: function () {
  59. wx.navigateBack({
  60. delta: 1
  61. })
  62. },
  63. /**
  64. * 生命周期函数--监听页面初次渲染完成
  65. */
  66. onReady: function() {
  67. },
  68. /**
  69. * 生命周期函数--监听页面显示
  70. */
  71. onShow: function() {
  72. },
  73. /**
  74. * 生命周期函数--监听页面隐藏
  75. */
  76. onHide: function() {
  77. },
  78. /**
  79. * 生命周期函数--监听页面卸载
  80. */
  81. onUnload: function() {
  82. },
  83. /**
  84. * 页面相关事件处理函数--监听用户下拉动作
  85. */
  86. onPullDownRefresh: function() {
  87. },
  88. /**
  89. * 页面上拉触底事件的处理函数
  90. */
  91. onReachBottom: function() {
  92. },
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage: function() {
  97. }
  98. })