App.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <script>
  2. export default {
  3. onLaunch: function() {
  4. console.log('App Launch')
  5. // 检查登录状态
  6. const token = uni.getStorageSync('token')
  7. if (token) {
  8. this.$store.commit('setToken', token)
  9. } else {
  10. // 无token时尝试自动登录
  11. this.tryAutoLogin()
  12. }
  13. },
  14. onShow: function() {
  15. console.log('App Show')
  16. },
  17. onHide: function() {
  18. console.log('App Hide')
  19. },
  20. methods: {
  21. async tryAutoLogin() {
  22. try {
  23. // 1. 先尝试用缓存的openid自动登录
  24. const cachedOpenid = uni.getStorageSync('openid')
  25. if (cachedOpenid) {
  26. const res = await this.request('/api/auth/auto-login', 'POST', { openid: cachedOpenid })
  27. if (res && res.data) {
  28. this.saveLoginInfo(res.data)
  29. console.log('openid自动登录成功')
  30. return
  31. }
  32. }
  33. // 2. openid自动登录失败,尝试微信静默登录
  34. const loginRes = await new Promise((resolve, reject) => {
  35. uni.login({
  36. provider: 'weixin',
  37. success: resolve,
  38. fail: reject
  39. })
  40. })
  41. if (loginRes.code) {
  42. const res = await this.request('/api/auth/silent-login', 'POST', { code: loginRes.code })
  43. if (res && res.data) {
  44. this.saveLoginInfo(res.data)
  45. console.log('微信静默登录成功')
  46. }
  47. }
  48. } catch (e) {
  49. console.log('自动登录失败,需要手动登录', e)
  50. }
  51. },
  52. saveLoginInfo(data) {
  53. uni.setStorageSync('token', data.token)
  54. uni.setStorageSync('userId', data.userId)
  55. uni.setStorageSync('role', data.role)
  56. uni.setStorageSync('familyId', data.familyId)
  57. // 设置 currentRole 为 role,用于控制 TabBar 页面显示
  58. uni.setStorageSync('currentRole', data.role)
  59. if (data.openid) {
  60. uni.setStorageSync('openid', data.openid)
  61. }
  62. uni.setStorageSync('userInfo', {
  63. nickname: data.nickname,
  64. userId: data.userId
  65. })
  66. this.$store.commit('setToken', data.token)
  67. },
  68. request(url, method, data) {
  69. return new Promise((resolve, reject) => {
  70. uni.request({
  71. url: 'http://localhost:8080' + url,
  72. method: method,
  73. data: data,
  74. header: { 'Content-Type': 'application/json' },
  75. success: (res) => {
  76. if (res.data.code === 200) {
  77. resolve(res.data)
  78. } else {
  79. reject(res.data)
  80. }
  81. },
  82. fail: reject
  83. })
  84. })
  85. }
  86. }
  87. }
  88. </script>
  89. <style>
  90. @import url('./common/uni.css');
  91. page {
  92. background-color: #f5f5f5;
  93. }
  94. .container {
  95. padding: 20rpx;
  96. }
  97. .btn-primary {
  98. background-color: #FF6B6B;
  99. color: #fff;
  100. border-radius: 20rpx;
  101. padding: 20rpx 40rpx;
  102. text-align: center;
  103. font-size: 28rpx;
  104. }
  105. .btn-primary:active {
  106. opacity: 0.8;
  107. }
  108. </style>