liujie
2026-09-21 20051550f01227ecebbe0bf064e188dd242f58f2
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
(function () {
  var userConfig = window.VISITOR_LOGIN_CONFIG || {};
  var config = Object.assign({
    frontendMode: "cluster",
    deploymentMode: "cluster",
    defaultServerIp: "",
    apiBaseUrl: "http://localhost:9098",
    deploymentConfigPath: "/api/deployment/config",
    defaultBackendPort: "9098",
    loginPath: "/api/login",
    captchaPath: "/captchaImage",
    title: "访客机自助登记\n管理平台"
  }, userConfig);
  config.frontendMode = normalizeDeploymentMode(userConfig.frontendMode || userConfig.deploymentMode || config.frontendMode);
  config.deploymentMode = normalizeDeploymentMode(config.deploymentMode);
  config.requireServerIp = typeof userConfig.requireServerIp === "boolean"
    ? userConfig.requireServerIp
    : config.frontendMode === "cluster";
 
  var loginCard = document.querySelector(".login-card");
  var title = document.getElementById("appTitle");
  var serverIpGroup = document.getElementById("serverIpGroup");
  var serverIp = document.getElementById("serverIp");
  var username = document.getElementById("username");
  var password = document.getElementById("password");
  var captcha = document.getElementById("captcha");
  var captchaBox = document.getElementById("captchaBox");
  var captchaText = document.getElementById("captchaText");
  var captchaImage = document.getElementById("captchaImage");
  var form = document.getElementById("loginForm");
  var message = document.getElementById("formMessage");
  var captchaUuid = "";
  var captchaRequired = true;
 
  document.title = String(config.title || "").replace(/\n/g, "");
  title.innerHTML = String(config.title || "").replace(/\n/g, "<br>");
  serverIp.value = config.defaultServerIp || "";
  applyLoginMode();
  loadBackendDeploymentConfig();
  loadCaptchaIfReady();
 
  captchaBox.addEventListener("click", function () {
    loadCaptcha();
  });
 
  serverIp.addEventListener("blur", function () {
    if (config.requireServerIp && isValidIpOrHost(serverIp.value.trim())) {
      loadCaptcha();
    }
  });
 
  form.addEventListener("submit", function (event) {
    event.preventDefault();
    setMessage("", "");
 
    var payload = {
      username: username.value.trim(),
      password: password.value,
      code: captcha.value.trim(),
      uuid: captchaUuid
    };
 
    if (config.requireServerIp) {
      payload.serverIp = serverIp.value.trim();
    }
 
    var error = validate(payload);
    if (error) {
      setMessage(error, "error");
      return;
    }
 
    submitLogin(payload);
  });
 
  function validate(payload) {
    if (config.requireServerIp && !isValidIpOrHost(payload.serverIp)) {
      return "请输入正确的IP地址";
    }
    if (!payload.username) {
      return "请输入用户名";
    }
    if (!payload.password) {
      return "请输入密码";
    }
    if (captchaRequired && !captchaUuid) {
      return "请先获取验证码";
    }
    if (captchaRequired && !payload.code) {
      return "请输入验证码";
    }
    return "";
  }
 
  function applyLoginMode() {
    serverIpGroup.hidden = !config.requireServerIp;
    loginCard.classList.toggle("no-server-ip", !config.requireServerIp);
    localStorage.setItem("visitor_admin_frontend_mode", config.frontendMode);
    localStorage.setItem("visitor_admin_require_server_ip", String(config.requireServerIp));
  }
 
  function loadCaptchaIfReady() {
    if (!config.requireServerIp || serverIp.value.trim()) {
      loadCaptcha();
    }
  }
 
  function loadBackendDeploymentConfig() {
    if (!config.deploymentConfigPath) {
      return;
    }
    fetch(getConfiguredApiBaseUrl() + normalizePath(config.deploymentConfigPath), {
      method: "GET"
    }).then(function (response) {
      if (!response.ok) {
        throw new Error("部署配置加载失败");
      }
      return response.json();
    }).then(function (result) {
      var data = result.data || {};
      if (result.code !== 200 || !data) {
        return;
      }
      if (data.deploymentMode) {
        config.backendDeploymentMode = normalizeDeploymentMode(data.deploymentMode);
      }
      config.middlePlatformEnabled = data.middlePlatformEnabled === true;
      localStorage.setItem("visitor_admin_backend_deployment_mode", config.backendDeploymentMode || "");
      localStorage.setItem("visitor_admin_middle_platform_enabled", String(config.middlePlatformEnabled));
      applyLoginMode();
      loadCaptchaIfReady();
    }).catch(function () {
      applyLoginMode();
    });
  }
 
  function isValidIpOrHost(value) {
    if (!value) {
      return false;
    }
    value = value.replace(/^https?:\/\//i, "");
    var ipPattern = /^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}(:\d{1,5})?$/;
    var hostPattern = /^[a-zA-Z0-9.-]+(:\d{1,5})?$/;
    return ipPattern.test(value) || hostPattern.test(value);
  }
 
  function loadCaptcha() {
    var baseUrl;
    try {
      baseUrl = getApiBaseUrl();
    } catch (error) {
      captchaText.textContent = "随机验证码区";
      captchaImage.hidden = true;
      captchaText.hidden = false;
      return;
    }
 
    captchaText.textContent = "加载中";
    captchaText.hidden = false;
    captchaImage.hidden = true;
 
    fetch(baseUrl + normalizePath(config.captchaPath), {
      method: "GET"
    }).then(function (response) {
      if (!response.ok) {
        throw new Error("验证码加载失败");
      }
      return response.json();
    }).then(function (result) {
      if (result.code !== 200) {
        throw new Error(result.msg || "验证码加载失败");
      }
      captcha.value = "";
      captchaUuid = result.uuid || "";
      if (result.captchaEnabled === false) {
        captchaRequired = false;
        captchaText.textContent = "无需验证码";
        return;
      }
      captchaRequired = true;
      if (!result.img) {
        throw new Error("验证码图片为空");
      }
      captchaImage.src = "data:image/jpeg;base64," + result.img;
      captchaImage.hidden = false;
      captchaText.hidden = true;
    }).catch(function (error) {
      captchaUuid = "";
      captchaRequired = true;
      captchaText.textContent = "点击重试";
      captchaText.hidden = false;
      captchaImage.hidden = true;
      setMessage(error.message || "验证码加载失败", "error");
    });
  }
 
  function submitLogin(payload) {
    setMessage("登录中...", "");
    fetch(getApiBaseUrl() + normalizePath(config.loginPath), {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(payload)
    }).then(function (response) {
      return response.json();
    }).then(function (result) {
      if (result.code !== 200) {
        throw new Error(result.msg || "登录失败");
      }
      if (result.token) {
        localStorage.setItem("visitor_admin_token", result.token);
      }
      localStorage.setItem("visitor_admin_api_base", getApiBaseUrl());
      localStorage.setItem("visitor_admin_login_result", JSON.stringify(result));
      setMessage("登录成功", "success");
      window.setTimeout(function () {
        window.location.href = "./pages/dashboard.html";
      }, 300);
    }).catch(function (error) {
      setMessage(error.message || "登录失败", "error");
      loadCaptcha();
    });
  }
 
  function setMessage(text, type) {
    message.textContent = text;
    message.classList.toggle("success", type === "success");
    if (!text || !window.AppMessage) {
      return;
    }
    if (type === "success") {
      AppMessage.success("操作成功");
    } else if (type === "error") {
      AppMessage.error("操作失败:" + text);
    }
  }
 
  function getApiBaseUrl() {
    if (!config.requireServerIp) {
      return getConfiguredApiBaseUrl();
    }
    return normalizeServerIp(serverIp.value.trim());
  }
 
  function getConfiguredApiBaseUrl() {
    return trimRightSlash(config.apiBaseUrl || window.location.origin);
  }
 
  function normalizeServerIp(value) {
    if (!isValidIpOrHost(value)) {
      throw new Error("请输入正确的IP地址");
    }
    var result = value;
    if (!/^https?:\/\//i.test(result)) {
      result = "http://" + result;
    }
    var url = new URL(result);
    if (!url.port && config.defaultBackendPort) {
      url.port = config.defaultBackendPort;
    }
    return trimRightSlash(url.origin);
  }
 
  function normalizePath(path) {
    return "/" + String(path || "").replace(/^\/+/, "");
  }
 
  function trimRightSlash(value) {
    return String(value || "").replace(/\/+$/, "");
  }
 
  function normalizeDeploymentMode(value) {
    return String(value || "").toLowerCase() === "cluster" ? "cluster" : "standalone";
  }
})();