refactor: reorganize modules and add Linux build tooling
This commit is contained in:
73
module/base/passport/internal/logic/account/get.go
Normal file
73
module/base/passport/internal/logic/account/get.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 通过会员所有信息
|
||||
func Get(ctx context.Context, in *pb.Empty) (reply *pb.GetFullReply, err error) {
|
||||
AUTH, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
account, err := models.GetPassportAccountByField("id", AUTH.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tagRecords := make([]models.PassportTags, 0)
|
||||
impl.DBService.Model(&models.PassportTags{}).Where("passport_id=?", AUTH.ID).Find(&tagRecords)
|
||||
|
||||
tags := make([]*pb.TagItem, 0)
|
||||
for _, tag := range tagRecords {
|
||||
tags = append(tags, &pb.TagItem{
|
||||
Name: tag.Name,
|
||||
Icon: tag.Icon,
|
||||
})
|
||||
}
|
||||
|
||||
reply = &pb.GetFullReply{
|
||||
Identity: account.Identity,
|
||||
Account: account.Account,
|
||||
Phone: account.Phone,
|
||||
Email: account.Email,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
data := &models.PassportData{}
|
||||
impl.DBService.Where("passport_id=?", AUTH.ID).First(data)
|
||||
if data != nil {
|
||||
bd := data.Birthday.Format("2006-01-02")
|
||||
if bd == "0001-01-01" {
|
||||
bd = ""
|
||||
}
|
||||
reply.Rights = data.Rights
|
||||
reply.Nickname = data.Nickname
|
||||
reply.Avatar = data.Avatar
|
||||
reply.Birthday = bd
|
||||
reply.Sex = int32(data.Sex)
|
||||
reply.Country = data.Country
|
||||
reply.Province = data.Province
|
||||
reply.City = data.City
|
||||
reply.Area = data.Area
|
||||
reply.Sign = data.Sign
|
||||
reply.Cover = data.Cover
|
||||
reply.Score = data.Score
|
||||
reply.Level = data.Level
|
||||
reply.VerifyStatus = &pb.VerifyStatus{
|
||||
EmailVerify: data.EmailVerify,
|
||||
PhoneVerify: data.PhoneVerify,
|
||||
FaceVerify: data.FaceVerify,
|
||||
DocumentVerify: data.DocumentVerify,
|
||||
KycVerify: data.KycVerify,
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
64
module/base/passport/internal/logic/account/set_data.go
Normal file
64
module/base/passport/internal/logic/account/set_data.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 更新会员的信息数据
|
||||
// 字段值为空或是0,将不更新此数据
|
||||
func SetData(ctx context.Context, in *pb.SetDataRequest) (reply *pb.StatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var birthday time.Time
|
||||
if in.Birthday != "" {
|
||||
birthday, err = time.Parse("2006-01-02", in.Birthday)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
}
|
||||
|
||||
var data = models.PassportData{
|
||||
Nickname: in.Nickname,
|
||||
Avatar: in.Avatar,
|
||||
Sex: int8(in.Sex),
|
||||
Birthday: birthday,
|
||||
Country: in.Country,
|
||||
Province: in.Province,
|
||||
City: in.City,
|
||||
Area: in.Area,
|
||||
Sign: in.Sign,
|
||||
Cover: in.Cover,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
var cnt int64
|
||||
impl.DBService.Model(&models.PassportData{}).Where("passport_id=?", auth.ID).Count(&cnt)
|
||||
|
||||
if cnt == 0 {
|
||||
data.PassportID = auth.ID
|
||||
data.PassportIdentity = auth.Identity
|
||||
err = impl.DBService.Create(&data).Error
|
||||
} else {
|
||||
impl.DBService.Where("passport_id=?", auth.ID).Updates(&data)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
printer.Error("first or create by passport_id %v error:%v", auth.ID, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
50
module/base/passport/internal/logic/account/set_password.go
Normal file
50
module/base/passport/internal/logic/account/set_password.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// 更新会员的密码
|
||||
func SetPassword(ctx context.Context, in *pb.SetPasswordRequest) (reply *pb.StatusReply, err error) {
|
||||
if in.OldPassword == "" || in.NewPassword == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pa, err := models.GetPassportAccountByField("id", auth.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword([]byte(pa.Password), []byte(in.OldPassword+pa.Salt))
|
||||
if err != nil {
|
||||
return nil, errcode.NewError(187, "Passport Error")
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(in.NewPassword+pa.Salt), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrInternal
|
||||
}
|
||||
|
||||
err = impl.DBService.Model(&models.PassportAccount{}).Where("id = ? ", auth.ID).Update("password", string(hashedPassword)).Error
|
||||
if err != nil {
|
||||
printer.Error("Update passport account password by id %+v error:%v", auth.ID, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
56
module/base/passport/internal/logic/account/statistics.go
Normal file
56
module/base/passport/internal/logic/account/statistics.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 获取会员的相关统计数据
|
||||
func Statistics(ctx context.Context, in *pb.StatisticsRequest) (reply *pb.StatisticsReply, err error) {
|
||||
// parse authorization meta.
|
||||
AUTH, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate request fields
|
||||
if len(in.Field) == 0 {
|
||||
return &pb.StatisticsReply{
|
||||
Data: make(map[string]int64),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Initialize result map
|
||||
result := make(map[string]int64)
|
||||
|
||||
// Get statistics based on requested fields
|
||||
for _, field := range in.Field {
|
||||
switch field {
|
||||
case "login_count":
|
||||
// Count login records for this user
|
||||
var count int64
|
||||
impl.DBService.Model(&models.PassportAccount{}).
|
||||
Where("id = ?", AUTH.ID).
|
||||
Count(&count)
|
||||
result[field] = count
|
||||
case "tag_count":
|
||||
// Count tags for this user
|
||||
var count int64
|
||||
impl.DBService.Model(&models.PassportTags{}).
|
||||
Where("passport_id = ?", AUTH.ID).
|
||||
Count(&count)
|
||||
result[field] = count
|
||||
default:
|
||||
// Unknown field, set to 0
|
||||
result[field] = 0
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.StatisticsReply{
|
||||
Data: result,
|
||||
}, nil
|
||||
}
|
||||
65
module/base/passport/internal/logic/account/tag_create.go
Normal file
65
module/base/passport/internal/logic/account/tag_create.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
)
|
||||
|
||||
// 新增标签
|
||||
func TagCreate(ctx context.Context, in *pb.TagItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
AUTH, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate input
|
||||
if in.Name == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// Check if tag already exists
|
||||
var cnt int64
|
||||
err = impl.DBService.Model(&models.PassportTags{}).
|
||||
Where("passport_id = ? AND name = ?", AUTH.ID, in.Name).
|
||||
Count(&cnt).Error
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if cnt == 0 {
|
||||
// Create new tag
|
||||
record := &models.PassportTags{
|
||||
Name: in.Name,
|
||||
Icon: in.Icon,
|
||||
}
|
||||
record.Identity = utils.UUID()
|
||||
record.PassportID = AUTH.ID
|
||||
record.PassportIdentity = AUTH.Identity
|
||||
|
||||
err = impl.DBService.Create(record).Error
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
} else {
|
||||
// Update existing tag icon
|
||||
err = impl.DBService.Model(&models.PassportTags{}).
|
||||
Where("passport_id = ? AND name = ?", AUTH.ID, in.Name).
|
||||
Update("icon", in.Icon).Error
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
46
module/base/passport/internal/logic/account/tag_remove.go
Normal file
46
module/base/passport/internal/logic/account/tag_remove.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 删除标签
|
||||
func TagRemove(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
AUTH, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate request identity
|
||||
if in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// Delete the tag with proper error handling
|
||||
result := impl.DBService.Model(&models.PassportTags{}).
|
||||
Unscoped().
|
||||
Where("passport_id = ? AND identity = ?", AUTH.ID, in.Identity).
|
||||
Delete(&models.PassportTags{})
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// Check if any record was actually deleted
|
||||
if result.RowsAffected == 0 {
|
||||
return nil, errcode.ErrNotFound(404, "Tag not found")
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user