gaoluyang
2026-05-18 da57fbd8e7fa021614fb32502fb1520ea4e34e1e
src/views/login.vue
@@ -1,255 +1,493 @@
<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">{{ title }}</h3>
      <el-divider />
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="账号"
          @input="getUserLoginFacotryList"
        >
          <template #prefix><el-icon><User /></el-icon></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="密码"
          show-password
          @keyup.enter="handleLogin"
        >
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="currentFatoryId">
        <el-select v-model="loginForm.currentFatoryId" placeholder="请选择公司" >
          <el-option  v-for="item in factoryList" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
        </el-select>
      </el-form-item>
<!--      <el-form-item prop="code" v-if="captchaEnabled">-->
<!--        <el-input-->
<!--          v-model="loginForm.code"-->
<!--          size="large"-->
<!--          auto-complete="off"-->
<!--          placeholder="验证码"-->
<!--          style="width: 63%"-->
<!--          @keyup.enter="handleLogin"-->
<!--        >-->
<!--          <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>-->
<!--        </el-input>-->
<!--        <div class="login-code">-->
<!--          <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
<!--        </div>-->
<!--      </el-form-item>-->
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width:100%;"
          @click.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
    </el-form>
    <!--  底部  -->
<!--    <div class="el-login-footer">-->
<!--      <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>-->
<!--    </div>-->
  </div>
