"use client"; import React, { useState, useEffect } from "react"; import { Form, Input, Toast, Checkbox } from "antd-mobile"; import { JSEncrypt } from "jsencrypt"; import { useRouter } from "next/navigation"; import { signIn } from "@/utils/auth"; import Link from "next/link"; import baseRequest from "@/utils/baseRequest"; import { generateSignature } from "@/utils/crypto"; import { getCookie } from "cookies-next"; export default function PhonenumLogin() { const router = useRouter(); //保存区号、手机号、验证码 const [regionCode, setRegionCode] = useState("86"); const [mobilePhone, setMobilePhone] = useState(""); //重新获取验证码的计时器 const [isCounting, setIsCounting] = useState(false); const [seconds, setSeconds] = useState(60); useEffect(() => { let interval; if (isCounting && seconds > 0) { interval = setInterval(() => { setSeconds(seconds - 1); }, 1000); } else { setIsCounting(false); setSeconds(60); clearInterval(interval); } return () => { clearInterval(interval); }; }, [isCounting, seconds]); //点击获取验证码 const handleVerification = async () => { //手机号校验 if (!mobilePhone.match(/^1[3456789]\d{9}$/)) { Toast.show({ content: "手机号码格式错误", }); return; } //开始倒计时 setIsCounting(true); //对手机号进行RSA加密 const encrypt = new JSEncrypt(); encrypt.setPublicKey(process.env.NEXT_PUBLIC_RSA_KEY); const mobile_phone = encrypt.encrypt(mobilePhone); const base = baseRequest(); const signature = generateSignature({ mobile_phone: mobile_phone, region_code: regionCode, ...base, }); //发送短信验证码 try { await fetch(`/api/veri_code/send?signature=${signature}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ mobile_phone: mobile_phone, region_code: regionCode, ...base, }), }); } catch (error) { console.error(error); } }; //设置checkbox const [checked, setChecked] = useState(false); //点击登录 const handleSubmit = async (value) => { if (!checked) { Toast.show({ content: "请先勾选同意《用户协议》与《隐私政策》", }); return; } if (!value.mobile_phone) { Toast.show({ content: "请填写手机号", }); return; } if (!value.mobile_phone.match(/^1[3456789]\d{9}$/)) { Toast.show({ content: "手机号码格式错误", }); return; } if (!value.veri_code) { Toast.show({ content: "请先输入正确的验证码", }); return; } //读取邀请人 const inviter = getCookie("inviter"); //对手机号进行RSA加密 const encrypt = new JSEncrypt(); encrypt.setPublicKey(process.env.NEXT_PUBLIC_RSA_KEY); const mobile_phone = encrypt.encrypt(value.mobile_phone); const base = baseRequest(); const signature = generateSignature({ mobile_phone: mobile_phone, region_code: regionCode, veri_code: value.veri_code, inviter: parseInt(inviter, 10), ...base, }); //发送登录请求 try { const response = await fetch( `/api/login/login_by_veri_code?signature=${signature}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ mobile_phone: mobile_phone, region_code: regionCode, veri_code: value.veri_code, inviter: parseInt(inviter, 10), ...base, }), } ); const data = await response.json(); if (data.ret === -1) { Toast.show({ content: data.msg, }); return; } //若已经设置了密码则直接登录 if (data.data.is_enabled) { signIn(data); router.back(); return; } //若没设置密码则前往设置密码 signIn(data); const replacedString = mobile_phone.replace(/\+/g, "%2B"); router.replace( `../setpassword?mobile_phone=${replacedString}®ion_code=${regionCode}` ); } catch (error) { console.error(error); } }; return (

+{regionCode}

setMobilePhone(value)} />

{isCounting ? `(${seconds})重新发送` : "获取验证码"}

} >

验证码

setChecked(!checked)} >

同意 《用户协议》 、 《隐私政策》

); }