This commit is contained in:
Felix
2025-12-05 10:16:55 +08:00
parent b70cf8a2fb
commit 586557935d
6 changed files with 23 additions and 59 deletions

View File

@@ -1,6 +1,6 @@
// index.ts - 纯粹的登录验证页面
import apiManager from '../../utils/api'
import { USE_CLOUD } from '../../utils/config'
import { USE_CLOUD, ENV } from '../../utils/config'
// 获取应用实例
const app = getApp<IAppOption>()
@@ -11,10 +11,12 @@ Page({
},
onLoad() {
console.log('登录验证页面加载')
console.log('登录验证页面加载', ENV, USE_CLOUD)
if (USE_CLOUD) {
console.log('云函数环境,直接跳转到主页')
this.navigateToMain()
} else {
console.log('非云函数环境,开始智能登录流程')
this.initLoginStatus()
}
},

View File

@@ -3,7 +3,7 @@
<!-- 加载状态 -->
<view wx:if="{{isLoading}}" class="loading-section">
<view class="loading-content">
<view class="loading-spinner"></view>
<!-- <view class="loading-spinner"></view> -->
<text class="loading-text">验证登录状态...</text>
</view>
</view>

View File

@@ -20,8 +20,6 @@ Page({
current_date: '',
currentYear: new Date().getFullYear(), // 添加当前年份
day_type: 'morning' as IDayType,
isLoggedIn: false,
showLoginView: false,
userInfo: null as IUserInfo | null,
isProcessing: false, // 是否正在处理图片
// 移除模拟数据改为从API获取
@@ -59,7 +57,7 @@ Page({
console.log('主功能页面加载')
this.checkLoginStatus().then(() => {
// 只有登录成功后才加载历史数据
if (this.data.isLoggedIn) {
if (app.globalData.isLoggedIn) {
this.loadTodaySummary() // 加载今日摘要数据
this.loadDailySummary()
this.checkDayType()
@@ -112,7 +110,6 @@ Page({
},
onShow() {
this.updateLoginStatus()
this.setData({ isProcessing: false })
if (this.data.shouldResetOnReturn) {
this.resetPageState()
@@ -591,7 +588,6 @@ Page({
// 登录成功,更新全局状态
app.globalData.isLoggedIn = true
app.globalData.token = loginResult.access_token
this.updateLoginStatus()
console.log('登录状态验证成功')
return true
} else {
@@ -612,12 +608,6 @@ Page({
}
},
// 更新登录状态
updateLoginStatus() {
const isLoggedIn = !!app.globalData.token
this.setData({ isLoggedIn, showLoginView: !isLoggedIn })
},
// 处理图片选择(合并拍照和相册选择功能)
handleImageSelect() {
if (this.data.isProcessing) return;

View File

@@ -5,16 +5,8 @@
</view>
</navigation-bar>
<view class="{{['upload-container', day_type]}}">
<!-- 登录检查界面 -->
<view wx:if="{{showLoginView}}">
<view class="login-prompt card">
<text class="prompt-title">请先登录</text>
<text class="prompt-desc">使用图片识别功能需要先登录</text>
<button class="goto-login-btn" bindtap="checkLoginStatus">去登录</button>
</view>
</view>
<!-- 主功能界面 -->
<view wx:else class="main-content">
<view class="main-content">
<!-- 欢迎区域 -->
<!-- <view class="welcome-section">
<view class="welcome-card">

View File

@@ -460,36 +460,30 @@ class ApiManager {
// 智能登录优先检查本地token
async smartLogin(forceRefresh: boolean = false): Promise<ILoginResponse | null> {
// 如果已经在登录中返回已有的Promise
if (USE_CLOUD) {
const app = getApp<IAppOption>()
const dictLevel = wx.getStorageSync('dictLevel') || 'PRIMARY'
app.globalData.isLoggedIn = true
app.globalData.dictLevel = dictLevel
return {
access_token: '',
access_token_expire_time: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString(),
session_uuid: wx.getStorageSync('sessionUuid') || '',
dict_level: dictLevel
}
}
if (this.loginPromise && !forceRefresh) {
console.log('登录正在进行中,等待现有登录完成')
return this.loginPromise
}
const app = getApp<IAppOption>()
console.log('开始智能登录,强制刷新:', forceRefresh)
// 如果不是强制刷新检查本地存储中的token和过期时间
if (!forceRefresh) {
const authInfo = this.getStoredAuthInfo()
console.log('本地认证信息:', {
hasToken: !!authInfo?.token,
hasExpiry: !!authInfo?.tokenExpiry,
tokenLength: authInfo?.token?.length || 0
})
if (authInfo && authInfo.token && authInfo.tokenExpiry) {
// 检查token是否过期
const isExpired = this.isTokenExpired()
if (!isExpired) {
console.log('Token未过期使用本地token')
// 更新全局状态
app.globalData.isLoggedIn = true
app.globalData.token = authInfo.token
app.globalData.dictLevel = authInfo.dictLevel || 'PRIMARY'
// 返回本地存储的登录信息
return {
access_token: authInfo.token,
access_token_expire_time: new Date(authInfo.tokenExpiry).toISOString(),
@@ -497,36 +491,22 @@ class ApiManager {
dict_level: authInfo.dictLevel || 'PRIMARY'
}
} else {
console.log('Token已过期需要重新登录')
// 清理过期的token
this.clearAuthData()
}
} else {
console.log('未找到本地token或过期时间需要登录')
}
} else {
console.log('强制刷新token重新登录')
// 清理现有数据
this.clearAuthData()
}
// Token过期或不存在进行新的登录
console.log('发起新的登录请求')
// 创建新的登录Promise
this.loginPromise = this.login().then(
(result) => {
console.log('智能登录成功')
this.loginPromise = null // 清理Promise
this.loginPromise = null
return result
},
(error) => {
console.error('智能登录失败:', error)
this.loginPromise = null // 清理Promise
this.loginPromise = null
throw error
}
)
return this.loginPromise
}

View File

@@ -3,8 +3,8 @@ const envVersion = accountInfo?.miniProgram?.envVersion || 'release'
const BASE_URL_MAP: Record<string, string> = {
develop: 'http://localhost:8080',
trial: 'https://guzjwuna.prod.ihnm9taa.e13h9xq5.com',
release: 'https://guzjwuna.prod.ihnm9taa.e13h9xq5.com'
trial: 'http://guzjwuna.prod.ihnm9taa.e13h9xq5.com',
release: 'http://guzjwuna.prod.ihnm9taa.e13h9xq5.com'
}
export const ENV = envVersion