目录
以下示例接口前缀:'/system/user'
get
//根据username模糊查询用户,若username不存在或为空,则查询所有用户
router.get('/', async (ctx, next) => {
let { username } = ctx.request.query
let sql = `select * from sys_user`
if (username) {
sql += ` where username like '%${username}%'`
}
let users = await db.query(sql);
ctx.body = {
code: 0,
message: "success",
data: users
}
})
let data = await axios.get("/api/system/user", {
params: {
username: state.searchByUsername,
},
});
post
let data = await axios.post("/api/system/user", {
username: state_dialog.form.username,
password: state_dialog.form.password,
});
//添加用户
router.post('/', async (ctx, next) => {
let { username, password } = ctx.request.body
let sql = `insert into sys_user(username,password) values ('${username}','${password}')`
await db.query(sql);
ctx.body = {
code: 0,
message: "success",
}
})
put
let data = await axios.put("/api/system/user", {
id: state_dialog.form.id,
username: state_dialog.form.username,
password: state_dialog.form.password,
});
//修改用户
router.put('/', async (ctx, next) => {
let { id, username, password } = ctx.request.body
let sql = `update sys_user set password='${password}',username='${username}' where id='${id}'`
await db.query(sql);
ctx.body = {
code: 0,
message: "success",
}
})
delete
方式一:query
router.delete('/', async (ctx, next) => {
let { id } = ctx.request.query
let sql = `delete from sys_user where id='${id}'`
await db.query(sql);
ctx.body = {
code: 0,
message: "success",
}
})
const handleDelete = async (index, row) => {
await axios.delete("/api/system/user", {
params: {
id: row.id,
},
});
getUserList();
};
方式二:params
router.delete('/:id', async (ctx, next) => {
let { id } = ctx.params
let sql = `delete from sys_user where id='${id}'`
await db.query(sql);
ctx.body = {
code: 0,
message: "success",
}
})
// 删除用户
const handleDelete = async (index, row) => {
await axios.delete(`/api/system/user/${row.id}`);
getUserList();
};
方式三:body
也可向post一样用body传,但delete很少用这种方式