From 03be02b5bbc1eab149f9f20b7a0895227180c4fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=8E=AE=E9=93=AD?= <2373854303@qq.com> Date: Mon, 19 May 2025 11:34:08 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=8F=90=E4=BA=A4519=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\347\246\273\351\241\271\347\233\256.md" | 660 ++++++++++++++++++ 1 file changed, 660 insertions(+) create mode 100644 "\350\224\241\347\216\256\351\223\255/20250519 \345\256\214\345\226\204VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256.md" diff --git "a/\350\224\241\347\216\256\351\223\255/20250519 \345\256\214\345\226\204VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256.md" "b/\350\224\241\347\216\256\351\223\255/20250519 \345\256\214\345\226\204VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256.md" new file mode 100644 index 0000000..1ee1da3 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20250519 \345\256\214\345\226\204VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256.md" @@ -0,0 +1,660 @@ +# 完善 VueJs 前后端分离项目 + +## 课堂笔记 + +### 完善 VueJs 前后端分离项目 + +#### 一、构建项目结构: + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/20250519110052.png) + +#### 二、编写代码 + +##### 后端 + +**app.js** + +```js +// 导入依赖包 +import koa from "koa"; +// 导入 路由依赖包 +import Router from "koa-router"; +// 导入 请求体解析 依赖包 +import bodyParser from "koa-bodyparser"; +// 跨域 依赖包 +import cors from "@koa/cors"; +// 导入 sequelize、Op、DataTypes 模块 +import { Sequelize, Op, DataTypes } from "sequelize"; + +// 创建 sequelize 实例 +export const sequelize = new Sequelize({ + dialect: "sqlite", + storage: "demo.db", +}); + +// 老师模型 +const Teacher = sequelize.define("teacher", { + // 姓名 + teacherName: { + type: DataTypes.STRING, + }, + // 年龄 + teacherAge: { + type: DataTypes.INTEGER, + }, + // 性别 + teacherSex: { + type: DataTypes.STRING, + }, + // 联系方式 + phone: { + type: DataTypes.STRING, + }, + // 教学班级 + className: { + type: DataTypes.STRING, + }, +}); + +// 同步数据库 +sequelize.sync(); + +// 创建 koa 实例 +const app = new koa(); + +// 创建 路由 实例 +const router = new Router(); + +// 使用 跨域 中间件 解决跨域问题 +app.use(cors()); + +// 请求体解析 +app.use(bodyParser()); + +/** =========================== */ +/* 查询所有老师信息、查询含有关键字的老师 */ +router.get('/teachers', async (ctx, next) => { + // 条件变量 + let where; + + // 获取 查询的关键字 + const query = ctx.query.query; + + // 判断是否有 查询的关键字 + if (query) { + // 条件查询 + where = { + // 模糊查询 + [Op.or]: [ + // 姓名 + { + teacherName: { [Op.like]: `%${query}%` }, + }, + // 年龄 + { + teacherAge: { [Op.like]: `%${query}%` }, + }, + // 性别 + { + teacherSex: { [Op.like]: `%${query}%` }, + }, + // 联系方式 + { + phone: { [Op.like]: `%${query}%` }, + }, + // 教学班级 + { + className: { [Op.like]: `%${query}%` }, + }, + ], + } + } else { + where = null + } + // 查询所有老师信息 + const teachers = await Teacher.findAll({ where }); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: teachers, + msg: "查询成功" + }; +}) + +/* 查询指定 id 的老师 */ +router.get('/teachers/:id', async (ctx, next) => { + // 拿到 请求id + const id = ctx.params.id; + + // 查询所有老师信息 + const teacher = await Teacher.findByPk(id); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: teacher, + msg: "查询成功" + }; +}) + +/* 新增老师 */ +router.post('/teachers', async (ctx, next) => { + // 拿到请求对象 + const obj = ctx.request.body; + + // 新增老师 + await Teacher.create(obj); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: null, + msg: "新增成功" + }; +}) + +/* 修改老师指定 id 的老师 */ +router.put('/teachers/:id', async (ctx, next) => { + // 拿到 请求id + const id = ctx.params.id; + + // 拿到请求对象 + const obj = ctx.request.body; + + // 老师是否存在 + await Teacher.update(obj, { where: { id } }); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: null, + msg: "修改成功" + }; +}) + +/* 删除指定 id 的老师 */ +router.delete('/teachers/:id', async (ctx, next) => { + // 拿到 请求id + const id = ctx.params.id; + + // 删除老师 + await Teacher.destroy({ where: { id } }); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: null, + msg: "删除成功" + }; +}) + +/** =========================== */ +// 注册路由 +app.use(router.routes()); + +// app 启动端口 +const appPort = 8000; + +// 监听服务器端口 +app.listen(appPort); + +// 当前服务器运行地址 +console.log(`当前服务器运行地址:http://localhost:${appPort}`); +``` + +##### 前端 + +**router.js** + +```js +import { createRouter, createWebHistory } from 'vue-router' + +const router = createRouter({ + history: createWebHistory(import.meta.env.BASE_URL), + routes: [ + { + path: '/', + name: 'home', + meta: { title: '首页' }, + component: () => import('@/views/Home.vue'), + }, + { + path: '/addTeacher', + name: 'addTeacher', + meta: { title: '新增老师' }, + component: () => import('@/views/AddTeacher.vue'), + }, + { + path: '/updateTeacher/:id', + name: 'updateTeacher', + meta: { title: '修改老师' }, + component: () => import('@/views/UpdateTeacher.vue'), + }, + ], +}) + +router.beforeEach((to, from, next) => { + if (to.meta.title) { + document.title = to.meta.title + } + next() +}) + +export default router +``` + +**App.vue** + +```vue + + + + + +``` + +**Home.vue** + +```vue + + + + + +``` + +**AddTeacher.vue** + +```vue + + + + + +``` + +**updateTeacher.vue** + +```vue + + + + + +``` + +#### 三、效果演示: + +##### [项目预览](https://gitee.com/hyo-ja/vuejs-frontend-backend-separation-projects) + +##### 效果预览 + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/2025-05-19-02.gif) + +## 课后作业 -- Gitee From b85f83ff901f4ab8b9f91b2498ed2c4c3809e262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=8E=AE=E9=93=AD?= <2373854303@qq.com> Date: Tue, 20 May 2025 11:35:15 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=8F=90=E4=BA=A4520=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\347\233\256\346\242\263\347\220\206.md" | 698 ++++++++++++++++++ 1 file changed, 698 insertions(+) create mode 100644 "\350\224\241\347\216\256\351\223\255/20250520 VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256\346\242\263\347\220\206.md" diff --git "a/\350\224\241\347\216\256\351\223\255/20250520 VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256\346\242\263\347\220\206.md" "b/\350\224\241\347\216\256\351\223\255/20250520 VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256\346\242\263\347\220\206.md" new file mode 100644 index 0000000..ff6b623 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20250520 VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256\346\242\263\347\220\206.md" @@ -0,0 +1,698 @@ +# VueJs 前后端分离项目梳理 + +## 课堂笔记 + +### VueJs 前后端分离项目梳理 + +#### 一、前后端项目构建 + +##### 1. 构建项目结构: + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/20250519110052.png) + +##### 2. 构建项目: + +###### 后端 + +1. 项目初始化: + +```bash +npm init -y +``` + +2. 安装依赖包: + +```bash +npm i +``` + +###### 前端 + +1. 项目初始化: + +```bash +yarn create vue +``` + +2. 安装依赖包: + +```bash +yarn +``` + +3. 代码格式化: + +```bash +yarn format +``` + +#### 二、代码编写 + +##### 后端 + +**app.js** + +```js +// 导入依赖包 +import koa from "koa"; +// 导入 路由依赖包 +import Router from "koa-router"; +// 导入 请求体解析 依赖包 +import bodyParser from "koa-bodyparser"; +// 跨域 依赖包 +import cors from "@koa/cors"; +// 导入 sequelize、Op、DataTypes 模块 +import { Sequelize, Op, DataTypes } from "sequelize"; + +// 创建 sequelize 实例 +export const sequelize = new Sequelize({ + dialect: "sqlite", + storage: "demo.db", +}); + +// 老师模型 +const Teacher = sequelize.define("teacher", { + // 姓名 + teacherName: { + type: DataTypes.STRING, + }, + // 年龄 + teacherAge: { + type: DataTypes.INTEGER, + }, + // 性别 + teacherSex: { + type: DataTypes.STRING, + }, + // 联系方式 + phone: { + type: DataTypes.STRING, + }, + // 教学班级 + className: { + type: DataTypes.STRING, + }, +}); + +// 同步数据库 +sequelize.sync(); + +// 创建 koa 实例 +const app = new koa(); + +// 创建 路由 实例 +const router = new Router(); + +// 使用 跨域 中间件 解决跨域问题 +app.use(cors()); + +// 请求体解析 +app.use(bodyParser()); + +/** =========================== */ +/* 查询所有老师信息、查询含有关键字的老师 */ +router.get('/teachers', async (ctx, next) => { + // 条件变量 + let where; + + // 获取 查询的关键字 + const query = ctx.query.query; + + // 判断是否有 查询的关键字 + if (query) { + // 条件查询 + where = { + // 模糊查询 + [Op.or]: [ + // 姓名 + { + teacherName: { [Op.like]: `%${query}%` }, + }, + // 年龄 + { + teacherAge: { [Op.like]: `%${query}%` }, + }, + // 性别 + { + teacherSex: { [Op.like]: `%${query}%` }, + }, + // 联系方式 + { + phone: { [Op.like]: `%${query}%` }, + }, + // 教学班级 + { + className: { [Op.like]: `%${query}%` }, + }, + ], + } + } else { + where = null + } + // 查询所有老师信息 + const teachers = await Teacher.findAll({ where }); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: teachers, + msg: "查询成功" + }; +}) + +/* 查询指定 id 的老师 */ +router.get('/teachers/:id', async (ctx, next) => { + // 拿到 请求id + const id = ctx.params.id; + + // 查询所有老师信息 + const teacher = await Teacher.findByPk(id); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: teacher, + msg: "查询成功" + }; +}) + +/* 新增老师 */ +router.post('/teachers', async (ctx, next) => { + // 拿到请求对象 + const obj = ctx.request.body; + + // 新增老师 + await Teacher.create(obj); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: null, + msg: "新增成功" + }; +}) + +/* 修改老师指定 id 的老师 */ +router.put('/teachers/:id', async (ctx, next) => { + // 拿到 请求id + const id = ctx.params.id; + + // 拿到请求对象 + const obj = ctx.request.body; + + // 老师是否存在 + await Teacher.update(obj, { where: { id } }); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: null, + msg: "修改成功" + }; +}) + +/* 删除指定 id 的老师 */ +router.delete('/teachers/:id', async (ctx, next) => { + // 拿到 请求id + const id = ctx.params.id; + + // 删除老师 + await Teacher.destroy({ where: { id } }); + + // 响应状态码 + ctx.status = 200; + + // 响应 Json 数据 + ctx.body = { + code: 200, + data: null, + msg: "删除成功" + }; +}) + +/** =========================== */ +// 注册路由 +app.use(router.routes()); + +// app 启动端口 +const appPort = 8000; + +// 监听服务器端口 +app.listen(appPort); + +// 当前服务器运行地址 +console.log(`当前服务器运行地址:http://localhost:${appPort}`); +``` + +##### 前端 + +**router.js** + +```js +import { createRouter, createWebHistory } from 'vue-router' + +const router = createRouter({ + history: createWebHistory(import.meta.env.BASE_URL), + routes: [ + { + path: '/', + name: 'home', + meta: { title: '首页' }, + component: () => import('@/views/Home.vue'), + }, + { + path: '/addTeacher', + name: 'addTeacher', + meta: { title: '新增老师' }, + component: () => import('@/views/AddTeacher.vue'), + }, + { + path: '/updateTeacher/:id', + name: 'updateTeacher', + meta: { title: '修改老师' }, + component: () => import('@/views/UpdateTeacher.vue'), + }, + ], +}) + +router.beforeEach((to, from, next) => { + if (to.meta.title) { + document.title = to.meta.title + } + next() +}) + +export default router +``` + +**App.vue** + +```vue + + + + + +``` + +**Home.vue** + +```vue + + + + + +``` + +**AddTeacher.vue** + +```vue + + + + + +``` + +**updateTeacher.vue** + +```vue + + + + + +``` + +#### 三、效果演示 + +##### [项目预览](https://gitee.com/hyo-ja/vuejs-frontend-backend-separation-projects) + +##### 效果预览 + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/2025-05-19-02.gif) + +## 课后作业 -- Gitee From cd14f395cc230e0d1df1f6f2b64fd170a620804d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=8E=AE=E9=93=AD?= <2373854303@qq.com> Date: Wed, 21 May 2025 18:48:11 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=8F=90=E4=BA=A4521=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\347\246\273\351\241\271\347\233\256.md" | 598 ++++++++++++++++++ 1 file changed, 598 insertions(+) create mode 100644 "\350\224\241\347\216\256\351\223\255/20250521 \351\207\215\345\206\231VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256.md" diff --git "a/\350\224\241\347\216\256\351\223\255/20250521 \351\207\215\345\206\231VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256.md" "b/\350\224\241\347\216\256\351\223\255/20250521 \351\207\215\345\206\231VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256.md" new file mode 100644 index 0000000..3c87d85 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20250521 \351\207\215\345\206\231VueJs\345\211\215\345\220\216\347\253\257\345\210\206\347\246\273\351\241\271\347\233\256.md" @@ -0,0 +1,598 @@ +# 重写 VueJs 前后端分离项目梳理 + +## 课堂笔记 + +### 重写 VueJs 前后端分离项目梳理 + +#### 一、前后端项目构建 + +##### 1. 构建项目结构: + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/20250519110052.png) + +##### 2. 构建项目: + +###### 后端 + +1. 项目初始化: + +```bash +npm init -y +``` + +2. 安装依赖包: + +```bash +npm i +``` + +###### 前端 + +1. 项目初始化: + +```bash +yarn create vue +``` + +2. 安装依赖包: + +```bash +yarn +``` + +3. 代码格式化: + +```bash +yarn format +``` + +#### 二、代码编写 + +##### 后端 + +**app.js** + +```js +// 导入依赖包 +import koa from "koa"; +import Router from "koa-router"; +import bodyParser from "koa-bodyparser"; +import cors from "@koa/cors"; +import { DataTypes, Sequelize, Op, STRING, DOUBLE, FLOAT, where } from "sequelize"; + +// 创建 koa 实例 +const app = new koa(); + +// 创建 sequelize 实例 +const sequelize = new Sequelize('test', 'root', '123456', { + dialect: "mysql", +}) + +// 定义模型 +const Book = sequelize.define('books', { + // 图书名称 + bookName: { + type: STRING + }, + // 作者 + autor: { + type: STRING + }, + // 价格 + price: { + type: FLOAT(2) + }, +}) + +// 同步模型 +// sequelize.sync(); + +// 插入数据 +// Book.create({ +// // 图书名称 +// bookName: "西游记2", +// // 作者 +// autor: "吴承恩", +// // 价格 +// price: 18.00 +// }) + +// 解决跨域问题 +app.use(cors()); + +// 请求体解析 +app.use(bodyParser()); + +// 创建路由实例 +const router = new Router() + +/** 路由方法 */ +// 查询所有图书 +router.get('/books', async (ctx, next) => { + // 获取关键词 + const keyWord = ctx.query.keyWord; + + // 查询条件 + let where; + + // 判断是否使用模糊查询 + if (keyWord) { + where = { + [Op.or]: { + bookName: { + [Op.like]: `%${keyWord}%` + }, + autor: { + [Op.like]: `%${keyWord}%` + }, + price: { + [Op.eq]: keyWord + } + } + } + } + else { + where = null; + } + + // 查询所有图书 + const books = await Book.findAll({ where }) + + // 成功状态码 + ctx.status = 200; + + // 响应数据 + ctx.body = { + code: 200, + data: books, + msg: "查询成功" + } +}) + +// 查询一本图书 +router.get('/books/:id', async (ctx, next) => { + // 获取 id + const id = ctx.params.id; + + // 查询所有图书 + const book = await Book.findByPk(id); + + // 成功状态码 + ctx.status = 200; + + // 响应数据 + ctx.body = { + code: 200, + data: book, + msg: "查询成功" + } +}) + +// 新增图书 +router.post('/books', async (ctx, next) => { + // 获取新增对象 + const obj = ctx.request.body; + + // 新增图书 + await Book.create(obj); + + // 成功状态码 + ctx.status = 200; + + // 响应数据 + ctx.body = { + code: 200, + data: null, + msg: "新增成功" + } +}) + +// 修改图书 +router.put('/books/:id', async (ctx, next) => { + // 获取新增对象 + const obj = ctx.request.body; + + // 获取 id + const id = ctx.params.id; + + // 新增图书 + await Book.update(obj, { where: { id } }); + + // 成功状态码 + ctx.status = 200; + + // 响应数据 + ctx.body = { + code: 200, + data: null, + msg: "修改成功" + } +}) + +// 删除图书 +router.delete('/books/:id', async (ctx, next) => { + // 获取 id + const id = ctx.params.id; + + // 删除图书 + await Book.destroy({ where: { id } }); + + // 成功状态码 + ctx.status = 200; + + // 响应数据 + ctx.body = { + code: 200, + data: null, + msg: "删除成功" + } +}) + +// 注册路由 +app.use(router.routes()) + +// 服务器端口 +const appPort = 8000; + +// 监听端口 +app.listen(appPort); + +console.log(`当前服务运行在:http://localhost:${appPort}`); +``` + +##### 前端 + +**router.js** + +```js +import { createRouter, createWebHistory } from 'vue-router' + +const router = createRouter({ + history: createWebHistory(import.meta.env.BASE_URL), + routes: [ + { + path: '/', + meta: { title: "图书列表" }, + component: () => import("@/views/Home.vue"), + }, + { + path: '/addBook', + meta: { title: "新增图书" }, + component: () => import("@/views/AddBook.vue"), + }, + { + path: '/updateBook/:id', + meta: { title: "修改图书" }, + component: () => import("@/views/UpdateBook.vue"), + }, + ], +}) + +// 全局守卫 +router.beforeEach((to, from, next) => { + if (to.meta.title) { + document.title = to.meta.title + } + next() +}) + +export default router +``` + +**App.vue** + +```vue + + + + + +``` + +**Home.vue** + +```vue + + + + + +``` + +**AddTeacher.vue** + +```vue + + + +``` + +**updateTeacher.vue** + +```vue + + + +``` + +#### 三、效果演示 + +##### [项目预览](https://gitee.com/hyo-ja/vuejs-frontend-backend-separation-projects) + +##### 效果预览 + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/2025-05-21-01.gif) + +## 课后作业 -- Gitee From 991b476e220cfec08953eaa22edbfc2a7e084a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=8E=AE=E9=93=AD?= <2373854303@qq.com> Date: Sun, 25 May 2025 13:54:11 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=8F=90=E4=BA=A4523=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...03\345\211\215\345\244\215\344\271\240.md" | 660 ++++++++++++++++++ 1 file changed, 660 insertions(+) create mode 100644 "\350\224\241\347\216\256\351\223\255/20250523 \350\200\203\345\211\215\345\244\215\344\271\240.md" diff --git "a/\350\224\241\347\216\256\351\223\255/20250523 \350\200\203\345\211\215\345\244\215\344\271\240.md" "b/\350\224\241\347\216\256\351\223\255/20250523 \350\200\203\345\211\215\345\244\215\344\271\240.md" new file mode 100644 index 0000000..1ed6ef1 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20250523 \350\200\203\345\211\215\345\244\215\344\271\240.md" @@ -0,0 +1,660 @@ +# 考前复习 + +## 课堂笔记 + +### 继续重写 VueJs 前后端分离项目 + +#### 一、前后端项目构建 + +##### 1. 构建项目结构: + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/20250519110052.png) + +##### 2. 构建项目: + +###### 后端 + +1. 项目初始化: + +```bash +npm init -y +``` + +2. 安装依赖包: + +```bash +npm i +``` + +###### 前端 + +1. 项目初始化: + +```bash +yarn create vue +``` + +2. 安装依赖包: + +```bash +yarn +``` + +3. 代码格式化: + +```bash +yarn format +``` + +#### 二、代码编写 + +##### 后端 + +**app.js** + +```js +// 导入依赖包 +import koa from "koa"; +import cors from "@koa/cors"; +import bodyparser from "koa-bodyparser"; +import Router from "koa-router"; +import { Op, Sequelize, DataTypes } from "sequelize" + +// 创建 sequelize 实例 +const sequelize = new Sequelize('test', 'root', '123456', { + dialect: "mysql" +}) + +// 创建模型 +const Student = sequelize.define('students', { + // 学生姓名 + studentName: { + type: DataTypes.STRING + }, + // 学生性别 + studentSex: { + type: DataTypes.STRING + }, + // 学生年龄 + studentAge: { + type: DataTypes.INTEGER + }, + // 所在班级 + className: { + type: DataTypes.STRING + } +}) + +// 同步数据库 +// sequelize.sync(); + +// 插入数据 + +await Student.create({ + // 学生姓名 + studentName: "张三", + // 学生性别 + studentSex: "男", + // 学生年龄 + studentAge: 18, + // 所在班级 + className: "222班级" +}) + +// 创建 koa 实例 +const app = new koa(); + +// 解决跨域问题 +app.use(cors()); + +// 请求体解析 +app.use(bodyparser()); + +// 创建路由实例 +const router = new Router(); + +/** 路由方法 */ +// 查询所有学生 +router.get('/students', async (ctx, netx) => { + // 查询关键词 + const keyWord = ctx.query.keyWord; + + // 条件对象 + let where; + + if (keyWord) { + where = { + [Op.or]: { + // 学生姓名 + studentName: { + [Op.like]: `%${keyWord}%` + }, + // 学生性别 + studentSex: { + [Op.like]: `%${keyWord}%` + }, + // 学生年龄 + studentAge: { + [Op.eq]: keyWord + }, + // 所在班级 + className: { + [Op.like]: `%${keyWord}%` + } + } + } + } else { + where = null; + } + + // 查询数据库 + const students = await Student.findAll({ where }) + + // 成功状态码 + ctx.status = 200; + + // 响应内容 + ctx.body = { + code: 200, + data: students, + msg: "查询成功" + } +}) + +// 查询一个学生 +router.get('/students/:id', async (ctx, netx) => { + // 查询关键词 + const keyWord = ctx.query.keyWord; + + // 获取 id + const id = ctx.params.id + + // 查询学生 + const student = await Student.findByPk(id) + + // 成功状态码 + ctx.status = 200; + + // 响应内容 + ctx.body = { + code: 200, + data: student, + msg: "查询成功" + } +}) + +// 新增一个学生 +router.post('/students', async (ctx, netx) => { + // 获取学生对象 + const obj = ctx.request.body; + + // 新增学生 + await Student.create(obj) + + // 成功状态码 + ctx.status = 200; + + // 响应内容 + ctx.body = { + code: 200, + data: null, + msg: "新增成功" + } +}) + +// 修改一个学生 +router.put('/students/:id', async (ctx, netx) => { + // 获取 id + const id = ctx.params.id + + // 获取学生对象 + const obj = ctx.request.body; + + // 修改学生 + await Student.update(obj, { where: { id } }) + + // 成功状态码 + ctx.status = 200; + + // 响应内容 + ctx.body = { + code: 200, + data: null, + msg: "修改成功" + } +}) + +// 删除一个学生 +router.delete('/students/:id', async (ctx, netx) => { + // 获取 id + const id = ctx.params.id + + // 删除学生 + await Student.destroy({ where: { id } }) + + // 成功状态码 + ctx.status = 200; + + // 响应内容 + ctx.body = { + code: 200, + data: null, + msg: "删除成功" + } +}) + +// 注册路由 +app.use(router.routes()) + +// 服务端口 +const appPort = 8000; + +// 监听端口 +app.listen(appPort); + +console.log(`当前服务运行在:http://localhost:${appPort}`); +``` + +##### 前端 + +**router.js** + +```js +import { createRouter, createWebHistory } from 'vue-router' + +const router = createRouter({ + history: createWebHistory(import.meta.env.BASE_URL), + routes: [ + { + path: '/', + name: 'home', + meta: { title: "学生列表" }, + component: () => import("@/views/Home.vue"), + }, + { + path: '/addStudent', + name: 'addStudent', + meta: { title: "新增学生" }, + component: () => import("@/views/AddStudent.vue"), + }, + { + path: '/updateStudent/:id', + name: 'updateStudent', + meta: { title: "修改学生" }, + component: () => import("@/views/UpdateStudent.vue"), + }, + ], +}) + +// 全局守卫 +router.beforeEach((to, from, next) => { + if (to.meta.title) { + document.title = to.meta.title + } + next() +}) + +export default router +``` + +**App.vue** + +```vue + + + + + +``` + +**Home.vue** + +```vue + + + + + +``` + +**AddTeacher.vue** + +```vue + + + +``` + +**updateTeacher.vue** + +```vue + + + +``` + +#### 三、效果演示 + +##### [项目预览](https://gitee.com/hyo-ja/vuejs-frontend-backend-separation-projects) + +##### 效果预览 + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/2025-05-21-01.gif) + +## 课后作业 -- Gitee From 50f3b102628cec050e5f3da555ab3bfcfc39b7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=8E=AE=E9=93=AD?= <2373854303@qq.com> Date: Mon, 26 May 2025 23:36:46 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=8F=90=E4=BA=A4526=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\212\241\351\241\271\347\233\256.md" | 632 ++++++++++++++++++ 1 file changed, 632 insertions(+) create mode 100644 "\350\224\241\347\216\256\351\223\255/20250526 \345\256\214\345\226\204\345\244\247\345\255\246\347\224\237\345\277\227\346\204\277\350\200\205\346\234\215\345\212\241\351\241\271\347\233\256.md" diff --git "a/\350\224\241\347\216\256\351\223\255/20250526 \345\256\214\345\226\204\345\244\247\345\255\246\347\224\237\345\277\227\346\204\277\350\200\205\346\234\215\345\212\241\351\241\271\347\233\256.md" "b/\350\224\241\347\216\256\351\223\255/20250526 \345\256\214\345\226\204\345\244\247\345\255\246\347\224\237\345\277\227\346\204\277\350\200\205\346\234\215\345\212\241\351\241\271\347\233\256.md" new file mode 100644 index 0000000..c8c0564 --- /dev/null +++ "b/\350\224\241\347\216\256\351\223\255/20250526 \345\256\214\345\226\204\345\244\247\345\255\246\347\224\237\345\277\227\346\204\277\350\200\205\346\234\215\345\212\241\351\241\271\347\233\256.md" @@ -0,0 +1,632 @@ +# 完善大学生志愿者服务项目 + +## 课堂笔记 + +### 完善大学生志愿者服务项目 + +#### 一、构建项目结构: + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202505262334906.png) + +#### 二、代码编写: + +**Home.vue** + +```vue + + + + +``` + +**Profile.vue** + +```vue + + + + + + +``` + +**App.vue** + +```vue + + + + + +``` + +**routes.js** + +```js +export const routes = [ + { + path: "/", + name: "home", + meta: { title: "大学生志愿者活动" }, + component: () => import("@/views/Home.vue"), + }, + { + path: "/ActivityList", + name: "ActivityList", + meta: { title: "活动列表" }, + component: () => import("@/views/ActivityList.vue"), + }, + { + path: "/ActivityListDetails", + name: "ActivityListDetails", + meta: { title: "活动详情" }, + component: () => import("@/views/ActivityListDetails.vue"), + }, + { + path: "/MyRegistration", + name: "MyRegistration", + meta: { title: "我的报名" }, + component: () => import("@/views/MyRegistration.vue"), + }, + { + path: "/PointsRanking", + name: "PointsRanking", + meta: { title: "积分排名" }, + component: () => import("@/views/PointsRanking.vue"), + }, + { + path: "/Profile", + name: "Profile", + meta: { title: "个人信息" }, + component: () => import("@/views/Profile.vue"), + }, + { + path: "/ServiceDetails", + name: "ServiceDetails", + meta: { title: "服务详情" }, + component: () => import("@/views/ServiceDetails.vue"), + }, + { + path: "/ServiceRecordList", + name: "ServiceRecordList", + meta: { title: "服务记录上传列表" }, + component: () => import("@/views/ServiceRecordList.vue"), + }, + { + path: "/ServiceRecordUpload", + name: "ServiceRecordUpload", + meta: { title: "服务记录上传" }, + component: () => import("@/views/ServiceRecordUpload.vue"), + }, +]; +``` + +#### 三、效果演示: + +##### [项目预览](https://gitee.com/hyo-ja/vue-js-framework/tree/master/20250526%20%E5%AE%8C%E5%96%84%E5%A4%A7%E5%AD%A6%E7%94%9F%E5%BF%97%E6%84%BF%E8%80%85%E6%9C%8D%E5%8A%A1%E9%A1%B9%E7%9B%AE/volunteer) + +##### 效果预览 + +###### 首页 + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202505262331779.png) + +###### 个人信息 + +![](https://gitee.com/hyo-ja/picture-warehouse/raw/master/images/202505262332492.png) + +## 课后作业 -- Gitee