gaoluyang
2026-05-18 e50eb636e24007ac32b8a6cec18cad13bbfcaf3b
src/views/login.vue
@@ -1,255 +1,538 @@
<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-page">
    <!-- 左侧插画区 -->
    <div class="illustration-side">
      <div class="illustration-content">
        <div class="scene">
          <!-- 仓库建筑 -->
          <div class="warehouse">
            <div class="building">
              <div class="roof"></div>
              <div class="wall">
                <div class="window"></div>
                <div class="window"></div>
                <div class="window"></div>
              </div>
              <div class="door"></div>
            </div>
          </div>
          <!-- 卡车 -->
          <div class="truck">
            <div class="truck-body"></div>
            <div class="truck-cab"></div>
            <div class="wheel wheel-1"></div>
            <div class="wheel wheel-2"></div>
          </div>
          <!-- 箱子 -->
          <div class="boxes">
            <div class="box box-1"></div>
            <div class="box box-2"></div>
            <div class="box box-3"></div>
          </div>
          <!-- 装饰元素 -->
          <div class="decor-circle c1"></div>
          <div class="decor-circle c2"></div>
          <div class="decor-circle c3"></div>
        </div>
        <h2>智能供应链管理</h2>
        <p>高效协同 · 精准管控 · 数据驱动</p>
      </div>
    </div>
    <!-- 右侧登录区 -->
    <div class="login-side">
      <div class="login-form-box">
        <div class="form-header">
          <h3>欢迎回来</h3>
          <p>登录您的账号继续使用</p>
        </div>
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              size="large"
              auto-complete="off"
              placeholder="用户名"
              class="login-input"
            >
              <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
              class="login-input"
              @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>
            <a href="#" class="forgot-link">忘记密码?</a>
          </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"
import { User, Lock } from '@element-plus/icons-vue'
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 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>
// 主题色变量
$primary: #1f7a72;
$primary-light: #2a9d8f;
$primary-lighter: #3abbae;
$primary-lightest: #e8f5f3;
.login-page {
  min-height: 100vh;
  display: flex;
}
/* 左侧插画区 */
.illustration-side {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, $primary-lightest 0%, #d4edea 100%);
  padding: 60px;
}
.illustration-content {
  text-align: center;
  h2 {
    font-size: 32px;
    font-weight: 600;
    color: $primary;
    margin: 40px 0 12px 0;
  }
  p {
    font-size: 16px;
    color: $primary-light;
    margin: 0;
  }
}
/* CSS 插画场景 */
.scene {
  position: relative;
  width: 320px;
  height: 240px;
  margin: 0 auto;
}
/* 仓库 */
.warehouse {
  position: absolute;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
}
.building {
  position: relative;
  width: 140px;
  height: 100px;
}
.roof {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 75px solid transparent;
  border-right: 75px solid transparent;
  border-bottom: 40px solid $primary;
}
.wall {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 120px;
  height: 70px;
  background: #fff;
  border: 3px solid $primary;
  border-radius: 0 0 4px 4px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 0 10px;
}
.window {
  width: 20px;
  height: 30px;
  background: $primary-lightest;
  border-radius: 2px;
}
.door {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 30px;
  height: 45px;
  background: $primary-light;
  border-radius: 4px 4px 0 0;
}
/* 卡车 */
.truck {
  position: absolute;
  bottom: 40px;
  right: 20px;
  width: 100px;
  height: 50px;
}
.truck-body {
  position: absolute;
  top: 10px;
  left: 0;
  width: 60px;
  height: 35px;
  background: $primary;
  border-radius: 4px;
}
.truck-cab {
  position: absolute;
  top: 18px;
  right: 0;
  width: 35px;
  height: 27px;
  background: $primary-light;
  border-radius: 0 8px 4px 0;
}
.wheel {
  position: absolute;
  bottom: 0;
  width: 16px;
  height: 16px;
  background: $primary;
  border-radius: 50%;
  border: 3px solid $primary-light;
}
.wheel-1 {
  left: 12px;
}
.wheel-2 {
  right: 8px;
}
/* 箱子 */
.boxes {
  position: absolute;
  bottom: 40px;
  left: 30px;
}
.box {
  position: absolute;
  border-radius: 3px;
}
.box-1 {
  width: 35px;
  height: 35px;
  background: $primary;
  bottom: 0;
  left: 0;
}
.box-2 {
  width: 30px;
  height: 30px;
  background: $primary-light;
  bottom: 0;
  left: 40px;
}
.box-3 {
  width: 28px;
  height: 28px;
  background: $primary-lighter;
  bottom: 38px;
  left: 5px;
}
/* 装饰圆圈 */
.decor-circle {
  position: absolute;
  border-radius: 50%;
  opacity: 0.3;
}
.c1 {
  width: 80px;
  height: 80px;
  background: $primary;
  top: 20px;
  left: 30px;
}
.c2 {
  width: 50px;
  height: 50px;
  background: $primary-light;
  top: 60px;
  right: 40px;
}
.c3 {
  width: 35px;
  height: 35px;
  background: $primary-lighter;
  bottom: 100px;
  left: 20px;
}
/* 右侧登录区 */
.login-side {
  width: 460px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  padding: 40px;
}
.login-form-box {
  width: 100%;
  max-width: 340px;
}
.form-header {
  margin-bottom: 32px;
  h3 {
    font-size: 26px;
    font-weight: 600;
    color: $primary;
    margin: 0 0 8px 0;
  }
  p {
    font-size: 14px;
    color: $primary-light;
    margin: 0;
  }
}
:deep(.login-input .el-input__wrapper) {
  background: $primary-lightest;
  border: 1px solid #d0e5e2;
  border-radius: 10px;
  box-shadow: none;
  padding: 0 16px;
  height: 48px;
  &:hover {
    border-color: $primary-light;
  }
  &:focus-within {
    border-color: $primary;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(31, 122, 114, 0.1);
  }
}
:deep(.login-input .el-input__inner) {
  height: 48px;
  font-size: 15px;
  color: $primary;
  &::placeholder {
    color: #8fb5af;
  }
}
:deep(.login-input .el-input__prefix) {
  color: $primary-light;
  margin-right: 10px;
}
.form-options {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 16px 0 24px;
  :deep(.el-checkbox__label) {
    font-size: 14px;
    color: $primary-light;
  }
  :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
    background-color: $primary;
    border-color: $primary;
  }
  :deep(.el-checkbox__input.is-checked + .el-checkbox__label) {
    color: $primary;
  }
  .forgot-link {
    font-size: 14px;
    color: $primary;
    text-decoration: none;
    font-weight: 500;
    &:hover {
      text-decoration: underline;
    }
  }
}
.login-btn {
  width: 100%;
  height: 48px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, $primary 0%, $primary-light 100%);
  border: none;
  transition: all 0.2s;
  &:hover {
    opacity: 0.9;
    transform: translateY(-1px);
    box-shadow: 0 6px 20px rgba(31, 122, 114, 0.3);
  }
}
/* 响应式 */
@media (max-width: 900px) {
  .illustration-side {
    display: none;
  }
  .login-side {
    width: 100%;
  }
}
</style>