import React, {Component, PureComponent} from 'react';
import copy from 'copy-to-clipboard';
import {API_BASE,SafeTextarea} from './Common';
import {MessageViewer} from './Message';
import {API_VERSION_PARAM,PKUHELPER_ROOT,API,get_json} from './flows_api';
import './UserAction.css';
const LOGIN_BASE=PKUHELPER_ROOT+'services/login';
const BASE64_RATE=4/3;
const MAX_IMG_DIAM=8000;
const MAX_IMG_PX=6000000;
const MAX_IMG_FILESIZE=450000*BASE64_RATE;
export const TokenCtx=React.createContext({
value: null,
set_value: ()=>{},
});
class ResetUsertokenWidget extends Component {
constructor(props) {
super(props);
this.state={
loading_status: 'done',
};
}
do_reset() {
if(window.confirm('您正在重置 UserToken!\n您的账号将会在【所有设备】上注销,您需要手动重新登录!')) {
let uid=window.prompt('您正在重置 UserToken!\n请输入您的学号以确认身份:');
if(uid)
this.setState({
loading_status: 'loading',
},()=>{
fetch(PKUHELPER_ROOT+'api_xmcp/hole/reset_usertoken', {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_token: this.props.token,
uid: uid,
}),
})
.then(get_json)
.then((json)=>{
if(json.error)
throw new Error(json.error);
else
alert('重置成功!您需要在所有设备上重新登录。');
this.setState({
loading_status: 'done',
});
})
.catch((e)=>{
alert('重置失败:'+e);
this.setState({
loading_status: 'done',
});
})
});
}
}
render() {
if(this.state.loading_status==='done')
return (重置);
else if(this.state.loading_status==='loading')
return ();
}
}
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(api_name) {
if(this.state.loading_status==='loading')
return;
this.setState({
loading_status: 'loading',
},()=>{
fetch(
PKUHELPER_ROOT+'api_xmcp/isop/'+api_name
+'?user='+encodeURIComponent(this.username_ref.current.value)
+API_VERSION_PARAM()
)
.then(get_json)
.then((json)=>{
console.log(json);
if(!json.success)
throw new Error(JSON.stringify(json));
alert(json.msg);
this.setState({
loading_status: 'done',
});
})
.catch((e)=>{
console.error(e);
alert('发送失败。'+e);
this.setState({
loading_status: 'done',
});
});
});
}
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'+API_VERSION_PARAM(), {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: data,
})
.then(get_json)
.then((json)=>{
if(json.code!==0) {
if(json.msg) alert(json.msg);
throw new Error(JSON.stringify(json));
}
let freshman_welcome=json.uid.indexOf('19')===0 && (+new Date())<1567958400000; // 2019-09-09 0:00 GMT+8
set_token(json.user_token);
alert(`成功以 ${json.name} 的身份登录`+(freshman_welcome ? '\n欢迎来到北京大学!' : ''));
this.setState({
loading_status: 'done',
});
})
.catch((e)=>{
console.error(e);
alert('登录失败');
this.setState({
loading_status: 'done',
});
});
});
}
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检验失败\n'+e);
this.setState({
loading_status: 'done',
});
console.error(e);
});
});
}
copy_token(token) {
if(copy(token))
alert('复制成功!\n请一定不要泄露哦');
}
render() {
return (
您已登录。
根据计算中心要求,访问授权三个月内有效。
{this.props.show_sidebar(
'系统消息',
复制 User Token 接收验证码来登录 PKU Helper
this.do_sendcode('validcode')}>
短信
/
this.do_sendcode('validcode2mail')}>
邮件
从其他设备导入登录状态
若提示“授权过期”,请注销后重新登录。
当您发送的内容违规时,我们将用系统消息提示您
User Token 用于迁移登录状态,切勿告知他人,若怀疑被盗号请尽快