From c6f3f9a77ef58f3dd6ed726ba56a957d18162454 Mon Sep 17 00:00:00 2001 From: 13551345092 <1435483502@qq.com> Date: Sun, 14 Apr 2024 20:57:42 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E5=91=A8=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6\347\253\257\345\210\206\347\246\2731.md" | 110 ++++++++++++++++++ ...6\347\253\257\345\210\206\347\246\2732.md" | 61 ++++++++++ 2 files changed, 171 insertions(+) create mode 100644 "\351\203\255\345\205\201\346\226\214/\347\254\224\350\256\260/20240408-\344\272\224\345\260\201\350\243\205\347\273\203\344\271\240\345\211\215\345\220\216\347\253\257\345\210\206\347\246\2731.md" create mode 100644 "\351\203\255\345\205\201\346\226\214/\347\254\224\350\256\260/20240409-\344\272\224\345\260\201\350\243\205\347\273\203\344\271\240\345\211\215\345\220\216\347\253\257\345\210\206\347\246\2732.md" diff --git "a/\351\203\255\345\205\201\346\226\214/\347\254\224\350\256\260/20240408-\344\272\224\345\260\201\350\243\205\347\273\203\344\271\240\345\211\215\345\220\216\347\253\257\345\210\206\347\246\2731.md" "b/\351\203\255\345\205\201\346\226\214/\347\254\224\350\256\260/20240408-\344\272\224\345\260\201\350\243\205\347\273\203\344\271\240\345\211\215\345\220\216\347\253\257\345\210\206\347\246\2731.md" new file mode 100644 index 0000000..4f3d726 --- /dev/null +++ "b/\351\203\255\345\205\201\346\226\214/\347\254\224\350\256\260/20240408-\344\272\224\345\260\201\350\243\205\347\273\203\344\271\240\345\211\215\345\220\216\347\253\257\345\210\206\347\246\2731.md" @@ -0,0 +1,110 @@ +``` +import bodyParser from "koa-bodyparser" +import koa from "koa" +import Router from "koa-router" +import cors from "koa2-cors" +import { AggregateError, DataTypes, Sequelize, where } from "sequelize" + +const prot = 3000 +let app = new koa() +let router = new Router() +const sql = new Sequelize('OvO', 'sa', '123456', { + host: '锅巴不糊\\MYGYB', + dialect: 'mssql' +}) +const data = sql.define('qaq', { + name: { type: DataTypes.STRING }, + age: { type: DataTypes.INTEGER } +}) + +app.use(bodyParser()) +app.use(router.routes()) +app.use(cors()) + +await data.sync() + +router.get('/:id?', async (xx) => { + + let id = xx.params.id || 0 + console.log(id); + + let qaq + if (id > 0) { + let list = await data.findByPk(id) + + qaq = list + } else { + let last = await data.findAll(id) + + qaq = last + } + xx.body = { + code: "1000", + data: qaq, + msg: "h获取信息成功" + } +}) +router.post('/', async (xx) => { + + let obj = xx.request.body + let row = await data.create(obj) + + xx.body = { + code: "200", + data: row, + msg: "创建成功" + } +}) +router.put('/:id?', async (xx) => { + let id = xx.params.id || 0 + let obj = xx.request.body + let item = await data.findByPk(id) + if (item) { + let qaq = await data.update(obj, { + where: { + id: id + } + }) + xx.body = { + code: 1000, + data: qaq, + msg: "修改成功" + } + } else { + xx.body = { + code: 1000, + data: null, + msg: "修改失败" + } + } + xx.body = '修改成功' +}) +router.delete('/:id', async (xx) => { + let id = xx.params.id || 0 + + let row = await data.findByPk(id) + if (row) { + data.destroy({ + where: { + id: id + } + }) + xx.body = { + code: 1000, + data: null, + msg: "删除成功" + } + }else{ + xx.body = { + code: 1000, + data: null, + msg: "删除失败" + } + } + +}) + +app.listen(prot, () => { + console.log(`http://127.0.0.1:${prot}`); +}) +``` \ No newline at end of file diff --git "a/\351\203\255\345\205\201\346\226\214/\347\254\224\350\256\260/20240409-\344\272\224\345\260\201\350\243\205\347\273\203\344\271\240\345\211\215\345\220\216\347\253\257\345\210\206\347\246\2732.md" "b/\351\203\255\345\205\201\346\226\214/\347\254\224\350\256\260/20240409-\344\272\224\345\260\201\350\243\205\347\273\203\344\271\240\345\211\215\345\220\216\347\253\257\345\210\206\347\246\2732.md" new file mode 100644 index 0000000..a804087 --- /dev/null +++ "b/\351\203\255\345\205\201\346\226\214/\347\254\224\350\256\260/20240409-\344\272\224\345\260\201\350\243\205\347\273\203\344\271\240\345\211\215\345\220\216\347\253\257\345\210\206\347\246\2732.md" @@ -0,0 +1,61 @@ +``` +$(function () { + axios.get("http://localhost:3000").then(res => { + let data = res.data.data; + dataTable(data); + }) +}) + +function Find() { + let keyword = $('[name="keyword"]').val(); + axios.get(`http://localhost:3000/?keyword=${keyword}`).then(res => { + let data = res.data; + if (data.code === 1000) { + let tr = $('[class^=tr]'); + tr.remove(); + dataTable(data.data); + } + }) +} + +function Add(){ + location.href="./edit.html"; +} + +function Edit(id){ + location.href="./edit.html?id="+id; +} + +function Delete(id){ + axios.delete(`http://localhost:3000/${id}?`).then(res=>{ + let data=res.data; + if(data.code===1000){ + let tr=$(".tr"+id); + tr.remove(); + }else{ + alert(data.msg); + } + }) +} + +function dataTable(arr) { + let num = 0; + arr.forEach(item => { + let tb = $("#table"); + num++; + let html = ` +