ResetPassword.vue 7.48 KB
<template>
  <div class="auth-bg">
    <div class="auth-container">
      <!-- 返回按钮 -->
      <div class="back-button" @click="goBack">
        <img src="/zuo.svg" alt="goBack" class="input-icon-svg" />
      </div>

      <!-- 头部Logo与标题 -->
      <div class="auth-header">
        <div class="logo">
          <img src="/newLogo.png" alt="Logo" class="logo-img" />
        </div>
        <h1 class="auth-title">{{ t("resetPassword.title") }}</h1>
        <p class="auth-subtitle">{{ t("resetPassword.subtitle") }}</p>
      </div>

      <!-- 重置密码表单(只保留新密码与确认密码) -->
      <el-form
        :model="form"
        :rules="rules"
        ref="resetFormRef"
        class="reset-form"
        label-position="top"
        @submit.prevent="onSubmit"
      >
        <el-form-item prop="newPassword" :label="t('resetPassword.newPasswordLabel')">
          <el-input
            v-model="form.newPassword"
            :placeholder="t('resetPassword.newPasswordPlaceholder')"
            show-password
            clearable
          >
            <template #prefix>
              <img src="/mima.svg" alt="password" class="input-icon-svg" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="confirmPassword" :label="t('resetPassword.confirmPasswordLabel')">
          <el-input
            v-model="form.confirmPassword"
            :placeholder="t('resetPassword.confirmPasswordPlaceholder')"
            show-password
            clearable
          >
            <template #prefix>
              <img src="/mima.svg" alt="password" class="input-icon-svg" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="submit-btn"
            @click="onSubmit"
            :loading="authStore.loading"
            round
            native-type="button"
          >
            {{ t("resetPassword.resetBtn") }}
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作链接:返回登录 -->
      <div class="action-links">
        <el-link @click="goBack" type="info">
          {{ t("resetPassword.backToLogin") }}
        </el-link>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useI18n } from "vue-i18n";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { useAuthStore } from "@/stores/auth";

const router = useRouter();
const route = useRoute();
const { t } = useI18n();
const authStore = useAuthStore();
const emit = defineEmits<{
  (e: "switch-to-login"): void;
}>();

const resetFormRef = ref<FormInstance>();

const form = reactive({
  newPassword: "",
  confirmPassword: "",
});

// 从邮件链接带入 ?token=...
const token = computed(() => {
  return (route.query.token as string) || "";
});

// 密码确认验证
const validateConfirmPassword = (_rule: any, value: string, callback: any) => {
  if (value !== form.newPassword) {
    callback(new Error(t("resetPassword.passwordMismatch")));
  } else {
    callback();
  }
};

const rules = computed<FormRules>(() => ({
  newPassword: [
    {
      required: true,
      message: t("resetPassword.newPasswordRequired"),
      trigger: "blur",
    },
    {
      min: 8,
      message: t("resetPassword.passwordMinLength"),
      trigger: "blur",
    },
  ],
  confirmPassword: [
    {
      required: true,
      message: t("resetPassword.confirmPasswordRequired"),
      trigger: "blur",
    },
    { validator: validateConfirmPassword, trigger: "blur" },
  ],
}));

const onSubmit = async () => {
  try {
    authStore.clearError();
    if (!resetFormRef.value) return;
    const valid = await resetFormRef.value.validate();
    if (!valid) return;

    if (!token.value) {
      ElMessage.error(t("resetPassword.tokenInvalid"));
      goBack();
      return;
    }

    const result = await authStore.resetPassword({
      token: token.value,
      newPassword: form.newPassword,
    });

    if (result.success) {
      ElMessage.success(t("resetPassword.success"));
      setTimeout(() => {
        goBack();
      }, 1200);
    } else {
      ElMessage.error(result.message || t("resetPassword.resetFailed"));
    }
  } catch (e) {
    console.error("重置密码错误:", e);
    ElMessage.error(t("resetPassword.resetFailed"));
  }
};

const goBack = () => {
  // 回到登录页;如果在 Auth 容器内可配合切卡
  emit("switch-to-login");
  if (route?.path !== "/auth") {
    router.push("/auth");
  }
};

onMounted(() => {
  authStore.clearError();
  // 没有 token 直接回登录
  if (!token.value) {
    ElMessage.warning(t("resetPassword.tokenMissing"));
    goBack();
  }
});
</script>

<style scoped>
.auth-bg {
  min-height: 100vh;
  background: var(--color-login-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.auth-container {
  background: var(--color-login-card);
  border-radius: 20px;
  padding: 40px 32px 32px 32px;
  max-width: 420px;
  width: 90vw;
  box-shadow: var(--box-shadow);
  position: relative;
  z-index: 1;
}

.back-button {
  position: absolute;
  top: 20px;
  left: 20px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 8px;
  transition: background-color 0.3s;
  z-index: 10;
}

.back-button:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.input-icon-svg {
  width: 20px;
  height: 20px;
  object-fit: contain;
}

.auth-header {
  text-align: center;
  margin-bottom: 32px;
}

.logo {
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
  background: transparent;
  border-radius: 0;
  box-shadow: none;
  overflow: visible;
}

.logo-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 0;
}

.auth-title {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
  background: linear-gradient(135deg, #165dff 0%, #4a7cff 100%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  line-height: 1.2;
  padding-bottom: 2px;
}

.auth-subtitle {
  font-size: 16px;
  color: #cccccc;
  margin-bottom: 10px;
}

.reset-form {
  margin-bottom: 24px;
}

.reset-form :deep(.el-form-item__label) {
  color: var(--el-text-color-regular);
  font-weight: 500;
  margin-bottom: 8px;
}

.reset-form :deep(.el-input__wrapper) {
  background-color: var(--el-fill-color-light);
  box-shadow: none;
  border: 1px solid var(--el-border-color);
  border-radius: 8px;
  padding: 8px 12px;
  transition: border-color 0.3s;
}

.reset-form :deep(.el-input__wrapper:hover) {
  border-color: var(--el-color-primary);
}

.reset-form :deep(.el-input__wrapper.is-focus) {
  border-color: var(--el-color-primary);
  box-shadow: 0 0 0 2px rgba(22, 93, 255, 0.1);
}

.reset-form :deep(.el-input__inner) {
  color: var(--el-text-color-primary);
}

.reset-form :deep(.el-input__prefix) {
  display: flex;
  align-items: center;
  margin-right: 8px;
}

.submit-btn {
  width: 100%;
  font-size: 16px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, #165dff 0%, #4a7cff 100%);
  border: none;
  font-weight: 600;
  margin-top: 8px;
}

.submit-btn:hover {
  opacity: 0.9;
}

.action-links {
  display: flex;
  justify-content: center;
  gap: 12px;
  align-items: center;
  margin-top: 16px;
}

.action-links :deep(.el-link) {
  font-size: 14px;
}
</style>