| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <script>
- import config from '@/config.js'
- import Vue from 'vue'
- Vue.prototype.$config = config
- export default {
- onLaunch: function(options) {
- console.log('App Launch', options)
- // 冷启动标记,首页根据此标记决定显示 splash overlay
- uni.setStorageSync('_showSplashOverlay', '1')
- // 处理小程序码场景值(从 wxacode 扫码进入)
- this.handleScene(options)
-
- // TabBar 始终显示 5 个底签,不因登录状态隐藏
- const token = uni.getStorageSync('token')
- if (token) {
- this.$store.commit('setToken', token)
- } else {
- // 无token时尝试自动登录
- this.tryAutoLogin()
- }
- },
- onShow: function(options) {
- console.log('App Show', options)
- // 每次 app 展示时也检查场景值(小程序码可能从冷启动后的 onShow 传入)
- this.handleScene(options)
-
- // 同步 storage → Vuex(防止 reLaunch/navigateTo 后 Vuex 未更新的问题)
- var token = uni.getStorageSync('token')
- if (token && token !== this.$store.state.token) {
- this.$store.commit('setToken', token)
- }
- },
- onHide: function() {
- console.log('App Hide')
- },
- methods: {
- async tryAutoLogin() {
- try {
- // 1. 先尝试用缓存的openid自动登录
- const cachedOpenid = uni.getStorageSync('openid')
- if (cachedOpenid) {
- const res = await this.request('/api/auth/auto-login', 'POST', { openid: cachedOpenid })
- if (res && res.data) {
- this.saveLoginInfo(res.data)
- console.log('openid自动登录成功')
- // 刷新页面到登录态
- uni.switchTab({ url: '/pages/index/index' })
- return
- }
- }
- // 2. openid自动登录失败,不自动调用uni.login(避免DevTools环境超时阻塞渲染层)
- // 用户会看到登录页,手动选择登录方式
- } catch (e) {
- console.log('自动登录失败,需要手动登录', e)
- }
- },
- saveLoginInfo(data) {
- uni.setStorageSync('token', data.token)
- uni.setStorageSync('userId', data.userId)
- uni.setStorageSync('role', data.role)
- uni.setStorageSync('familyId', data.familyId)
- // 自动登录后重置到真实登录角色,避免残留切换状态
- uni.setStorageSync('currentRole', data.role)
- uni.setStorageSync('isSwitchedChild', false)
- uni.setStorageSync('isSwitchedTeacher', false)
- if (data.openid) {
- uni.setStorageSync('openid', data.openid)
- }
- uni.setStorageSync('userInfo', {
- nickname: data.nickname,
- userId: data.userId
- })
- this.$store.commit('setToken', data.token)
- },
- request(url, method, data) {
- return new Promise((resolve, reject) => {
- uni.request({
- url: config.api(url),
- method: method,
- data: data,
- timeout: 10000,
- header: { 'Content-Type': 'application/json' },
- success: (res) => {
- if (res.data.code === 200) {
- resolve(res.data)
- } else {
- reject(res.data)
- }
- },
- fail: reject
- })
- })
- },
- // 处理小程序码场景值,跳转到邀请页
- handleScene(options) {
- if (!options || !options.query) return
- var scene = options.query.scene
- if (!scene) return
- var sceneDecoded = decodeURIComponent(scene)
- if (sceneDecoded.indexOf('invite=') === 0) {
- var token = sceneDecoded.substring(7)
- uni.navigateTo({
- url: '/pages/invite/join?token=' + token
- })
- }
- }
- }
- }
- </script>
- <style>
- @import url('./common/uni.css');
- page {
- background-color: var(--bg, #F5F9FC);
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI',
- 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
- 'Helvetica Neue', Helvetica, Arial, sans-serif;
- color: var(--text, #1E293B);
- font-size: 28rpx;
- line-height: 1.6;
- -webkit-font-smoothing: antialiased;
- }
- /* 页面容器 */
- .page {
- padding: 0 32rpx 32rpx;
- min-height: 100vh;
- }
- .page--no-padding {
- padding: 0;
- }
- /* 分段标题 */
- .section-header {
- font-weight: 700;
- font-size: 32rpx;
- margin: 32rpx 0 16rpx;
- color: var(--text, #1E293B);
- display: flex;
- align-items: center;
- justify-content: space-between;
- border-left: 8rpx solid #F08A7A;
- padding-left: 20rpx;
- }
- .section-header__more {
- font-size: 24rpx;
- color: var(--muted, #94A3B8);
- font-weight: 400;
- }
- /* 安全区底部 */
- .safe-area-bottom {
- padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
- padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
- }
- </style>
|