ResetForm.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Button, Form, Input } from "ant-design-vue";
  2. import { queenApi } from "queenjs";
  3. import { defineComponent, reactive } from "vue";
  4. import { useAuth } from "../..";
  5. import { CaptchaInput } from "./captcha";
  6. const layout = {
  7. wrapperCol: { span: 24 },
  8. };
  9. const tailLayout = {
  10. wrapperCol: { span: 24 },
  11. };
  12. export default defineComponent({
  13. emits: ["success"],
  14. setup(props, { emit }) {
  15. const auth = useAuth();
  16. const state = reactive({
  17. loading: false,
  18. });
  19. const formState = reactive({
  20. phone: "",
  21. code: "",
  22. password: "",
  23. rePwd: "",
  24. });
  25. const rules = reactive({
  26. phone: [{ required: true, message: "手机号不能为空", trigger: "change" }],
  27. code: [{ required: true, message: "验证码不能为空", trigger: "change" }],
  28. password: [
  29. { required: true, message: "密码不能为空", trigger: "change" },
  30. ],
  31. rePwd: [{ required: true, message: "请确认密码", trigger: "change" }],
  32. });
  33. const { validate, validateInfos } = Form.useForm(formState, rules);
  34. const submit = async () => {
  35. try {
  36. state.loading = true;
  37. const { rePwd, ...formData } = await validate();
  38. if (rePwd !== formData.password) {
  39. queenApi.messageError("输入密码不一致!");
  40. return;
  41. }
  42. await auth.actions.resetPassword(formData);
  43. emit("success");
  44. } finally {
  45. state.loading = false;
  46. }
  47. };
  48. function getCodeByClick() {
  49. return auth.actions.getSmsCode({
  50. phone: formState.phone,
  51. use: "resetPasswd",
  52. });
  53. }
  54. return () => (
  55. <Form {...layout} name="basic">
  56. <Form.Item name="phone" {...validateInfos.phone}>
  57. <Input
  58. placeholder="请输入手机号码"
  59. type=""
  60. v-model={[formState.phone, "value"]}
  61. maxlength={30}
  62. />
  63. </Form.Item>
  64. <Form.Item name="code" {...validateInfos.code}>
  65. <CaptchaInput
  66. disabled={!/^1[3456789]\d{9}$/.test(formState.phone)}
  67. value={formState.code}
  68. onChange={(v) => (formState.code = v)}
  69. onGetCode={getCodeByClick}
  70. />
  71. </Form.Item>
  72. <Form.Item name="password" {...validateInfos.password}>
  73. <Input
  74. type="password"
  75. placeholder="请设置密码"
  76. v-model={[formState.password, "value"]}
  77. maxlength={12}
  78. />
  79. </Form.Item>
  80. <Form.Item name="rePwd" {...validateInfos.rePwd}>
  81. <Input
  82. type="password"
  83. placeholder="再次确认密码"
  84. v-model={[formState.rePwd, "value"]}
  85. maxlength={12}
  86. />
  87. </Form.Item>
  88. <Form.Item {...tailLayout}>
  89. <Button
  90. htmlType="submit"
  91. block
  92. type="primary"
  93. size="large"
  94. shape="round"
  95. loading={state.loading}
  96. onClick={submit}
  97. >
  98. 修改密码
  99. </Button>
  100. </Form.Item>
  101. </Form>
  102. );
  103. },
  104. });