完善一部分websocket的代码
This commit is contained in:
parent
190010a6fe
commit
6919d61ef5
|
@ -2,6 +2,7 @@ package gmodel
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"fusenapi/utils/auth"
|
||||
"time"
|
||||
|
||||
|
@ -45,6 +46,37 @@ func (u *FsUserModel) Transaction(ctx context.Context, fc func(tx *gorm.DB) erro
|
|||
return u.db.WithContext(ctx).Transaction(fc)
|
||||
}
|
||||
|
||||
// 继承guest_id的资源表
|
||||
func InheritGuestIdResource(tx *gorm.DB, userId, guestId int64) error {
|
||||
var err error
|
||||
if guestId != 0 {
|
||||
// 继承guest_id的资源表
|
||||
err = tx.Model(&FsResource{}).
|
||||
Where("guest_id = ?", guestId).
|
||||
UpdateColumn("user_id", userId).Error
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Model(&FsUserMaterial{}).
|
||||
Where("guest_id = ?", guestId).
|
||||
UpdateColumn("user_id", userId).Error
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = tx.Model(&FsUserInfo{}).
|
||||
Where("guest_id = ?", guestId).
|
||||
UpdateColumn("user_id", userId).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("guest_id must not be 0")
|
||||
}
|
||||
|
||||
// 谷歌平台的注册流程
|
||||
func (u *FsUserModel) RegisterByGoogleOAuth(ctx context.Context, token *auth.RegisterToken) (*FsUser, error) {
|
||||
user := &FsUser{}
|
||||
|
@ -67,11 +99,8 @@ func (u *FsUserModel) RegisterByGoogleOAuth(ctx context.Context, token *auth.Reg
|
|||
|
||||
if token.GuestId != 0 {
|
||||
// 继承guest_id的资源表
|
||||
return tx.Model(&FsResource{}).
|
||||
Where("guest_id = ?", token.GuestId).
|
||||
UpdateColumn("user_id", user.Id).Error
|
||||
return InheritGuestIdResource(tx, user.Id, token.GuestId)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -90,6 +119,46 @@ func (u *FsUserModel) RegisterByGoogleOAuth(ctx context.Context, token *auth.Reg
|
|||
return user, nil
|
||||
}
|
||||
|
||||
// 自平台的注册流程
|
||||
func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterToken) (*FsUser, error) {
|
||||
user := &FsUser{}
|
||||
|
||||
err := u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
err := tx.Model(user).Where("email = ?", token.Email).Take(user).Error
|
||||
if err != nil {
|
||||
// 没有找到在数据库就创建注册
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
createAt := time.Now().Unix()
|
||||
user.Email = &token.Email
|
||||
user.CreatedAt = &createAt
|
||||
user.PasswordHash = &token.Password
|
||||
err = tx.Model(user).Create(user).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if token.GuestId != 0 {
|
||||
// 继承guest_id的资源表
|
||||
return InheritGuestIdResource(tx, user.Id, token.GuestId)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
return fmt.Errorf("the email had registered")
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (u *FsUserModel) UpdateUserBasicInfoById(ctx context.Context, Id int64, user *UserBasicInfoForSave) (err error) {
|
||||
|
||||
err = u.db.WithContext(ctx).Model(&FsUser{}).
|
||||
|
|
|
@ -54,7 +54,7 @@ func (m *EmailSender) ProcessEmailTasks() {
|
|||
|
||||
m.emailSending[emailformat.TargetEmail] = &EmailTask{
|
||||
Email: emailformat,
|
||||
SendTime: time.Now(),
|
||||
SendTime: time.Now().UTC(),
|
||||
}
|
||||
m.lock.Unlock()
|
||||
|
||||
|
@ -82,7 +82,7 @@ func (m *EmailSender) Resend(emailTarget string, content []byte) {
|
|||
defer m.lock.Unlock()
|
||||
|
||||
// Check if the email task still exists and has not been sent successfully
|
||||
if task, ok := m.emailSending[emailTarget]; ok && task.SendTime.Add(m.ResendTimeLimit).After(time.Now()) {
|
||||
if task, ok := m.emailSending[emailTarget]; ok && task.SendTime.Add(m.ResendTimeLimit).After(time.Now().UTC()) {
|
||||
err := smtp.SendMail(emailTarget, m.Auth, m.FromEmail, []string{emailTarget}, content)
|
||||
if err != nil {
|
||||
log.Printf("Failed to resend email to %s: %v\n", emailTarget, err)
|
||||
|
@ -102,7 +102,7 @@ func (m *EmailSender) ClearExpiredTasks() {
|
|||
|
||||
m.lock.Lock()
|
||||
for email, task := range m.emailSending {
|
||||
if task.SendTime.Add(m.ResendTimeLimit).Before(time.Now()) {
|
||||
if task.SendTime.Add(m.ResendTimeLimit).Before(time.Now().UTC()) {
|
||||
delete(m.emailSending, email)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,6 +76,28 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
|
|||
case "facebook":
|
||||
case "fusen":
|
||||
|
||||
user, err := l.svcCtx.AllModels.FsUser.RegisterByFusen(l.ctx, token)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeDbSqlErr)
|
||||
}
|
||||
|
||||
// 创建签证
|
||||
jwtToken, err := auth.GenerateJwtTokenUint64(
|
||||
auth.StringToHash(*user.PasswordHash),
|
||||
l.svcCtx.Config.Auth.AccessExpire,
|
||||
time.Now().Unix(),
|
||||
user.Id,
|
||||
0,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(jwtToken)
|
||||
|
||||
}
|
||||
|
||||
default:
|
||||
|
|
|
@ -25,7 +25,7 @@ func BenchmarkConfirmationLink(b *testing.B) {
|
|||
cl := NewConfirmationLink[Register](key, "http://localhost:9900/api/auth/oauth2/register")
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
uri, _ := cl.Generate(&Register{Id: 39, Password: "21dsadsad", platform: "google", Expired: time.Now()})
|
||||
uri, _ := cl.Generate(&Register{Id: 39, Password: "21dsadsad", platform: "google", Expired: time.Now().UTC()})
|
||||
u, _ := url.Parse(uri)
|
||||
token := u.Query()["token"]
|
||||
cl.Decrypt(token[0])
|
||||
|
@ -125,7 +125,7 @@ func TestConfirmationLink(t *testing.T) {
|
|||
key := "21321321"
|
||||
|
||||
cl := NewConfirmationLink[Register](key, "http://localhost:9900/api/auth/oauth2/register")
|
||||
uri, _ := cl.Generate(&Register{Id: 39, Password: "21dsadsad", platform: "google", Expired: time.Now()})
|
||||
uri, _ := cl.Generate(&Register{Id: 39, Password: "21dsadsad", platform: "google", Expired: time.Now().UTC()})
|
||||
log.Println(uri)
|
||||
|
||||
u, _ := url.Parse(uri)
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
// TestGenJwt 测试jwt序列化
|
||||
func TestGenJwt(t *testing.T) {
|
||||
now := time.Now().Unix()
|
||||
now := time.Now().UTC().Unix()
|
||||
secret := "fusen123"
|
||||
a, err := GenerateJwtToken(&secret, 3600, now, 123, 1234)
|
||||
if err != nil {
|
||||
|
@ -34,7 +34,7 @@ func TestGenJwt(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGenBackendJwt(t *testing.T) {
|
||||
now := time.Now().Unix()
|
||||
now := time.Now().UTC().Unix()
|
||||
secret := "fusen_backend_2023"
|
||||
a, err := GenerateBackendJwtToken(&secret, 3600*24*7, now, 1, 1)
|
||||
if err != nil {
|
||||
|
|
|
@ -8,6 +8,6 @@ import (
|
|||
)
|
||||
|
||||
func GenSnNum() string {
|
||||
a := fmt.Sprintf("%s%.8d", time.Now().Format("20060102150405.000"), rand.Intn(1000000))
|
||||
a := fmt.Sprintf("%s%.8d", time.Now().UTC().Format("20060102150405.000"), rand.Intn(1000000))
|
||||
return strings.ReplaceAll(a, ".", "")
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ func ThousandFaceImageFormat(req *ThousandFaceImageFormatReq) {
|
|||
req.Cover = ""
|
||||
req.CoverDefault = req.CoverImg
|
||||
if req.Size >= 200 && len(coverSlice) >= 2 && len(coverImgSlice) >= 2 {
|
||||
req.CoverImg = fmt.Sprintf("%s/test/%d/%d_%d.png?%d", constants.DOMAIN_RENDER_IMG_NAME, req.UserId, req.UserId, req.ProductId, time.Now().Unix())
|
||||
req.CoverImg = fmt.Sprintf("%s/test/%d/%d_%d.png?%d", constants.DOMAIN_RENDER_IMG_NAME, req.UserId, req.UserId, req.ProductId, time.Now().UTC().Unix())
|
||||
req.CoverDefault = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
|
||||
}
|
||||
}
|
||||
|
|
59
utils/websocket_data/base_event.go
Normal file
59
utils/websocket_data/base_event.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package websocket_data
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// 和前端交流的事件机制
|
||||
type EventType string
|
||||
|
||||
const (
|
||||
UserEmailRegister EventType = "E_USER_EMAIL_REGISTER" // 用户注册
|
||||
)
|
||||
|
||||
// WebsocketEvent 所有事件都必须继承这个结构体
|
||||
type WebsocketEvent struct {
|
||||
Type EventType `json:"event_type"` // 事件
|
||||
SenderTime time.Time `json:"sender_time"` // 发送的时间, 可能用来统计时间
|
||||
TraceId string `json:"trace_id"` // 链路ID
|
||||
Code int `json:"code"` // 状态码
|
||||
Message string `json:"msg"` // 描述
|
||||
Data any `json:"data"` // 关注的数据
|
||||
}
|
||||
|
||||
// NewWebsocketEvent 创建一个Websocket事件
|
||||
func NewWebsocketEvent(etype EventType) *WebsocketEvent {
|
||||
uid := uuid.NewString()
|
||||
return &WebsocketEvent{
|
||||
Type: etype,
|
||||
SenderTime: time.Now().UTC(),
|
||||
TraceId: uid,
|
||||
}
|
||||
}
|
||||
|
||||
func (event *WebsocketEvent) WithData(data any) *WebsocketEvent {
|
||||
event.Data = data
|
||||
return event
|
||||
}
|
||||
|
||||
func (event *WebsocketEvent) WithMessgae(msg string) *WebsocketEvent {
|
||||
event.Message = msg
|
||||
return event
|
||||
}
|
||||
|
||||
func (event *WebsocketEvent) WithCode(code int) *WebsocketEvent {
|
||||
event.Code = code
|
||||
return event
|
||||
}
|
||||
|
||||
func (event *WebsocketEvent) WithTraceId(traceId string) *WebsocketEvent {
|
||||
event.TraceId = traceId
|
||||
return event
|
||||
}
|
||||
|
||||
func (event *WebsocketEvent) WithSenderTime(senderTime time.Time) *WebsocketEvent {
|
||||
event.SenderTime = senderTime
|
||||
return event
|
||||
}
|
6
utils/websocket_data/event.go
Normal file
6
utils/websocket_data/event.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package websocket_data
|
||||
|
||||
// 用户注册成功的事件关注的数据
|
||||
type DataEmailRegister struct {
|
||||
JwtToken string `json:"token"` // 注册成功的事件码
|
||||
}
|
13
utils/websocket_data/event_test.go
Normal file
13
utils/websocket_data/event_test.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package websocket_data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEvent(t *testing.T) {
|
||||
|
||||
data, _ := json.Marshal(NewWebsocketEvent(UserEmailRegister).WithCode(200))
|
||||
log.Println(string(data))
|
||||
}
|
Loading…
Reference in New Issue
Block a user