Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.tsx 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { useState, useEffect, useRef } from 'react';
  2. import { history, useModel } from '@umijs/max';
  3. import type { CaptFieldRef } from '@ant-design/pro-components';
  4. import { LockOutlined, UserOutlined, FileImageOutlined, } from '@ant-design/icons';
  5. import { LoginForm, ProFormCaptcha, ProForm, ProFormText, ProFormDependency, } from '@ant-design/pro-components';
  6. import { message, Row, Col, Image, ConfigProvider } from 'antd';
  7. import type { CSSProperties } from 'react';
  8. import { Login, PostSmsSend, GetCaptcha, GetUserMenulist } from '@/apis/api';
  9. const contentStyle: CSSProperties = {
  10. display: 'flex',
  11. alignItems: 'center',
  12. justifyContent: 'center',
  13. paddingTop: '200px'
  14. };
  15. function flattenPermissions(permissions: { id: number; action: string; childs?: { id: number; action: string }[] }[]): string[] {
  16. const flattenedPermissions: string[] = [];
  17. permissions.forEach(permission => {
  18. flattenedPermissions.push(permission.action);
  19. if (permission.childs) {
  20. flattenedPermissions.push(...flattenPermissions(permission.childs));
  21. }
  22. });
  23. return flattenedPermissions;
  24. }
  25. export default () => {
  26. const { initialState, setInitialState } = useModel('@@initialState');
  27. const formRef = useRef();
  28. const captRef = useRef();
  29. const captchaRef = useRef<CaptFieldRef | null | undefined>();
  30. const [codeImage, setCodeImage] = useState<string>('');
  31. const [capt_id, setCaptId] = useState<string>('');
  32. useEffect(() => {
  33. GetCaptcha().then(res => {
  34. setCodeImage(res.data.img);
  35. setCaptId(res.data.capt_id);
  36. })
  37. }, []);
  38. return (
  39. <ConfigProvider theme={{
  40. token: {
  41. colorPrimary: '#4FBE70',
  42. }
  43. }}>
  44. <div style={contentStyle} >
  45. <LoginForm<{
  46. mobile: String,
  47. password: String,
  48. sms_code: Number
  49. }
  50. >
  51. formRef={formRef}
  52. title="菊城人才后台管理系统"
  53. subTitle="欢迎登录"
  54. onFinish={async (values) => {
  55. // 重新获取权限信息
  56. try {
  57. values.sms_code = Number(values.sms_code)
  58. let res = await Login(values)
  59. message.success('登录成功!');
  60. sessionStorage.setItem('token', res.data.jwttoken.accesstoken)
  61. let permissions = [];
  62. const res1 = await GetUserMenulist();
  63. permissions = flattenPermissions(res1.data.menulist);
  64. // 重新获取权限信息
  65. setInitialState((s) => ({
  66. ...s,
  67. permissions: permissions,
  68. }));
  69. history.push('/');
  70. } catch (error) {
  71. captRef.current.resetFields();
  72. formRef.current.resetFields();
  73. let res = await GetCaptcha()
  74. setCodeImage(res.data.img);
  75. setCaptId(res.data.capt_id);
  76. }
  77. }}
  78. >
  79. <>
  80. <ProFormText
  81. name="mobile"
  82. fieldProps={{
  83. size: 'large',
  84. prefix: <UserOutlined className={'prefixIcon'} />,
  85. }}
  86. placeholder={'账号'}
  87. rules={[
  88. {
  89. required: true,
  90. message: '请输入账号!',
  91. },
  92. ]}
  93. />
  94. <ProFormText.Password
  95. name="password"
  96. fieldProps={{
  97. size: 'large',
  98. prefix: <LockOutlined className={'prefixIcon'} />,
  99. }}
  100. placeholder={'密码'}
  101. rules={[
  102. {
  103. required: true,
  104. message: '请输入密码',
  105. },
  106. ]}
  107. />
  108. <ProForm<{
  109. captcha: string;
  110. }>
  111. formRef={captRef}
  112. submitter={{ render: () => null }}
  113. >
  114. <Row gutter={10}>
  115. <Col span={14}>
  116. <ProFormText
  117. name='captcha'
  118. fieldProps={{
  119. size: 'large',
  120. prefix: <FileImageOutlined className={'prefixIcon'} />,
  121. }}
  122. placeholder={'请输入图形验证码'}
  123. rules={[
  124. {
  125. required: true,
  126. message: '请输入图形验证码',
  127. },
  128. ]}
  129. />
  130. </Col>
  131. <Col span={10}>
  132. <Image src={codeImage} width='100%' height={40} preview={false} style={{ borderRadius: '8px' }} onClick={async () => {
  133. let res = await GetCaptcha()
  134. setCodeImage(res.data.img);
  135. setCaptId(res.data.capt_id);
  136. }} />
  137. </Col>
  138. </Row>
  139. </ProForm>
  140. <ProFormDependency name={['mobile']}>
  141. {({ mobile }) => {
  142. return (
  143. <ProFormCaptcha
  144. fieldRef={captchaRef}
  145. fieldProps={{
  146. size: 'large',
  147. prefix: <LockOutlined className={'prefixIcon'} />
  148. }}
  149. captchaProps={{
  150. size: 'large',
  151. }}
  152. placeholder={'短信验证码'}
  153. captchaTextRender={(timing, count) => {
  154. if (timing) {
  155. return `${count} ${'获取验证码'}`;
  156. }
  157. return '获取验证码';
  158. }}
  159. name="sms_code"
  160. rules={[
  161. {
  162. required: true,
  163. message: '请输入验证码!',
  164. }
  165. ]}
  166. onGetCaptcha={async (value) => {
  167. try {
  168. let captcha = captRef.current?.getFieldsValue().captcha;
  169. let res = await PostSmsSend({ mobile: mobile, captcha: captcha, capt_id: capt_id })
  170. message.success('发送验证码成功,验证码有效期为一分钟');
  171. } catch (error) {
  172. let res = await GetCaptcha()
  173. setCodeImage(res.data.img);
  174. setCaptId(res.data.capt_id);
  175. throw new Error()
  176. }
  177. }}
  178. />
  179. );
  180. }}
  181. </ProFormDependency>
  182. </>
  183. <div
  184. style={{
  185. marginBlockEnd: 24,
  186. }}
  187. >
  188. </div>
  189. </LoginForm>
  190. </div>
  191. </ConfigProvider>
  192. );
  193. };