app.js 1.44 KB
/*
 * @Date: 2025-06-28 10:33:00
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2026-01-29 18:29:57
 * @FilePath: /manulife-weapp/src/app.js
 * @Description: 应用入口文件
 */
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import './utils/polyfill'
import './app.less'
import { saveCurrentPagePath, hasAuth, silentAuth, navigateToAuth } from '@/utils/authRedirect'

const App = createApp({
    // 对应 onLaunch
    async onLaunch(options) {
        const path = options?.path || ''
        const query = options?.query || {}

        const query_string = Object.keys(query)
            .map((key) => `${key}=${encodeURIComponent(query[key])}`)
            .join('&')
        const full_path = query_string ? `${path}?${query_string}` : path

        // 保存当前页面路径,用于授权后跳转回原页面
        if (full_path) {
            saveCurrentPagePath(full_path)
        }

        // 如果用户已授权,则不需要额外操作
        if (hasAuth()) {
            return
        }

        if (path === 'pages/auth/index') return

        try {
            // 尝试静默授权
            await silentAuth()
        } catch (error) {
            console.error('静默授权失败:', error)
            // 授权失败则跳转至授权页面
            navigateToAuth(full_path || undefined)
        }

        return
    },
    onShow() {
    },
});

App.use(createPinia())

export default App