updatePwd.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <div class="page">
  3. <div class="form" :class="{ 'mobile-form': !pcShow }">
  4. <div class="title">修改密码</div>
  5. <n-form ref="formRef" label-placement="left" :label-width="100" :model="form" :rules="rules" size="large" require-mark-placement="left">
  6. <n-form-item label="密码:" path="newPassword">
  7. <n-input v-model:value="form.newPassword" @input="handlePasswordInput" type="password" show-password-on="click" placeholder="请输入密码" />
  8. </n-form-item>
  9. <n-form-item label="确认密码:" path="confirmNewPassword" ref="confirmPasswordRef">
  10. <n-input v-model:value="form.confirmNewPassword" type="password" show-password-on="click" placeholder="请输入确认密码" />
  11. </n-form-item>
  12. </n-form>
  13. <n-button class="login-btn" attr-type="button" type="info" color="#18A058" @click="handleSure">
  14. {{ submitLoading ? "提交中..." : "确认" }}
  15. </n-button>
  16. </div>
  17. </div>
  18. </template>
  19. <script lang="ts" setup>
  20. import { ref, reactive, onMounted } from "vue";
  21. import {
  22. NForm,
  23. NFormItem,
  24. NInput,
  25. NButton,
  26. FormRules,
  27. FormItemRule,
  28. createDiscreteApi,
  29. } from "naive-ui";
  30. import { useRouter, useRoute } from "vue-router";
  31. import { useUserStore } from "@/store/user";
  32. const router = useRouter();
  33. const userStore = useUserStore();
  34. const pcShow = ref(true);
  35. onMounted(() => {
  36. pcShow.value = !isMobile();
  37. handleUrl();
  38. });
  39. const emit = defineEmits(["closeSginDialog"]);
  40. const message = createDiscreteApi(["message"]);
  41. const formRef = ref();
  42. const confirmPasswordRef = ref();
  43. const form: forgot = reactive({
  44. uuid: "",
  45. newPassword: "",
  46. confirmNewPassword: "",
  47. });
  48. const submitLoading = ref(false);
  49. const Api = ref<any>(updatePwd_Api);
  50. const confirmPasswordValidate = (
  51. rule: FormItemRule,
  52. value: string
  53. ): boolean => {
  54. return value && value == form.newPassword;
  55. };
  56. const rules: FormRules = {
  57. newPassword: [
  58. {
  59. required: true,
  60. message: "请输入密码",
  61. trigger: ["input", "blur", "password-input"],
  62. },
  63. ],
  64. confirmNewPassword: [
  65. {
  66. required: true,
  67. message: "请输入密码",
  68. trigger: ["input", "blur"],
  69. },
  70. {
  71. required: true,
  72. validator: confirmPasswordValidate,
  73. message: "两次密码输入不一致",
  74. trigger: ["blur", "password-input"],
  75. },
  76. ],
  77. };
  78. const handlePasswordInput = () => {
  79. if (form.confirmNewPassword) {
  80. confirmPasswordRef.value?.validate({ trigger: "password-input" });
  81. }
  82. };
  83. const handleSure = () => {
  84. formRef.value?.validate(async (errors: any) => {
  85. if (!errors) {
  86. submitLoading.value = true;
  87. const params = JSON.parse(JSON.stringify(form));
  88. params.newPassword = encryptByBase64(params.newPassword);
  89. params.confirmNewPassword = encryptByBase64(params.confirmNewPassword);
  90. const { code, data } = await Api.value(params);
  91. if (code === 200) {
  92. userStore.setToken(data?.token);
  93. userStore.setUserInfo(data?.user);
  94. router.push("/");
  95. }
  96. submitLoading.value = false;
  97. }
  98. });
  99. };
  100. const handleUrl = () => {
  101. if (typeof window !== "undefined") {
  102. const queryUrl = window.location.search;
  103. const queryStr = queryUrl?.split("?")[1];
  104. const queryParams = queryStr?.split("&");
  105. let obj = {};
  106. if (queryParams && queryParams.length > 0) {
  107. for (let item of queryParams) {
  108. let arr = item.split("=");
  109. obj[arr[0]] = arr[1];
  110. }
  111. }
  112. if (obj.uuid) {
  113. form.uuid = obj.uuid;
  114. Api.value = forget_Api;
  115. } else {
  116. Api.value = updatePwd_Api;
  117. }
  118. }
  119. };
  120. </script>
  121. <style lang="scss" scoped>
  122. .page {
  123. background-color: #fff;
  124. .title {
  125. text-align: center;
  126. margin-bottom: 32px;
  127. font-size: 24px;
  128. font-weight: bold;
  129. color: #1a1a1a;
  130. }
  131. .form {
  132. position: fixed;
  133. top: 50%;
  134. left: 50%;
  135. width: 550px;
  136. padding: 50px 70px;
  137. transform: translate(-50%, -50%);
  138. background: #ffffff;
  139. border-radius: 20px;
  140. box-shadow: 0 0 8px 8px #e1e1e1;
  141. .login-btn {
  142. display: block;
  143. width: 50%;
  144. height: 50px;
  145. color: #fff;
  146. font-size: 18px;
  147. border-radius: 8px;
  148. margin: 0 auto;
  149. :deep(.n-button__content) {
  150. display: block;
  151. }
  152. }
  153. }
  154. .mobile-form {
  155. top: 60%;
  156. width: 85%;
  157. padding: 20px;
  158. }
  159. // .n-form {
  160. // :deep(.n-form-item-label__text) {
  161. // color: #1a1a1a;
  162. // font-size: 16px;
  163. // }
  164. // .n-input {
  165. // border-radius: 8px;
  166. // }
  167. // }
  168. }
  169. </style>