Skip to content

Commit

Permalink
feat: 更改獲取修課明細function
Browse files Browse the repository at this point in the history
現在可以獲取指定學期的修課明細了
  • Loading branch information
Chinlinlee committed Jan 11, 2021
1 parent efd1332 commit 695987a
Showing 1 changed file with 53 additions and 28 deletions.
81 changes: 53 additions & 28 deletions routes/api/Course/controller/get_Course.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const myFunc = require("../../../My_Func");
const fetch = require('node-fetch');
const _ = require("lodash");

//const QueryParams = require("../../../models/common/httparams.js");


Expand All @@ -19,44 +20,68 @@ async function getCourse(req) {
if (req.session.Course.length > 0) {
return Promise.resolve(req.session.Course);
}
let courses_URL = `http://system8.ntunhs.edu.tw/myNTUNHS_student/Modules/Profile/qry/Profile_qry_14.aspx?stno=undefined&size=100&action=LoadJSon`;
let courseJson = await getCourseJson(req);
if (!courseJson) {
return Promise.resolve(false);
}
req.session.Course = courseJson;
return Promise.resolve(courseJson);
}

async function getCourseJson (req , semno) {
let parameter = new URLSearchParams({
st_no : req.session.STNO ,
sem_no : semno ,
size : 100 ,
action : "LoadJSon"
});
if (!semno) {
parameter.delete('sem_no');
}
let courses_URL = new URL("http://system8.ntunhs.edu.tw/myNTUNHS_student/Modules/Profile/qry/Profile_qry_14.aspx");
courses_URL.search = parameter;
let reqOption = {
method: "GET",
uri: courses_URL,
uri: courses_URL.href,
}
let fetchCookie = require('fetch-cookie')(fetch, myFunc.getJar(req));
let courseFetch = await fetchCookie(reqOption.uri, {
method: reqOption.method
});
let course = await courseFetch.text();
let courseJson = "";
let course = "";
try {
let courseJson = await courseFetch.json();
_.unset(courseJson, "0");
let result = [];
for (let i in courseJson) {
let item = courseJson[i];
let courseCode = item.課程代碼與名稱_L.substr(0, 10);
let courseClassCode = item.上課班級_L.substr(3 ,2);
let courseFullCode = courseCode + courseClassCode;
result.push({
Name: item.課程代碼與名稱_L.substr(11),
Code: courseCode ,
FullCode : courseFullCode ,
Place: item.教室,
Day: item.星期,
Period: item.節次 ,
Credit: item.學分,
Type: item.課程性質,
Teacher: item.任課教師_L.replace(/<br\/>/gi, ""),
Other: item.備註_L
});
}
course = result;
} catch (e) {
course = [];
}
try {
courseJson = JSON.parse(course);
return Promise.resolve(course);
} catch (e) {
console.error(e);
return Promise.resolve(false);
}
_.unset(courseJson, "0");
let result = [];
for (let i in courseJson) {
let item = courseJson[i];
let courseCode = item.課程代碼與名稱_L.substr(0, 10);
let courseClassCode = item.上課班級_L.substr(3 ,2);
let courseFullCode = courseCode + courseClassCode;
result.push({
Name: item.課程代碼與名稱_L.substr(11),
Code: courseCode ,
FullCode : courseFullCode ,
Place: item.教室,
Day: item.星期,
Period: item.節次 ,
Credit: item.學分,
Type: item.課程性質,
Teacher: item.任課教師_L.replace(/<br\/>/gi, ""),
Other: item.備註_L
});
}
req.session.Course = result;
return Promise.resolve(result);
}

module.exports.getCourse = getCourse;
module.exports.getCourse = getCourse;
module.exports.getCourseJson = getCourseJson;

0 comments on commit 695987a

Please sign in to comment.