"use client"; import React, { useState, useRef, useEffect, useMemo } from "react"; import { // Input, Button, Swiper, Tabs, Divider, Checkbox, Toast, Dialog, Popup, } from "antd-mobile"; import { useRouter } from "next/navigation"; import styles from "./index.module.scss"; import { JSEncrypt } from "jsencrypt"; import { handleLogin } from "@/store/actions"; import { saveUserInfo, get, save } from "@/utils/storeInfo"; import { connect } from "react-redux"; import { cryptoPassword } from "@/utils/crypto"; import requireAPI from "@/utils/requireAPI"; import { signIn, checkAuth } from "@/utils/auth"; import OwnInput from "@/components/OwnInput"; import OwnIcon from "@/components/OwnIcon"; /* params格式: { mid: item.mid, } */ const tabItems = [ { key: "veri_code", title: "验证码登录" }, { key: "password", title: "帐号密码登录" }, ]; function Login({ handleLogin }) { const [activeIndex, setActiveIndex] = useState(0); const [veriCode, setVeriCode] = useState(""); const [isCounting, setIsCounting] = useState(false); const [isLoading, setIsLoading] = useState(false); const [seconds, setSeconds] = useState(60); const [deviceType, setDeviceType] = useState(""); const [popVisible, setPopVisible] = useState(false); const [loginInfo, setLoginInfo] = useState({ mobilePhone: "", regionCode: "86", password: "", checked: false, }); const router = useRouter(); const swiperRef = useRef(null); const showMobal = useRef(); const [iframePageUrl, setIframePageUrl] = useState(null); useEffect(() => { const userAgent = window && window.navigator?.userAgent; //区分设备类型 if (/Android/i.test(userAgent)) { setDeviceType("Android"); } else if (/iPhone|iPad|iPod/i.test(userAgent)) { setDeviceType("ios"); } else { setDeviceType("pc"); } handleLogin({ isSignin: false, userToken: null }); checkAuth().then((res) => { const account = get("account"); if (res && account) { router.replace("/"); } }); }, []); 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 handleCheck = async (type) => { const { mobilePhone, password, checked } = loginInfo; if (!mobilePhone.match(/^1[3456789]\d{9}$/)) { Toast.show({ icon: "fail", content: "手机号码格式错误", position: "top", }); return; } if (type === "password") { if (password.length < 8) { Toast.show({ icon: "fail", content: "密码不得小于8位", position: "top", maskClassName: "z-20", }); return; } if (password.length > 15) { Toast.show({ icon: "fail", content: "密码不得大于15位", position: "top", }); return; } } else { if (veriCode.length !== 6) { Toast.show({ icon: "fail", content: "请输入正确的验证码", position: "top", }); return; } } //验证数据格式 if (!checked) { handleShowDialog(type); return; } handleSubmit(type); }; const handleSubmit = async (type) => { const { mobilePhone, password, regionCode, checked } = loginInfo; //对手机号进行RSA加密 const encrypt = new JSEncrypt(); encrypt.setPublicKey(process.env.NEXT_PUBLIC_RSA_KEY); const mobile_phone = encrypt.encrypt(mobilePhone); //MD5加密password const encryptedPassword = cryptoPassword(password); //发送登录请求 let body = { mobile_phone, region_code: regionCode, }; body = type === "password" ? { ...body, password: encryptedPassword, } : { ...body, veri_code: veriCode, }; setIsLoading(true); try { const data = await requireAPI( "POST", `/api/login/${ type === "password" ? "login_by_pswd" : "login_by_veri_code" }`, { body, } ); if (data.ret === -1) { setIsLoading(false); Toast.show({ icon: "fail", content: data.msg, position: "top", }); return; } //登录 saveUserInfo(data, mobilePhone, regionCode); signIn(data); handleLogin({ isSignin: true, userToken: data.data.token }); if (get("firstLogin") == null) { save("firstLogin", 1); } else { save("firstLogin", 0); } setIsLoading(false); router.push( !data?.data?.is_enabled && type != "password" ? "/my/setting/editPassword?is_enabled=" + data?.data?.is_enabled : "/" ); } catch (error) { setIsLoading(false); // console.error(error); } }; //点击获取验证码 const handleVerification = async () => { const { mobilePhone, regionCode } = loginInfo; //手机号校验 if (!mobilePhone.match(/^1[3456789]\d{9}$/)) { Toast.show({ icon: "fail", content: "手机号码格式错误", position: "top", }); return; } //开始倒计时 setIsCounting(true); //对手机号进行RSA加密 const encrypt = new JSEncrypt(); encrypt.setPublicKey(process.env.NEXT_PUBLIC_RSA_KEY); const mobile_phone = encrypt.encrypt(mobilePhone); //发送短信验证码 try { const data = await fetch(`/api/veri_code/send`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ mobile_phone, region_code: regionCode, }), }); if (data.ret === -1) { Toast.show({ icon: "fail", content: data.msg, position: "top", }); return; } } catch (error) { // console.error(error); } }; const handleShowDialog = (type) => { showMobal.current = Dialog.show({ title: "登录提示", content: (
为了更好保障你的合法权益,请阅读和同意 { setIframePageUrl( `/webView/${encodeURIComponent( `${process.env.NEXT_PUBLIC_WEB_URL}/doc/useragreement` )}` ); }} > 《用户协议》 { setIframePageUrl( `/webView/${encodeURIComponent( `${process.env.NEXT_PUBLIC_WEB_URL}/doc/privatypolicy` )}` ); }} > 《隐私政策》
), bodyStyle: { maxHeight: "none", width: "80vw", position: "fixed", top: "200px", left: "10vw", "--text-color": "#fff", color: "#fff", }, // cancelText:"确认", // confirmText:"取消", style: { "--text-color": "#fff", }, closeOnAction: true, actions: [ [ { key: "close", text: "取消", bold: true, style: { color: "#ffffff80" }, onClick: () => { showMobal.current?.close(); }, }, { key: "submit", text: "确认", style: { color: "#fff" }, onClick: () => handleSubmit(type), }, ], ], }); // if (result) { // Toast.show({ content: "点击了确认", position: "bottom" }); // } }; const iframePage = useMemo(() => { try { if (!iframePageUrl) return null; setPopVisible(true); return (