auth.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. console.log(e)
  93. wx.getUserProfile({
  94. desc: '用于完善用户资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  95. success: async (userInfo_res) => {
  96. console.log(userInfo_res.userInfo)
  97. let info = {userInfo:userInfo_res.userInfo}
  98. let code = await this.getCode()
  99. //登录远程服务器
  100. util.request(api.AuthLoginByWeixin, {
  101. code: code,
  102. userInfo: info
  103. }, 'POST', 'application/json').then(res => {
  104. if (res.errno === 0) {
  105. console.log('&&&&&&&')
  106. console.log(res.data)
  107. //存储用户信息
  108. res.data.userInfo.userId = res.data.userId
  109. res.data.userInfo.sessionKey = res.data.sessionKey
  110. console.log('-------', res.data.userInfo)
  111. wx.setStorageSync('userInfo', res.data.userInfo);
  112. wx.setStorageSync('token', res.data.token);
  113. wx.setStorageSync('userId', res.data.userId);
  114. wx.setStorageSync('isLogin', true);
  115. getApp().setLoginProps(true)
  116. this.authLogin()
  117. getSubOpen()
  118. } else {
  119. // util.showErrorToast(res.errmsg)
  120. // wx.showModal({
  121. // title: '提示',
  122. // content: res.errmsg,
  123. // showCancel: false
  124. // });
  125. }
  126. });
  127. }
  128. })
  129. }
  130. }
  131. })