App.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <script>
  2. import Vue from 'vue'
  3. import config from '@/config.js'
  4. import { ssoLogin } from './utils/api.js'
  5. Vue.prototype.$config = config
  6. export default {
  7. onLaunch: function(options) {
  8. console.log('App Launch', options)
  9. // 从 storage 恢复登录态到 store,保证 state 与 storage 一致
  10. this.$store.dispatch('restore')
  11. // 跨小程序单点登录:从 cfc 跳转进入时携带 ssoTicket → 换取本地登录态
  12. this.handleSsoLogin(options)
  13. },
  14. onShow: function(options) {
  15. console.log('App Show', options)
  16. // 热启动兜底:若 store 空但 storage 有 token(如 HBuilderX 热更新后 store 重置),重新恢复
  17. if (!this.$store.state.token && uni.getStorageSync('token')) {
  18. this.$store.dispatch('restore')
  19. }
  20. // 热启动携带 ssoTicket 进入同样处理
  21. this.handleSsoLogin(options)
  22. },
  23. onHide: function() {
  24. console.log('App Hide')
  25. },
  26. methods: {
  27. // 跨小程序单点登录:从 cfc 跳转进入时携带 ssoTicket → 换取本地登录态。
  28. // 一次性票据(后端原子消费),用 _ssoHandledTicket 去重,避免 onLaunch/onShow 双处理。
  29. handleSsoLogin(options) {
  30. if (!options || !options.query) return
  31. var ticket = options.query.ssoTicket
  32. if (!ticket) return
  33. if (this._ssoHandledTicket === ticket) return
  34. this._ssoHandledTicket = ticket
  35. var self = this
  36. uni.showLoading({ title: '登录中...' })
  37. ssoLogin(ticket).then(function(res) {
  38. uni.hideLoading()
  39. if (res && res.code === 200 && res.data) {
  40. self.$store.dispatch('login', res.data)
  41. uni.reLaunch({ url: '/pages/index/index' })
  42. } else {
  43. uni.showToast({ title: (res && res.message) || '单点登录失败', icon: 'none' })
  44. }
  45. }).catch(function(e) {
  46. uni.hideLoading()
  47. uni.showToast({ title: (e && e.message) || '单点登录失败', icon: 'none' })
  48. })
  49. }
  50. }
  51. }
  52. </script>
  53. <style>
  54. page {
  55. background-color: #F5FAFE;
  56. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI',
  57. 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
  58. 'Helvetica Neue', Helvetica, Arial, sans-serif;
  59. color: #1E3A5F;
  60. font-size: 28rpx;
  61. line-height: 1.6;
  62. -webkit-font-smoothing: antialiased;
  63. }
  64. .page {
  65. padding: 0 32rpx 32rpx;
  66. min-height: 100vh;
  67. }
  68. .safe-area-bottom {
  69. padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
  70. padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
  71. }
  72. </style>