Newer
Older
smartwell_front / src / views / login-pc.vue
liyaguang on 2 Apr 13 KB 细节部分完善
<route lang="yaml">
meta:
  title: 登录
  constant: true
  layout: false
  </route>

<script lang="ts" setup name="LoginPc">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import useUserStore from '@/store/modules/user'
import { RSAencrypt } from '@/utils/security'
import useMenuStore from '@/store/modules/menu'
import { getKaptcha } from '@/api/system/login'
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance() as any
const userStore = useUserStore()
const menuStore = useMenuStore()
import indexDB from '@/utils/indexDB'

const banner = new URL('../assets/images/login-banner.png', import.meta.url)
  .href
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('')
const currentTime = ref(0)

// 获取系统基础配置: 公钥,是否开启验证码
const kaptchaImg = ref('')
function getBaseConfig() {
  userStore.getBaseConfig().then((res) => {
    showKaptcha.value = res.kaptcha
    publicKey.value = res.publicKey
    sid.value = res.sid
    // 获取验证码
    if (showKaptcha.value) {
      getKaptcha(sid.value).then(res => {
        kaptchaImg.value = res.data.kaptcha
      })
    }
    ElMessage({
      message: '连接服务器成功',
      type: 'success',
    })
  })
}
// 刷新验证码
const resetkaptchaImg = () => {
  getKaptcha(sid.value).then(res => {
    kaptchaImg.value = res.data.kaptcha
  })
}
// 登录
const loginFormRef = ref<FormInstance>()
const loginPasswordRef = ref<HTMLElement>()
const loginForm = ref({
  username: '',
  password: '',
  kaptcha: '',
  remember: !!localStorage.login_username_bjwell,
})
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位' },
  ],
  kaptcha: [{ required: true, trigger: ['blur', 'change'], message: '请输入验证码' }],
})

// 处理登录
function handleLogin() {
  // 比较当前时间戳和页面挂载时候的时间戳,大于五分钟不能登录
  // currentTime.value = new Date().getTime()
  if (new Date().getTime() - currentTime.value >= 1000 * 60 * 30) {
    ElMessage.error('页面超时,即将自动刷新')
    // 自动刷新
    // 重新加载当前页面,忽略缓存(相当于按F5)
    setTimeout(() => {
      window.location.reload()
    }, 1000 * 3)
    return
  }
  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_bjwell', loginForm.value.username)
            }
            else {
              localStorage.removeItem('login_username_bjwel')
            }
            router.push(redirect.value)
          })
          .catch(() => {
            loading.value = false
            setTimeout(() => {
              resetkaptchaImg()
            }, 500);
          })
      }
    })
}
// 注册
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(() => {
  loginForm.value.username = localStorage.login_username_bjwell || ''
  getBaseConfig()
})
onMounted(() => {
  // 暂停播放报警声音
  proxy.pauseAudio()
  localStorage.setItem('eventAudio', '')
  // 清空面包屑
  menuStore.resetBreadcrumb()
  localStorage.removeItem('token')
  // 清空缓存
  indexDB.deleteAll()

  // 记录当前时间戳
  currentTime.value = new Date().getTime()
})
</script>

<template>
  <div>
    <div class="bg-top-banner">
      感知数据汇聚平台
    </div>
    <div class="bg-banner" />
    <div id="login-box">
      <el-form v-show="formType === 'login'" ref="loginFormRef" :model="loginForm" :rules="loginRules"
        class="login-form" autocomplete="on">
        <div class="title-container">
          <h3 class="title">
            用户登录
          </h3>
        </div>
        <div>
          <el-form-item prop="username">
            <el-input ref="name" v-model="loginForm.username" placeholder="用户名" text tabindex="1" 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" @keyup.enter="handleLogin">
              <template #prefix>
                <el-icon>
                  <svg-icon name="ep:lock" />
                </el-icon>
              </template>
              <template #suffix>
                <el-icon>
                  <svg-icon :name="passwordType === 'password' ? 'eye' : 'eye-open'"
                    @click="showPassword(loginPasswordRef)" />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item v-if="showKaptcha" prop="kaptcha">
            <div style="width: 100%;display: flex;justify-content: space-between;">
              <el-input ref="loginPasswordRef" v-model="loginForm.kaptcha" type="text" placeholder="验证码"
                style="width: 60%;">
              </el-input>
              <img class="kaptcha-img" :src="kaptchaImg" alt="" style="width: 38%;height: 48px;"
                @click="resetkaptchaImg">
            </div>
          </el-form-item>
        </div>
        <!-- <div class="flex-bar">
          <el-checkbox v-model="loginForm.remember">
            记住我
          </el-checkbox>
        </div> -->
        <el-button :loading="loading" type="primary" size="large" style="width: 100%;" @click.prevent="handleLogin">
          登录
        </el-button>
        <!-- <div v-if="showKaptcha" style="margin-top: 20px; margin-bottom: -20px; text-align: center;">
          <el-divider>演示账号一键登录</el-divider>
          <el-button type="primary" size="small" plain @click="testusername('admin')">
            admin
          </el-button>
          <el-button size="small" plain @click="testusername('test')">
            test
          </el-button>
        </div> -->
      </el-form>
    </div>
    <copyright />
  </div>
