import {useEffect, useState} from "react"; import Taro from '@tarojs/taro' import {Input, Radio, Button} from '@nutui/nutui-react-taro' import {loginBySms, getCaptcha, sendSmsCaptcha} from '@/api/passport/login' const Login = () => { const [isAgree, setIsAgree] = useState(false) // 只保留短信登录方式 const [loginType, setLoginType] = useState('sms') // const [username, setUsername] = useState('') // const [password, setPassword] = useState('') const [phone, setPhone] = useState('') const [smsCode, setSmsCode] = useState('') const [captchaImg, setCaptchaImg] = useState('') const [captchaCode, setCaptchaCode] = useState('') const [showCaptchaModal, setShowCaptchaModal] = useState(false) const [countdown, setCountdown] = useState(0) // 短信验证码倒计时 const [loading, setLoading] = useState(false) const reload = () => { Taro.hideTabBar() } // 获取图形验证码 const fetchCaptcha = async () => { try { const res = await getCaptcha() setCaptchaImg(res.base64) } catch (error) { Taro.showToast({ title: '获取验证码失败', icon: 'error' }) } } // 发送短信验证码 // const handleSendSmsCode = async () => { // if (!phone) { // Taro.showToast({ // title: '请输入手机号', // icon: 'error' // }) // return // } // // // 验证手机号格式 // const phoneReg = /^1[3-9]\d{9}$/ // if (!phoneReg.test(phone)) { // Taro.showToast({ // title: '手机号格式不正确', // icon: 'error' // }) // return // } // // // 显示图形验证码弹窗 // fetchCaptcha() // setShowCaptchaModal(true) // } // 确认发送短信验证码 const confirmSendSmsCode = async () => { if (!captchaCode) { Taro.showToast({ title: '请输入图形验证码', icon: 'error' }) return } try { setLoading(true) // 发送短信验证码时需要传入手机号和图形验证码 await sendSmsCaptcha({ phone, code: captchaCode }) Taro.showToast({ title: '短信验证码已发送', icon: 'success' }) setShowCaptchaModal(false) setCaptchaCode('') // 开始倒计时 setCountdown(60) } catch (error) { Taro.showToast({ title: error.message || '发送失败', icon: 'error' }) } finally { setLoading(false) } } // 短信验证码登录 const handleSmsLogin = async () => { if (!phone) { Taro.showToast({ title: '请输入账号', icon: 'error' }) return } // 验证手机号格式 // const phoneReg = /^1[3-9]\d{9}$/ // if (!phoneReg.test(phone)) { // Taro.showToast({ // title: '手机号格式不正确', // icon: 'error' // }) // return // } if (!smsCode) { Taro.showToast({ title: '请输入短信验证码', icon: 'error' }) return } try { setLoading(true) // 短信登录时传入手机号和短信验证码 const res = await loginBySms({ phone, code:smsCode }) console.log(res,'.......') Taro.showToast({ title: '登录成功', icon: 'success' }) // 跳转到首页 setTimeout(() => { Taro.switchTab({ url: '/pages/index/index' }) }, 1500) } catch (error) { Taro.showToast({ title: error.message || '登录失败', icon: 'error' }) } finally { setLoading(false) } } // 登录处理 const onLogin = async () => { if (!isAgree) { Taro.showToast({ title: '请先同意服务协议', icon: 'error' }) return } handleSmsLogin() } // 倒计时处理 useEffect(() => { let timer: any if (countdown > 0) { timer = setTimeout(() => { setCountdown(countdown - 1) }, 1000) } return () => clearTimeout(timer) }, [countdown]) useEffect(() => { reload() }, []) return ( <>
管理员登录
{/* 登录方式切换 - 隐藏账号登录 */}
setLoginType('account')} > 账号登录
setLoginType('sms')} > 短信登录
{/* 短信验证码登录 - 始终显示 */}
setPhone(val)} style={{ backgroundColor: '#ffffff', borderRadius: '8px', width: '100%', padding: '10px' }} />
setSmsCode(val)} style={{ flex: 1, border: 'none', padding: '10px' }} />
setIsAgree(!isAgree)} /> setIsAgree(!isAgree)}>已阅读并同意 Taro.navigateTo({url: '/passport/agreement'})} style={{color: '#1890ff'}} > 《服务协议及隐私政策》
{/* 图形验证码弹窗 */} {showCaptchaModal && (
请输入图形验证码
{captchaImg && ( 验证码 )}
setCaptchaCode(val)} style={{ marginBottom: '15px' }} />
)} ) } export default Login