|
@@ -0,0 +1,40 @@
|
|
|
+class LoginManager {
|
|
|
+ constructor(apiUrl) {
|
|
|
+ this.apiUrl = apiUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ validateInput(username, password) {
|
|
|
+ const invalidChars = /[^a-zA-Z0-9]/;
|
|
|
+ if (invalidChars.test(username) || invalidChars.test(password)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ async login(username, password) {
|
|
|
+ if (!this.validateInput(username, password)) {
|
|
|
+ alert("用户名和密码只能包含字母和数字!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const response = await fetch(this.apiUrl, {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
+ },
|
|
|
+ body: JSON.stringify({ username, password })
|
|
|
+ });
|
|
|
+ if (response.ok) {
|
|
|
+ const data = await response.json();
|
|
|
+ return data;
|
|
|
+ } else {
|
|
|
+ throw new Error('登录失败!');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('登录请求失败:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export default LoginManager;
|