LoginView.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import React from 'react'
  2. import styled from 'styled-components'
  3. import { Form, Input, Button, message } from 'antd'
  4. import { UserOutlined, LockOutlined } from '@ant-design/icons'
  5. import { useNavigate } from 'react-router-dom'
  6. import { loginUser } from '../services'
  7. import Base64 from 'base-64'
  8. const LoginWrapper = styled.div`
  9. width:100%;
  10. height: 100vh;
  11. background-image: url(${require('../style/img/login_bg.jpeg')});
  12. background-size: cover;
  13. display:flex;
  14. justify-content: center;
  15. align-items: center;
  16. .form_wrapper {
  17. width: 600px;
  18. height: 640px;
  19. background: #FFFFFF;
  20. box-shadow: 0px 8px 32px 0px rgba(0,0,0,0.32);
  21. border-radius: 10px;
  22. }
  23. .welcome {
  24. justify-content: center;
  25. display: flex;
  26. font-size: 40px;
  27. font-family: PingFangSC-Semibold, PingFang SC;
  28. font-weight: 600;
  29. color: #333333;
  30. line-height: 56px;
  31. letter-spacing: 4px;
  32. margin-top:80px;
  33. }
  34. .line_wrapper {
  35. width:100%;
  36. justify-content: center;
  37. display: flex;
  38. }
  39. .line {
  40. width: 100px;
  41. height: 10px;
  42. border-bottom: 4px solid #4883fb;
  43. }
  44. .login_form {
  45. margin: 80px 70px 80px;
  46. }
  47. .form_item {
  48. margin-bottom:40px;
  49. }
  50. .form_input {
  51. height:52px;
  52. background: #FFFFFF;
  53. box-shadow: inset 2px 2px 12px 0px rgba(43,67,141,0.08);
  54. border-radius: 10px;
  55. }
  56. .btns {
  57. display:flex;
  58. justify-content: center;
  59. }
  60. .btn {
  61. width: 460px;
  62. height: 48px;
  63. background: #4883FB;
  64. box-shadow: 4px 4px 16px 0px rgba(1,71,170,0.24);
  65. border-radius: 9px;
  66. font-size: 16px;
  67. font-family: PingFangSC-Semibold, PingFang SC;
  68. font-weight: 600;
  69. color: #FFFFFF;
  70. line-height: 30px;
  71. letter-spacing: 2px;
  72. }
  73. .register {
  74. display: flex;
  75. justify-content: flex-end;
  76. color: #1890ff;
  77. margin: 30px;
  78. span {
  79. cursor: pointer;
  80. }
  81. }
  82. }`
  83. const FormItem = Form.Item
  84. const { Password } = Input
  85. const LoginView = () => {
  86. const navigate = useNavigate()
  87. const [loginForm] = Form.useForm()
  88. const login = () => {
  89. loginForm
  90. .validateFields()
  91. .then(async () => {
  92. const { account, password } = loginForm.getFieldValue()
  93. const { data } = await loginUser({
  94. username: account,
  95. password: Base64.encode(password),
  96. })
  97. if (data.code === 200) {
  98. sessionStorage.setItem('user_token', data.data.auth_token)
  99. sessionStorage.setItem('user_id', data.data.id)
  100. navigate('/datasource')
  101. } else {
  102. message.error(data.msg)
  103. }
  104. })
  105. .catch(err => {
  106. message.error('请检查表单数据是否完整')
  107. })
  108. }
  109. return (
  110. <LoginWrapper>
  111. <div className="form_wrapper">
  112. <div className="welcome">欢迎登陆</div>
  113. <div className="line_wrapper">
  114. <div className="line" />
  115. </div>
  116. <Form className="login_form" form={loginForm}>
  117. <FormItem
  118. name="account"
  119. className="form_item"
  120. rules={[
  121. {
  122. required: true,
  123. message: '请输入账号!',
  124. },
  125. ]}>
  126. <Input prefix={<UserOutlined />} className="form_input" />
  127. </FormItem>
  128. <FormItem
  129. name="password"
  130. rules={[
  131. {
  132. required: true,
  133. message: '请输入密码!',
  134. },
  135. ]}>
  136. <Password prefix={<LockOutlined />} className="form_input" />
  137. </FormItem>
  138. </Form>
  139. <div className="btns">
  140. <Button className="btn" type="primary" onClick={() => login()}>
  141. 登陆
  142. </Button>
  143. </div>
  144. <div className="register">
  145. <span
  146. onClick={() => {
  147. navigate('/register')
  148. }}>
  149. 前往注册
  150. </span>
  151. </div>
  152. </div>
  153. </LoginWrapper>
  154. )
  155. }
  156. export default LoginView