合作测试开发日志1

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)
};
相关推荐
程序员爱技术1 小时前
Vue 2 + JavaScript + vue-count-to 集成案例
前端·javascript·vue.js
并不会2 小时前
常见 CSS 选择器用法
前端·css·学习·html·前端开发·css选择器
悦涵仙子2 小时前
CSS中的变量应用——:root,Sass变量,JavaScript中使用Sass变量
javascript·css·sass
衣乌安、2 小时前
【CSS】居中样式
前端·css·css3
兔老大的胡萝卜2 小时前
ppk谈JavaScript,悟透JavaScript,精通CSS高级Web,JavaScript DOM编程艺术,高性能JavaScript pdf
前端·javascript
低代码布道师2 小时前
CSS的三个重点
前端·css
耶啵奶膘4 小时前
uniapp-是否删除
linux·前端·uni-app
王哈哈^_^5 小时前
【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
前端·人工智能·深度学习·yolo·目标检测·计算机视觉·pyqt
cs_dn_Jie6 小时前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
开心工作室_kaic6 小时前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js