1 Star 0 Fork 0

mq-go / bluebell

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
user.go 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
柒染 提交于 2023-10-03 11:39 . 帖子详情及列表分页
/*
* @Author: drowningwhale 351231768@qq.com
* @Date: 2023-09-30 13:50:19
* @LastEditors: drowningwhale 351231768@qq.com
* @LastEditTime: 2023-10-02 18:51:56
* @FilePath: \bluebell\dao\mysql\user.go
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
package mysql
import (
"crypto/md5"
"encoding/hex"
"fmt"
"gitee.com/mq-go/bluebell/dao"
"gitee.com/mq-go/bluebell/global"
"gitee.com/mq-go/bluebell/models"
"go.uber.org/zap"
)
// 创建盐值
var secret = "https://gitee.com/mq-go/bluebell"
// HavingUser 查询用户是否存在
func HavingUser(username string) error {
user := models.User{
UserName: username,
}
result := global.DB.Where(&models.User{UserName: username}).First(&user)
if result.Error != nil {
zap.L().Error("select user failed : ", zap.Error(result.Error))
return result.Error
}
return nil
}
// SignUpUser 创建用户
func SignUpUser(param *models.ParamSignUp) error {
user := models.User{
UserId: int(dao.GenID()),
UserName: param.UserName,
PassWord: param.PassWord,
}
user.PassWord = encryptPassword(user.PassWord)
result := global.DB.Create(&user)
if result.Error != nil {
zap.L().Error("create user failed : ", zap.Error(result.Error))
return result.Error
}
return nil
}
// ComparePassWord 密码验证
func ComparePassWord(user *models.User) error {
password := encryptPassword(user.PassWord)
result := global.DB.Where(&models.User{UserName: user.UserName}).First(&user)
if result.RowsAffected == 0 {
return fmt.Errorf("record not found")
}
if result.Error != nil {
zap.L().Error("select error : ", zap.Error(result.Error))
return result.Error
}
//比对
if user.PassWord != password {
return fmt.Errorf("password error")
}
return nil
}
func GetUserById(id int)(User models.User, err error){
result := global.DB.Where(&models.User{UserId: id}).First(&User)
if result.Error != nil{
err = result.Error
return
}
return
}
// 密码加密
func encryptPassword(password string) string {
h := md5.New()
h.Write([]byte(secret))
password = hex.EncodeToString(h.Sum([]byte(password)))
return hex.EncodeToString(h.Sum([]byte(password)))
}
Go
1
https://gitee.com/mq-go/bluebell.git
git@gitee.com:mq-go/bluebell.git
mq-go
bluebell
bluebell
d993e0e507c7

搜索帮助