This commit is contained in:
shaoyongjun 2024-09-29 20:14:50 +08:00
parent f0e5646c25
commit 3e3dc3f0a4
11 changed files with 177 additions and 149 deletions

0
README.md Normal file
View File

View File

@ -15,10 +15,10 @@ rpc:
# pgSql
pgSql:
logSql: true
dsn: u1:zELLqwk2hBp8QyJgEtH5YGc6WKMS3dXP!@tcp(123.207.198.132:10119)/user_center?charset=utf8mb4&parseTime=True&loc=Local
dsn: postgres://postgres:zym520!@123.207.198.132:15432/mylomen_user?charset=utf8&parseTime=True&loc=Local
maxIdleConn: 4
maxOpenConn: 4
connMaxLifetime: 3000
connMaxLifetime: 0
# redis

View File

@ -15,10 +15,10 @@ rpc:
# pgSql
pgSql:
logSql: false
dsn: u1:zELLqwk2hBp8QyJgEtH5YGc6WKMS3dXP!@tcp(10.0.12.8:10119)/user_center?charset=utf8mb4&parseTime=True&loc=Local
maxIdleConn: 10
maxOpenConn: 10
connMaxLifetime: 28000
dsn: postgres://postgres:zym520!@10.0.12.8:15432/mylomen_user?charset=utf8&parseTime=True&loc=Local
maxIdleConn: 30
maxOpenConn: 30
connMaxLifetime: 0
# redis
redis:

3
go.mod
View File

