consumer模块完成度95%,安卓端大部分页面能正常获取数据,页面样式显示基本正常,逐渐完善;消费者端的积分、余额、评价、优惠券等小模块正在完善
This commit is contained in:
75
App.uvue
75
App.uvue
@@ -1,13 +1,88 @@
|
||||
<script lang="uts">
|
||||
import { setIsLoggedIn, setUserProfile, getCurrentUser } from '@/utils/store.uts'
|
||||
import supa from '@/components/supadb/aksupainstance.uts'
|
||||
|
||||
// 自动登录凭据(开发测试用)
|
||||
const AUTO_LOGIN_EMAIL = 'test@mall.com'
|
||||
const AUTO_LOGIN_PASSWORD = 'Hf2152111'
|
||||
|
||||
export default {
|
||||
onLaunch: function () {
|
||||
console.log('App Launch')
|
||||
|
||||
// 尝试自动登录并跳转(使用 Promise 链)
|
||||
this.tryAutoLogin()
|
||||
},
|
||||
onShow: function () {
|
||||
console.log('App Show')
|
||||
},
|
||||
onHide: function () {
|
||||
console.log('App Hide')
|
||||
},
|
||||
methods: {
|
||||
tryAutoLogin: function(): void {
|
||||
// 检查是否已有有效会话
|
||||
const session = supa.getSession()
|
||||
if (session.user != null) {
|
||||
console.log('已有有效会话,跳过自动登录')
|
||||
setIsLoggedIn(true)
|
||||
uni.reLaunch({ url: '/pages/mall/consumer/index' })
|
||||
return
|
||||
}
|
||||
|
||||
// 检查本地存储的登录状态
|
||||
const savedUserId = uni.getStorageSync('user_id')
|
||||
if (savedUserId != null && savedUserId != '') {
|
||||
console.log('本地存储中有用户ID,尝试恢复会话')
|
||||
getCurrentUser().then((profile) => {
|
||||
if (profile != null) {
|
||||
console.log('会话恢复成功')
|
||||
setIsLoggedIn(true)
|
||||
uni.reLaunch({ url: '/pages/mall/consumer/index' })
|
||||
return
|
||||
}
|
||||
// 恢复失败,执行自动登录
|
||||
this.doAutoLogin()
|
||||
}).catch(() => {
|
||||
console.log('会话恢复失败,尝试自动登录')
|
||||
this.doAutoLogin()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 执行自动登录
|
||||
this.doAutoLogin()
|
||||
},
|
||||
doAutoLogin: function(): void {
|
||||
console.log('开始自动登录...')
|
||||
supa.signIn(AUTO_LOGIN_EMAIL, AUTO_LOGIN_PASSWORD).then((result) => {
|
||||
if (result.user != null) {
|
||||
console.log('自动登录成功')
|
||||
setIsLoggedIn(true)
|
||||
|
||||
// 保存用户ID到本地存储
|
||||
const uid = result.user.getString('id')
|
||||
if (uid != null) {
|
||||
uni.setStorageSync('user_id', uid)
|
||||
console.log('用户ID已保存:', uid)
|
||||
}
|
||||
|
||||
// 获取用户资料
|
||||
getCurrentUser().then(() => {
|
||||
console.log('获取用户资料成功')
|
||||
}).catch((e) => {
|
||||
console.log('获取用户资料失败(忽略)')
|
||||
})
|
||||
|
||||
// 直接跳转到首页
|
||||
uni.reLaunch({ url: '/pages/mall/consumer/index' })
|
||||
} else {
|
||||
console.log('自动登录失败,用户需要手动登录')
|
||||
}
|
||||
}).catch((e) => {
|
||||
console.error('自动登录异常:', e)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user