1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| import React, { useState, useEffect, useContext } from 'react' import UserContext from './UserContext'
const Login = (props) => { const [email, setEmail] = useState('123@qq.com') const [password, setPassword] = useState('123a3442') let userContext = useContext(UserContext) ///////在这里传入
useEffect(() => { document.title = '登录' })
let [dirty, setDirty] = useState({ email: false, password: false, })
const [errors, setErrors] = useState({ email: [], password: [], }) const [message, setMessage] = useState('')
const validate = () => { const errorsData = {} errorsData.email = [] if (!email) { errorsData.email.push('邮箱不能为空') } let emailReg = /[^@ \t\r\n]+@[^@ \t\r\n]+\.[^@ \t\r\n]+/ if (email) { if (!emailReg.test(email)) { errorsData.email.push('邮箱输入格式有误') } }
errorsData.password = [] if (!password) { errorsData.password.push('密码不能为空') } let passwordReg = /^(?=.*?[a-z])(?=.*?[0-9]).{8,16}$/ if (password) { if (!passwordReg.test(password)) { errorsData.password.push('密码输入格式有误') } } setErrors(errorsData) } useEffect(validate, [password, email, setErrors])
let onClickLogin = async () => { let dirtyData = dirty Object.keys(dirtyData).forEach((control) => { dirtyData[control] = true }) setDirty(dirtyData) validate() if (isValid()) { let response = await fetch( `http://localhost:5000/users?email=${email}&password=${password}`, { method: 'GET' } ) if (response.ok) { let body = await response.json() if (body.length > 0) { setMessage(<span className="text-success">登录成功</span>) userContext.setUser({ ...userContext.user, isLoggin: true, currentUserName: body[0].fullName, currentUserId: body[0].id, currentUserRole: body[0].role, }) if (body[0].role === 'user') { props.history.replace('/dashboard') } else if(body[0].role==="admin") { props.history.replace('./products') } } else { setMessage(<span className="text-danger">无效登录,请重新尝试</span>) } } } }
let isValid = () => { let valid = true for (let control in errors) { if (errors[control].length > 0) { valid = false } } return valid } return ( <div className="row"> <div className="col-lg-5 col-md-7 mx-auto"> <div className="card boder-success shadow-lg my-2"> <div className="card-header border-bottom border-success"> <h4 style={{ fontSize: '40px' }} className="text-success"> Login </h4> </div>
<div className="card-body boder-bottom boder-success"> <div className="form-group"> <label htmlFor="email">Email</label> <input value={email} type="text" className="form-control" id="email" placeholder="Email" onChange={(e) => { setEmail(e.target.value) }} onBlur={() => { setDirty({ ...dirty, email: true }) validate() }} /> <div className="text-danger"> {dirty.email && errors.email.length > 0 ? errors.email : ''} </div> </div>
<div className="form-group"> <label htmlFor="password">Password</label> <input type="password" className="form-control" id="password" placeholder="Password" value={password} onChange={(e) => { setPassword(e.target.value) }} onBlur={() => { setDirty({ ...dirty, password: true }) validate() }} /> <div className="text-danger"> {dirty.password && errors.password.length > 0 ? errors.password : ''} </div> </div> <div className="card-footer text-right"> <div className="">{message}</div> <div> <button className="btn btn-primary mycolor" onClick={onClickLogin} > 登录 </button> </div> </div> </div> </div> </div> </div> ) }
export default Login
|