zhangwencui
2026-06-17 6ce67a8e4a226e4c97dfeb53b843489f8c9c21bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<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>