auth.js 2.0 KB

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