ResetPassword.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, {
  21. useMemo,
  22. useCallback,
  23. useState,
  24. ReactElement,
  25. useEffect
  26. } from 'react'
  27. import { Link } from 'react-router-dom'
  28. import { Form, Input, Button, message } from 'antd'
  29. const FormItem = Form.Item
  30. import { FormComponentProps } from 'antd/lib/form/Form'
  31. import {
  32. IResetPasswordParams,
  33. IOperateStates
  34. } from './types'
  35. import {
  36. resetPasswordUnlogged
  37. } from 'containers/App/actions'
  38. import { useDispatch } from 'react-redux'
  39. const styles = require('./index.less')
  40. const ResetPassword: React.FC<IOperateStates & FormComponentProps> = React.memo(
  41. ({ form, ticket, token, type }) => {
  42. const {
  43. getFieldDecorator,
  44. isFieldTouched,
  45. getFieldsError,
  46. getFieldError
  47. } = form
  48. const [resetStatus, setResetStatus] = useState<boolean>(false)
  49. const dispatch = useDispatch()
  50. const formItemLayout = useMemo(
  51. () => ({
  52. labelCol: { span: 0 },
  53. wrapperCol: { span: 24 }
  54. }),
  55. ['nf']
  56. )
  57. const buttonItemLayout = useMemo(
  58. () => ({
  59. wrapperCol: { span: 24, offset: 0 }
  60. }),
  61. ['nf']
  62. )
  63. const handleSubmit = useCallback(
  64. (e) => {
  65. e.preventDefault()
  66. form.validateFieldsAndScroll((err, values) => {
  67. if (!err) {
  68. const { password, checkCode } = values
  69. const params: IResetPasswordParams = {
  70. type,
  71. ticket,
  72. password,
  73. checkCode,
  74. token,
  75. resolve: (header) => {
  76. const { msg, code } = header
  77. if (code === 200) {
  78. message.success(msg)
  79. setResetStatus(true)
  80. }
  81. }
  82. }
  83. onResetPasswordUnlogged(params)
  84. }
  85. })
  86. },
  87. ['nf']
  88. )
  89. const buttonTips: ReactElement = useMemo(() => {
  90. return !resetStatus ? (
  91. <>确认修改</>
  92. ) : (
  93. <>
  94. <Link to="/login">返回登录</Link>
  95. </>
  96. )
  97. }, [resetStatus])
  98. const onResetPasswordUnlogged = useCallback(
  99. (params: IResetPasswordParams) => {
  100. dispatch(resetPasswordUnlogged(params))
  101. },
  102. []
  103. )
  104. useEffect(() => {
  105. form.validateFields()
  106. }, [])
  107. const checkPasswordConfirm = (rule, value, callback) => {
  108. if (value && value !== form.getFieldValue('password')) {
  109. callback('两次输入的密码不一致')
  110. } else {
  111. callback()
  112. }
  113. }
  114. const forceCheckConfirm = (rule, value, callback) => {
  115. if (form.getFieldValue('confirmPassword')) {
  116. form.validateFields(['confirmPassword'], { force: true })
  117. }
  118. callback()
  119. }
  120. const hasErrors = (fieldsError) => {
  121. return Object.keys(fieldsError).some((field) => fieldsError[field])
  122. }
  123. const newPassError = isFieldTouched('password') && getFieldError('password')
  124. const confirmPasswordError =
  125. isFieldTouched('confirmPassword') && getFieldError('confirmPassword')
  126. const checkCodeError =
  127. isFieldTouched('checkCode') && getFieldError('checkCode')
  128. const isSubmit = hasErrors(getFieldsError())
  129. return (
  130. <Form className={styles.form} onSubmit={handleSubmit}>
  131. <div className={styles.top}>
  132. <FormItem
  133. label="新密码"
  134. {...formItemLayout}
  135. validateStatus={newPassError ? 'error' : 'success'}
  136. help={newPassError || ''}
  137. >
  138. {getFieldDecorator('password', {
  139. rules: [
  140. {
  141. required: true,
  142. message: '新密码不能为空'
  143. },
  144. {
  145. min: 6,
  146. max: 20,
  147. message: '密码长度为6-20位'
  148. },
  149. {
  150. validator: forceCheckConfirm
  151. }
  152. ]
  153. })(
  154. <Input type="password" size="large" placeholder="请输入新密码" />
  155. )}
  156. </FormItem>
  157. <FormItem
  158. label="确认新密码"
  159. {...formItemLayout}
  160. validateStatus={confirmPasswordError ? 'error' : 'success'}
  161. help={confirmPasswordError || ''}
  162. >
  163. {getFieldDecorator('confirmPassword', {
  164. rules: [
  165. {
  166. required: true,
  167. message: '请确认密码'
  168. },
  169. {
  170. validator: checkPasswordConfirm
  171. }
  172. ]
  173. })(<Input type="password" size="large" placeholder="请确认密码" />)}
  174. </FormItem>
  175. <FormItem
  176. {...formItemLayout}
  177. validateStatus={checkCodeError ? 'error' : 'success'}
  178. help={checkCodeError || ''}
  179. >
  180. {getFieldDecorator('checkCode', {
  181. rules: [
  182. {
  183. required: true,
  184. message: '验证码不能为空'
  185. }
  186. ]
  187. })(<Input size="large" placeholder="请输入验证码" />)}
  188. </FormItem>
  189. </div>
  190. <div className={styles.bottom}>
  191. <Button
  192. {...buttonItemLayout}
  193. size="large"
  194. type="primary"
  195. htmlType="submit"
  196. disabled={isSubmit}
  197. >
  198. {buttonTips}
  199. </Button>
  200. </div>
  201. </Form>
  202. )
  203. }
  204. )
  205. export default Form.create<FormComponentProps & IOperateStates>()(ResetPassword)