Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 适配升级后的宿舍区网络登录 #9

Merged
merged 2 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"permissions": [
"https://drcom.szu.edu.cn/a70.htm",
"http://172.30.255.2/0.htm"
"http://172.30.255.2/0.htm",
"http://172.30.255.42:801/"
]
}
3 changes: 2 additions & 1 deletion src/js/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { login } from './login-post.js';
import { login, newLogin } from './login-post.js';
import { show, hide, warn } from './animation.js';

const securityText = '*账号信息仅保存于本地,以保证隐私安全';
Expand Down Expand Up @@ -133,6 +133,7 @@ const showResult = (isSuccess, msg) => {
Promise.race([
login('nth'),
login('wifi'),
newLogin(),
timeoutTimer,
])
.then((res) => {
Expand Down
105 changes: 104 additions & 1 deletion src/js/login-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,112 @@ export function login(type) {
}
};
});

// 发送请求
request.send(postBody);

return result;
}

// 宿舍区宽带升级后的登录
export function newLogin() {
// 创建 XMLHttpRequest 对象
const request = new XMLHttpRequest();

// 学号、密码
const cid = localStorage.getItem('cid');
const password = localStorage.getItem('password');

// 构造url
const query = serialize({
callback: 'dr1003',
login_method: 1,
user_account: `%2C0%2C${cid}`,
user_password: password,
wlan_user_ip: '',
wlan_user_ipv6: '',
wlan_user_mac: '000000000000',
wlan_ac_ip: '',
wlan_ac_name: '',
jsVersion: '4.1.3',
terminal_type: 1,
lang: 'zh-cn',
v: 10353,
lang: 'zh',
});
const url = `http://172.30.255.42:801/eportal/portal/login?${query}`;
request.open('GET', url, true);

// 根据连接情况返回结果
const result = new Promise((resolve, reject) => {
request.onreadystatechange = () => {
if (request.status !== 200) {
setTimeout(() => {
resolve({
type: false,
msg: '连接失败,请检查网络连接情况',
})
}, 3000);
}
if (
request.readyState === 4 &&
request.status === 200
) {
/**
* 新版协议的返回格式:
* dr1003({\"result\":1,\"msg\":\"Portal协议认证成功!\"});
* dr1003({\"result\":0,\"msg\":\"账号不存在\",\"ret_code\":1});
* dr1003({\"result\":0,\"msg\":\"IP: 172.30.237.57 已经在线!\",\"ret_code\":2});
* 这里dr1003来自于GET请求中的callback参数
*/

// 切除前面的"dr1003("一共7个字符
// 以及后面的");"两个字符,得到完整JSON字符串
const responseText = request.responseText.substring(7, request.responseText.length - 2);

// 尝试解析JSON
let responseJson = {};
try {
responseJson = JSON.parse(responseText);
} catch (e) {
chimaoshu marked this conversation as resolved.
Show resolved Hide resolved
console.error(e);
resolve({
type: false,
msg: `😥登陆失败:${request.responseText}`
});
}

// 成功
const isSucceedResult = (responseJson.result === 1);
if (isSucceedResult) {
resolve({
type: true,
msg: '登录成功😊'
});
}

// "IP已经在线"算作登录成功
const isAlreadyOnline = (responseJson.ret_code === 2);
if (isAlreadyOnline) {
resolve({
type: true,
msg: `登录成功😊:${responseJson.msg}` // 顺便显示一下登录的IP
});
}

// 失败
let msg = responseJson.msg;
// 错误情况简单翻译
if (msg === 'ldap auth error') msg = `账号或密码错误(${msg})`
if (msg === 'error hid') msg = `登录行为异常,请过几分钟后再试(${msg})`
resolve({
type: false,
msg: `😥登陆失败:${msg}`
});
}
};
});
// 发送请求
request.send();

return result;
}