}else if(userName == "chenxuehui" && password == "123321"){
res.json({"code":100,"verson":true,"msg":"登陆成功","token":token});
}else{
res.json({"code":0,"verson":false,"msg":"密码错误"});
}
}
}
module.exports = Login
captcha方法是生成一张含四位数字的图片,然后将图片保存到session中。
将此方法在 router/index.js 中引用
router.get('/captcha', login.captcha);
也就是说我们访问localhost:3000/captcha就会返回一张图片。
有了这个连接后我们就可以通过图片的src属性拿到该图片,但是当点击图片的时候要重新刷新,所以我们需要给图片添加一
个点击刷新事件。将下面代码插入到login.tsx中
setHash() {
this.setState({
hash : Math.random()
})
}
img标签也变成了
代码如下:
<img src={“http://localhost:3000/captcha?aaa=”+this.state.hash} className=”yz-img” onClick={this.setHash.bind(this)} />
此时login.tsx全部代码:
import * as React from 'react'
import * as ReactDom from 'react-dom'
import {Link, browserHistory} from 'react-router';
import * as axios from 'axios';
export default class Login extends React.Component<any,any>{
constructor(props){
super(props)
this.state = {
userName : '',
password : '',
yzNoId : '',
hash : Math.random()
}
}
public async sbumit(params : any) : Promise<any>{
let res = await axios.post('http://localhost:3000/login',params);
}
handleUserName(e) : any {
this.setState({
userName : e.target.value
})
}
handlePassword(e) : any {
this.setState({
password : e.target.value
})
}
handleYzId(e) : any {
this.setState({
yzNoId : e.target.value
})
}
setHash() {
this.setState({
hash : Math.random()
})
}
render(){