import { useState } from 'react'; import { View, Text, Input, Button } from '@tarojs/components'; import Taro from '@tarojs/taro'; import { safeNavigateTo } from '@/utils/navigate'; import { useAuthStore } from '../../stores/auth'; import './index.scss'; const IS_DEV = process.env.NODE_ENV !== 'production'; const IS_SIMULATOR = typeof __wxConfig !== 'undefined' && (__wxConfig as any).envVersion !== 'release'; export default function Login() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [agreed, setAgreed] = useState(false); const [needBind, setNeedBind] = useState(false); const credentialLogin = useAuthStore((s) => s.credentialLogin); const login = useAuthStore((s) => s.login); const bindPhone = useAuthStore((s) => s.bindPhone); const loading = useAuthStore((s) => s.loading); const isMedicalStaff = useAuthStore((s) => s.isMedicalStaff); const navigateAfterLogin = () => { if (isMedicalStaff()) { // 使用 redirectTo 替代 reLaunch 避免分包加载超时 // redirectTo 只替换当前页面,不销毁整个页栈,分包预加载不会被中断 Taro.redirectTo({ url: '/pages/pkg-doctor-core/index', fail: () => { // fallback: 先跳首页再 redirectTo Taro.switchTab({ url: '/pages/index/index', success: () => { setTimeout(() => { Taro.navigateTo({ url: '/pages/pkg-doctor-core/index' }); }, 500); }, }); }, }); } else { Taro.switchTab({ url: '/pages/index/index' }); } }; const handleCredentialLogin = async () => { if (!username.trim()) { Taro.showToast({ title: '请输入账号', icon: 'none' }); return; } if (!password.trim()) { Taro.showToast({ title: '请输入密码', icon: 'none' }); return; } if (!agreed) { Taro.showToast({ title: '请先阅读并同意用户协议', icon: 'none' }); return; } try { const success = await credentialLogin(username.trim(), password); if (success) { navigateAfterLogin(); } else { Taro.showToast({ title: '账号或密码错误', icon: 'none' }); } } catch { Taro.showToast({ title: '登录失败,请重试', icon: 'none' }); } }; const handleWechatLogin = async () => { if (!agreed) { Taro.showToast({ title: '请先阅读并同意用户协议', icon: 'none' }); return; } try { const { code } = await Taro.login(); const result = await login(code); if (result) { navigateAfterLogin(); } else { setNeedBind(true); Taro.showToast({ title: '请授权手机号完成绑定', icon: 'none' }); } } catch (err: any) { const msg = err?.message || '登录失败,请重试'; Taro.showToast({ title: msg.substring(0, 20), icon: 'none', duration: 3000 }); } }; const handleDevQuickLogin = async () => { try { const success = await credentialLogin('admin', 'Admin@2026'); if (success) { navigateAfterLogin(); } } catch (err: any) { Taro.showToast({ title: err?.message || '登录失败', icon: 'none' }); } }; const handleGetPhone = async (e: { detail: { errMsg: string; encryptedData: string; iv: string } }) => { if (e.detail.errMsg !== 'getPhoneNumber:ok') { Taro.showToast({ title: '需要授权手机号', icon: 'none' }); return; } const { encryptedData, iv } = e.detail; try { const success = await bindPhone(encryptedData, iv); if (success) { navigateAfterLogin(); } } catch (err: any) { Taro.showModal({ title: '绑定手机号失败', content: err?.message || '绑定失败', confirmText: '重新登录', cancelText: '取消', success: (res) => { if (res.confirm) setNeedBind(false); }, }); } }; return ( {/* 品牌区 */} H HMS 健康 您的专属健康管家 {!needBind ? ( <> {/* 账号输入 */} setUsername(e.detail.value)} /> {/* 密码输入 */} setPassword(e.detail.value)} /> setShowPassword(!showPassword)} > {showPassword ? '隐藏' : '显示'} {/* 登录按钮 */} {loading ? '登录中...' : '登录'} {/* 分隔线 */} {/* 微信一键登录 */} 微信一键登录 ) : ( )} {/* 协议 */} setAgreed(!agreed)} > {agreed && } 登录即同意 safeNavigateTo('/pages/legal/user-agreement')}>《用户协议》 safeNavigateTo('/pages/legal/privacy-policy')}>《隐私政策》 {/* 开发模式 */} {(IS_DEV || IS_SIMULATOR) && ( 开发模式快速登录 › )} ); }