<script setup>
|
import { ElMessage } from "element-plus"
|
import Cookies from "js-cookie"
|
import { encrypt, decrypt } from "@/utils/jsencrypt"
|
import useUserStore from "@/store/modules/user"
|
import defaultBrandLogo from "@/assets/logo/logo.png"
|
|
const userStore = useUserStore()
|
const route = useRoute()
|
const router = useRouter()
|
|
const appTitle = String(import.meta.env.VITE_APP_TITLE || "数字工厂 MOM 系统").trim()
|
const companySubtitle = String(import.meta.env.VITE_LOGIN_SUBTITLE || "Digital Factory Operation Center").trim()
|
const configuredLogo = String(import.meta.env.VITE_APP_LOGO || "").trim()
|
const logoModules = import.meta.glob("/src/assets/logo/*.png", { eager: true })
|
const brandIconUrl = `${import.meta.env.BASE_URL}favicon.ico`
|
|
const redirect = ref("")
|
const loading = ref(false)
|
const now = ref(new Date())
|
const brandLogoUrl = ref(defaultBrandLogo)
|
|
const loginForm = ref({
|
username: "",
|
password: "",
|
rememberMe: false,
|
})
|
|
const companyName = computed(() => {
|
const currentFactoryName = String(userStore.currentFactoryName || "").trim()
|
return currentFactoryName || appTitle
|
})
|
|
const todayLabel = computed(() => {
|
const date = now.value
|
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
|
})
|
|
const clockLabel = computed(() => {
|
const date = now.value
|
return `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
|
})
|
|
watch(
|
route,
|
(newRoute) => {
|
redirect.value = String(newRoute.query?.redirect || "")
|
},
|
{ immediate: true }
|
)
|
|
watch(
|
() => userStore.currentFactoryName,
|
() => updateBrandLogo(),
|
{ immediate: true }
|
)
|
|
let timer = 0
|
onMounted(() => {
|
timer = window.setInterval(() => {
|
now.value = new Date()
|
}, 1000)
|
})
|
|
onBeforeUnmount(() => {
|
if (timer) {
|
window.clearInterval(timer)
|
timer = 0
|
}
|
})
|
|
function pad(value) {
|
return String(value).padStart(2, "0")
|
}
|
|
function resolveConfiguredLogo() {
|
if (!configuredLogo) {
|
return ""
|
}
|
|
if (/^(https?:)?\/\//.test(configuredLogo) || configuredLogo.startsWith("data:")) {
|
return configuredLogo
|
}
|
|
const cleanPath = configuredLogo.replace(/^\/+/, "")
|
const fullPath = cleanPath.startsWith("src/") ? `/${cleanPath}` : `/src/${cleanPath}`
|
const localLogo = logoModules[fullPath]
|
|
if (localLogo && localLogo.default) {
|
return localLogo.default
|
}
|
|
if (configuredLogo.startsWith("/")) {
|
return configuredLogo
|
}
|
|
return `${import.meta.env.BASE_URL}${cleanPath}`
|
}
|
|
function updateBrandLogo() {
|
const logoFromConfig = resolveConfiguredLogo()
|
if (logoFromConfig) {
|
brandLogoUrl.value = logoFromConfig
|
return
|
}
|
|
const currentFactoryName = String(userStore.currentFactoryName || "").trim()
|
if (!currentFactoryName) {
|
brandLogoUrl.value = defaultBrandLogo
|
return
|
}
|
|
const factoryLogoPath = `/src/assets/logo/${currentFactoryName}.png`
|
const matched = logoModules[factoryLogoPath]
|
brandLogoUrl.value = matched && matched.default ? matched.default : defaultBrandLogo
|
}
|
|
function handleLogoError() {
|
brandLogoUrl.value = defaultBrandLogo
|
}
|
|
function handleRememberCookie() {
|
if (!loginForm.value.rememberMe) {
|
Cookies.remove("username")
|
Cookies.remove("password")
|
Cookies.remove("rememberMe")
|
return
|
}
|
|
Cookies.set("username", loginForm.value.username, { expires: 30 })
|
Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
|
Cookies.set("rememberMe", "true", { expires: 30 })
|
}
|
|
function getCookie() {
|
const username = Cookies.get("username")
|
const password = Cookies.get("password")
|
const rememberMe = Cookies.get("rememberMe")
|
|
loginForm.value.username = username || ""
|
loginForm.value.password = password ? decrypt(password) : ""
|
loginForm.value.rememberMe = rememberMe === "true"
|
}
|
|
function handleLogin() {
|
if (!loginForm.value.username) {
|
ElMessage.error("请输入账号")
|
return
|
}
|
if (!loginForm.value.password) {
|
ElMessage.error("请输入密码")
|
return
|
}
|
|
loading.value = true
|
handleRememberCookie()
|
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(() => {})
|
.finally(() => {
|
loading.value = false
|
})
|
}
|
|
getCookie()
|
</script>
|
|