ResetForm.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { useAuth } from "@queenjs-modules/auth";
  2. import { CaptchaInput } from "@queenjs-modules/auth/components/components/captcha";
  3. import { Button, Form, Input } from "ant-design-vue";
  4. import { queenApi } from "queenjs";
  5. import { defineComponent, reactive } from "vue";
  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. v-model={[formState.phone, "value"]}
  60. maxlength={30}
  61. />
  62. </Form.Item>
  63. <Form.Item name="code" {...validateInfos.code}>
  64. <CaptchaInput
  65. disabled={!/^1[3456789]\d{9}$/.test(formState.phone)}
  66. value={formState.code}
  67. onChange={(v) => (formState.code = v)}
  68. onGetCode={getCodeByClick}
  69. />
  70. </Form.Item>
  71. <Form.Item name="password" {...validateInfos.password}>
  72. <Input
  73. type="password"
  74. placeholder="请设置密码"
  75. v-model={[formState.password, "value"]}
  76. maxlength={12}
  77. />
  78. </Form.Item>
  79. <Form.Item name="rePwd" {...validateInfos.rePwd}>
  80. <Input
  81. type="password"
  82. placeholder="再次确认密码"
  83. v-model={[formState.rePwd, "value"]}
  84. maxlength={12}
  85. />
  86. </Form.Item>
  87. <Form.Item {...tailLayout}>
  88. <Button
  89. htmlType="submit"
  90. block
  91. type="primary"
  92. size="large"
  93. shape="round"
  94. loading={state.loading}
  95. onClick={submit}
  96. >
  97. 修改密码
  98. </Button>
  99. </Form.Item>
  100. </Form>
  101. );
  102. },
  103. });