feat(小程序): 修复运行时与登录链路
This commit is contained in:
@@ -1,101 +1,77 @@
|
||||
import { View, Text, Button } from '@tarojs/components'
|
||||
import { useEffect } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import Taro from '@tarojs/taro'
|
||||
import { wechatLogin } from '@/services/auth'
|
||||
import { getToken, wechatLogin } from '@/services/auth'
|
||||
import './index.scss'
|
||||
|
||||
export default function Login() {
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
console.log('[Login] Page mounted, checking token...')
|
||||
const token = Taro.getStorageSync('token')
|
||||
console.log('[Login] Token exists:', !!token)
|
||||
if (token) {
|
||||
console.log('[Login] Already logged in, redirecting to device page')
|
||||
if (getToken()) {
|
||||
Taro.reLaunch({ url: '/pages/device/index' })
|
||||
}
|
||||
}, [])
|
||||
|
||||
const doWeChatLogin = async () => {
|
||||
try {
|
||||
console.log('[Login] Starting WeChat login...')
|
||||
Taro.showLoading({ title: '登录中...' })
|
||||
const handleLogin = async () => {
|
||||
if (submitting) return
|
||||
|
||||
// 1. 调用微信登录获取 code
|
||||
console.log('[Login] Step 1: Calling Taro.login()...')
|
||||
setSubmitting(true)
|
||||
Taro.showLoading({ title: '登录中...' })
|
||||
|
||||
try {
|
||||
const loginRes = await Taro.login()
|
||||
const code = loginRes.code
|
||||
console.log('[Login] Got code:', code ? 'yes' : 'no')
|
||||
if (!code) {
|
||||
if (!loginRes.code) {
|
||||
throw new Error('未获取到微信登录凭证')
|
||||
}
|
||||
|
||||
// 2. 获取用户头像信息 (可选)
|
||||
let userInfo = null
|
||||
let userInfo:
|
||||
| {
|
||||
nickName?: string
|
||||
avatarUrl?: string
|
||||
}
|
||||
| null = null
|
||||
|
||||
try {
|
||||
console.log('[Login] Step 2: Calling Taro.getUserProfile()...')
|
||||
const profileRes = await Taro.getUserProfile({
|
||||
desc: '用于完善用户资料'
|
||||
desc: '用于完善家长资料展示',
|
||||
})
|
||||
userInfo = profileRes.userInfo
|
||||
console.log('[Login] Got userInfo:', !!userInfo)
|
||||
} catch (e) {
|
||||
console.log('[Login] getUserProfile failed (expected if user denied):', e)
|
||||
userInfo = profileRes.userInfo || null
|
||||
} catch (error) {
|
||||
console.log('[login] getUserProfile skipped:', error)
|
||||
}
|
||||
|
||||
// 3. 调用后端登录接口 (微信登录)
|
||||
console.log('[Login] Step 3: Calling backend /auth/login...')
|
||||
const res = await wechatLogin({
|
||||
code,
|
||||
await wechatLogin({
|
||||
code: loginRes.code,
|
||||
nickname: userInfo?.nickName,
|
||||
avatar_url: userInfo?.avatarUrl
|
||||
avatar_url: userInfo?.avatarUrl,
|
||||
})
|
||||
console.log('[Login] Backend login success, user_id:', res.user_id)
|
||||
|
||||
// 4. 保存用户信息用于显示
|
||||
console.log('[Login] Step 4: Saving user info...')
|
||||
if (userInfo) {
|
||||
Taro.setStorageSync('userInfo', {
|
||||
nickname: userInfo.nickName,
|
||||
avatar_url: userInfo.avatarUrl
|
||||
avatar_url: userInfo.avatarUrl,
|
||||
})
|
||||
} else {
|
||||
Taro.removeStorageSync('userInfo')
|
||||
}
|
||||
|
||||
Taro.hideLoading()
|
||||
console.log('[Login] Step 5: Login complete, showing success toast')
|
||||
Taro.showToast({ title: '登录成功', icon: 'success' })
|
||||
|
||||
// 6. 跳转到首页
|
||||
console.log('[Login] Redirecting to /pages/device/index...')
|
||||
setTimeout(() => {
|
||||
Taro.reLaunch({ url: '/pages/device/index' })
|
||||
}, 500)
|
||||
|
||||
} catch (err: any) {
|
||||
}, 300)
|
||||
} catch (error: any) {
|
||||
console.error('[login] failed:', error)
|
||||
Taro.showToast({
|
||||
title: error?.message || '登录失败,请重试',
|
||||
icon: 'none',
|
||||
})
|
||||
} finally {
|
||||
Taro.hideLoading()
|
||||
console.error('[Login] Login failed:', err)
|
||||
|
||||
// 401 通常表示微信 code 失效或已被使用,提示用户重试登录
|
||||
if (err.status === 401) {
|
||||
Taro.showToast({
|
||||
title: '登录失败,请重试',
|
||||
icon: 'none'
|
||||
})
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title: err.message || '网络错误,请重试',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
setSubmitting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleLogin = () => {
|
||||
console.log('Button clicked, starting login...')
|
||||
doWeChatLogin()
|
||||
}
|
||||
|
||||
return (
|
||||
<View className='login-page'>
|
||||
<View className='login-header'>
|
||||
@@ -123,12 +99,9 @@ export default function Login() {
|
||||
</View>
|
||||
|
||||
<View className='login-btn-wrapper'>
|
||||
<Button
|
||||
className='login-btn'
|
||||
onClick={handleLogin}
|
||||
>
|
||||
<Button className='login-btn' loading={submitting} disabled={submitting} onClick={handleLogin}>
|
||||
<Text className='btn-icon'>🌐</Text>
|
||||
<Text className='btn-text'>微信一键登录</Text>
|
||||
<Text className='btn-text'>{submitting ? '登录中...' : '微信一键登录'}</Text>
|
||||
</Button>
|
||||
</View>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user