login.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import shuoAPI, { shuoAPIClient } from '../api/api.js'
  2. import shuoUser from '../user/user.js';
  3. Component({
  4. properties: {
  5. // 属性值可以在组件使用时指定
  6. },
  7. data: {
  8. // 这里是一些组件内部数据
  9. selfAuthVisible: false
  10. },
  11. ready: function () {
  12. // 在组件实例进入页面节点树时执行
  13. let that = this
  14. //获取授权
  15. wx.checkSession({
  16. success: function () {
  17. console.log(" onLoad sessionId:", shuoUser.getMyTokenSync());
  18. if (!shuoUser.checkMyToken()) {
  19. that.loginAndInitData();
  20. }
  21. },
  22. fail: function () {
  23. console.log('fail session ')
  24. that.loginAndInitData();
  25. }
  26. });
  27. },
  28. detached: function () {
  29. // 在组件实例被从页面节点树移除时执行
  30. },
  31. methods: {
  32. // 这里是一个自定义方法
  33. loginAndInitData() {
  34. shuoAPI.wechatUserLogin().then(res => {
  35. console.log('wechatUserLogin res:', res);
  36. if (!res) {
  37. this.setData({
  38. selfAuthVisible: true
  39. })
  40. return;
  41. } else {
  42. this.initData()
  43. }
  44. });
  45. },
  46. // 授权遮罩
  47. authMask() {
  48. this.setData({
  49. selfAuthVisible: false
  50. })
  51. },
  52. // 点击获取用户信息
  53. onGotUserInfo(e) {
  54. console.log('点击获取用户信息: ', e.detail)
  55. wx.setStorageSync('userInfo', e.detail.userInfo)
  56. this.loginAndInitData()
  57. this.setData({
  58. selfAuthVisible: false
  59. })
  60. console.log('getCurrentPages()', getCurrentPages())
  61. },
  62. //初始化数据
  63. initData() {
  64. this.triggerEvent('loginCallBack', true) //myevent自定义名称事件,父组件中使用
  65. },
  66. }
  67. })