import React, { Component } from 'react'
import { Form, Input, Button, Row, Col } from 'antd';
import { UserOutlined, UnlockOutlined, DingtalkOutlined } from '@ant-design/icons';
import util from '../../util/util.js';
import './index.less';
export class index extends Component {
constructor(props) {
super(props)
this.state = {
codeList: 0,
loginBut: true
}
}
onFinish = (values) => {
console.log('Success:', values.Codelist);
console.log('this.state.codeList', this.state.codeList[0].code + this.state.codeList[1].code + this.state.codeList[2].code + this.state.codeList[3].code);
// values.CodeList = this.state.codeList
if (values.Codelist === this.state.codeList[0].code + this.state.codeList[1].code + this.state.codeList[2].code + this.state.codeList[3].code) {
// alert('ok')
util.httpRequest({
url: `${util.API}/login/json`,
method: 'POST',
data: {
username: values.username,
password: values.password,
},
})
.then(res => {
if (res) {
window.location.hash = `${util.ROUTER}/myData`
}
})
}
}
// 【生命周期】 UNSAFE_componentWillMount 在render之前調(diào)用
UNSAFE_componentWillMount() {
this.VerificationCode()
}
// 驗(yàn)證碼
VerificationCode = () => {
const codeList = []
// const chars = 'abcdefhijkmnprstwxyz0123456789'
const chars = '0123456789'
const charsLen = chars.length // 數(shù)字-10 扔水、 字母+數(shù)字-30
console.log('chars', chars.charAt(8));
console.log('0-1隨機(jī)數(shù)', Math.random()) // 0-1 之間的隨機(jī)數(shù) 0.3171147866516528
console.log('乘以10倍的隨機(jī)數(shù)', Math.random() * charsLen) // 0-1 隨機(jī)數(shù)*10倍 3.171147866516528
console.log('測(cè)試Math.floor => 5.9', Math.floor(5.9)) // 向下取整數(shù)
console.log('Math.floor', Math.floor(Math.random() * charsLen)) // 0-10 隨機(jī)整數(shù)
// 生成
for (let i = 0; i < 4; i++) {
const rgb = [Math.round(Math.random() * 220), Math.round(Math.random() * 240), Math.round(Math.random() * 200)]
codeList.push({
code: chars.charAt(Math.floor(Math.random() * charsLen)),
color: `rgb(${rgb})`,
// fontSize: '20px',
// padding: '6px'
})
}
console.log('4個(gè)隨機(jī)數(shù)', codeList)
this.setState({
codeList: codeList
})
}
getStyle(data) {
return `color: ${data.color}; font-size: ${data.fontSize}; padding: ${data.padding}; transform: ${data.transform}`
}
refreshCode = () => {
this.VerificationCode()
}
// 驗(yàn)證碼校驗(yàn)
Codelist = (rule, value) => {
console.log('驗(yàn)證碼校驗(yàn)', value);
if (value === this.state.codeList[0].code + this.state.codeList[1].code + this.state.codeList[2].code + this.state.codeList[3].code) {
this.setState({
loginBut: false
})
return Promise.resolve();
}
// this.state.codeList[0].code + this.state.codeList[1].code + this.state.codeList[2].code + this.state.codeList[3].code
// const enPattern = /^[0-9a-zA-Z_]*$/ // 求只能輸入英文字母和數(shù)字的正則表達(dá)式
// if (enPattern.test(value)) {
// return Promise.resolve();
// }
return Promise.reject('驗(yàn)證碼錯(cuò)誤')
}
render() {
return (
<div className="loginWrap">
<h1>深度云三期</h1>
<Form
name="basic"
initialValues={{ remember: true }}
onFinish={this.onFinish}
style={{ width: '66%' }}
>
<Form.Item name="username" >
<Input ref='username'
placeholder="賬號(hào)"
prefix={<UserOutlined className="site-form-item-icon" />}
suffix="賬號(hào)"
/>
</Form.Item>
<Form.Item name="password" >
<Input.Password ref='password'
placeholder="密碼"
prefix={<UnlockOutlined className="site-form-item-icon" />}
/>
</Form.Item>
<Row>
<Col span={15}>
<Form.Item name="Codelist" rules={[{ required: true, validator: this.Codelist }]}>
<Input ref="Codelist"
width="200"
placeholder="驗(yàn)證碼"
prefix={<DingtalkOutlined className="site-form-item-icon" />}
suffix="驗(yàn)證碼"
/>
</Form.Item>
</Col>
<Col span={1}></Col>
<Col span={8}>
<div onClick={this.refreshCode} style={{background: '#ddd'}}>
{
this.state.codeList.map((item, index) => {
return (
<span key={index} style={{ color: item.color, fontSize: '20px', padding: '0 6px' }}>{item.code}</span>
)
})
}
</div>
</Col>
</Row>
<Form.Item >
<Button disabled={this.state.loginBut} type="primary" htmlType="submit" style={{width: '100%'}}>登陸</Button>
</Form.Item>
</Form>
</div>
)
}
}
export default index
react登陸時(shí)隨機(jī)驗(yàn)證碼
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門涤垫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人竟终,你說我怎么就攤上這事蝠猬。” “怎么了衡楞?”我有些...
- 文/不壞的土叔 我叫張陵吱雏,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我瘾境,道長(zhǎng)歧杏,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任迷守,我火速辦了婚禮犬绒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘兑凿。我一直安慰自己凯力,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布礼华。 她就那樣靜靜地躺著咐鹤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪圣絮。 梳的紋絲不亂的頭發(fā)上祈惶,一...
- 文/蒼蘭香墨 我猛地睜開眼力麸,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼可款!你這毒婦竟也來了育韩?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對(duì)情侶失蹤筑舅,失蹤者是張志新(化名)和其女友劉穎座慰,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體翠拣,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡版仔,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了误墓。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蛮粮。...
- 正文 年R本政府宣布变泄,位于F島的核電站,受9級(jí)特大地震影響恼琼,放射性物質(zhì)發(fā)生泄漏妨蛹。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一晴竞、第九天 我趴在偏房一處隱蔽的房頂上張望蛙卤。 院中可真熱鬧,春花似錦噩死、人聲如沸颤难。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽行嗤。三九已至,卻和暖如春垛耳,著一層夾襖步出監(jiān)牢的瞬間昂验,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓泡嘴,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親逆济。 傳聞我的和親對(duì)象是個(gè)殘疾皇子酌予,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 歡迎留言、轉(zhuǎn)發(fā) 微信極速開發(fā)系列文章(微信支付建椰、授權(quán)獲取用戶信息等):點(diǎn)擊這里 最近很多人問 開源項(xiàng)目中的截圖如何...
- 我是黑夜里大雨紛飛的人啊 1 “又到一年六月雕欺,有人笑有人哭,有人歡樂有人憂愁棉姐,有人驚喜有人失落屠列,有的覺得收獲滿滿有...
- 步驟:發(fā)微博01-導(dǎo)航欄內(nèi)容 -> 發(fā)微博02-自定義TextView -> 發(fā)微博03-完善TextView和...