auth.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // pages/auth/auth.js
  2. const app = getApp();
  3. var Api = require('../../utils/api.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. canIUse: wx.canIUse('button.open-type.getUserInfo')
  10. },
  11. onAuth() {
  12. wx.getSetting({
  13. success: (res) => {
  14. if (res.authSetting['scope.userInfo']) {
  15. // app.globalData.userInfo = res.userInfo
  16. wx.getUserInfo({
  17. success: res => {
  18. app.globalData.userInfo = res.userInfo
  19. }
  20. })
  21. }
  22. }
  23. })
  24. //登录
  25. wx.login({
  26. success: res => {
  27. // console.log(res)
  28. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  29. if (res.code) {
  30. //发起网络请求
  31. wx.request({
  32. url: Api.postgetOpenId(),
  33. data: {
  34. codeId: res.code
  35. },
  36. success (res) {
  37. // console.log(res)
  38. if(res.statusCode==200){
  39. // app.globalData.openid=res.data.data.openid
  40. wx.setStorageSync('openid', res.data.data.openid)
  41. var pages = getCurrentPages(); // 当前页面
  42. var beforePage = pages[pages.length - 2]
  43. wx.navigateBack({
  44. success: function() {
  45. beforePage.onLoad(); // 执行前一个页面的onLoad方法
  46. }
  47. })
  48. }else{
  49. }
  50. }
  51. })
  52. } else {
  53. console.log('登录失败!' + res.errMsg)
  54. }
  55. }
  56. })
  57. },
  58. /**
  59. * 生命周期函数--监听页面加载
  60. */
  61. onLoad: function (options) {
  62. },
  63. /**
  64. * 生命周期函数--监听页面初次渲染完成
  65. */
  66. onReady: function () {
  67. },
  68. /**
  69. * 生命周期函数--监听页面显示
  70. */
  71. onShow: function () {
  72. wx.hideHomeButton({
  73. success: function() {
  74. },
  75. })
  76. },
  77. /**
  78. * 生命周期函数--监听页面隐藏
  79. */
  80. onHide: function () {
  81. },
  82. /**
  83. * 生命周期函数--监听页面卸载
  84. */
  85. onUnload: function () {
  86. },
  87. /**
  88. * 页面相关事件处理函数--监听用户下拉动作
  89. */
  90. onPullDownRefresh: function () {
  91. },
  92. /**
  93. * 页面上拉触底事件的处理函数
  94. */
  95. onReachBottom: function () {
  96. },
  97. /**
  98. * 用户点击右上角分享
  99. */
  100. onShareAppMessage: function () {
  101. }
  102. })