41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
const db = require("../db");
|
|
|
|
const handlerLogin = async (ctx) => {
|
|
const { username, password } = ctx.request.body;
|
|
console.log(username + password);
|
|
|
|
const [rows] = await db.query("select * from users where username = ? ", [username]);
|
|
if (rows.length > 0) {
|
|
const user = rows[0];
|
|
console.log(user.password, password);
|
|
|
|
if (user.password === password) {
|
|
ctx.body = "登录成功";
|
|
} else {
|
|
ctx.body = "密码错误";
|
|
}
|
|
} else {
|
|
ctx.body = "用户名不存在";
|
|
}
|
|
}
|
|
|
|
const handlerRegister = async (ctx) => {
|
|
const { username, password, email, gender, hobbies, city, description } = ctx.request.body;
|
|
try {
|
|
await db.query("insert into users(username, password, email, gender, hobbies, city, description) values(?, ?, ?, ?, ?, ?, ?)", [username, password, email, gender, JSON.stringify(hobbies), city, description]);
|
|
ctx.status = 201;
|
|
ctx.body = {
|
|
message: "注册成功"
|
|
};
|
|
} catch (err) {
|
|
console.error(err);
|
|
ctx.status = 500;
|
|
ctx.body = {
|
|
error: "服务器内部错误, 请稍后重试"
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
handlerLogin, handlerRegister
|
|
} |