Newer
Older
adminAccountabilityFront / src / views / login.vue
<route lang="yaml">
meta:
  title: 登录
  constant: true
  layout: false
</route>

<script lang="ts" setup name="Login">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import useUserStore from '@/store/modules/user'
import { RSAdecrypt, RSAencrypt } from '@/utils/security'
import { getPrivateKey, removePrivateKey } from '@/utils/auth'
const route = useRoute()
const router = useRouter()

const userStore = useUserStore()

const banner = new URL('../assets/images/login-banner.png', import.meta.url)
const title = import.meta.env.VITE_APP_TITLE

// 表单类型,login 登录,reset 重置密码
const formType = ref('login')
// 加载状态
const loading = ref(false)
// 密码类型:password 密码
const passwordType = ref('password')
// 重定向路径
const redirect = ref(route.query.redirect?.toString() ?? '/')
// 是否需要验证码
const showKaptcha = ref(false)
// 公钥
const publicKey = ref('')
// sid
const sid = ref('')

// 获取系统基础配置: 公钥,是否开启验证码
function getBaseConfig() {
  userStore.getBaseConfig().then((res) => {
    showKaptcha.value = res.kaptcha
    publicKey.value = res.publicKey
    sid.value = res.sid
    ElMessage({
      message: '连接服务器成功',
      type: 'success',
    })
  })
}

// 登录
const loginFormRef = ref<FormInstance>()
const loginPasswordRef = ref<HTMLElement>()
const loginForm = ref({
  username: '',
  password: '',
  kaptcha: '',
  remember: !!localStorage.login_username,
})
const loginRules = ref<FormRules>({
  username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
  password: [
    { required: true, trigger: 'blur', message: '请输入密码' },
    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' },
  ],
})

// 处理登录
function handleLogin() {
  loginFormRef.value
    && loginFormRef.value.validate(async (valid) => {
      if (valid) {
        loading.value = true
        // 表单对象
        const finalForm = {
          sid: sid.value,
          username: loginForm.value.username,
          password: '',
          kaptcha: loginForm.value.kaptcha,
        }
        // 加密
        finalForm.password = await RSAencrypt(loginForm.value.password)
        userStore
          .login(finalForm)
          .then(() => {
            ElMessage({
              message: '登录成功',
              type: 'success',
            })
            loading.value = false
            if (loginForm.value.remember) {
              // 保存用户名
              localStorage.setItem('login_username', loginForm.value.username)
              // 保存 加密密码串
              localStorage.setItem('login_password', finalForm.password)
            }
            else {
              // 移除用户名
              localStorage.removeItem('login_username')
              // 移除 加密密码串
              localStorage.removeItem('login_password')
              // 移除私钥
              removePrivateKey()
            }
            router.push(redirect.value)
            // router.push({
            //   path: '/index',
            // })
          })
          .catch(() => {
            loading.value = false
          })
      }
    })
}
// 注册
const registerFormRef = ref<FormInstance>()
const registerPasswordRef = ref<HTMLElement>()
const registerCheckPasswordRef = ref<HTMLElement>()
const registerForm = ref({
  username: '',
  captcha: '',
  password: '',
  checkPassword: '',
})
const registerRules = ref<FormRules>({
  username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
  captcha: [{ required: true, trigger: 'blur', message: '请输入验证码' }],
  password: [
    { required: true, trigger: 'blur', message: '请输入密码' },
    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' },
  ],
  checkPassword: [
    { required: true, trigger: 'blur', message: '请再次输入密码' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.value.password) {
          callback(new Error('两次输入的密码不一致'))
        }
        else {
          callback()
        }
      },
    },
  ],
})
function handleRegister() {
  ElMessage({
    message: '注册模块仅提供界面演示,无实际功能,需开发者自行扩展',
    type: 'warning',
  })
  registerFormRef.value
    && registerFormRef.value.validate((valid) => {
      if (valid) {
        // 这里编写业务代码
      }
    })
}

// 重置密码
const resetFormRef = ref<FormInstance>()
const resetNewPasswordRef = ref<HTMLElement>()
const resetForm = ref({
  username: localStorage.login_username || '',
  captcha: '',
  newPassword: '',
})
const resetRules = ref<FormRules>({
  username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
  captcha: [{ required: true, trigger: 'blur', message: '请输入验证码' }],
  newPassword: [
    { required: true, trigger: 'blur', message: '请输入新密码' },
    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' },
  ],
})
function handleReset() {
  ElMessage({
    message: '重置密码模块仅提供界面演示,无实际功能,需开发者自行扩展',
    type: 'warning',
  })
  resetFormRef.value
    && resetFormRef.value.validate((valid) => {
      if (valid) {
        // 这里编写业务代码
      }
    })
}

