import React, {Component, PureComponent} from 'react';
import copy from 'copy-to-clipboard';
import {SafeTextarea} from './Common';
import {API_VERSION_PARAM,PKUHELPER_ROOT,API} from './flows_api'
import md5 from 'md5';
import './UserAction.css';
import {API_BASE} from './Common';
const LOGIN_BASE=PKUHELPER_ROOT+'services/login';
const MAX_IMG_PX=2500;
const MAX_IMG_FILESIZE=300000;
export const ISOP_APPKEY='0feb3a8a831e11e8933a0050568508a5';
export const ISOP_APPCODE='0fec960a831e11e8933a0050568508a5';
export 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();
this.input_token_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=webhole', {
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.stringify(json));
}
set_token(json.user_token);
alert(`成功以 ${json.name} 的身份登录`);
this.setState({
loading_status: 'done',
});
})
.catch((e)=>{
alert('登录失败');
this.setState({
loading_status: 'done',
});
console.error(e);
});
});
}
do_input_token(set_token) {
if(this.state.loading_status==='loading')
return;
let token=this.input_token_ref.current.value;
this.setState({
loading_status: 'loading',
},()=>{
API.get_attention(token)
.then((_)=>{
this.setState({
loading_status: 'done',
});
set_token(token);
})
.catch((e)=>{
alert('Token检验失败');
this.setState({
loading_status: 'done',
});
console.error(e);
});
});
}
copy_token(token) {
if(copy(token))
alert('复制成功!\n请一定不要泄露哦');
}
render() {
return (
您已登录。
复制 User Token 登录后可以发树洞、回复、关注树洞
从其他设备导入登录状态
User Token 可用于迁移登录状态,请勿泄露,因为它与您的账户唯一对应且泄露后无法重置