scu-pecourse-utils
Version:
221 lines (191 loc) • 5.28 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var CryptoJS = require('crypto-js');
var axios = require('axios');
var qs = require('querystring');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var CryptoJS__default = /*#__PURE__*/_interopDefaultLegacy(CryptoJS);
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
var qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
/**
* generate timestamp
* @return {string}
*/
function getTimestamp() {
return Date.parse(new Date().toString()).toString();
}
/**
* to base64
* @param str
*/
function base64(str) {
return CryptoJS__default['default'].enc.Base64.parse(str).toString(CryptoJS__default['default'].enc.Hex);
}
/**
* get app_key
* constant js is in line 12204 in app.js
* @return {string}
*/
function getAppKey() {
const jt = ['==wn', 'D0g6', '6qYL7', 'qDSn3', 'ZOox4', '6'].join('');
return base64(jt.split('').reverse().join(''));
}
/**
* get app_secret
* @return {string}
*/
function getAppSecret() {
const xt = ['fNR', '2q4Z', 'rSde', 'peIr', 'Z9H', 'iUlQ', '=', '='].join('');
return base64(xt);
}
/**
* md5 crypto a string to hex
* @param {string} str
* @return {string}
*/
function md5(str) {
return CryptoJS__default['default'].MD5(str).toString(CryptoJS__default['default'].enc.Hex);
}
/**
* generate sign
* @param {string} path
* @param {string} appsecret
* @param {string} timestamp
* @param {Object} data
*/
function getSign(path, appsecret, timestamp, data) {
const i = [];
let str = appsecret + path;
if (data) {
for (const key in data) i.push(key);
i.sort();
for (const key of i) str += key + data[key];
}
str += timestamp + ' ' + appsecret;
return md5(str);
}
const app_key = getAppKey();
const app_secret = getAppSecret();
const http = axios__default['default'].create({
baseURL: 'http://211.83.159.5:8086',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
});
http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
/**
* get
* @param {string} path
* @param {*} authorHeaders
*/
async function emitAjaxGet(path, authorHeaders) {
const queryString = {
app_key,
timestamp: getTimestamp(),
sign: getSign(path, app_secret, getTimestamp())
};
const res = await http.get(path, {
params: queryString,
headers: authorHeaders
});
return await res.data;
}
/**
* post
* @param {string} path
* @param {Object} data
* @param {*} authorHeaders
*/
async function emitAjaxPost(path, data, authorHeaders) {
const queryString = {
app_key,
timestamp: getTimestamp(),
sign: getSign(path, app_secret, getTimestamp(), data),
...data
};
const res = await http.post(path, qs__default['default'].stringify(queryString), {
headers: authorHeaders
});
return await res.data;
}
/**
* define api
*/
/**
* @param {*} headers
* get current term id
*/
async function getTermId(headers) {
const path = '/api/terms';
const res = await emitAjaxGet(path, headers);
console.log(res.code);
for (const term of res.data.content) {
if (term.currentTerm === 1) return term.id;
}
}
/**
* get classid and teacher id
* @param {string} teacherName
* @param {string} studentUid
* @param {*} headers
*/
async function getClassIdAndTeacherId(teacherName, studentUid, headers) {
const path = `/api/term/${await getTermId(headers)}/student/${studentUid}/course/classes`;
const res = await emitAjaxGet(path, headers);
const lists = res.data;
for (const course of lists) {
if (course.teacherName === teacherName) {
return {
courseClassId: course.id.toString(),
teacherUid: course.teacherUid
};
}
}
}
/**
* choose function
* @param {string} courseClassId
* @param {string} teacherUid
* @param {string} teacherName
* @param {string} studentUid
* @param {*} headers
*/
async function choose(courseClassId, teacherUid, teacherName, studentUid, headers) {
const path = '/api/courses/students';
const data = {
courseClassId,
studentUid,
teacherUid,
teacherName
};
const res = await emitAjaxPost(path, data, headers);
if (res.code === 200 && res.message === 'OK') return true;else return false;
}
async function startTimer(teacherName, studentUid, callback, headers) {
const {
courseClassId,
teacherUid
} = await getClassIdAndTeacherId(teacherName, studentUid, headers);
let count = 1;
const timer = setInterval(async () => {
const res = await choose(courseClassId, teacherUid, teacherName, studentUid, headers);
if (res === true) {
callback('选课成功');
clearInterval(timer);
} else {
callback(`已经选课${count}次`);
count++;
}
}, 1000);
return timer;
}
exports.app_key = app_key;
exports.app_secret = app_secret;
exports.choose = choose;
exports.emitAjaxGet = emitAjaxGet;
exports.emitAjaxPost = emitAjaxPost;
exports.getClassIdAndTeacherId = getClassIdAndTeacherId;
exports.getSign = getSign;
exports.getTermId = getTermId;
exports.getTimestamp = getTimestamp;
exports.startTimer = startTimer;