This commit is contained in:
shaoyongjun 2024-09-30 15:41:00 +08:00
parent 41b609c783
commit 821dda5395
7 changed files with 87 additions and 16 deletions

View File

@ -18,7 +18,7 @@ func getInstance() *gorm.DB {
// unmarshal config
ll := logs.NewZapLogger()
if config.Instance.PgSql.LogSql {
ll = ll.LogMode(loglevel.Info)
ll = ll.LogMode(loglevel.Warn)
} else {
ll = ll.LogMode(loglevel.Warn)
}

View File

@ -33,7 +33,7 @@ func TestPg(t *testing.T) {
}
var total int
db.Raw("select max(id) from user").Scan(&total)
db.Raw("select max(id) from user_base").Scan(&total)
// 打印成功信息
fmt.Println("连接数据库成功", total)

View File

@ -19,8 +19,8 @@ type UserDO struct {
Avatar *string `gorm:"column:avatar;type:varchar(128);comment:头像" json:"avatar"`
Gender uint `gorm:"column:gender;type:smallint(4) unsigned;default:0;comment:性别。0:未知 ;1:男;2:女; 3:其他" json:"gender"`
Phone *string `gorm:"column:phone;type:varchar(16);comment:手机号码" json:"phone"`
WxId *string `gorm:"column:wxId;type:varchar(16);comment:微信unionId" json:"wxId"`
QqId *string `gorm:"column:qqId;type:varchar(16);comment:qqId" json:"qqId"`
WxId *string `gorm:"column:wx_id;type:varchar(16);comment:微信unionId" json:"wx_id"`
QqId *string `gorm:"column:qq_id;type:varchar(16);comment:qqId" json:"qq_id"`
Email *string `gorm:"column:email;type:varchar(64);comment:email" json:"email"`
GoogleId *string `gorm:"column:google_id;type:varchar(32);comment:GoogleId" json:"google_id"`
FacebookId *string `gorm:"column:facebook_id;type:varchar(32);comment:FacebookId" json:"facebook_id"`
@ -30,17 +30,17 @@ type UserDO struct {
RegisterSource string `gorm:"column:register_source;type:varchar(16);comment:注册来源" json:"register_source"`
LatestIp string `gorm:"column:latest_ip;type:varchar(32);comment:最近登录的ip" json:"latest_ip"`
LatestLoginTime string `gorm:"column:latest_login_time;type:varchar(32);comment:最近登录的时间戳" json:"latest_login_time"`
LatestLoginTime uint64 `gorm:"column:latest_login_time;type:varchar(32);comment:最近登录的时间戳" json:"latest_login_time"`
Status uint `gorm:"column:status;type:smallint(4) unsigned;default:1;comment:是否有效。0:否1:是;NOT NULL" json:"status"`
Deleted uint `gorm:"column:deleted;type:smallint(4) unsigned;default:0;comment:逻辑删除。 0:未删除1:已删除;NOT NULL" json:"deleted"`
Deleted bool `gorm:"column:deleted;type:smallint(4) unsigned;default:0;comment:逻辑删除。 0:未删除1:已删除;NOT NULL" json:"deleted"`
Remark string `gorm:"column:remark;type:varchar(64);comment:备注" json:"remark"`
CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间;NOT NULL" json:"create_time"`
UpdateTime time.Time `gorm:"column:update_time;type:timestamp;default:CURRENT_TIMESTAMP;comment:更新时间;NOT NULL" json:"update_time"`
}
func (m *UserDO) TableName() string {
return "g_account"
return "user_base"
}
type gUserRepositoryImpl struct {
@ -151,7 +151,7 @@ func (rp *gUserRepositoryImpl) FindByWxId(ctx context.Context, wxId string) *Use
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("wxId=?", wxId).First(&model).Error
sqlErr := db.Model(&model).Where("wx_id=?", wxId).First(&model).Error
if sqlErr != nil {
return nil
}
@ -168,7 +168,7 @@ func (rp *gUserRepositoryImpl) FindByQqId(ctx context.Context, qqId string) *Use
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("qqId=?", qqId).First(&model).Error
sqlErr := db.Model(&model).Where("qq_id=?", qqId).First(&model).Error
if sqlErr != nil {
return nil
}
@ -185,7 +185,7 @@ func (rp *gUserRepositoryImpl) FindByGoogleId(ctx context.Context, googleId stri
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("googleId=?", googleId).First(&model).Error
sqlErr := db.Model(&model).Where("google_id=?", googleId).First(&model).Error
if sqlErr != nil {
return nil
}
@ -202,7 +202,7 @@ func (rp *gUserRepositoryImpl) FindByFacebookId(ctx context.Context, facebookId
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("facebookId=?", facebookId).First(&model).Error
sqlErr := db.Model(&model).Where("facebook_id=?", facebookId).First(&model).Error
if sqlErr != nil {
return nil
}
@ -216,7 +216,7 @@ func (rp *gUserRepositoryImpl) Create(ctx context.Context, data *UserDO) error {
db = Db
}
data.Deleted = 0
data.Deleted = false
return db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "email"}},

View File

@ -0,0 +1,71 @@
package repository
import (
"context"
"testing"
)
func TestFindById(t *testing.T) {
data := GUser.FindById(context.Background(), 2)
if data.Id != uint64(2) {
t.Error("err")
}
}
func TestFindByAccount(t *testing.T) {
query := "syj"
data := GUser.FindByAccount(context.Background(), query)
if *data.Account != query {
t.Error("err")
}
}
func TestFindByPhone(t *testing.T) {
query := "19901639350"
data := GUser.FindByPhone(context.Background(), query)
if *data.Phone != query {
t.Error("err")
}
}
func TestFindByEmail(t *testing.T) {
query := "1290251929@qq.com"
data := GUser.FindByEmail(context.Background(), query)
if *data.Email != query {
t.Error("err")
}
}
func TestFindByWxId(t *testing.T) {
query := "unionsyj"
data := GUser.FindByWxId(context.Background(), query)
if *data.WxId != query {
t.Error("err")
}
}
func TestFindByQqId(t *testing.T) {
query := "1290251929"
data := GUser.FindByQqId(context.Background(), query)
if *data.QqId != query {
t.Error("err")
}
}
func TestFindByGoogleId(t *testing.T) {
query := "googleIdsyj"
data := GUser.FindByGoogleId(context.Background(), query)
if *data.GoogleId != query {
t.Error("err")
}
}
func TestFindByFacebookId(t *testing.T) {
query := "facebookIdSyj"
data := GUser.FindByFacebookId(context.Background(), query)
if *data.FacebookId != query {
t.Error("err")
}
}

View File

@ -32,7 +32,7 @@ var limiter = rate.NewLimiter(10, 10)
// Register 注册
func (l login) Register(ctx context.Context, req dto.RegisterReq) error {
acUser := repository.GUser.FindByReq(ctx, req.LoginReq)
if acUser != nil && acUser.Deleted == 0 {
if acUser != nil && acUser.Deleted == false {
return errors.New("user exist")
}
@ -51,7 +51,7 @@ func (l login) Login(ctx context.Context, req dto.LoginReq) (*dto.UserVO, error)
//1. 查询用户
acUser := repository.GUser.FindByReq(ctx, req)
if acUser == nil || acUser.Deleted == 1 {
if acUser == nil || acUser.Deleted == true {
return nil, errors.New("user not exist")
}
@ -84,7 +84,7 @@ func (l login) Login(ctx context.Context, req dto.LoginReq) (*dto.UserVO, error)
func (l login) SendResetPwdCode(ctx context.Context, account string) error {
//1. 验证账号
acUser := repository.GUser.FindByAccount(ctx, account)
if acUser == nil || acUser.Deleted == 1 {
if acUser == nil || acUser.Deleted == true {
return errors.New("user not exist")
}
@ -119,7 +119,7 @@ func (l login) ResetPwd(ctx context.Context, req dto.ResetPwdReq) error {
//2. 查询用户
acUser := repository.GUser.FindByAccount(ctx, req.Account)
if acUser == nil || acUser.Deleted == 1 {
if acUser == nil || acUser.Deleted == true {
return errors.New("user not exist")
}