Login.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React from 'react';
  2. import { Row, Col, Form, Input, Button, Card, message, Image } from 'antd';
  3. import { useSelector } from 'react-redux';
  4. import { RootState } from '../../states/store';
  5. import { SystemMode } from '../../states/systemModeSlice';
  6. import loginBrand from 'src/assets/imgs/login-brand.jpeg';
  7. import { useDispatch } from 'react-redux';
  8. import { login as loginApi } from '../../API/security/userActions';
  9. import { setUserInfo, type UserInfoState } from '../../states/user_info'; // 同时导入 setUserInfo 和类型 UserInfoState
  10. import handleEmergencyOperation from '../../domain/patient/handleEmergencyOperation';
  11. import { setSystemMode } from '../../states/systemModeSlice';
  12. const Login: React.FC = () => {
  13. const [form] = Form.useForm();
  14. const dispatch = useDispatch();
  15. const systemMode = useSelector((state: RootState) => state.systemMode.mode);
  16. const userInfo = useSelector((state: RootState) => state.userInfo);
  17. console.log(`========${systemMode}`);
  18. if (systemMode === SystemMode.Emergency) {
  19. return null;
  20. }
  21. //非急诊,但已经登录,不显示login
  22. if (systemMode === SystemMode.Normal && !!userInfo && userInfo.uid !== 0) {
  23. return null;
  24. }
  25. // 2. 登录请求函数
  26. const loginRequest = async (values: {
  27. username: string;
  28. password: string;
  29. }) => {
  30. const res = await loginApi(values.username, values.password);
  31. return res.data;
  32. };
  33. // 3. handleFinish 处理流程
  34. const handleFinish = async (values: {
  35. username: string;
  36. password: string;
  37. }) => {
  38. try {
  39. const result = await loginRequest(values);
  40. if (result.code === '0x000000') {
  41. // 4. 转换为 UserInfoState 类型
  42. const userInfo: UserInfoState = {
  43. token: result.data.token,
  44. expire: result.data.expire,
  45. uid: result.data.uid,
  46. name: result.data.name,
  47. avatar: result.data.avatar,
  48. };
  49. // 5. 分发 redux action
  50. dispatch(setUserInfo(userInfo));
  51. dispatch(setSystemMode(SystemMode.Normal));
  52. message.success('登录成功'); //todo 更详细的提示与异步过程
  53. } else {
  54. message.error(result.description || '登录失败');
  55. }
  56. } catch (e) {
  57. message.error(`网络错误: ${e instanceof Error ? e.message : '未知错误'}`);
  58. }
  59. };
  60. return (
  61. <Row
  62. className="fixed inset-0 z-50 bg-white bg-opacity-100 w-full h-full flex items-center justify-center"
  63. justify="center"
  64. align="middle"
  65. >
  66. <Col
  67. xs={24}
  68. sm={24}
  69. md={14}
  70. lg={12}
  71. xl={12}
  72. className="flex items-center justify-center h-full"
  73. >
  74. <Card className="max-w-md shadow-lg w-[480px] max-auto">
  75. <Form
  76. form={form}
  77. layout="vertical"
  78. onFinish={handleFinish}
  79. className="space-y-6 "
  80. >
  81. <Form.Item
  82. label="用户名"
  83. name="username"
  84. rules={[{ required: true, message: '请输入用户名' }]}
  85. >
  86. <Input size="large" placeholder="请输入用户名" />
  87. </Form.Item>
  88. <Form.Item
  89. label="密码"
  90. name="password"
  91. rules={[{ required: true, message: '请输入密码' }]}
  92. >
  93. <Input.Password size="large" placeholder="请输入密码" />
  94. </Form.Item>
  95. <Form.Item>
  96. <Row gutter={16}>
  97. <Col span={12}>
  98. <Button
  99. type="primary"
  100. htmlType="submit"
  101. size="large"
  102. className="w-full"
  103. >
  104. 登录
  105. </Button>
  106. </Col>
  107. <Col span={12}>
  108. <Button
  109. type="primary"
  110. size="large"
  111. className="w-full"
  112. onClick={handleEmergencyOperation}
  113. >
  114. 急诊
  115. </Button>
  116. </Col>
  117. </Row>
  118. </Form.Item>
  119. </Form>
  120. </Card>
  121. </Col>
  122. <Col
  123. xs={0}
  124. sm={0}
  125. md={10}
  126. lg={12}
  127. xl={12}
  128. className="h-full flex items-center justify-center"
  129. >
  130. {/* 右侧区域可用于展示插画或品牌信息,当前留空 */}
  131. <Image
  132. src={loginBrand}
  133. alt="Brand Logo"
  134. className="max-w-full h-auto"
  135. />
  136. </Col>
  137. </Row>
  138. );
  139. };
  140. export default Login;