From ad37ceb3fe337d307ebd8e97d2c9f316bf09aad0 Mon Sep 17 00:00:00 2001 From: "Chen,Hui Chen" Date: Thu, 13 Jun 2024 16:44:10 +0800 Subject: [PATCH] =?UTF-8?q?Add=20main=20form=E3=80=81modify=20item=5Fconte?= =?UTF-8?q?nt=20in=20.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/main.js | 72 ++++++++ controllers/todo.js | 29 --- db.js | 6 +- index.js | 4 +- js/main.js | 106 +++++++++-- models/todo.js | 24 --- pages/main.js | 4 +- scholarship.sql | 16 +- templates/main.html | 347 +++++++----------------------------- templates/main2.html | 292 ++++++++++++++++++++++++++++++ utilities/utilities.js | 6 +- utilities/utilities_main.js | 133 ++++++++++++++ views/todo.ejs | 3 - views/todos.ejs | 165 ----------------- 14 files changed, 670 insertions(+), 537 deletions(-) create mode 100644 api/main.js delete mode 100644 controllers/todo.js delete mode 100644 models/todo.js create mode 100644 templates/main2.html create mode 100644 utilities/utilities_main.js delete mode 100644 views/todo.ejs delete mode 100644 views/todos.ejs diff --git a/api/main.js b/api/main.js new file mode 100644 index 0000000..7428bd7 --- /dev/null +++ b/api/main.js @@ -0,0 +1,72 @@ +// Required modules +const router = require('express').Router(); +const util = require("./../utilities/utilities_main.js"); +const moment = require("moment"); + +router.get("/", async function(req, res) { + try { + let conn; + try { + conn = await util.getDBConnection(); // get connection from db + const result = await conn.query("SELECT item_content FROM item_info;"); + res.json(result); + } + catch(e) { + console.error(e); + res.json({suc : false}); + } + finally { + util.closeDBConnection(conn); // close db connection + } + } + catch(e) { + console.log(e); + } +}) + +router.post("/", async function(req, res) { + try { + let conn; + try { + console.log(req.body); + // data + const apply_infos = req.body.apply_infos; // get data from request + const time = moment(new Date()).format("YYYY-MM-DD"); + + conn = await util.getDBConnection(); // get connection from db + await conn.beginTransaction(); + + // insert data into table : scholarship_application + const scholarship_application_info = await conn.batch("INSERT INTO scholarship_application(`application_date`, `student_id`) VALUES(?, ?);", [time, req.body.student_id]); + const scholarship_application_id = scholarship_application_info.insertId; // get the application_id of previous record + + // 這邊你們要再改 + // const application_unit = "test unit"; + // const subsidy = 1000; + + console.log(scholarship_application_info.insertId); + console.log(apply_infos); + + // insert each apply item into item_form + for (let i = 0;i < apply_infos.length;i++) { + await conn.batch("INSERT INTO item_form(`application_id`, `item_info_id`, `application_unit`, `subsidy`) VALUES(?, ?, ?, ?);", [scholarship_application_id, apply_infos[i], application_unit, subsidy]); + } + await conn.commit(); + + res.json({suc : true}); + } + catch(e) { + console.error(e); + await conn.rollback(); + res.json({suc : false}); + } + finally { + util.closeDBConnection(conn); // close db connection + } + } + catch(e) { + console.log(e); + } +}) + +module.exports = router; \ No newline at end of file diff --git a/controllers/todo.js b/controllers/todo.js deleted file mode 100644 index bebb5cc..0000000 --- a/controllers/todo.js +++ /dev/null @@ -1,29 +0,0 @@ -// // 先從 model 引入 todos 資料 -// const todoModel = require('../models/todo') - -// const todoController = { -// getAll: (req, res) => { -// // 改成 callback 非同步操作 -// todoModel.getAll((err, results) => { -// // 如果有 err 就印出錯誤訊息 -// if (err) return console.log(err); -// // 不然就把 todos 傳給 view -// res.render('todos', { -// todos: results -// }) -// }) -// }, - -// get: (req, res) => { -// const id = req.params.id -// todoModel.get(id, (err, results) => { -// if (err) return console.log(err); -// res.render('todos', { -// // 注意回傳的結果 array,必須取 results[0] 才會是一個 todo -// todos: results[0] -// }) -// }) -// } -// } - -// module.exports = todoController \ No newline at end of file diff --git a/db.js b/db.js index cf8992a..b6f6dba 100644 --- a/db.js +++ b/db.js @@ -23,14 +23,14 @@ module.exports = connection; // require module const jwt = require('jsonwebtoken'); -const db = require("mariadb"); +const db = require("mysql"); // create pool const pool = db.createPool({ connectionLimit : 500, host : 'localhost', - user : 'test', - password : '123', + user : 'root', + password : '', database : 'scholarship' }); diff --git a/index.js b/index.js index a277937..52d0961 100644 --- a/index.js +++ b/index.js @@ -15,14 +15,14 @@ app.use(cookieParser()); //解析 HTTP 請求的 cookie // // routing // // pages +app.use("/example", require("./pages/example.js")); -// app.use("/main", require("./pages/main.js")); ++app.use("/main", require("./pages/main.js")); // app.use("/login", require("./pages/login.js")); // app.use("/apply", require("./pages/apply.js")); // // api // app.use("/api/login", require("./api/login.js")); app.use("/api/example", require("./api/example.js")); - +app.use("/api/main", require("./api/main.js")); // // static files app.use('/js', express.static('./js')); app.use('/css', express.static('./css')); diff --git a/js/main.js b/js/main.js index 53261cf..ba11358 100644 --- a/js/main.js +++ b/js/main.js @@ -1,17 +1,97 @@ -console.log("hello world"); +let item_info_len = 0; -async function submit() { - const account = document.getElementById("account").value; - const password = document.getElementById("password").value; - console.log(account, password); - data = {account : account, password : password}; - let suc_login = await axios.post('/login', data); - suc_login = suc_login.data; - console.log(suc_login); - if (suc_login.suc) { - location.href = '/main'; +async function getItemInfo() { + // get the data from table : item_info + try { + let result = await axios.get('/api/main'); + result = result.data; + return result; + } + catch (e) { + console.error(e); + return null; + } +} + +async function putDataInTable(table_id) { + // get data + const data = await getItemInfo(); + if (data != null && table_id != null) { + // insert data into table + let table_content = document.getElementById(table_id).innerHTML; + + for (let i = 0; i < data.length; i++) { + if (i === 2 || i === 3 || i === 10 || i === 11) { + table_content += ` + + + ${data[i].item_content} +
申請單位:
+
獲得補助金額:
+ `; + } else if (i === 4 || i === 5 || i === 12 || i === 13) { + table_content += ` + + + ${data[i].item_content} +
申請單位:
+ `; + } else { + table_content += ` + + + ${data[i].item_content} + `; + } + } + + // write back data into table + document.getElementById(table_id).innerHTML = table_content; + // set info length + item_info_len = data.length; } else { - alert("帳號或密碼錯誤"); + console.error("data and table id can not be null"); } -} \ No newline at end of file +} + +// async function setStudentName() { +// // get student name, and show on page +// document.getElementById("student_name").innerHTML = "test"; +// } + +async function sendApplyData() { + // get student name + const student_name = document.getElementById("student_name").innerHTML; + const student_id = document.getElementById("student_id").innerHTML; + const department_and_grade = document.getElementById("department_and_grade").innerHTML; + const advisor_name = document.getElementById("advisor_name").innerHTML; + // get checked infos + let apply_infos = []; + let application_units = []; + let subsidy_amounts = []; + for (let i = 0; i < item_info_len; i++) { + if (document.getElementById(`checkbox_${i}`).checked) { + apply_infos.push(i + 1); + // get unit、subsidy + if (document.getElementById(`unit_${i}`) && document.getElementById(`subsidy_${i}`)) { + application_units.push(document.getElementById(`unit_${i}`).value); + subsidy_amounts.push(document.getElementById(`subsidy_${i}`).value); + } else { + application_units.push(null); + subsidy_amounts.push(null); + } + } + } + + const data = {student_id,student_name,department_and_grade, advisor_name, + apply_infos, + application_units, + subsidy_amounts + }; + // send data + let result = await axios.post('/api/main', data); + console.log(result); +} + +putDataInTable("info_item"); \ No newline at end of file diff --git a/models/todo.js b/models/todo.js deleted file mode 100644 index 349e171..0000000 --- a/models/todo.js +++ /dev/null @@ -1,24 +0,0 @@ -// // 引入 db,也就是 connection -// const db = require('../db') - -// const todoModel = { -// // 這裡要用 callback 來拿取資料 -// getAll: (cb) => { -// db.query( -// 'SELECT * FROM todos', (err, results) => { -// if (err) return cb(err); -// // cb: 第一個參數為是否有錯誤,沒有的話就是 null,第二個才是結果 -// cb(null, results) -// }); -// }, - -// get: (id, cb) => { -// db.query( -// 'SELECT * FROM todos WHERE id = ?', [id], (err, results) => { -// if (err) return cb(err); -// cb(null, results) -// }); -// } -// } - -// module.exports = todoModel \ No newline at end of file diff --git a/pages/main.js b/pages/main.js index 624b859..1502f9a 100644 --- a/pages/main.js +++ b/pages/main.js @@ -1,5 +1,6 @@ const router = require('express').Router(); -const util = require("./../utilities/utilities.js"); +const { error } = require('console'); +const util = require("./../utilities/utilities_main.js"); // processing request router.get('/', async function(req, res) { @@ -8,6 +9,7 @@ router.get('/', async function(req, res) { } catch(e) { console.log(e); + res.json({error_msg : "failed to access the file"}); } return; }); diff --git a/scholarship.sql b/scholarship.sql index 5482545..3e13961 100644 --- a/scholarship.sql +++ b/scholarship.sql @@ -119,18 +119,18 @@ CREATE TABLE `item_info` ( INSERT INTO `item_info` (`item_info_id`, `item_content`) VALUES (1, '參加校內、外(含國內、國際性)資管領域相關專業或學術性重要比賽得獎者。'), (2, '參加國際性學術研討會或國際性會議並以外文發表論文者(不含摘要及海報論文):'), -(3, ' 已向校內申請補助,申請單位:,獲得補助金額'), -(4, '已向校外申請補助,申請單位:,獲得補助金額'), -(5, '已向校內申請補助,申請單位:,未獲得補助(需付未獲得補助證明)'), -(6, '已向校外申請補助,申請單位:未獲得補助(需付未獲得補助證明)'), +(3, ' 已向校內申請補助'), +(4, '已向校外申請補助'), +(5, '已向校內申請補助,未獲得補助(需付未獲得補助證明)'), +(6, '已向校外申請補助,未獲得補助(需付未獲得補助證明)'), (7, '未向校內外單位申請補助'), (8, '促進本系招生、聲譽提升、協助教學或行政工作等有具體事蹟者'), (9, '碩士畢業生學位口試以英文發表者 參加資管領域相關學術研討會並榮獲最佳論文獎'), (10, '獲得「國科會大專學生研究計畫獎勵」者口 學生申請國際交換生'), -(11, ' 已向校內申請補助,申請單位:,獲得補助金額'), -(12, '已向校外申請補助,申請單位:,獲得補助金額'), -(13, '已向校內申請補助,申請單位:,未獲得補助(需付未獲得補助證明)'), -(14, '已向校外申請補助,申請單位:未獲得補助(需付未獲得補助證明)'), +(11, ' 已向校內申請補助'), +(12, '已向校外申請補助'), +(13, '已向校內申請補助,未獲得補助(需付未獲得補助證明)'), +(14, '已向校外申請補助,未獲得補助(需付未獲得補助證明)'), (15, '未向校內外單位申請補助'), (16, '參加國內學術研討會發表論文並獲獎者'), (17, '學生參加校外學術性比賽之交通費補助'), diff --git a/templates/main.html b/templates/main.html index 0c37ebb..89a21da 100644 --- a/templates/main.html +++ b/templates/main.html @@ -1,292 +1,67 @@ - - - Main Page - - - 國立暨南國際大學資管系獎學金申請表 - - -

國立暨南國際大學資管系獎學金申請表

-
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
+ + + 國立暨南國際大學資管系獎學金申請表 + + +

國立暨南國際大學資管系獎學金申請表

+
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+


申請項目及說明

-
- - -
-
- - -
-
- - - - - -
-
- - - - - -
-
- - - - -
-
- - - - -
-
- -
- - -
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - - - - -
-
- - - - - -
-
- - - - -
-
- - - - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - - -
-
- - -
-
- -
- -
-
-
- - -
- -

證明文件是否備齊

-
- - - - - -
- -

學生事務委員會審核結果

-
- - - - 次通過,核發獎學金 - 元 -
- - - -
-
- - -
-
- - -
-
- - -
-
-
- - - -
-
-
-
-
- - -
-

- 說明:
- 一、依據「國立暨南國際大學資訊管理學系獎助學金作業要點」辦理。
- 二、申辦流程:由學生詳填本申請書並檢附相關證明文件,逕送系辦審查資料是否齊全,提交學生事務委員會審核後,由系辦通知申請學生核定結果(獎學金將逕行撥入申請學生帳戶)。
- 三、獲得本系獎助學金補助者須提供申請事項之詳細資訊,供系上公告與相關宣傳使用。若後續有召開分享會等相關需要,獲補助者需配合辦理。 - -

-
- - - - - - -
- - - - - - - - - - - - - - + + + + + +
是否申請項目內容
+ - - - + + + \ No newline at end of file diff --git a/templates/main2.html b/templates/main2.html new file mode 100644 index 0000000..a688f88 --- /dev/null +++ b/templates/main2.html @@ -0,0 +1,292 @@ + + + + + Main Page + + + 國立暨南國際大學資管系獎學金申請表 + + +

國立暨南國際大學資管系獎學金申請表

+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+

申請項目及說明

+
+ + +
+
+ + +
+
+ + + + + +
+
+ + + + + +
+
+ + + + +
+
+ + + + +
+
+ +
+ + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + + + +
+
+ + + + + +
+
+ + + + +
+
+ + + + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + +
+
+ + +
+
+ +
+ +
+
+
+ + +
+ +

證明文件是否備齊

+
+ + + + + +
+ +

學生事務委員會審核結果

+
+ + + + 次通過,核發獎學金 + 元 +
+ + + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + + +
+
+
+
+
+ + +
+

+ 說明:
+ 一、依據「國立暨南國際大學資訊管理學系獎助學金作業要點」辦理。
+ 二、申辦流程:由學生詳填本申請書並檢附相關證明文件,逕送系辦審查資料是否齊全,提交學生事務委員會審核後,由系辦通知申請學生核定結果(獎學金將逕行撥入申請學生帳戶)。
+ 三、獲得本系獎助學金補助者須提供申請事項之詳細資訊,供系上公告與相關宣傳使用。若後續有召開分享會等相關需要,獲補助者需配合辦理。 + +

+
+ + + + + + +
+ + + + +
+ + + + + + + + + + + + + diff --git a/utilities/utilities.js b/utilities/utilities.js index b059ff6..8a4e34e 100644 --- a/utilities/utilities.js +++ b/utilities/utilities.js @@ -1,13 +1,13 @@ // require module const jwt = require('jsonwebtoken'); -const db = require("mariadb"); +const db = require("mysql"); // create pool const pool = db.createPool({ connectionLimit : 500, host : 'localhost', - user : 'test', - password : '123', + user : 'root', + password : '', database : 'scholarship' }); diff --git a/utilities/utilities_main.js b/utilities/utilities_main.js new file mode 100644 index 0000000..b5674f8 --- /dev/null +++ b/utilities/utilities_main.js @@ -0,0 +1,133 @@ +// require module +const jwt = require('jsonwebtoken'); +const db = require("mysql"); + +// create pool +const pool = db.createPool({ + connectionLimit : 500, + host : 'localhost', + user : 'user', + password : '', + database : 'scholarship' +}); + +// global variable +const jwt_key = "goodjwtkey"; + +module.exports = { + // shared variable + jwt_key, + + // shared function + + signJwtToken: function(data) { + try { + const result = jwt.sign({ data, exp: Math.floor(Date.now() / 1000) + (60 * 15) }, jwt_key); + return result; + } + catch (e) { + console.log(e); + } + }, + + authenToken: function(token) { + return new Promise((resolve, reject) => { + try { + const data = jwt.verify(token, jwt_key).data; + if (data.uid) { + resolve(true); + } else { + resolve(false); + } + } catch (error) { + console.error(error); + resolve(false); + } + }); + }, + + loginAuthentication: function(account, password) { + return new Promise((resolve, reject) => { // 包裝成 Promise + const spawn = require("child_process").spawn; + const pythonScript = path.join(__dirname, 'catch.py'); // path/to/catch.py + const pythonProcess = spawn('python', [pythonScript, account, password]); + + //console.log(`account: ${account}`); + //console.log(`password: ${password}`); + + pythonProcess.stdout.on('data', (data) => { + data = data.toString().slice(0, -1); // remove the last char + if (data === 'login falied') { + resolve(false); // 登入失敗,解析 Promise 為 True + } else { + resolve(data.toString().trim()); // 登入成功 + } + }); + + pythonProcess.stderr.on('data', (data) => { + console.error(`stderr: ${data.toString()}`); + }); + + pythonProcess.on('exit', (code) => { + //console.log(`child process exited with code ${code}`); + if (code !== 0) { + reject(new Error(`child process exited with code ${code}`)); // 非 0 退出代碼表示錯誤 + } + }); + + pythonProcess.on('error', (err) => { + console.error(err); + reject(err); // 子進程啟動失敗 + }); + }); + }, + + // get parent absolute path + getParentPath : function(dir) { + try { + n_dir = ""; + dir = dir.split(""); + // determine the type of slash, which will be different between windows and linux + if (dir.includes("/")) { + slash_type = "/"; + } + else { + slash_type = "\\"; + } + // pop the last one directory + while (dir.pop() != "\\") { + // pass + } + // restructure the full path + for (let i = 0;i < dir.length;i++) { + n_dir += dir[i]; + } + return n_dir; + } + catch(e) { + console.log(e); + } + }, + + // return connection of db + getDBConnection : async function() { + try { + const conn = await pool.getConnection(); + return conn; + } + catch(e) { + console.error("error getting db connection : ", e); + return null; + } + }, + + // close connection of db + closeDBConnection : function(conn) { + try { + conn.release(); + } + catch(e) { + console.error("error closing db connection : ", e); + } + } +} diff --git a/views/todo.ejs b/views/todo.ejs deleted file mode 100644 index f15682f..0000000 --- a/views/todo.ejs +++ /dev/null @@ -1,3 +0,0 @@ -

Todo

- -

<%= todo %>

\ No newline at end of file diff --git a/views/todos.ejs b/views/todos.ejs deleted file mode 100644 index 5d34195..0000000 --- a/views/todos.ejs +++ /dev/null @@ -1,165 +0,0 @@ - - - - - - - 國立暨南國際大學資管系獎學金申請表 - - -

國立暨南國際大學資管系獎學金申請表

-
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
-
-

申請項目及說明

-
- - -
-
- - -
-
-
- - - - - -
-
- - - - - -
-
- - - - -
-
- - - - -
-
- - -
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
-
- - - - - -
-
- - - - - -
-
- - - - -
-
- - - - -
-
- - -
-
-
- - -
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
- -
-
- -