合作测试开发日志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)
};
相关推荐
F-2H1 小时前
C语言:指针4(常量指针和指针常量及动态内存分配)
java·linux·c语言·开发语言·前端·c++
gqkmiss1 小时前
Chrome 浏览器插件获取网页 iframe 中的 window 对象
前端·chrome·iframe·postmessage·chrome 插件
m0_748247553 小时前
Web 应用项目开发全流程解析与实战经验分享
开发语言·前端·php
m0_748255024 小时前
前端常用算法集合
前端·算法
真的很上进4 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
web130933203984 小时前
vue elementUI form组件动态添加el-form-item并且动态添加rules必填项校验方法
前端·vue.js·elementui
NiNg_1_2345 小时前
Echarts连接数据库,实时绘制图表详解
前端·数据库·echarts
如若1235 小时前
对文件内的文件名生成目录,方便查阅
java·前端·python
滚雪球~6 小时前
npm error code ETIMEDOUT
前端·npm·node.js
沙漏无语6 小时前
npm : 无法加载文件 D:\Nodejs\node_global\npm.ps1,因为在此系统上禁止运行脚本
前端·npm·node.js