function showPassword(passwordEl: HTMLElement | undefined) {
  passwordType.value = passwordType.value === 'password' ? '' : 'password'
  nextTick(() => {
    passwordEl?.focus()
  })
}

function testusername(username: string) {
  loginForm.value.username = username
  loginForm.value.password = '111111'
  handleLogin()
}
onBeforeMount(async () => {
  loginForm.value.username = localStorage.login_username || ''
  // if (localStorage.login_username) {
  //   const passwoed = await RSAdecrypt(localStorage.login_password)
  //   loginForm.value.password = passwoed
  // }
  getBaseConfig()
})
</script>

<template>
  <div class="login-container">
    <div class="login-div">
      <div id="left-div" class="left-div">
        <div class="logo-div">
          <img>
        </div>
        <div class="title-div">
          <h3 class="title">
            行政问效综合评价平台
          </h3>
        </div>
      </div>
      <div ref="element" class="right-div">
        <el-form
          id="login-form" ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          autocomplete="on" auto-complete="on" label-position="right"
        >
          <h5 class="login-title">
            登录
          </h5>
          <el-form-item prop="username">
            <el-input
              ref="name"
              v-model="loginForm.username"
              placeholder="用户名"
              text
              tabindex="1"
              style="width: 100%;"
              autocomplete="on"
            >
              <template #prefix>
                <el-icon>
                  <svg-icon name="ep:user" />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              ref="loginPasswordRef"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              tabindex="2"
              autocomplete="on"
              style="width: 100%;"
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon>
                  <svg-icon name="ep:lock" />
                </el-icon>
              </template>
              <template #suffix>
                <el-icon>
                  <svg-icon
                    :name="passwordType === 'password' ? 'ep:eye' : 'ep:eye-open'"
                    @click="showPassword(loginPasswordRef)"
                  />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" type="primary" style="width: 100%;" @click.prevent="handleLogin">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="clear-div" />
    </div>
  </div>
</template>

<style rel="stylesheet/scss" lang="scss">
  $bg: #fff;
  $light_gray: #fff;
  $dark_gray: #889aa4;

  /* reset element-ui css */
  .login-container {
    background-image: url("../assets/images/login_images/theme0/bg.png");

    .el-input {
      display: inline-block;
      height: 40px;
      width: 85%;

      input {
        background: transparent;
        border: 0;
        appearance: none;
        border-radius: 0;
        padding: 8px 5px 8px 8px;
        color: $dark_gray;
        height: 40px;

        &:-webkit-autofill {
          box-shadow: 0 0 0 1000px $light_gray inset !important;
          -webkit-text-fill-color: $dark_gray !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid #c5c5c5;
      background: #fff;
      border-radius: 5px;
      color: #000;
    }

    .el-button {
      line-height: 1.2;
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg: #fff;
  $dark_gray: #889aa4;
  $light_gray: #fff;

  ::v-deep(.el-input__wrapper) {
    border: none;
    padding: 0;
    border-radius: 0;
    box-shadow: none;
    width: 100%;
  }

  .login-container {
    position: relative;
    height: 100%;
    width: 100%;

    /* background: #000000 url("/static/images/login_images/bg.png") no-repeat; */
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-color: $bg;

    .login-div {
      /* height:385px; */
      width: 800px;
      position: absolute;
      overflow: hidden;
      left: 50%;
      top: 50%;
      margin-left: -400px;
      margin-top: -220px;
      border: 1px solid #889aa4;
      border-radius: 3px;
      box-shadow: 0 2px 4px rgb(0 0 0 / 12%), 0 0 6px rgb(0 0 0 / 4%);
    }

    .left-div {
      width: 60%;
      float: left;
      // height: 100%;
      height: 258px;

      /* background: url("/static/images/login_images/left.jpg"); */
      background-image: url("../assets/images/login_images/theme0/left.jpg");
      background-color: #ffff;
    }

    .title-div {
      margin: 20px 30px 30px;
    }

    .title {
      color: $light_gray;
      font-size: 26px;
      font-weight: 400;
    }

    .right-div {
      width: 40%;
      float: left;
      background-color: white;
    }

    .clear-div {
      clear: both;
    }

    .login-form {
      max-width: 100%;
      padding: 15px 15px 5px;
      margin: auto;
      overflow: hidden;
      border-radius: 3px;
      box-shadow: 0 5px 8px rgb(0 0 0 / 12%), 0 0 10px rgb(0 0 0 / 4%);
    }

    .tips {
      font-size: 14px;

      /* color: #fff; */
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 0 3px 0 10px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .login-title {
      font-size: 18px;
      font-weight: 400;
      color: #000;
      margin: 10px auto 30px;
      text-align: center;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>