| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <script>
- import Vue from 'vue'
- import config from '@/config.js'
- import { ssoLogin } from './utils/api.js'
- Vue.prototype.$config = config
- export default {
- onLaunch: function(options) {
- console.log('App Launch', options)
- // 从 storage 恢复登录态到 store,保证 state 与 storage 一致
- this.$store.dispatch('restore')
- // 跨小程序单点登录:从 cfc 跳转进入时携带 ssoTicket → 换取本地登录态
- this.handleSsoLogin(options)
- },
- onShow: function(options) {
- console.log('App Show', options)
- // 热启动兜底:若 store 空但 storage 有 token(如 HBuilderX 热更新后 store 重置),重新恢复
- if (!this.$store.state.token && uni.getStorageSync('token')) {
- this.$store.dispatch('restore')
- }
- // 热启动携带 ssoTicket 进入同样处理
- this.handleSsoLogin(options)
- },
- onHide: function() {
- console.log('App Hide')
- },
- methods: {
- // 跨小程序单点登录:从 cfc 跳转进入时携带 ssoTicket → 换取本地登录态。
- // 一次性票据(后端原子消费),用 _ssoHandledTicket 去重,避免 onLaunch/onShow 双处理。
- handleSsoLogin(options) {
- if (!options || !options.query) return
- var ticket = options.query.ssoTicket
- if (!ticket) return
- if (this._ssoHandledTicket === ticket) return
- this._ssoHandledTicket = ticket
- var self = this
- uni.showLoading({ title: '登录中...' })
- ssoLogin(ticket).then(function(res) {
- uni.hideLoading()
- if (res && res.code === 200 && res.data) {
- self.$store.dispatch('login', res.data)
- uni.reLaunch({ url: '/pages/index/index' })
- } else {
- uni.showToast({ title: (res && res.message) || '单点登录失败', icon: 'none' })
- }
- }).catch(function(e) {
- uni.hideLoading()
- uni.showToast({ title: (e && e.message) || '单点登录失败', icon: 'none' })
- })
- }
- }
- }
- </script>
- <style>
- page {
- background-color: #F5FAFE;
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI',
- 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
- 'Helvetica Neue', Helvetica, Arial, sans-serif;
- color: #1E3A5F;
- font-size: 28rpx;
- line-height: 1.6;
- -webkit-font-smoothing: antialiased;
- }
- .page {
- padding: 0 32rpx 32rpx;
- min-height: 100vh;
- }
- .safe-area-bottom {
- padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
- padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
- }
- </style>
|