feat(web): 桌面端前端 Web 独立部署适配——浏览器降级桥+登录态引导+页面动态修正
- 新增 web-fallback.ts:无 pywebview 时以浏览器能力降级(文件选择/上传/模板下载/配置), 其余桌面能力(enqueue_json/vc_*/folder 选择等)保持 undefined 交由页面既有请在本地客户端中打开提示 - 新增 ensure-auth.ts:无 uid 时经 /newApi/check_login 恢复登录态,无 token/失效跳登录页; 21 个入口 main.ts 挂载前先引导(桌面端已注入 uid 时零开销) - pywebview.ts:getPywebviewApi() 在纯浏览器环境返回降级桥;新增 isDesktopRuntime 标志 (首页更新面板/退出链接据此区分桌面与 Web);hasPywebview 语义修正为真实桥判定 - 登录页跳转、首页登出链接、品牌检测页 hasBridge 判定(select_brand_folder 存在性)适配 Web
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 页面登录态引导
|
||||
*
|
||||
* 桌面客户端由 Flask 在页面 <head> 注入 localStorage.uid;前端 Web 独立部署后没有注入,
|
||||
* 直接访问子页面(书签/刷新)时 uid 缺失会导致用户信息取用失败。
|
||||
* 页面入口先执行 ensureAuth 再挂载:
|
||||
* - localStorage.uid 已存在(桌面注入 / 登录页已写入)→ 直接放行,零额外请求;
|
||||
* - 无 uid 但有 JWT → 调 /newApi/check_login 恢复 uid/username;
|
||||
* - 无 token 或校验失败 → 清本地登录态并跳转登录页。
|
||||
*/
|
||||
|
||||
import { get } from '@/shared/api/http'
|
||||
import { resolvePageHref } from '@/shared/page-prefix'
|
||||
|
||||
const AUTH_TOKEN_KEY = 'aiimage_auth_token'
|
||||
const UID_KEY = 'uid'
|
||||
|
||||
interface CheckLoginData {
|
||||
userId?: number | string
|
||||
username?: string
|
||||
}
|
||||
|
||||
function redirectToLogin() {
|
||||
window.location.replace(resolvePageHref('/new_web_source/login.html'))
|
||||
}
|
||||
|
||||
/** 确保当前页面具备登录态;返回是否可继续挂载应用 */
|
||||
export async function ensureAuth(): Promise<boolean> {
|
||||
if (typeof window === 'undefined') return true
|
||||
try {
|
||||
const rawUid = window.localStorage.getItem(UID_KEY) || ''
|
||||
if (rawUid && Number(rawUid) > 0) {
|
||||
return true
|
||||
}
|
||||
const token = window.localStorage.getItem(AUTH_TOKEN_KEY) || ''
|
||||
if (!token) {
|
||||
redirectToLogin()
|
||||
return false
|
||||
}
|
||||
const response = await get<{ success: boolean; data?: CheckLoginData }>('/newApi/check_login')
|
||||
if (response?.success !== true || !response.data?.userId) {
|
||||
throw new Error('登录已失效')
|
||||
}
|
||||
window.localStorage.setItem(UID_KEY, String(response.data.userId))
|
||||
if (response.data.username) {
|
||||
window.localStorage.setItem('username', String(response.data.username))
|
||||
}
|
||||
return true
|
||||
} catch {
|
||||
// token 失效:清掉本地登录态后回登录页
|
||||
try {
|
||||
window.localStorage.removeItem(AUTH_TOKEN_KEY)
|
||||
} catch {
|
||||
/* 忽略 */
|
||||
}
|
||||
redirectToLogin()
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user