@ -58,7 +58,7 @@ require (
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
@ -87,6 +87,7 @@ require (
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.47.0 // indirect
github.com/refraction-networking/utls v1.6.7 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sagikazarmark/crypt v0.24.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect

8
go.sum
View File

@ -170,8 +170,8 @@ github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc=
github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR/prTM=
@ -307,8 +307,8 @@ github.com/refraction-networking/utls v1.6.7 h1:zVJ7sP1dJx/WtVuITug3qYUq034cDq9B
github.com/refraction-networking/utls v1.6.7/go.mod h1:BC3O4vQzye5hqpmDTWUqi4P5DDhzJfkV1tdqtawQIH0=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sagikazarmark/crypt v0.24.0 h1:v/RbcfZT1U6CfGXV+I1WUtWUgo3ewpoSBHyUT6qIGfY=
github.com/sagikazarmark/crypt v0.24.0/go.mod h1:RNCCVzIbELuCbLqhzOubaxqLiWnijPEVKWe5UBtEsaQ=

View File

@ -22,7 +22,7 @@ func TestPg(t *testing.T) {
// 创建一个 gorm.DB 类型的变量
var db *gorm.DB
// 调用 Open 方法,传入驱动名和连接字符串
db, err := gorm.Open(postgres.Open("postgres://postgres:zym520!@123.207.198.132:15432/template1"), &gorm.Config{
db, err := gorm.Open(postgres.Open("postgres://postgres:zym520!@123.207.198.132:15432/mylomen_user"), &gorm.Config{
Logger: ll,
SkipDefaultTransaction: true,
})

View File

@ -3,14 +3,21 @@ package repository
import "context"
type gUserRepository interface {
FindById(ctx context.Context, id int64) *UserDO
//FindByAccount 根据 account 查询用户
FindByAccount(ctx context.Context, account string) *AccountDO
FindByAccount(ctx context.Context, account string) *UserDO
FindByEmail(ctx context.Context, email string) *UserDO
FindByPhone(ctx context.Context, phone string) *UserDO
FindByWxId(ctx context.Context, wxId string) *UserDO
FindByQqId(ctx context.Context, qqId string) *UserDO
FindByGoogleId(ctx context.Context, googleId string) *UserDO
FindByFacebookId(ctx context.Context, facebookId string) *UserDO
//Create 创建用户
Create(ctx context.Context, data *AccountDO) error
Create(ctx context.Context, data *UserDO) error
//UpdateById 根据ID更新用户
UpdateById(ctx context.Context, data *AccountDO) error
UpdateById(ctx context.Context, data *UserDO) error
}
var GUser gUserRepository = new(gUserRepositoryImpl)

View File

@ -7,54 +7,180 @@ import (
"time"
)
// AccountDO 游戏帐号表
type AccountDO struct {
Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
Email string `gorm:"column:email;type:varchar(64);comment:帐号;NOT NULL" json:"email"`
Pwd string `gorm:"column:pwd;type:varchar(128);comment:密码;NOT NULL" json:"pwd"`
Name string `gorm:"column:name;type:varchar(64);comment:用户名" json:"name"`
Avatar string `gorm:"column:avatar;type:varchar(128);comment:头像" json:"avatar"`
Gender uint `gorm:"column:gender;type:tinyint(4) unsigned;comment:性别。0:未知 ;1:男;2:女; 3:其他" json:"gender"`
Phone string `gorm:"column:phone;type:varchar(32);comment:手机号码" json:"phone"`
Status uint `gorm:"column:status;type:tinyint(4) unsigned;default:1;comment:是否有效。0:否1:是;NOT NULL" json:"status"`
RegisterSource string `gorm:"column:register_source;type:varchar(16);comment:注册来源" json:"register_source"`
LastIp string `gorm:"column:last_ip;type:varchar(32);comment:最近登录的ip" json:"last_ip"`
Deleted uint `gorm:"column:deleted;type:tinyint(4) unsigned;default:0;comment:逻辑删除。 0:未删除1:已删除;NOT NULL" json:"deleted"`
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"`
// UserDO 游戏帐号表
type UserDO struct {
Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
Account string `gorm:"column:account;type:varchar(64);comment:账号;NOT NULL" json:"account"`
Pwd string `gorm:"column:pwd;type:varchar(128);comment:密码;NOT NULL" json:"pwd"`
NickName string `gorm:"column:nickname;type:varchar(64);nickname:昵称" json:"nickname"`
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"`
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"`
VipType uint `gorm:"column:vip_type;type:smallint(4) unsigned;default:0;comment:会员类型0:非会员1:普通会员2:永久会员;NOT NULL" json:"vip_type"`
VipExpired *uint64 `gorm:"column:vip_expired;type:smallint(4) unsigned;default:0;comment:会员过期时间;NOT NULL" json:"vip_expired"`
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"`
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"`
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 *AccountDO) TableName() string {
func (m *UserDO) TableName() string {
return "g_account"
}
type gUserRepositoryImpl struct {
}
// FindByAccount 根据 account 和 pwd 查询用户
func (gUserRepositoryImpl) FindByAccount(ctx context.Context, account string) *AccountDO {
func (gUserRepositoryImpl) FindById(ctx context.Context, id int64) *UserDO {
//验证参数
if account == "" {
if id <= 0 {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model AccountDO
sqlErr := db.Model(&model).Where("email=?", account).First(&model).Error
var model UserDO
sqlErr := db.Model(&model).Where("id=?", id).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (gUserRepositoryImpl) FindByAccount(ctx context.Context, account string) *UserDO {
//验证参数
if account == "" {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("account=?", account).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (gUserRepositoryImpl) FindByEmail(ctx context.Context, email string) *UserDO {
//验证参数
if email == "" {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("email=?", email).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (gUserRepositoryImpl) FindByPhone(ctx context.Context, phone string) *UserDO {
//验证参数
if phone == "" {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("phone=?", phone).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (gUserRepositoryImpl) FindByWxId(ctx context.Context, wxId string) *UserDO {
//验证参数
if wxId == "" {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("wxId=?", wxId).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (gUserRepositoryImpl) FindByQqId(ctx context.Context, qqId string) *UserDO {
//验证参数
if qqId == "" {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("qqId=?", qqId).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (gUserRepositoryImpl) FindByGoogleId(ctx context.Context, googleId string) *UserDO {
//验证参数
if googleId == "" {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("googleId=?", googleId).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (gUserRepositoryImpl) FindByFacebookId(ctx context.Context, facebookId string) *UserDO {
//验证参数
if facebookId == "" {
return nil
}
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model UserDO
sqlErr := db.Model(&model).Where("facebookId=?", facebookId).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
// Create 创建用户
func (gUserRepositoryImpl) Create(ctx context.Context, data *AccountDO) error {
func (gUserRepositoryImpl) Create(ctx context.Context, data *UserDO) error {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
@ -69,7 +195,7 @@ func (gUserRepositoryImpl) Create(ctx context.Context, data *AccountDO) error {
}
// UpdateById 根据ID更新用户
func (gUserRepositoryImpl) UpdateById(ctx context.Context, data *AccountDO) error {
func (gUserRepositoryImpl) UpdateById(ctx context.Context, data *UserDO) error {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db

View File

@ -1,15 +0,0 @@
package repository
import "context"
type thirdUserTokenRepository interface {
FindByToken(ctx context.Context, token string) *GLoginToken
FindByUid(ctx context.Context, uid uint64) *GLoginToken
SaveUserLoginToken(ctx context.Context, data *GLoginToken) error
UpdateUserLoginToken(ctx context.Context, data *GLoginToken) error
}
var GUserToken thirdUserTokenRepository = new(thirdUserTokenRepositoryImpl)

View File

@ -1,91 +0,0 @@
package repository
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"time"
)
// GLoginToken 全局用户登录token表
type GLoginToken struct {
Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT;comment:主键" json:"id"`
Uid uint64 `gorm:"column:uid;type:bigint(20) unsigned;default:0;comment:uid;NOT NULL" json:"uid"`
Platform string `gorm:"column:platform;type:varchar(8);default:web;comment:平台类型;NOT NULL" json:"platform"`
AccessToken string `gorm:"column:access_token;type:varchar(64);comment:访问token" json:"access_token"`
ExpireTime int64 `gorm:"column:expire_time;type:bigint(20) unsigned;comment:超时时间;NOT NULL" json:"expire_time"`
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 *GLoginToken) TableName() string {
return "g_login_token"
}
type thirdUserTokenRepositoryImpl struct {
}
func (thirdUserTokenRepositoryImpl) FindByToken(ctx context.Context, token string) *GLoginToken {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model GLoginToken
sqlErr := db.Model(&model).Where("access_token=?", token).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (thirdUserTokenRepositoryImpl) FindByUid(ctx context.Context, uid uint64) *GLoginToken {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var model GLoginToken
sqlErr := db.Model(&model).Where("uid=? and platform='web'", uid).First(&model).Error
if sqlErr != nil {
return nil
}
return &model
}
func (thirdUserTokenRepositoryImpl) SaveUserLoginToken(ctx context.Context, data *GLoginToken) error {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
return db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "uid"}, {Name: "platform"}},
DoUpdates: clause.AssignmentColumns([]string{"access_token", "expire_time"}),
}).Create(data).Error
}
func (thirdUserTokenRepositoryImpl) UpdateUserLoginToken(ctx context.Context, data *GLoginToken) error {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
db = Db
}
var args []string
if data.AccessToken != "" {
args = append(args, "access_token")
}
if data.ExpireTime != 0 {
args = append(args, "expire_time")
}
sqlErr := db.Model(data).Select(args).Where("id=?", data.Id).Updates(*data).Error
if sqlErr != nil {
return sqlErr
}
return nil
}

View File

@ -35,7 +35,7 @@ func (l login) Register(ctx context.Context, req dto.ThirdRegisterReq) error {
return errors.New("user exist")
}
var user repository.AccountDO
var user repository.UserDO
user.Name = req.Name
user.Avatar = req.Icon