47 lines
1.1 KiB
Plaintext
47 lines
1.1 KiB
Plaintext
|
|
package user
|
||
|
|
|
||
|
|
import (
|
||
|
|
"common/connection"
|
||
|
|
"goutil/logUtilPlus"
|
||
|
|
)
|
||
|
|
|
||
|
|
// AddUser 添加用户
|
||
|
|
// AddUser 添加新的用户到数据库中。
|
||
|
|
// 参数 User: 包含用户信息的对象。
|
||
|
|
// 返回值: 插入操作影响的行数和可能发生的错误。
|
||
|
|
func AddUser(User *User) (int64, error) {
|
||
|
|
|
||
|
|
//处理一些验证
|
||
|
|
|
||
|
|
// 写入到数据库
|
||
|
|
result := connection.GetUserDB().Create(&User) // 通过数据的指针来创建
|
||
|
|
|
||
|
|
if result.Error != nil {
|
||
|
|
logUtilPlus.ErrorLog("添加用户失败 错误信息:", result.Error.Error())
|
||
|
|
}
|
||
|
|
return User.ID, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetUserByID 根据用户ID获取用户信息
|
||
|
|
func GetUserByID(UserID int64) (*User, error) {
|
||
|
|
var User User
|
||
|
|
|
||
|
|
//缓存判断等一些设置
|
||
|
|
|
||
|
|
result := connection.GetUserDB().First(&User, UserID)
|
||
|
|
if result.Error != nil {
|
||
|
|
return nil, result.Error
|
||
|
|
}
|
||
|
|
return &User, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// 用户登录
|
||
|
|
func Login(account string, password string) (*User, error) {
|
||
|
|
var User User
|
||
|
|
result := connection.GetUserDB().Where("account = ? AND password = ?", account, password).First(&User)
|
||
|
|
if result.Error != nil {
|
||
|
|
return nil, result.Error
|
||
|
|
}
|
||
|
|
return &User, nil
|
||
|
|
}
|