import { Box, Button, CssBaseline, Grid, Paper, TextField, Link, ThemeProvider, Typography, Alert, Collapse, Checkbox, FormGroup, FormControlLabel, Dialog, DialogTitle, DialogContentText, DialogActions, DialogContent } from "@mui/material" import { useNavigate } from "react-router-dom" import { api, theme } from '../index' import axios from "axios" import { useRef, useState } from "react" import "./custom.css" export const PageRegister = () => { const navigate = useNavigate() const usernameInput = useRef(null) const passwordInput = useRef(null) const emailInput = useRef(null) const invitationCodeInput = useRef(null) const validationCodeInput = useRef(null) const [useMojangSkin, setUseMojangSkin] = useState(false) const [disableBtn, setDisableBtn] = useState(false) const [cors, setCors] = useState(false) const [illegal, setIllegal] = useState(false) const [illegalMessgae, setIllegalMessgae] = useState("") const handleRegister = () => { setDisableBtn(true); (async () => { const username = usernameInput.current?.value const password = passwordInput.current?.value const email = emailInput.current?.value const invitationCode = invitationCodeInput.current?.value const validationCode = validationCodeInput.current?.value if(!username || !password) { setIllegalMessgae("请将以下内容全部输入") setIllegal(true) } let textureMigrations = undefined if(useMojangSkin) { try { const response = await axios.get("https://api.mojang.com/users/profiles/minecraft/" + username) const profile = await axios.get("https://sessionserver.mojang.com/session/minecraft/profile/" + response.data.id) profile.data.properties.forEach((prop: any) => { if(prop.name === "textures") { const texture = JSON.parse(atob(prop.value)).textures if(texture.SKIN || texture.CAPE) { textureMigrations = { skin: texture.SKIN?.url ?? undefined, cape: texture.CAPE?.url ?? undefined, } } } }) } catch(err: any) { if(err.code === "ERR_NETWORK") { setCors(true) return } setIllegalMessgae(`无法同步正版皮肤,可能是用户不存在`) setIllegal(true) } } console.log(textureMigrations) try { const response = await axios.post(api("/api/register"), { username, password, email, invitationCode, validationCode, textureMigrations }) if(response.data.err !== 1.048596) { setIllegalMessgae(`注册失败! 错误代码 ${response.data.err} 原因: ${response.data.msg}`) setIllegal(true) } else { navigate("/") } } catch (err: any) { if(err.code === "ERR_NETWORK") { setCors(true) return } setIllegalMessgae(`注册失败! 错误代码 ${err.code} 原因: ${err.message}`) setIllegal(true) } })().finally(() => { setDisableBtn(false); }) } //TODO return {"无法发送请求!"} 由于您的浏览器有 CORS 限制导致页面无法向 Mojang API/LSP-Yggdrasil API 发送请求。您可以选择 点我安装绕过CORS Chrome浏览器插件。或者添加 Chrome 启动参数: --disable-web-security 来解决。
注册账户 {illegalMessgae} setIllegal(false)} /> setIllegal(false)} /> setIllegal(false)} /> setIllegal(false)} /> setIllegal(false)} /> {setUseMojangSkin(!useMojangSkin)}} />}/>
}