import React, {Component, PureComponent} from 'react'; import './UserAction.css'; const LOGIN_BASE=window.location.protocol==='https:' ? '/login_proxy' : 'http://www.pkuhelper.com/services/login'; export const TokenCtx=React.createContext({ value: null, set_value: ()=>{}, }); export class LoginForm extends Component { constructor(props) { super(props); this.state={ loading_status: 'done', }; this.username_ref=React.createRef(); this.password_ref=React.createRef(); } do_login(event,set_token) { event.preventDefault(); this.setState({ loading_status: 'loading', }); let data=new URLSearchParams(); data.append('uid', this.username_ref.current.value); data.append('password', this.password_ref.current.value); fetch(LOGIN_BASE+'/login.php?platform=hole_xmcp_ml', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: data, }) .then((res)=>res.json()) .then((json)=>{ if(json.code!==0) throw new Error(json); set_token(json.token); alert(`成功以 ${json.name} 的身份登录`); this.setState({ loading_status: 'done', }); }) .catch((e)=>{ alert('登录失败'); this.setState({ loading_status: 'done', }); console.trace(e); }); } render() { return ( {(token)=>
this.do_login(e,token.set_value)} className="box">

Token: {token.value||'(null)'}

{this.state.loading_status==='loading' ? : }

  • 我们不会记录您的密码和个人信息。
  • 请勿泄露 Token,它代表您的登录状态,与您的账户唯一对应且泄露后无法重置。
  • 如果您不愿输入密码,可以直接修改 localStorage['TOKEN']
}
) } }