</template>
<script setup>
import {getCodeImg} from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from "@/utils/jsencrypt"
import useUserStore from '@/store/modules/user'
import {userLoginFacotryList} from "@/api/system/user.js"
const title = import.meta.env.VITE_APP_TITLE
const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()
const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  currentFatoryId:'',
})
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
}
const codeUrl = ref("")
const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(true)
// 注册开关
const register = ref(false)
const redirect = ref(undefined)
const factoryList = ref([])
const currentFatoryId = ref('')
watch(route, (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect
}, { immediate: true })
function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      Cookies.set("username", loginForm.value.username, { expires: 30 })
      Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
      Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 })
      userStore.loginCheckFactory(loginForm.value).then(res => {
        const query = route.query
        const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
          if (cur !== "redirect") {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
        router.push({ path: redirect.value || "/", query: otherQueryParams })
      }).catch(() => {
        loading.value = false
        // 重新获取验证码
        if (captchaEnabled.value) {
          getCode()
        }
      })
    }
  })
}
function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img
      loginForm.value.uuid = res.uuid
    }
  })
}
function getCookie() {
  const username = Cookies.get("username")
  const password = Cookies.get("password")
  const rememberMe = Cookies.get("rememberMe")
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  }
}
function getUserLoginFacotryList() {
  if(loginForm.value.username){
    userLoginFacotryList({userName:loginForm.value.username}).then(res => {
      console.log('res', res)
      factoryList.value = res.data
    })
  }else {
    factoryList.value = []
  }
}
getCode()
getCookie()
getUserLoginFacotryList()
</script>
<style lang='scss' scoped>
.login {
  height: 100%;
  background-image: url("../assets/indexViews/JZYJView.png");
  background-size: cover;
  position: relative;
}
.title {
  margin: 20px auto 14px auto;
  text-align: center;
  color: #2C51D9;
  font-size: 28px;
  font-weight: 700;
}
.login-form {
  position: absolute;
  top: 50%;
  right: 15%;
  transform: translate(0, -50%);
  border-radius: 6px;
  background: #ffffff;
  width: 420px;
  height: 500px;
  padding: 40px;
  z-index: 1;
   box-shadow: 0 0 5px 1px #ccc;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}
:deep() {
  .el-form-item--default {
    margin-bottom: 36px;
  }
}
</style>
<template>
  <div class="login-container">
    <!-- 左侧品牌区域 -->
    <div class="brand-section">
      <div class="brand-content">
        <div class="brand-icon">
          <el-icon :size="48" color="#fff"><Box /></el-icon>
        </div>
        <h1 class="brand-title">仓储物流管理系统</h1>
        <p class="brand-desc">智能化仓储管理 · 高效物流配送 · 全程可视追踪</p>
        <div class="brand-features">
          <div class="feature-item">
            <div class="feature-icon">
              <el-icon :size="28" color="#fff"><Goods /></el-icon>
            </div>
            <span>库存精准管理</span>
          </div>
          <div class="feature-item">
            <div class="feature-icon">
              <el-icon :size="28" color="#fff"><Van /></el-icon>
            </div>
            <span>物流实时追踪</span>
          </div>
          <div class="feature-item">
            <div class="feature-icon">
              <el-icon :size="28" color="#fff"><TrendCharts /></el-icon>
            </div>
            <span>数据智能分析</span>
          </div>
        </div>
      </div>
      <div class="brand-decoration">
        <div class="deco-circle"></div>
        <div class="deco-line"></div>
        <div class="deco-box"></div>
      </div>
    </div>
    <!-- 右侧登录区域 -->
    <div class="login-section">
      <div class="login-box">
        <div class="login-header">
          <h2>欢迎登录</h2>
          <p>请使用您的账号密码登录系统</p>
        </div>
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <div class="input-label">账号</div>
            <el-input
              v-model="loginForm.username"
              type="text"
              size="large"
              auto-complete="off"
              placeholder="请输入账号"
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <div class="input-label">密码</div>
            <el-input
              v-model="loginForm.password"
              type="password"
              size="large"
              auto-complete="off"
              placeholder="请输入密码"
              show-password
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <div class="form-options">
            <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            <router-link v-if="register" :to="'/register'">注册账号</router-link>
          </div>
          <el-button
            :loading="loading"
            size="large"
            type="primary"
            class="login-btn"
            @click.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script setup>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from "@/utils/jsencrypt"
import useUserStore from "@/store/modules/user"
const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()
const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
})
const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入密码" }],
}
const loading = ref(false)
const captchaEnabled = ref(true)
const register = ref(false)
const redirect = ref(undefined)
watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect
  },
  { immediate: true }
)
function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true
      Cookies.set("username", loginForm.value.username, { expires: 30 })
      Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
      Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 })
      userStore
        .loginCheckFactory(loginForm.value)
        .then(() => {
          const query = route.query
          const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
            if (cur !== "redirect") {
              acc[cur] = query[cur]
            }
            return acc
          }, {})
          router.push({ path: redirect.value || "/", query: otherQueryParams })
        })
        .catch(() => {
          loading.value = false
          if (captchaEnabled.value) {
            getCode()
          }
        })
    }
  })
}
function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      loginForm.value.uuid = res.uuid
    }
  })
}
function getCookie() {
  const username = Cookies.get("username")
  const password = Cookies.get("password")
  const rememberMe = Cookies.get("rememberMe")
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  }
}
getCode()
getCookie()
</script>
<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  background: #f8fafc;
}
// 左侧品牌区域
.brand-section {
  flex: 1;
  background: linear-gradient(145deg, var(--el-color-primary-dark-2) 0%, var(--el-color-primary) 50%, var(--el-color-primary-dark-2) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background:
      radial-gradient(circle at 20% 80%, var(--el-color-primary-light-5) 0%, transparent 50%),
      radial-gradient(circle at 80% 20%, var(--el-color-primary-light-7) 0%, transparent 50%);
  }
}
.brand-content {
  text-align: center;
  color: #fff;
  z-index: 1;
  padding: 40px;
}
.brand-icon {
  width: 100px;
  height: 100px;
  margin: 0 auto 32px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  :deep(svg) {
    width: 48px;
    height: 48px;
    color: #fff;
  }
}
.brand-title {
  font-size: 36px;
  font-weight: 700;
  margin: 0 0 16px;
  letter-spacing: 2px;
}
.brand-desc {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.7);
  margin: 0 0 48px;
  letter-spacing: 1px;
}
.brand-features {
  display: flex;
  justify-content: center;
  gap: 48px;
}
.feature-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  .feature-icon {
    width: 56px;
    height: 56px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.15);
    :deep(svg) {
      width: 28px;
      height: 28px;
      color: #fff;
    }
  }
  span {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.8);
  }
}
.brand-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  .deco-circle {
    position: absolute;
    width: 400px;
    height: 400px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 50%;
    top: -100px;
    right: -100px;
  }
  .deco-line {
    position: absolute;
    width: 2px;
    height: 200px;
    background: linear-gradient(to bottom, transparent, rgba(255, 255, 255, 0.1), transparent);
    bottom: 10%;
    left: 20%;
  }
  .deco-box {
    position: absolute;
    width: 60px;
    height: 60px;
    border: 1px solid rgba(255, 255, 255, 0.08);
    bottom: 20%;
    right: 15%;
    transform: rotate(15deg);
  }
}
// 右侧登录区域
.login-section {
  width: 480px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  box-shadow: -10px 0 40px rgba(0, 0, 0, 0.05);
}
.login-box {
  width: 360px;
  padding: 40px 0;
}
.login-header {
  margin-bottom: 40px;
  h2 {
    font-size: 28px;
    font-weight: 600;
    color: #1e293b;
    margin: 0 0 8px;
  }
  p {
    font-size: 14px;
    color: #64748b;
    margin: 0;
  }
}
.login-form {
  .input-label {
    font-size: 14px;
    color: #374151;
    margin-bottom: 8px;
    font-weight: 500;
  }
  :deep(.el-form-item) {
    margin-bottom: 24px;
  }
  :deep(.el-input__wrapper) {
    border-radius: 10px;
    box-shadow: 0 0 0 1px #e2e8f0;
    padding: 0 16px;
    height: 48px;
    transition: all 0.3s;
    background: #f8fafc;
    &:hover {
      box-shadow: 0 0 0 1px #cbd5e1;
    }
    &:focus-within {
      box-shadow: 0 0 0 2px var(--el-color-primary);
      background: #fff;
    }
  }
  :deep(.el-input__inner) {
    height: 48px;
    font-size: 15px;
    color: #1e293b;
    &::placeholder {
      color: #94a3b8;
    }
  }
  :deep(.el-input__prefix) {
    color: #64748b;
    font-size: 18px;
    margin-right: 10px;
  }
}
.form-options {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 8px 0 32px;
  :deep(.el-checkbox__label) {
    color: #64748b;
    font-size: 14px;
  }
  :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
    background-color: var(--el-color-primary);
    border-color: var(--el-color-primary);
  }
  a {
    color: var(--el-color-primary);
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    &:hover {
      color: var(--el-color-primary-dark-2);
    }
  }
}
.login-btn {
  width: 100%;
  height: 48px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 2px;
  background: linear-gradient(135deg, var(--el-color-primary) 0%, var(--el-color-primary-dark-2) 100%);
  border: none;
  box-shadow: 0 4px 16px var(--el-color-primary-light-5);
  transition: all 0.3s;
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px var(--el-color-primary-light-3);
  }
}
.login-footer {
  margin-top: 48px;
  text-align: center;
  p {
    color: #94a3b8;
    font-size: 13px;
    margin: 0;
  }
}
// 响应式
@media (max-width: 992px) {
  .brand-section {
    display: none;
  }
  .login-section {
    width: 100%;
    box-shadow: none;
  }
}
@media (max-width: 480px) {
  .login-box {
    width: 90%;
    padding: 20px 0;
  }
  .login-header {
    h2 {
      font-size: 24px;
    }
  }
}
</style>