auth.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. wx.reLaunch({
  20. url: '/pages/index/index',
  21. })
  22. }
  23. })
  24. }
  25. }
  26. })
  27. // 登录
  28. wx.login({
  29. success: res => {
  30. console.log(res)
  31. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  32. if (res.code) {
  33. //发起网络请求
  34. wx.request({
  35. url: Api.postgetOpenId(),
  36. data: {
  37. codeId: res.code
  38. },
  39. success (res) {
  40. console.log(res)
  41. if(res.statusCode==200){
  42. // app.globalData.openid=res.data.data.openid
  43. wx.setStorageSync('openid', res.data.data.openid)
  44. }
  45. }
  46. })
  47. } else {
  48. console.log('登录失败!' + res.errMsg)
  49. }
  50. }
  51. })
  52. },
  53. /**
  54. * 生命周期函数--监听页面加载
  55. */
  56. onLoad: function (options) {
  57. },
  58. /**
  59. * 生命周期函数--监听页面初次渲染完成
  60. */
  61. onReady: function () {
  62. },
  63. /**
  64. * 生命周期函数--监听页面显示
  65. */
  66. onShow: function () {
  67. wx.hideHomeButton({
  68. success: function() {
  69. },
  70. })
  71. },
  72. /**
  73. * 生命周期函数--监听页面隐藏
  74. */
  75. onHide: function () {
  76. },
  77. /**
  78. * 生命周期函数--监听页面卸载
  79. */
  80. onUnload: function () {
  81. },
  82. /**
  83. * 页面相关事件处理函数--监听用户下拉动作
  84. */
  85. onPullDownRefresh: function () {
  86. },
  87. /**
  88. * 页面上拉触底事件的处理函数
  89. */
  90. onReachBottom: function () {
  91. },
  92. /**
  93. * 用户点击右上角分享
  94. */
  95. onShareAppMessage: function () {
  96. }
  97. })