import React, {Component, PureComponent} from 'react';
import {SafeTextarea} from './Common';
import md5 from 'md5';
import './UserAction.css';
import {API_BASE} from './Common';
const LOGIN_BASE=window.location.protocol==='https:' ? '/login_proxy' : 'http://www.pkuhelper.com/services/login';
const MAX_IMG_PX=2000;
const MAX_IMG_FILESIZE=256000;
const ISOP_APPKEY='0feb3a8a831e11e8933a0050568508a5';
const ISOP_APPCODE='0fec960a831e11e8933a0050568508a5';
const ISOP_SVCID='PERSON_BASE_INFO,STUDENT_SCORE,STUDENT_COURSE_TABLE,STUDENT_COURSE_TABLE_ROOM,CARD_BALANCE';
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_sendcode() {
if(this.state.loading_status==='loading')
return;
let param=
'user='+this.username_ref.current.value+
'&svcId='+ISOP_SVCID+
'&appKey='+ISOP_APPKEY+
'×tamp='+(+new Date());
fetch(
'https://isop.pku.edu.cn/svcpub/svc/oauth/validcode?'+param+
'&msg='+md5(param+ISOP_APPCODE),
{mode: 'no-cors'}
);
alert('短信验证码应该会发到您的手机上,请注意查收!');
}
do_login(set_token) {
if(this.state.loading_status==='loading')
return;
this.setState({
loading_status: 'loading',
});
let data=new URLSearchParams();
data.append('username', this.username_ref.current.value);
data.append('valid_code', this.password_ref.current.value);
data.append('isnewloginflow', 'true');
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) {
if(json.msg) alert(json.msg);
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.error(e);
});
}
render() {
return (
您已登录。
Token: 登录后可以使用关注、回复等功能
登录请求会被发送到北大统一验证接口和 PKU Helper 服务器 {token.value||'(null)'}
请勿泄露 Token,它代表您的登录状态,与您的账户唯一对应且泄露后无法重置
我们不会记录或使用您的登录信息