</template>

<style lang="scss" scoped>
[data-mode="mobile"] {
  #login-box {
    position: relative;
    width: 100%;
    height: 100%;
    top: inherit;
    left: inherit;
    transform: translateX(0) translateY(0);
    flex-direction: column;
    justify-content: start;
    border-radius: 0;
    box-shadow: none;

    .login-banner {
      width: 100%;
      padding: 20px 0;

      .banner {
        position: relative;
        right: inherit;
        width: 100%;
        max-width: 375px;
        margin: 0 auto;
        display: inherit;
        top: inherit;
        transform: translateY(0);
      }
    }

    .login-form {
      width: 100%;
      min-height: auto;
      padding: 30px;
    }
  }

  .copyright {
    position: relative;
    bottom: 0;
    padding-bottom: 10px;
  }
}

:deep(input[type="password"]::-ms-reveal) {
  display: none;
}

.kaptcha-img {
  &:hover {
    cursor: pointer;
  }
}

.bg-top-banner {
  position: fixed;
  top: 0;
  width: 100%;
  height: 17vh;
  z-index: 999;
  line-height: 10vh;
  text-align: center;
  font-size: 30px;
  color: #fff;
  background: url("../assets/images/login-image/banner.png") no-repeat center / cover;
}

.bg-banner {
  position: fixed;
  z-index: 0;
  width: 100%;
  height: 100%;
  // background: radial-gradient(circle at center, var(--el-fill-color-lighter), var(--el-bg-color-page));
  background: url("../assets/images/login-image/bg.png") no-repeat center / cover;
}

#login-box {
  display: flex;
  justify-content: space-between;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  // background-color: var(--el-bg-color);

  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--el-box-shadow);

  .login-banner {
    position: relative;
    width: 450px;
    background-color: var(--el-fill-color-light);
    overflow: hidden;

    .banner {
      width: 100%;

      @include position-center(y);
    }

    .logo {
      position: absolute;
      top: 20px;
      left: 20px;
      width: 30px;
      height: 30px;
      border-radius: 4px;
      background: url("../assets/images/logo.png") no-repeat;
      background-size: contain;
      box-shadow: var(--el-box-shadow-light);
    }
  }

  .login-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-height: 500px;
    width: 500px;
    padding: 50px;
    background: url("../assets/images/login-image/frame.png") no-repeat center / cover;
    overflow: hidden;

    .title-container {
      position: relative;

      .title {
        font-size: 1.3em;
        color: #fff;
        margin: 0 auto 30px;
        text-align: center;
        font-weight: bold;
      }
    }
  }

  .el-form-item {
    margin-bottom: 24px;

    :deep(.el-input) {
      height: 48px;
      line-height: inherit;
      width: 100%;

      input {
        height: 48px;
      }

      .el-input__prefix,
      .el-input__suffix {
        display: flex;
        align-items: center;
      }

      .el-input__prefix {
        left: 10px;
      }

      .el-input__suffix {
        right: 10px;
      }
    }
  }

  .flex-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  .sub-link {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 20px;
    font-size: 14px;
    color: var(--el-text-color-secondary);

    .text {
      margin-right: 10px;
    }
  }
}

.copyright {
  position: absolute;
  bottom: 30px;
  width: 100%;
  margin: 0;
}

:deep .el-input__wrapper {
  background-color: transparent !important;
}

:deep .el-input__inner {
  color: #fff !important;
}
</style>