2 Star 0 Fork 1

taotechip/modules

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
repository.go 4.29 KB
一键复制 编辑 原始数据 按行查看 历史
kyugao 提交于 2025-05-26 00:59 +08:00 . 为了swagger文档生成,调整目录结构
package account
import (
"errors"
"gitee.com/taotechip/modules/database"
"gitee.com/taotechip/modules/utils/coder"
"gitee.com/taotechip/modules/utils/genstr"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
type AccountRepo struct {
database.GeneralRepo
}
func GetAccountRepo(transactionalConn *gorm.DB) (repo *AccountRepo) {
repo = database.NewRepo(transactionalConn, &AccountRepo{})
return
}
func (repo *AccountRepo) UpdateAccountEmail(accountId string, email string, password, salt string) (err error) {
account, err := repo.FindAccountById(accountId)
if err != nil {
return
}
account.Email = email
account.Salt = salt
account.Password = password
return repo.UpdateAccount(account, "email", "password", "salt")
}
func (repo *AccountRepo) IsAdminAccount(accountId string) (isAdmin bool, err error) {
var account Account
err = repo.GetConn().Where("id = ?", accountId).First(&account).Error
log.Debug().Msgf("Check accountId [%s] admin role [%v]", accountId, account.IsAdmin)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
}
return false, err
}
return account.IsAdmin, nil
}
func (repo *AccountRepo) UpdateAccountPassword(accountId string, srcPassword string) (err error) {
newSalt := genstr.StrGen.RandLow(6)
newMd5Pwd := coder.Md5.Md5Hex(srcPassword + newSalt)
err = repo.UpdateAccount(&Account{
Model: database.Model{ID: accountId},
Password: newMd5Pwd,
Salt: newSalt}, "password", "salt")
return
}
func (repo *AccountRepo) UpdateAccount(account *Account, columns ...string) (err error) {
err = repo.GetConn().Model(account).Select(columns).Updates(account).Error
return
}
func (repo *AccountRepo) FindAccountById(accountId string) (account *Account, err error) {
account = &Account{}
err = repo.GetConn().First(account, accountId).Error
return
}
func (repo *AccountRepo) ExistAccountEmail(email string) (exist bool, err error) {
var count int64 = 0
err = repo.GetConn().Model(&Account{}).Where("email = ?", email).Count(&count).Error
if err == nil {
exist = count > 0
} else if errors.Is(err, gorm.ErrRecordNotFound) {
err = nil
exist = false
}
return
}
func (repo *AccountRepo) FindAccountEmail(email string) (account *Account, err error) {
account = &Account{}
err = repo.GetConn().Model(account).Where("email = ?", email).Find(account).Error
return
}
func (repo *AccountRepo) UpdateAccountAvatar(id string, headerFileId string) (err error) {
err = repo.GetConn().Model(&Account{}).Where("id = ?", id).Update("header_id", headerFileId).Error
return
}
func (repo *AccountRepo) UpdateLoginFailIncrease(id string, cnt int) (err error) {
if cnt == 0 {
return
}
account := &Account{
Model: database.Model{ID: id},
}
err = repo.GetConn().Model(account).Update("fail_login_count", gorm.Expr("fail_login_count + ?", cnt)).Error
return
}
func (repo *AccountRepo) CreateAccount(email string, srcPassword string) (account *Account, err error) {
salt := genstr.StrGen.RandLow(6)
destPwd := coder.Md5.Md5Hex(srcPassword + salt)
account = &Account{
Model: database.Model{
ID: genstr.Snowflake.GetString(),
},
Email: email,
Password: destPwd,
Salt: salt,
Status: ASNormal,
FailLoginCount: 0,
Remark: "New Create",
}
err = repo.GetConn().Model(&Account{}).Create(account).Error
return
}
func (repo *AccountRepo) CreateDummyAccountWithEmail(email string, license string) (account *Account, err error) {
salt := genstr.StrGen.RandLow(6)
destPwd := coder.Md5.Md5Hex(license + salt)
account = &Account{
Model: database.Model{
ID: genstr.Snowflake.GetString(),
},
Email: email,
Password: destPwd,
Salt: salt,
Status: ASNormal,
FailLoginCount: 0,
Remark: license,
}
err = repo.GetConn().Model(&Account{}).Create(account).Error
return
}
func (repo *AccountRepo) CreateDummyAccount() (account *Account, err error) {
salt := genstr.StrGen.RandLow(6)
destPwd := coder.Md5.Md5Hex(genstr.StrGen.RandLow(6) + salt)
account = &Account{
Model: database.Model{
ID: genstr.Snowflake.GetString(),
},
Email: genstr.Snowflake.GetString(),
Password: destPwd,
Salt: salt,
Status: ASNormal,
FailLoginCount: 0,
Remark: "New Dummy",
}
err = repo.GetConn().Model(&Account{}).Create(account).Error
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/taotechip/modules.git
git@gitee.com:taotechip/modules.git
taotechip
modules
modules
v1.10.5

搜索帮助