updatePwd.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <div class="container">
  3. <n-form ref="formRef" label-placement="left" :label-width="100" :model="form" :rules="rules" size="large" require-mark-placement="left">
  4. <n-form-item label="密码:" path="newPassword">
  5. <n-input v-model:value="form.newPassword" @input="handlePasswordInput" type="password" show-password-on="click" placeholder="请输入密码" />
  6. </n-form-item>
  7. <n-form-item label="确认密码:" path="confirmNewPassword" ref="confirmPasswordRef">
  8. <n-input v-model:value="form.confirmNewPassword" type="password" show-password-on="click" placeholder="请输入确认密码" />
  9. </n-form-item>
  10. </n-form>
  11. <n-button class="login-btn" attr-type="button" type="info" color="#18A058" :disabled="submitLoading" @click="handleSure">
  12. {{ submitLoading ? "提交中..." : "确认" }}
  13. </n-button>
  14. </div>
  15. </template>
  16. <script lang="ts" setup>
  17. import {
  18. NForm,
  19. NFormItem,
  20. NInput,
  21. NButton,
  22. FormRules,
  23. FormItemRule,
  24. createDiscreteApi,
  25. } from "naive-ui";
  26. import { ref, reactive } from "vue";
  27. const emit = defineEmits(["closeSginDialog"]);
  28. const message = createDiscreteApi(["message"]);
  29. const formRef = ref();
  30. const submitLoading = ref(false);
  31. const confirmPasswordRef = ref();
  32. const form = reactive({
  33. newPassword: "",
  34. confirmNewPassword: "",
  35. });
  36. const confirmPasswordValidate = (
  37. rule: FormItemRule,
  38. value: string
  39. ): boolean => {
  40. return value && value == form.newPassword;
  41. };
  42. const rules: FormRules = {
  43. newPassword: [
  44. {
  45. required: true,
  46. message: "请输入密码",
  47. trigger: ["input", "blur", "password-input"],
  48. },
  49. ],
  50. confirmNewPassword: [
  51. {
  52. required: true,
  53. message: "请输入密码",
  54. trigger: ["input", "blur"],
  55. },
  56. {
  57. required: true,
  58. validator: confirmPasswordValidate,
  59. message: "两次密码输入不一致",
  60. trigger: ["blur", "password-input"],
  61. },
  62. ],
  63. };
  64. const handlePasswordInput = () => {
  65. if (form.confirmNewPassword) {
  66. confirmPasswordRef.value?.validate({ trigger: "password-input" });
  67. }
  68. };
  69. const handleSure = () => {
  70. formRef.value?.validate(async (errors: any) => {
  71. if (!errors) {
  72. submitLoading.value = true;
  73. const params = JSON.parse(JSON.stringify(form));
  74. params.newPassword = encryptByBase64(params.newPassword);
  75. params.confirmNewPassword = encryptByBase64(params.confirmNewPassword);
  76. try {
  77. const { code } = await updatePwd_Api(params);
  78. if (code === 200) {
  79. message.message.success("修改成功");
  80. emit("closeSginDialog", "success");
  81. }
  82. } catch (error) {}
  83. submitLoading.value = false;
  84. }
  85. });
  86. };
  87. </script>
  88. <style lang="scss" scoped>
  89. .container {
  90. width: 100%;
  91. padding: 30px 20px;
  92. .login-btn {
  93. display: block;
  94. width: 50%;
  95. height: 50px;
  96. color: #fff;
  97. font-size: 18px;
  98. border-radius: 8px;
  99. margin: 0 auto;
  100. :deep(.n-button__content) {
  101. display: block;
  102. }
  103. }
  104. }
  105. </style>