From c1df96118f11440fdbd81e1ff12f2f7305ea661d Mon Sep 17 00:00:00 2001 From: SjvavE Date: Sun, 15 Oct 2023 14:20:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E7=9B=B8=E5=85=B3=E7=9A=84=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E5=8C=85=E6=8B=AC=E5=AF=B9=E8=AF=BE=E7=A8=8B=E7=9A=84?= =?UTF-8?q?=E6=8B=89=E5=8F=96=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 1 - src/workserver/controllers/xah/course.js | 79 ++++++++++++++++++- src/workserver/controllers/xah/task.js | 98 +++++++++++++++++++++++- src/workserver/model/xah/course.js | 8 ++ src/workserver/model/xah/task.js | 5 ++ src/workserver/routes/index.js | 2 +- src/workserver/routes/xah/course.js | 15 ++-- src/workserver/routes/xah/task.js | 26 ++++++- 8 files changed, 219 insertions(+), 15 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..6e86672 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/workserver/controllers/xah/course.js b/src/workserver/controllers/xah/course.js index 4f12270..861b317 100644 --- a/src/workserver/controllers/xah/course.js +++ b/src/workserver/controllers/xah/course.js @@ -1,5 +1,6 @@ const Course = require('../../model/xah/course'); - +const {spawn}=require('child_process'); +const path = require('path'); const courseController = { // 展示一个课程的详细内容 @@ -20,10 +21,12 @@ const courseController = { }, - // 展示所有课程 + // 展示(一个)用户的所有课程 showCourse: async function(req,res,next){ try{ - let courseData = await Course.all() + const userId=req.body.userId; + //let courseData = await Course.getCourseByUserId(userId) + let courseData = await Course.all(); console.log(courseData); res.json({ code: 200, @@ -34,7 +37,7 @@ const courseController = { res.json({ code: 0, message: "操作失败", data: e }) } }, - // 新增一个课程 + // 给某个用户新增一个课程 addCourse: async function(req, res, next) { try { const courseData = req.body; // 从请求中获取用户数据 @@ -88,6 +91,74 @@ const courseController = { data: e }); } + }, + + //爬取学校课程相关的信息并返回 + getAllCourse:async function(req, res, next) { + try { + let info = req.body; + //let userid = info.userId; + const passwd = info.passwd; + const year = info.year; + const term = info.term; + console.log(info); + //console.log(userid); + console.log(passwd); + console.log(year); + console.log(term); + + //定位到脚本的位置 + const pythonScriptPath = path.join(__dirname,'..', '..', 'utils', 'getCourse', 'getSchedule.py'); + + //用子进程执行代码 + const pythonProcess = spawn('python', [pythonScriptPath]); + + //输入部分 + pythonProcess.stdin.write(info.userId + '\n'); + pythonProcess.stdin.write(passwd + '\n'); + pythonProcess.stdin.write(year + '\n'); + pythonProcess.stdin.write(term + '\n'); + + //关闭 + pythonProcess.stdin.end(); + + let ret = ''; + + //获取输出 + pythonProcess.stdout.on('data', (data) => { + ret += data.toString(); + }); + + //处理数据 + pythonProcess.on('close', (code) => { + if (code === 0) { + // Successfully executed the Python script + // 'ret' now contains the output from the Python script + console.log('Python script执行成功'); + console.log('输出结果:', ret); + + // You can now send 'ret' as a response to your API request + res.json({ + code: 1, // or another success code + message: '获取课程成功', + data: ret, + }); + } else { + // Error in executing the Python script + res.json({ + code: 0, + message: '获取课程失败', + data: 'Python script执行失败', + }); + } + }); + } catch (e) { + res.json({ + code: 0, + message: '获取课程失败', + data: e, + }); + } } } diff --git a/src/workserver/controllers/xah/task.js b/src/workserver/controllers/xah/task.js index a442647..65dff1d 100644 --- a/src/workserver/controllers/xah/task.js +++ b/src/workserver/controllers/xah/task.js @@ -1 +1,97 @@ -const Task = require('../../model/xah/task'); \ No newline at end of file +const task = require('../../model/xah/task'); + +const taskController = { + + // 展示一个任务的详细内容 + showOnetask: async function(req,res,next){ + try{ + const id=req.body.id; + console.log(id); + let taskData = await task.selectOne(id); + console.log(taskData); + res.json({ + code: 200, + message: "操作成功", + data: taskData + }) + }catch(e){ + res.json({ code: 0, message: "操作失败", data: e }) + } + }, + + + // 展示一个用户的所有任务 + showtask: async function(req,res,next){ + try{ + const id=req.body.userId; + console.log(id); + let taskData = await task.getTaskByUserId(id) + //let taskData = await task.all() + console.log(taskData); + res.json({ + code: 200, + message: "操作成功", + data: taskData + }) + }catch(e){ + res.json({ code: 0, message: "操作失败", data: e }) + } + }, + // 新增一个任务 + addtask: async function(req, res, next) { + try { + const taskData = req.body; // 从请求中获取用户数据 + console.log(taskData); + const result = await task.insert(taskData); + res.json({ + code: 200, + message: "任务添加成功", + data: result + }); + } catch (e) { + res.json({ + code: 0, + message: "任务添加失败", + data: e + }); + } + }, + //修改任务内容 + updatetask: async function(req, res, next) { + try { + const taskData = req.body; // 从请求中获取用户数据 + const result = await task.update(taskData.id,taskData); + res.json({ + code: 200, + message: "任务修改成功", + data: taskData + }); + } catch (e) { + res.json({ + code: 0, + message: "任务修改失败", + data: e + }); + } + }, + //删除任务内容 + deletetask: async function(req, res, next) { + try { + const id = req.body.id; // 从请求中获取用户数据 + const result = await task.delete(id); + res.json({ + code: 200, + message: "任务删除成功", + data: result + }); + } catch (e) { + res.json({ + code: 0, + message: "任务删除失败", + data: e + }); + } + } + } + + module.exports = taskController; \ No newline at end of file diff --git a/src/workserver/model/xah/course.js b/src/workserver/model/xah/course.js index 305a947..2437991 100644 --- a/src/workserver/model/xah/course.js +++ b/src/workserver/model/xah/course.js @@ -1,10 +1,18 @@ const Base = require('../base'); +const knex=require('../kenx') class Course extends Base { // 定义参数默认值为 user 表 constructor(props = 'course'){ super(props); } + + //根据用户id查询用户所有的课程信息 + async getCourseByUserId(userId){ + return await knex.select('*').from('course').where('userId',userId) + } + + } module.exports = new Course(); diff --git a/src/workserver/model/xah/task.js b/src/workserver/model/xah/task.js index 6b4c6f4..a0b40ec 100644 --- a/src/workserver/model/xah/task.js +++ b/src/workserver/model/xah/task.js @@ -1,10 +1,15 @@ const Base = require('../base'); +const knex=require('../kenx') class Task extends Base { // 定义参数默认值为 user 表 constructor(props = 'tasks'){ super(props); } + //根据用户id查询用户所有的课程信息 + async getTaskByUserId(userId){ + return await knex.select('*').from('tasks').where('userId',userId) + } } module.exports = new Task(); diff --git a/src/workserver/routes/index.js b/src/workserver/routes/index.js index 385a70a..a93a3db 100644 --- a/src/workserver/routes/index.js +++ b/src/workserver/routes/index.js @@ -9,9 +9,9 @@ router.get('/get_user', userController.showUser); router.post('/add_user', userController.addUser); - //引入和clocks相关的接口 require('./xah/course')(router); +require('./xah/task')(router); module.exports = router; diff --git a/src/workserver/routes/xah/course.js b/src/workserver/routes/xah/course.js index ff4a687..1ccecd5 100644 --- a/src/workserver/routes/xah/course.js +++ b/src/workserver/routes/xah/course.js @@ -1,24 +1,27 @@ const courseController=require('../../controllers/xah/course'); -const clockControllers= function clockControllers(router){ +const courseControllers= function courseControllers(router){ - //查看所有的番茄时钟 + //查看一个用户的所有课程 router.get('/get_course', courseController.showCourse); - //添加一个课程 + //给某个用户添加一个课程 router.post('/add_course', courseController.addCourse); - //查询课程具体内容 + //查询某个课程具体内容 router.get('/get_one',courseController.showOneCourse) - //修改课程内容 + //修改某个课程内容 router.put('/update_course',courseController.updateCourse) //删除课程 router.delete('/delete_course',courseController.deleteCourse) + //爬取学校网站的接口 + router.get('/get_all_course',courseController.getAllCourse) + } -module.exports=clockControllers; \ No newline at end of file +module.exports=courseControllers; \ No newline at end of file diff --git a/src/workserver/routes/xah/task.js b/src/workserver/routes/xah/task.js index 81a0482..fd42cdd 100644 --- a/src/workserver/routes/xah/task.js +++ b/src/workserver/routes/xah/task.js @@ -1,2 +1,24 @@ -var express = require('express'); -var router = express.Router(); \ No newline at end of file +const taskController=require('../../controllers/xah/task'); + + +const taskControllers= function taskControllers(router){ + + //查看一个人所有的任务 + router.get('/get_task', taskController.showtask); + + //添加一个课程 + router.post('/add_task', taskController.addtask); + + //查询课程具体内容 + router.get('/get_one',taskController.showOnetask) + + //修改课程内容 + router.put('/update_task',taskController.updatetask) + + //删除课程 + router.delete('/delete_task',taskController.deletetask) + + +} + +module.exports=taskControllers; \ No newline at end of file