diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2235388..a0f68cb 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -32,8 +32,6 @@ golang-build: docker-package: stage: package - services: - - name: docker:23.0.1-dind script: - | docker info diff --git a/apps/user.go b/apps/user.go index b45227b..6ec1434 100644 --- a/apps/user.go +++ b/apps/user.go @@ -12,21 +12,71 @@ import ( ) func InitUserGroup(g *echo.Group) { - g.POST("ai/login", func(c echo.Context) error { - req := new(dto.ThirdLoginReq) + + //注册 + g.POST("user/register", func(c echo.Context) error { + req := new(dto.ThirdRegisterReq) if err := c.Bind(req); err != nil { - return c.JSON(500, err) + return c.JSON(http.StatusOK, utils.Error(err)) + } + + ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db) + + err := service.Login.Register(ctx, *req) + if err != nil { + return c.JSON(http.StatusOK, utils.Error(err)) + } + + return c.JSON(http.StatusOK, utils.Ok(true)) + }) + + //登录 + g.POST("user/login", func(c echo.Context) error { + req := new(dto.ThirdLoginReq) + if err := c.Bind(req); err != nil { + return c.JSON(http.StatusOK, utils.Error(err)) } - req.BizId = constant.BizIdAI.Val() ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db) token, err := service.Login.Login(ctx, *req) if err != nil { - return c.JSON(500, err) + return c.JSON(http.StatusOK, utils.Error(err)) } return c.JSON(http.StatusOK, utils.Ok(token)) }) + //发送 重置密码 验证码 + g.POST("user/sendResetPwdCode", func(c echo.Context) error { + req := new(dto.SendResetPwdCodeReq) + if err := c.Bind(req); err != nil { + return c.JSON(http.StatusOK, utils.Error(err)) + } + + err := service.Login.SendResetPwdCode(context.Background(), req.Account) + if err != nil { + return c.JSON(http.StatusOK, utils.Error(err)) + } + + return c.JSON(http.StatusOK, utils.Ok(true)) + }) + + //重置密码 + g.POST("user/resetPwd", func(c echo.Context) error { + req := new(dto.ThirdResetPwdReq) + if err := c.Bind(req); err != nil { + return c.JSON(http.StatusOK, utils.Error(err)) + } + + ctx := context.WithValue(context.Background(), constant.DBKey, repository.Db) + + err := service.Login.ResetPwd(ctx, *req) + if err != nil { + return c.JSON(http.StatusOK, utils.Error(err)) + } + + return c.JSON(http.StatusOK, utils.Ok(true)) + }) + } diff --git a/common/config/config.dev.yaml b/common/config/config.dev.yaml index f9a2ac3..827203a 100644 --- a/common/config/config.dev.yaml +++ b/common/config/config.dev.yaml @@ -6,7 +6,7 @@ fbServer: fbConsul: address: https://consul.mylomen.com datacenter: dg01 - token: 447b9c88-8983-f525-d118-f83a1383ad86 + token: 4b29ca6c-3233-4c70-7bb1-a3f4c2a18281 # rpc rpc: @@ -15,11 +15,12 @@ rpc: # mysql mysql: logSql: true - dsn: root:zym520!@tcp(43.133.218.116:10119)/user_center?charset=utf8mb4&parseTime=True&loc=Local - maxIdleConn: 10 - maxOpenConn: 10 + dsn: uc:zELLqwk2hBp8QyJgEtH5YGc6WKMS3dXP@tcp(43.133.218.116:10119)/user_center?charset=utf8mb4&parseTime=True&loc=Local + maxIdleConn: 4 + maxOpenConn: 4 connMaxLifetime: 3000 + # redis redis: db: 1 diff --git a/common/config/config.prod.yaml b/common/config/config.prod.yaml index c9da8c6..09b11a2 100644 --- a/common/config/config.prod.yaml +++ b/common/config/config.prod.yaml @@ -6,7 +6,7 @@ fbServer: fbConsul: address: 10.0.0.5:8500 datacenter: dg01 - token: 447b9c88-8983-f525-d118-f83a1383ad86 + token: 4b29ca6c-3233-4c70-7bb1-a3f4c2a18281 # rpc rpc: @@ -15,9 +15,9 @@ rpc: # mysql mysql: logSql: false - dsn: root:zym520!@tcp(10.0.0.5:10119)/user_center?charset=utf8mb4&parseTime=True&loc=Local + dsn: uc:zELLqwk2hBp8QyJgEtH5YGc6WKMS3dXP@tcp(10.0.0.5:10119)/user_center?charset=utf8mb4&parseTime=True&loc=Local maxIdleConn: 4 - maxOpenConn: 8 + maxOpenConn: 4 connMaxLifetime: 3000 # redis diff --git a/common/constant/redis.go b/common/constant/redis.go index d20caf5..5b7cea6 100644 --- a/common/constant/redis.go +++ b/common/constant/redis.go @@ -2,4 +2,6 @@ package constant const ( THIRD_LOGIN_TOKEN = "third:login:token:" + + G_RESET_PWD_CODE = "g:reset:pwd:email:" ) diff --git a/common/dto/third_user_login.go b/common/dto/third_user_login.go index 424eda6..068dc6f 100644 --- a/common/dto/third_user_login.go +++ b/common/dto/third_user_login.go @@ -1,17 +1,34 @@ package dto -type ThirdLoginReq struct { - CorpID uint64 `json:"corpId"` // 企业id - BizId uint64 `json:"bizId"` // 业务id +type ThirdRegisterReq struct { + Account string `json:"account"` // 帐号 + Password string `json:"password"` // 密码 + + Name string `json:"name"` // 昵称 + + Icon string `json:"icon"` // 头像 +} + +type SendResetPwdCodeReq struct { + Account string `json:"account"` // 帐号 +} + +type ThirdResetPwdReq struct { + Account string `json:"account"` // 帐号 + + Password string `json:"password"` // 密码 + + Code string `json:"code"` // 验证码 +} + +type ThirdLoginReq struct { Account string `json:"account"` // 帐号 Password string `json:"password"` // 密码 } type ThirdUserLoginToken struct { - CorpId uint64 `json:"corp_id"` - BizId uint64 `json:"biz_id"` Uid uint64 `json:"uid"` AccessToken string `json:"accessToken"` } diff --git a/common/email/email_tool.go b/common/email/email_tool.go new file mode 100644 index 0000000..e95e227 --- /dev/null +++ b/common/email/email_tool.go @@ -0,0 +1,34 @@ +package email + +import ( + "ai-gateway/common/logs" + "gopkg.in/gomail.v2" +) + +func sendNoticeEmail(subject, content string, address ...string) error { + gm := gomail.NewMessage() + gm.SetHeader("From", "") + if len(address) == 1 { + gm.SetHeader("To", address[0]) + } else { + gm.SetHeader("To", address...) + } + + gm.SetHeader("Subject", subject) + gm.SetBody("text/html", content) + d := gomail.NewDialer( + "email-smtp.us-east-2.amazonaws.com", + 465, + "AKIA6ILZQBMNF4T7HLJV", + "BH5TmE3FdgYs8tepWQZW5LLvbryQ+sWJde7+sAm++0/E", + ) + + err := d.DialAndSend(gm) + if err != nil { + logs.NewLog("").Errorf("html邮件 发送异常 subject: %s address: %s err: %+v", subject, address[0], err) + return err + } + + logs.NewLog("").Infof("html邮件 发送成功 subject: %s address: %s", subject, address[0]) + return nil +} diff --git a/common/email/email_tool_test.go b/common/email/email_tool_test.go new file mode 100644 index 0000000..bbfe1e6 --- /dev/null +++ b/common/email/email_tool_test.go @@ -0,0 +1,11 @@ +package email + +import ( + "fmt" + "testing" +) + +func TestSendDiscountCodeEmail(t *testing.T) { + err := sendNoticeEmail("我是测试标题", "我是测试内容", "mylomen.shao@freebrio.com") + fmt.Println(err) +} diff --git a/common/email/send_reset_pwd_code.go b/common/email/send_reset_pwd_code.go new file mode 100644 index 0000000..5d11f49 --- /dev/null +++ b/common/email/send_reset_pwd_code.go @@ -0,0 +1,237 @@ +package email + +import ( + "fmt" + "strings" +) + +const mylomen_code_email_template = ` + + + + + + code + + + +
+
+ +
+
Your verification code to reset your password
+

%s

+
Please complete the account verification process in one hours!
+
+
+
IMPORTANT:
+
+ Please do not share this email with anyone!
+ This email was automatically generated, please do not reply.
+ If you do not know why you have received this email, please discard it. +
+
+ +
+
+ +` + +func SendEmailVerifyCodeByEmail(code string, address ...string) error { + content := fmt.Sprintf(mylomen_code_email_template, strings.TrimSpace(code)) + return sendNoticeEmail( + "🎉 Your verification code to reset your password", + content, + address...) +} diff --git a/common/filter/auth.go b/common/filter/auth.go index 69aade2..8edab69 100644 --- a/common/filter/auth.go +++ b/common/filter/auth.go @@ -25,8 +25,6 @@ func UserAuth(next echo.HandlerFunc) echo.HandlerFunc { } //存入数据 - c.Set(constant.CorpId, loginResult.CorpId) - c.Set(constant.BizId, loginResult.BizId) c.Set(constant.UID, loginResult.Uid) return next(c) diff --git a/common/limit/init.go b/common/limit/init.go new file mode 100644 index 0000000..319f4b3 --- /dev/null +++ b/common/limit/init.go @@ -0,0 +1,5 @@ +package limit + +func init() { + +} diff --git a/common/utils/randomUtil.go b/common/utils/randomUtil.go new file mode 100644 index 0000000..42ae6a8 --- /dev/null +++ b/common/utils/randomUtil.go @@ -0,0 +1,49 @@ +package utils + +import ( + tRand "crypto/rand" + "math/big" + "math/rand" + "time" +) + +var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + +// GetPseudoRandomCode 伪随机数(性能高) +func GetPseudoRandomCode(n int, allowedChars ...[]rune) string { + //rand.Seed(time.Now().UnixNano()) + rand.New(rand.NewSource(time.Now().UnixNano())) + + var letters []rune + + if len(allowedChars) == 0 { + letters = defaultLetters + } else { + letters = allowedChars[0] + } + + b := make([]rune, n) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + + return string(b) +} + +// GetTrueRandomCode 真随机数 (在linux上已经有一个实现就是/dev/urandom,crypto/rand 就是从这个地方读“真随机”数字返回,但性能比较慢) +func GetTrueRandomCode(n int, allowedChars ...[]rune) string { + var letters []rune + if len(allowedChars) == 0 { + letters = defaultLetters + } else { + letters = allowedChars[0] + } + + b := make([]rune, n) + for i := range b { + n, _ := tRand.Int(tRand.Reader, big.NewInt(int64(len(letters)))) + b[i] = letters[n.Int64()] + } + + return string(b) +} diff --git a/common/utils/randomUtil_test.go b/common/utils/randomUtil_test.go new file mode 100644 index 0000000..3834b5f --- /dev/null +++ b/common/utils/randomUtil_test.go @@ -0,0 +1,11 @@ +package utils + +import ( + "fmt" + "testing" +) + +func TestRandom1(t *testing.T) { + fmt.Println(GetPseudoRandomCode(6)) + fmt.Println(GetTrueRandomCode(6)) +} diff --git a/go.mod b/go.mod index bec8378..fb83d07 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/robfig/cron/v3 v3.0.1 github.com/spf13/viper v1.18.2 go.uber.org/zap v1.27.0 + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gorm.io/driver/mysql v1.5.4 gorm.io/gorm v1.25.7 ) @@ -114,6 +115,7 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect google.golang.org/grpc v1.59.0 // indirect google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index fecc065..beddf58 100644 --- a/go.sum +++ b/go.sum @@ -497,11 +497,15 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= diff --git a/infrastructure/repository/g_account.go b/infrastructure/repository/g_account.go new file mode 100644 index 0000000..33434c3 --- /dev/null +++ b/infrastructure/repository/g_account.go @@ -0,0 +1,16 @@ +package repository + +import "context" + +type gUserRepository interface { + //FindByAccount 根据 account 查询用户 + FindByAccount(ctx context.Context, account string) *AccountDO + + //Create 创建用户 + Create(ctx context.Context, data *AccountDO) error + + //UpdateById 根据ID更新用户 + UpdateById(ctx context.Context, data *AccountDO) error +} + +var GUser gUserRepository = new(gUserRepositoryImpl) diff --git a/infrastructure/repository/g_account_repository.go b/infrastructure/repository/g_account_repository.go new file mode 100644 index 0000000..3905ef0 --- /dev/null +++ b/infrastructure/repository/g_account_repository.go @@ -0,0 +1,79 @@ +package repository + +import ( + "context" + "gorm.io/gorm" + "gorm.io/gorm/clause" + "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"` +} + +func (m *AccountDO) TableName() string { + return "g_account" +} + +type gUserRepositoryImpl struct { +} + +// FindByAccount 根据 account 和 pwd 查询用户 +func (gUserRepositoryImpl) FindByAccount(ctx context.Context, account string) *AccountDO { + db, ok := ctx.Value("db").(*gorm.DB) + if !ok { + db = Db + } + + var model AccountDO + sqlErr := db.Model(&model).Where("email=?", account).First(&model).Error + + if sqlErr != nil { + return nil + } + + return &model +} + +// Create 创建用户 +func (gUserRepositoryImpl) Create(ctx context.Context, data *AccountDO) error { + db, ok := ctx.Value("db").(*gorm.DB) + if !ok { + db = Db + } + + data.Deleted = 0 + + return db.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "email"}}, + DoUpdates: clause.AssignmentColumns([]string{"deleted"}), + }).Create(data).Error +} + +// UpdateById 根据ID更新用户 +func (gUserRepositoryImpl) UpdateById(ctx context.Context, data *AccountDO) error { + db, ok := ctx.Value("db").(*gorm.DB) + if !ok { + db = Db + } + + sqlErr := db.Model(data).Where("id=?", data.Id).Updates(data).Error + + if sqlErr != nil { + return sqlErr + } + return nil +} diff --git a/infrastructure/repository/g_login_token.go b/infrastructure/repository/g_login_token.go new file mode 100644 index 0000000..fa35dec --- /dev/null +++ b/infrastructure/repository/g_login_token.go @@ -0,0 +1,15 @@ +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) diff --git a/infrastructure/repository/third_user_token_repository.go b/infrastructure/repository/g_login_token_repository.go similarity index 64% rename from infrastructure/repository/third_user_token_repository.go rename to infrastructure/repository/g_login_token_repository.go index 4b00544..cf1294d 100644 --- a/infrastructure/repository/third_user_token_repository.go +++ b/infrastructure/repository/g_login_token_repository.go @@ -7,32 +7,31 @@ import ( "time" ) -// FThirdUserTokenDO 第三方用户token表 -type FThirdUserTokenDO struct { - Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"` - CorpId uint64 `gorm:"column:corp_id;type:bigint(20) unsigned;comment:企业id;NOT NULL" json:"corp_id"` - BizId uint64 `gorm:"column:biz_id;type:bigint(20);comment:业务id。 " json:"biz_id"` - CorpUid uint64 `gorm:"column:corp_uid;type:bigint(20) unsigned;comment:企业uid;NOT NULL" json:"corp_uid"` +// 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 *FThirdUserTokenDO) TableName() string { - return "f_third_user_token" +func (m *GLoginToken) TableName() string { + return "g_login_token" } type thirdUserTokenRepositoryImpl struct { } -func (thirdUserTokenRepositoryImpl) FindByToken(ctx context.Context, token string) *FThirdUserTokenDO { +func (thirdUserTokenRepositoryImpl) FindByToken(ctx context.Context, token string) *GLoginToken { db, ok := ctx.Value("db").(*gorm.DB) if !ok { db = Db } - var model FThirdUserTokenDO + var model GLoginToken sqlErr := db.Model(&model).Where("access_token=?", token).First(&model).Error if sqlErr != nil { @@ -41,14 +40,15 @@ func (thirdUserTokenRepositoryImpl) FindByToken(ctx context.Context, token strin return &model } -func (thirdUserTokenRepositoryImpl) FindByBizAndCorpId(ctx context.Context, bizId, corpUid uint64) *FThirdUserTokenDO { + +func (thirdUserTokenRepositoryImpl) FindByUid(ctx context.Context, uid uint64) *GLoginToken { db, ok := ctx.Value("db").(*gorm.DB) if !ok { db = Db } - var model FThirdUserTokenDO - sqlErr := db.Model(&model).Where("biz_id=? and corp_uid=?", bizId, corpUid).First(&model).Error + var model GLoginToken + sqlErr := db.Model(&model).Where("uid=? and platform=web", uid).First(&model).Error if sqlErr != nil { return nil @@ -57,19 +57,19 @@ func (thirdUserTokenRepositoryImpl) FindByBizAndCorpId(ctx context.Context, bizI return &model } -func (thirdUserTokenRepositoryImpl) SaveUserLoginToken(ctx context.Context, data *FThirdUserTokenDO) error { +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: "corp_uid"}, {Name: "biz_id"}}, + 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 *FThirdUserTokenDO) error { +func (thirdUserTokenRepositoryImpl) UpdateUserLoginToken(ctx context.Context, data *GLoginToken) error { db, ok := ctx.Value("db").(*gorm.DB) if !ok { db = Db diff --git a/infrastructure/repository/third_agent_corp_relation.go b/infrastructure/repository/third_agent_corp_relation.go deleted file mode 100644 index 5180ed0..0000000 --- a/infrastructure/repository/third_agent_corp_relation.go +++ /dev/null @@ -1,15 +0,0 @@ -package repository - -import "context" - -type thirdAgentCorpRepository interface { - QueryPage(ctx context.Context, page, size int) *[]FThirdAgentCorpRelationDO - FindById(ctx context.Context, id uint64) *FThirdAgentCorpRelationDO - Count(ctx context.Context) int64 - - Create(ctx context.Context, data *FThirdAgentCorpRelationDO) error - UpdateById(ctx context.Context, data FThirdAgentCorpRelationDO) error - deleteById(ctx context.Context, id uint64, operator string) error -} - -var ThirdAgentCorp thirdAgentCorpRepository = new(thirdAgentCorpRepositoryImpl) diff --git a/infrastructure/repository/third_agent_corp_relation_repository.go b/infrastructure/repository/third_agent_corp_relation_repository.go deleted file mode 100644 index 5debda6..0000000 --- a/infrastructure/repository/third_agent_corp_relation_repository.go +++ /dev/null @@ -1,119 +0,0 @@ -package repository - -import ( - "ai-gateway/common/utils" - "context" - "gorm.io/gorm" - "time" -) - -// FThirdAgentCorpRelationDO 第三方 代理&企业 关系表 -type FThirdAgentCorpRelationDO struct { - Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"` - AgentId uint64 `gorm:"column:agent_id;type:bigint(20) unsigned;comment:代理id;NOT NULL" json:"agent_id"` - CorpId uint64 `gorm:"column:corp_id;type:bigint(20) unsigned;comment:企业id;NOT NULL" json:"corp_id"` - BizId int64 `gorm:"column:biz_id;type:bigint(20);comment:业务id。 " json:"biz_id"` - QualityLimit uint64 `gorm:"column:quality_limit;type:bigint(20) unsigned;default:0;comment:数量限制;NOT NULL" json:"quality_limit"` - StartTime uint64 `gorm:"column:start_time;type:bigint(20) unsigned;comment:开始时间;NOT NULL" json:"start_time"` - EndTime uint64 `gorm:"column:end_time;type:bigint(20) unsigned;comment:结束时间;NOT NULL" json:"end_time"` - Deleted uint `gorm:"column:deleted;type:tinyint(1) 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"` -} - -func (m *FThirdAgentCorpRelationDO) TableName() string { - return "f_third_agent_corp_relation" -} - -type thirdAgentCorpRepositoryImpl struct { -} - -func (thirdAgentCorpRepositoryImpl) QueryPage(ctx context.Context, page, size int) *[]FThirdAgentCorpRelationDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - offset := (page - 1) * size - offset = utils.If(offset >= 0, offset, 0) - var dataList []FThirdAgentCorpRelationDO - sqlErr := db.Model(&FThirdAgentCorpRelationDO{}).Order("id DESC").Limit(size).Offset(offset).Find(&dataList).Error - if sqlErr != nil { - return nil - } - - if len(dataList) == 0 { - return nil - } - - return &dataList - -} - -func (thirdAgentCorpRepositoryImpl) FindById(ctx context.Context, id uint64) *FThirdAgentCorpRelationDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var model FThirdAgentCorpRelationDO - sqlErr := db.Where("id=?", id).First(&model).Error - if sqlErr != nil { - return nil - } - - return &model -} - -func (thirdAgentCorpRepositoryImpl) Count(ctx context.Context) int64 { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var total int64 - sqlErr := db.Model(&FThirdAgentCorpRelationDO{}).Select("count(*)").First(&total).Error - if sqlErr != nil { - return 0 - } - - return total -} - -func (thirdAgentCorpRepositoryImpl) Create(ctx context.Context, data *FThirdAgentCorpRelationDO) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - //保存数据 - return db.Create(data).Error -} - -func (thirdAgentCorpRepositoryImpl) UpdateById(ctx context.Context, data FThirdAgentCorpRelationDO) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - //更新数据 - return db.Updates(&data).Error -} - -func (thirdAgentCorpRepositoryImpl) deleteById(ctx context.Context, id uint64, operator string) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - data := FThirdAgentCorpRelationDO{} - sqlErr := db.Model(&data). - Select("deleted"). - Where("id=?", id). - Updates(FThirdAgentCorpRelationDO{Deleted: 1}).Error - if sqlErr != nil { - return sqlErr - } - - return nil -} diff --git a/infrastructure/repository/third_agent_info.go b/infrastructure/repository/third_agent_info.go deleted file mode 100644 index 0d85847..0000000 --- a/infrastructure/repository/third_agent_info.go +++ /dev/null @@ -1,17 +0,0 @@ -package repository - -import "context" - -type thirdAgentInfoRepository interface { - QueryPage(ctx context.Context, page, size int) *[]FThirdAgentInfoDO - FindById(ctx context.Context, id uint64) *FThirdAgentInfoDO - Count(ctx context.Context) int64 - - Create(ctx context.Context, data *FThirdAgentInfoDO) error - UpdateById(ctx context.Context, data FThirdAgentInfoDO) error - UpdateStatusById(ctx context.Context, id uint64, status int, operator string) error - - deleteById(ctx context.Context, id uint64, operator string) error -} - -var ThirdAgentInfo thirdAgentInfoRepository = new(thirdAgentInfoRepositoryImpl) diff --git a/infrastructure/repository/third_agent_info_repository.go b/infrastructure/repository/third_agent_info_repository.go deleted file mode 100644 index c258275..0000000 --- a/infrastructure/repository/third_agent_info_repository.go +++ /dev/null @@ -1,139 +0,0 @@ -package repository - -import ( - "ai-gateway/common/utils" - "context" - "gorm.io/gorm" - "time" -) - -// FThirdAgentInfoDO 三方代理 信息表 -type FThirdAgentInfoDO struct { - Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"` - Sn string `gorm:"column:sn;type:varchar(64);comment:sn" json:"sn"` - Name string `gorm:"column:name;type:varchar(64);comment:名称" json:"name"` - Account string `gorm:"column:account;type:varchar(64);comment:帐号" json:"account"` - Pwd string `gorm:"column:pwd;type:varchar(128);comment:密码" json:"pwd"` - Iphone string `gorm:"column:iphone;type:varchar(32);comment:联系方式" json:"iphone"` - Email string `gorm:"column:email;type:varchar(64);comment:email" json:"email"` - Remark string `gorm:"column:remark;type:varchar(64);comment:备注" json:"remark"` - Status int `gorm:"column:status;type:tinyint(1) unsigned;default:1;comment:是否有效。0:否;1:是;NOT NULL" json:"status"` - Deleted uint `gorm:"column:deleted;type:tinyint(1) 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"` -} - -func (m *FThirdAgentInfoDO) TableName() string { - return "f_third_agent_info" -} - -type thirdAgentInfoRepositoryImpl struct { -} - -func (thirdAgentInfoRepositoryImpl) QueryPage(ctx context.Context, page, size int) *[]FThirdAgentInfoDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - offset := (page - 1) * size - offset = utils.If(offset >= 0, offset, 0) - var dataList []FThirdAgentInfoDO - sqlErr := db.Model(&FThirdAgentInfoDO{}).Order("id DESC").Limit(size).Offset(offset).Find(&dataList).Error - if sqlErr != nil { - return nil - } - - if len(dataList) == 0 { - return nil - } - - return &dataList - -} - -func (thirdAgentInfoRepositoryImpl) FindById(ctx context.Context, id uint64) *FThirdAgentInfoDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var model FThirdAgentInfoDO - sqlErr := db.Where("id=?", id).First(&model).Error - if sqlErr != nil { - return nil - } - - return &model -} - -func (thirdAgentInfoRepositoryImpl) Count(ctx context.Context) int64 { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var total int64 - sqlErr := db.Model(&FThirdAgentInfoDO{}).Select("count(*)").First(&total).Error - if sqlErr != nil { - return 0 - } - - return total -} - -func (thirdAgentInfoRepositoryImpl) Create(ctx context.Context, data *FThirdAgentInfoDO) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - //保存数据 - return db.Create(data).Error -} - -func (thirdAgentInfoRepositoryImpl) UpdateById(ctx context.Context, data FThirdAgentInfoDO) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - //更新数据 - return db.Updates(&data).Error - -} - -func (thirdAgentInfoRepositoryImpl) UpdateStatusById(ctx context.Context, id uint64, status int, operator string) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - data := FThirdAgentInfoDO{} - sqlErr := db.Model(&data). - Select("status"). - Where("id=?", id). - Updates(FThirdAgentInfoDO{Status: status}).Error - if sqlErr != nil { - return sqlErr - } - - return nil -} - -func (thirdAgentInfoRepositoryImpl) deleteById(ctx context.Context, id uint64, operator string) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - data := FThirdAgentInfoDO{} - sqlErr := db.Model(&data). - Select("deleted"). - Where("id=?", id). - Updates(FThirdAgentInfoDO{Deleted: 1}).Error - if sqlErr != nil { - return sqlErr - } - - return nil -} diff --git a/infrastructure/repository/third_corp_info.go b/infrastructure/repository/third_corp_info.go deleted file mode 100644 index 31d49b2..0000000 --- a/infrastructure/repository/third_corp_info.go +++ /dev/null @@ -1,16 +0,0 @@ -package repository - -import "context" - -type thirdCorpInfoRepository interface { - QueryPage(ctx context.Context, page, size int) *[]FThirdCorpInfoDO - FindById(ctx context.Context, id uint64) *FThirdCorpInfoDO - Count(ctx context.Context) int64 - - Create(ctx context.Context, data *FThirdCorpInfoDO) error - UpdateById(ctx context.Context, data FThirdCorpInfoDO) error - UpdateStatusById(ctx context.Context, id uint64, state int, operator string) error - deleteById(ctx context.Context, id uint64, operator string) error -} - -var ThirdCorpInfo thirdCorpInfoRepository = new(thirdCorpInfoRepositoryImpl) diff --git a/infrastructure/repository/third_corp_info_repository.go b/infrastructure/repository/third_corp_info_repository.go deleted file mode 100644 index 7f3cf5c..0000000 --- a/infrastructure/repository/third_corp_info_repository.go +++ /dev/null @@ -1,142 +0,0 @@ -package repository - -import ( - "ai-gateway/common/logs" - "ai-gateway/common/utils" - "context" - "gorm.io/gorm" - "time" -) - -// FThirdCorpInfoDO 第三方企业 信息表 -type FThirdCorpInfoDO struct { - Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"` - Sn string `gorm:"column:sn;type:varchar(64);comment:sn" json:"sn"` - SimpleName string `gorm:"column:simple_name;type:varchar(64);comment:企业简称" json:"simple_name"` - Name string `gorm:"column:name;type:varchar(64);comment:名称" json:"name"` - Iphone string `gorm:"column:iphone;type:varchar(32);comment:联系方式" json:"iphone"` - Email string `gorm:"column:email;type:varchar(64);comment:email" json:"email"` - Remark string `gorm:"column:remark;type:varchar(64);comment:备注" json:"remark"` - Status int `gorm:"column:status;type:tinyint(1) unsigned;default:1;comment:是否有效。0:否;1:是;NOT NULL" json:"status"` - Deleted uint `gorm:"column:deleted;type:tinyint(1) 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"` -} - -func (m *FThirdCorpInfoDO) TableName() string { - return "f_third_corp_info" -} - -type thirdCorpInfoRepositoryImpl struct { -} - -func (thirdCorpInfoRepositoryImpl) QueryPage(ctx context.Context, page, size int) *[]FThirdCorpInfoDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - offset := (page - 1) * size - offset = utils.If(offset >= 0, offset, 0) - var dataList []FThirdCorpInfoDO - sqlErr := db.Model(&FThirdCorpInfoDO{}).Order("id DESC").Limit(size).Offset(offset).Find(&dataList).Error - if sqlErr != nil { - return nil - } - - if len(dataList) == 0 { - return nil - } - - return &dataList - -} - -func (thirdCorpInfoRepositoryImpl) FindById(ctx context.Context, id uint64) *FThirdCorpInfoDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var model FThirdCorpInfoDO - sqlErr := db.Where("id=?", id).First(&model).Error - if sqlErr != nil { - logs.NewLog("").Errorf("FindById_sqlErr id:%d %+v", id, sqlErr) - return nil - } - - return &model -} - -func (thirdCorpInfoRepositoryImpl) Count(ctx context.Context) int64 { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var total int64 - sqlErr := db.Model(&FThirdCorpInfoDO{}).Select("count(*)").First(&total).Error - if sqlErr != nil { - logs.NewLog("").Errorf("Count_sqlErr %+v", sqlErr) - return 0 - } - - return total -} - -func (thirdCorpInfoRepositoryImpl) Create(ctx context.Context, data *FThirdCorpInfoDO) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - //保存数据 - return db.Create(data).Error -} - -func (thirdCorpInfoRepositoryImpl) UpdateById(ctx context.Context, data FThirdCorpInfoDO) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - //更新数据 - return db.Updates(&data).Error - -} - -func (thirdCorpInfoRepositoryImpl) UpdateStatusById(ctx context.Context, id uint64, status int, operator string) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - data := FThirdCorpInfoDO{} - sqlErr := db.Model(&data). - Select("status"). - Where("id=?", id). - Updates(FThirdCorpInfoDO{Status: status}).Error - if sqlErr != nil { - return sqlErr - } - - return nil -} - -func (thirdCorpInfoRepositoryImpl) deleteById(ctx context.Context, id uint64, operator string) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - data := FThirdCorpInfoDO{} - sqlErr := db.Model(&data). - Select("deleted"). - Where("id=?", id). - Updates(FThirdCorpInfoDO{Deleted: 1}).Error - if sqlErr != nil { - return sqlErr - } - - return nil -} diff --git a/infrastructure/repository/third_corp_user_account.go b/infrastructure/repository/third_corp_user_account.go deleted file mode 100644 index 45cfef3..0000000 --- a/infrastructure/repository/third_corp_user_account.go +++ /dev/null @@ -1,18 +0,0 @@ -package repository - -import "context" - -type thirdCorpUserAccountRepository interface { - QueryPage(ctx context.Context, page, size int) *[]FThirdCorpUserAccountDO - FindById(ctx context.Context, id uint64) *FThirdCorpUserAccountDO - Count(ctx context.Context) int64 - - Create(ctx context.Context, data *FThirdCorpUserAccountDO) error - UpdateById(ctx context.Context, data FThirdCorpUserAccountDO) error - UpdateStatusById(ctx context.Context, id uint64, state int, operator string) error - deleteById(ctx context.Context, id uint64, operator string) error - - FindThirdAccountBy(ctx context.Context, bizId, corpId uint64, account string) *FThirdCorpUserAccountDO -} - -var ThirdCorpUserAccount thirdCorpUserAccountRepository = new(thirdCorpUserAccountRepositoryImpl) diff --git a/infrastructure/repository/third_corp_user_account_repository.go b/infrastructure/repository/third_corp_user_account_repository.go deleted file mode 100644 index 49bb8da..0000000 --- a/infrastructure/repository/third_corp_user_account_repository.go +++ /dev/null @@ -1,160 +0,0 @@ -package repository - -import ( - "ai-gateway/common/logs" - "ai-gateway/common/utils" - "context" - "gorm.io/gorm" - "time" -) - -// FThirdCorpUserAccountDO 第三方 企业用户 帐号表 -type FThirdCorpUserAccountDO struct { - Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"` - Sn string `gorm:"column:sn;type:varchar(64);comment:sn" json:"sn"` - CorpId uint64 `gorm:"column:corp_id;type:bigint(20) unsigned;comment:企业id;NOT NULL" json:"corp_id"` - BizId int64 `gorm:"column:biz_id;type:bigint(20);comment:业务id。 " json:"biz_id"` - Name string `gorm:"column:name;type:varchar(64);comment:名称" json:"name"` - Admin uint `gorm:"column:admin;type:tinyint(1) unsigned;default:0;comment:是否管理员。0:否;1:是;NOT NULL" json:"admin"` - Account string `gorm:"column:account;type:varchar(64);comment:帐号" json:"account"` - Pwd string `gorm:"column:pwd;type:varchar(128);comment:密码" json:"pwd"` - Status int `gorm:"column:status;type:tinyint(1) unsigned;default:1;comment:是否有效。0:否;1:是;NOT NULL" json:"status"` - Deleted uint `gorm:"column:deleted;type:tinyint(1) 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"` -} - -func (m *FThirdCorpUserAccountDO) TableName() string { - return "f_third_corp_user_account" -} - -type thirdCorpUserAccountRepositoryImpl struct { -} - -func (thirdCorpUserAccountRepositoryImpl) QueryPage(ctx context.Context, page, size int) *[]FThirdCorpUserAccountDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - offset := (page - 1) * size - offset = utils.If(offset >= 0, offset, 0) - var dataList []FThirdCorpUserAccountDO - sqlErr := db.Model(&FThirdCorpUserAccountDO{}).Order("id DESC").Limit(size).Offset(offset).Find(&dataList).Error - if sqlErr != nil { - logs.NewLog("").Errorf("QueryPage_sqlErr page:%d %+v", page, sqlErr) - return nil - } - - if len(dataList) == 0 { - return nil - } - - return &dataList - -} - -func (thirdCorpUserAccountRepositoryImpl) FindById(ctx context.Context, id uint64) *FThirdCorpUserAccountDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var model FThirdCorpUserAccountDO - sqlErr := db.Where("id=?", id).First(&model).Error - if sqlErr != nil { - logs.NewLog("").Errorf("FindById_sqlErr id:%d %+v", id, sqlErr) - return nil - } - - return &model -} - -func (thirdCorpUserAccountRepositoryImpl) Count(ctx context.Context) int64 { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var total int64 - sqlErr := db.Model(&FThirdCorpUserAccountDO{}).Select("count(*)").First(&total).Error - if sqlErr != nil { - logs.NewLog("").Errorf("Count_sqlErr %+v", sqlErr) - return 0 - } - - return total -} - -func (thirdCorpUserAccountRepositoryImpl) Create(ctx context.Context, data *FThirdCorpUserAccountDO) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - //保存数据 - return db.Create(data).Error -} - -func (thirdCorpUserAccountRepositoryImpl) UpdateById(ctx context.Context, data FThirdCorpUserAccountDO) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - //更新数据 - return db.Updates(&data).Error - -} - -func (thirdCorpUserAccountRepositoryImpl) UpdateStatusById(ctx context.Context, id uint64, status int, operator string) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - data := FThirdCorpUserAccountDO{} - sqlErr := db.Model(&data). - Select("status"). - Where("id=?", id). - Updates(FThirdCorpUserAccountDO{Status: status}).Error - if sqlErr != nil { - return sqlErr - } - - return nil -} - -func (thirdCorpUserAccountRepositoryImpl) deleteById(ctx context.Context, id uint64, operator string) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - data := FThirdCorpUserAccountDO{} - sqlErr := db.Model(&data). - Select("deleted"). - Where("id=?", id). - Updates(FThirdCorpUserAccountDO{Deleted: 1}).Error - if sqlErr != nil { - return sqlErr - } - - return nil -} - -func (thirdCorpUserAccountRepositoryImpl) FindThirdAccountBy(ctx context.Context, bizId, corpId uint64, account string) *FThirdCorpUserAccountDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var model FThirdCorpUserAccountDO - sqlErr := db.Where("biz_id=? and corp_id=? And account=?", bizId, corpId, account).First(&model).Error - if sqlErr != nil { - logs.NewLog("").Errorf("FindThirdAccountBy_sqlErr corpId:%d account:%s %+v", corpId, account, sqlErr) - return nil - } - - return &model -} diff --git a/infrastructure/repository/third_corp_user_log.go b/infrastructure/repository/third_corp_user_log.go deleted file mode 100644 index b1f8a8d..0000000 --- a/infrastructure/repository/third_corp_user_log.go +++ /dev/null @@ -1,11 +0,0 @@ -package repository - -import "context" - -type thirdCorpUserLogRepository interface { - QueryList(ctx context.Context, id uint64, size int) *[]FThirdCorpUserLogDO - FindById(ctx context.Context, id uint64) *FThirdCorpUserLogDO - Create(ctx context.Context, data *FThirdCorpUserLogDO) error -} - -var ThirdCorpUserLog thirdCorpUserLogRepository = new(thirdCorpUserLogRepositoryImpl) diff --git a/infrastructure/repository/third_corp_user_log_repository.go b/infrastructure/repository/third_corp_user_log_repository.go deleted file mode 100644 index 31ece8b..0000000 --- a/infrastructure/repository/third_corp_user_log_repository.go +++ /dev/null @@ -1,72 +0,0 @@ -package repository - -import ( - "ai-gateway/common/logs" - "context" - "gorm.io/gorm" - "time" -) - -// FThirdCorpUserLogDO 第三方 企业用户 日志表 -type FThirdCorpUserLogDO struct { - Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"` - CorpId uint64 `gorm:"column:corp_id;type:bigint(20) unsigned;comment:企业id;NOT NULL" json:"corp_id"` - BizId int64 `gorm:"column:biz_id;type:bigint(20);comment:业务id。 " json:"biz_id"` - CorpUid uint64 `gorm:"column:corp_uid;type:bigint(20) unsigned;comment:企业uid;NOT NULL" json:"corp_uid"` - BizContext string `gorm:"column:biz_context;type:json;comment:业务内容 " json:"biz_context"` - 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 *FThirdCorpUserLogDO) TableName() string { - return "f_third_corp_user_log" -} - -type thirdCorpUserLogRepositoryImpl struct { -} - -func (thirdCorpUserLogRepositoryImpl) QueryList(ctx context.Context, id uint64, size int) *[]FThirdCorpUserLogDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var dataList []FThirdCorpUserLogDO - sqlErr := db.Model(&FThirdCorpUserLogDO{}).Where("id>?", id).Order("id DESC").Limit(size).Find(&dataList).Error - if sqlErr != nil { - logs.NewLog("").Errorf("QueryList_sqlErr id:%d %+v", id, sqlErr) - return nil - } - - if len(dataList) == 0 { - return nil - } - - return &dataList -} - -func (thirdCorpUserLogRepositoryImpl) FindById(ctx context.Context, id uint64) *FThirdCorpUserLogDO { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - var model FThirdCorpUserLogDO - sqlErr := db.Where("id=?", id).First(&model).Error - if sqlErr != nil { - logs.NewLog("").Errorf("FindById_sqlErr id:%d %+v", id, sqlErr) - return nil - } - - return &model -} - -func (thirdCorpUserLogRepositoryImpl) Create(ctx context.Context, data *FThirdCorpUserLogDO) error { - db, ok := ctx.Value("db").(*gorm.DB) - if !ok { - db = Db - } - - //保存数据 - return db.Create(data).Error -} diff --git a/infrastructure/repository/third_user_token.go b/infrastructure/repository/third_user_token.go deleted file mode 100644 index acaa139..0000000 --- a/infrastructure/repository/third_user_token.go +++ /dev/null @@ -1,14 +0,0 @@ -package repository - -import "context" - -type thirdUserTokenRepository interface { - FindByToken(ctx context.Context, token string) *FThirdUserTokenDO - FindByBizAndCorpId(ctx context.Context, bizId, corpUid uint64) *FThirdUserTokenDO - - SaveUserLoginToken(ctx context.Context, data *FThirdUserTokenDO) error - - UpdateUserLoginToken(ctx context.Context, data *FThirdUserTokenDO) error -} - -var ThirdUserToken thirdUserTokenRepository = new(thirdUserTokenRepositoryImpl) diff --git a/service/third_login.go b/service/third_login.go index 9ac6481..697642f 100644 --- a/service/third_login.go +++ b/service/third_login.go @@ -3,6 +3,7 @@ package service import ( "ai-gateway/common/constant" "ai-gateway/common/dto" + "ai-gateway/common/email" "ai-gateway/common/utils" "ai-gateway/infrastructure/redis" "ai-gateway/infrastructure/repository" @@ -13,6 +14,7 @@ import ( "errors" "github.com/google/uuid" "github.com/labstack/echo/v4" + "golang.org/x/time/rate" "strings" "time" ) @@ -22,12 +24,36 @@ type login struct { var Login login +// 令牌桶大小为 100, 以每秒 10 个 Token 的速率向桶中放置 Token +var limiter = rate.NewLimiter(10, 10) + +// Register 注册 +func (l login) Register(ctx context.Context, req dto.ThirdRegisterReq) error { + acUser := repository.GUser.FindByAccount(ctx, req.Account) + if acUser != nil && acUser.Deleted == 0 { + return errors.New("user exist") + } + + var user repository.AccountDO + user.Name = req.Name + user.Avatar = req.Icon + + user.Email = req.Account + //密码加密 + h := sha256.Sum256([]byte(req.Password)) + passHash := hex.EncodeToString(h[:]) + user.Pwd = passHash + + repository.GUser.Create(ctx, &user) + return nil +} + func (l login) Login(ctx context.Context, req dto.ThirdLoginReq) (string, error) { //1. 验证账号密码 h := sha256.Sum256([]byte(req.Password)) passHash := hex.EncodeToString(h[:]) - acUser := repository.ThirdCorpUserAccount.FindThirdAccountBy(ctx, req.BizId, req.CorpID, req.Account) + acUser := repository.GUser.FindByAccount(ctx, req.Account) if acUser == nil || acUser.Deleted == 1 { return "", errors.New("user not exist") } @@ -38,24 +64,92 @@ func (l login) Login(ctx context.Context, req dto.ThirdLoginReq) (string, error) //生成token token := uuid.New().String() token = strings.ReplaceAll(token, "-", "") - var thirdUserToken = repository.FThirdUserTokenDO{ - CorpId: req.CorpID, - BizId: req.BizId, - CorpUid: acUser.Id, + var thirdUserToken = repository.GLoginToken{ + Uid: acUser.Id, AccessToken: token, ExpireTime: time.Now().Add(time.Duration(24*365*100) * time.Hour).UnixMilli(), } - loginUserToken := repository.ThirdUserToken.FindByBizAndCorpId(ctx, req.BizId, acUser.Id) - if loginUserToken == nil { - repository.ThirdUserToken.SaveUserLoginToken(ctx, &thirdUserToken) + gUserToken := repository.GUserToken.FindByUid(ctx, acUser.Id) + if gUserToken == nil { + repository.GUserToken.SaveUserLoginToken(ctx, &thirdUserToken) return l.GenAccessToken(&thirdUserToken), nil } + gUserToken.AccessToken = thirdUserToken.AccessToken + gUserToken.ExpireTime = thirdUserToken.ExpireTime + repository.GUserToken.UpdateUserLoginToken(ctx, gUserToken) + return l.GenAccessToken(gUserToken), nil +} + +func (l login) SendResetPwdCode(ctx context.Context, account string) error { + //1. 验证账号 + acUser := repository.GUser.FindByAccount(ctx, account) + if acUser == nil || acUser.Deleted == 1 { + return errors.New("user not exist") + } + + //2. 生成code & 发送 + code := utils.GetPseudoRandomCode(6) + + //3. save into redis + if err := redis.Set(constant.G_RESET_PWD_CODE+account, code, time.Duration(30)*time.Minute); err != nil { + return errors.New("cache illegal") + } + + //频控 + if !limiter.Allow() { + return errors.New("rate limit, please try again later") + } + + //4. send code + if err := email.SendEmailVerifyCodeByEmail(code, account); err != nil { + return errors.New("send email code illegal") + } + + return nil +} + +// ResetPwd 重置密码 +func (l login) ResetPwd(ctx context.Context, req dto.ThirdResetPwdReq) error { + //1. 验证code + redisStr, err := redis.Get(constant.G_RESET_PWD_CODE + req.Account) + if err != nil && redisStr != req.Code { + return errors.New("code is error") + } + + //2. 查询用户 + acUser := repository.GUser.FindByAccount(ctx, req.Account) + if acUser == nil || acUser.Deleted == 1 { + return errors.New("user not exist") + } + + //3. 重置密码 + h := sha256.Sum256([]byte(req.Password)) + passHash := hex.EncodeToString(h[:]) + acUser.Pwd = passHash + repository.GUser.UpdateById(ctx, acUser) + + //生成token + token := uuid.New().String() + token = strings.ReplaceAll(token, "-", "") + var thirdUserToken = repository.GLoginToken{ + Uid: acUser.Id, + Platform: "web", + AccessToken: token, + ExpireTime: time.Now().Add(time.Duration(24*365*100) * time.Hour).UnixMilli(), + } + + loginUserToken := repository.GUserToken.FindByUid(ctx, acUser.Id) + if loginUserToken == nil { + repository.GUserToken.SaveUserLoginToken(ctx, &thirdUserToken) + return nil + } + loginUserToken.AccessToken = thirdUserToken.AccessToken loginUserToken.ExpireTime = thirdUserToken.ExpireTime - repository.ThirdUserToken.UpdateUserLoginToken(ctx, loginUserToken) - return l.GenAccessToken(loginUserToken), nil + repository.GUserToken.UpdateUserLoginToken(ctx, loginUserToken) + return nil } func (l login) GetLoginResult(ctx context.Context, c *echo.Context) *dto.ThirdUserLoginToken { @@ -64,13 +158,8 @@ func (l login) GetLoginResult(ctx context.Context, c *echo.Context) *dto.ThirdUs return nil } - thirdLoginToken := l.ParseThirdUserLoginTokenByToken(accessToken) - if thirdLoginToken == nil { - return nil - } - //redis - redisStr, err := redis.Get(constant.THIRD_LOGIN_TOKEN + thirdLoginToken.AccessToken) + redisStr, err := redis.Get(constant.THIRD_LOGIN_TOKEN + accessToken) if err != nil && redisStr != "" { var loginInfo dto.ThirdUserLoginToken if redisErr := json.Unmarshal([]byte(redisStr), &loginInfo); redisErr == nil { @@ -79,15 +168,13 @@ func (l login) GetLoginResult(ctx context.Context, c *echo.Context) *dto.ThirdUs } //repository - dbData := repository.ThirdUserToken.FindByToken(ctx, thirdLoginToken.AccessToken) + dbData := repository.GUserToken.FindByToken(ctx, accessToken) if dbData == nil { return nil } var finalData = dto.ThirdUserLoginToken{ - CorpId: dbData.CorpId, - BizId: dbData.BizId, - Uid: dbData.CorpUid, + Uid: dbData.Uid, AccessToken: l.GenAccessToken(dbData), } @@ -99,28 +186,6 @@ func (l login) GetLoginResult(ctx context.Context, c *echo.Context) *dto.ThirdUs return &finalData } -func (login) ParseThirdUserLoginTokenByToken(accessToken string) *dto.ThirdUserLoginToken { - splits := strings.Split(accessToken, "_") - if len(splits) != 4 { - return nil - } - - return &dto.ThirdUserLoginToken{ - CorpId: utils.ToUint64(splits[0]), - BizId: utils.ToUint64(splits[1]), - Uid: utils.ToUint64(splits[2]), - AccessToken: splits[3], - } -} - -func (login) GenAccessToken(data *repository.FThirdUserTokenDO) string { - var buf strings.Builder - buf.WriteString(utils.ToString(data.CorpId)) - buf.WriteString("_") - buf.WriteString(utils.ToString(data.BizId)) - buf.WriteString("_") - buf.WriteString(utils.ToString(data.CorpUid)) - buf.WriteString("_") - buf.WriteString(data.AccessToken) - return buf.String() +func (login) GenAccessToken(data *repository.GLoginToken) string { + return data.AccessToken }