2024/3/16
创建了DreamWind项目,用于开发测试
搭建后端
userService.js
javascript
const Service = require("egg").Service;
class UserInfoService extends Service {
//1.登录验证(根据用户名和密码进行查询)
async QueryUserByUserNameByUserPassword(user){
let result;
try{
let sql = `SELECT COUNT(*) FROM userinfo WHERE UserName = '${user.UserName}'`;
let sqlResult = await this.app.mysql.query(sql)
let IsUserNamehave = sqlResult[0]['COUNT(*)'];
if(IsUserNamehave == 0){
result = "400 不存在该账号,请注册";
}
if(IsUserNamehave == 1){
result = "200 查询到该账号"
sql = `SELECT COUNT(*) FROM userinfo WHERE UserName = '${user.UserName}' and UserPassword = '${user.UserPassword}'`;
sqlResult = await this.app.mysql.query(sql)
let IsPasswordhave = sqlResult[0]['COUNT(*)'];
if(IsPasswordhave == 0){
result = "401 密码不正确";
}
if(IsPasswordhave == 1){
//后续在这里添加cookie
result = "200 成功登录"
}
}
} catch (error) {
console.log(error)
}
return result;
}
//2.注册账号(需要用户名和密码)
async RegisterUserByUserNameByUserPassword(user) {
let result;
try {
if(user.UserName == "" || user.UserPassword == ""){
result = "400 请求数据格式不正确"
return result;
}
let sql = `SELECT COUNT(*) FROM userinfo WHERE UserName = '${user.UserName}'`;
let sqlResult = await this.app.mysql.query(sql)
let Ishave = sqlResult[0]['COUNT(*)'];
if(Ishave == 0){
sql = `INSERT INTO userinfo (UserName , UserPassword) VALUES ("${user.UserName}", "${user.UserPassword}")`;
await this.app.mysql.query(sql)
result = "200 成功添加账号"
}
if(Ishave == 1){
result = "400 该账号已存在";
}
} catch (error) {
console.log(error)
}
return result;
}
//3.修改密码
async ChangePasswordByUserNameByUserPassword(user){
let result;
try{
if(user.UserName == "" || user.UserPassword == ""){
result = "400 请求数据格式不正确"
return result;
}
let sql = `SELECT COUNT(*) FROM userinfo WHERE UserName = '${user.UserName}'`;
let sqlResult = await this.app.mysql.query(sql)
let IsUserNamehave = sqlResult[0]['COUNT(*)'];
if(IsUserNamehave == 0){
result = "400 不存在该账号,请注册";
}
if(IsUserNamehave == 1){
result = "200 查询到该账号"
sql = `SELECT COUNT(*) FROM userinfo WHERE UserName = '${user.UserName}' and UserPassword = '${user.UserPassword}'`;
sqlResult = await this.app.mysql.query(sql)
let IsPasswordhave = sqlResult[0]['COUNT(*)'];
if(IsPasswordhave == 0){
sql = `update userinfo set UserPassword = "${user.UserPassword}" where UserName = "${user.UserName}"`;
await this.app.mysql.query(sql)
result = "200 成功修改密码"
}
if(IsPasswordhave == 1){
result = "400 与原密码相同"
}
}
} catch (error) {
console.log(error)
}
return result;
}
}
module.exports = UserInfoService;
userController.js
javascript
const { Controller } = require("egg");
class UserController extends Controller {
//1.登录验证(根据用户名和密码进行查询)
async QueryUserByUserNameByUserPassword() {
const { ctx } = this;
const result = await this.ctx.service.userService.QueryUserByUserNameByUserPassword(this.ctx.request.body);
ctx.body = result
}
//2.注册账号(根据用户名和密码进行注册)
async RegisterUserByUserNameByUserPassword() {
const { ctx } = this;
const result = await this.ctx.service.userService.RegisterUserByUserNameByUserPassword(this.ctx.request.body);
ctx.body = result
}
//3.修改密码(根据用户名与新密码)
async ChangePasswordByUserNameByUserPassword() {
const { ctx } = this;
const result = await this.ctx.service.userService.ChangePasswordByUserNameByUserPassword(this.ctx.request.body);
ctx.body = result
}
}
module.exports = UserController
router.js
javascript
/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
//1.登录验证
router.post("/login" , controller.userController.QueryUserByUserNameByUserPassword)
//2.注册账号
router.post("/register" , controller.userController.RegisterUserByUserNameByUserPassword)
//3.修改密码
router.post("/change" , controller.userController.ChangePasswordByUserNameByUserPassword)
};