auth.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // component/auth/auth.js
  2. const api = require('../../config/api')
  3. const util = require('../../utils/util.js');
  4. import {
  5. getSubOpen
  6. } from './api'
  7. Component({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. show: false,
  13. canIUse: wx.canIUse('button.open-type.getUserInfo')
  14. },
  15. attached() {
  16. this.callback = () => {
  17. this.setData({
  18. show: !getApp().globalData.loginProps.isLogin
  19. })
  20. let pages = getCurrentPages()
  21. let currPage = pages[pages.length - 1].route
  22. if (currPage === 'pages/discover/discover') {
  23. this.setData({
  24. bottom: '50px'
  25. })
  26. } else {
  27. this.setData({
  28. bottom: 0
  29. })
  30. }
  31. }
  32. this.callback()
  33. getApp().addLoginListener(this.callback)
  34. getSubOpen()
  35. },
  36. detached() {
  37. getApp().removeLoginListener(this.callback)
  38. },
  39. methods: {
  40. quitHandle: function () {
  41. getApp().setLoginProps(true)
  42. },
  43. getCode: function () {
  44. return new Promise(r => {
  45. wx.login({
  46. success: function (res) {
  47. console.log(res)
  48. if (res.code) {
  49. r(res.code)
  50. }
  51. }
  52. });
  53. })
  54. },
  55. authLogin() {
  56. this.triggerEvent('login')
  57. },
  58. bindGetUserInfo: async function (e) {
  59. let code = await this.getCode()
  60. console.log('******')
  61. console.log(e.detail)
  62. //登录远程服务器
  63. util.request(api.AuthLoginByWeixin, {
  64. code: code,
  65. userInfo: e.detail
  66. }, 'POST', 'application/json').then(res => {
  67. if (res.errno === 0) {
  68. console.log('&&&&&&&')
  69. console.log(res.data)
  70. //存储用户信息
  71. res.data.userInfo.userId = res.data.userId
  72. res.data.userInfo.sessionKey = res.data.sessionKey
  73. console.log('-------', res.data.userInfo)
  74. wx.setStorageSync('userInfo', res.data.userInfo);
  75. wx.setStorageSync('token', res.data.token);
  76. wx.setStorageSync('userId', res.data.userId);
  77. wx.setStorageSync('isLogin', true);
  78. getApp().setLoginProps(true)
  79. this.authLogin()
  80. getSubOpen()
  81. } else {
  82. // util.showErrorToast(res.errmsg)
  83. // wx.showModal({
  84. // title: '提示',
  85. // content: res.errmsg,
  86. // showCancel: false
  87. // });
  88. }
  89. });
  90. },
  91. getUserProfile: function (e) {
  92. wx.getUserProfile({
  93. desc: '用于完善用户资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  94. success: async (userInfo_res) => {
  95. console.log(userInfo_res.userInfo)
  96. let info = {userInfo:userInfo_res.userInfo}
  97. let code = await this.getCode()
  98. //登录远程服务器
  99. util.request(api.AuthLoginByWeixin, {
  100. code: code,
  101. userInfo: info
  102. }, 'POST', 'application/json').then(res => {
  103. if (res.errno === 0) {
  104. console.log('&&&&&&&')
  105. console.log(res.data)
  106. //存储用户信息
  107. res.data.userInfo.userId = res.data.userId
  108. res.data.userInfo.sessionKey = res.data.sessionKey
  109. console.log('-------', res.data.userInfo)
  110. wx.setStorageSync('userInfo', res.data.userInfo);
  111. wx.setStorageSync('token', res.data.token);
  112. wx.setStorageSync('userId', res.data.userId);
  113. wx.setStorageSync('isLogin', true);
  114. getApp().setLoginProps(true)
  115. this.authLogin()
  116. getSubOpen()
  117. } else {
  118. // util.showErrorToast(res.errmsg)
  119. // wx.showModal({
  120. // title: '提示',
  121. // content: res.errmsg,
  122. // showCancel: false
  123. // });
  124. }
  125. });
  126. }
  127. })
  128. }
  129. }
  130. })