feat: import services and standardize Go 1.26.5
This commit is contained in:
73
apps/base/passport/internal/logic/account/get.go
Normal file
73
apps/base/passport/internal/logic/account/get.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/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
apps/base/passport/internal/logic/account/set_data.go
Normal file
64
apps/base/passport/internal/logic/account/set_data.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/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
apps/base/passport/internal/logic/account/set_password.go
Normal file
50
apps/base/passport/internal/logic/account/set_password.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/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
apps/base/passport/internal/logic/account/statistics.go
Normal file
56
apps/base/passport/internal/logic/account/statistics.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/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
apps/base/passport/internal/logic/account/tag_create.go
Normal file
65
apps/base/passport/internal/logic/account/tag_create.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/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
apps/base/passport/internal/logic/account/tag_remove.go
Normal file
46
apps/base/passport/internal/logic/account/tag_remove.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/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
|
||||
|
||||
}
|
||||
24
apps/base/passport/internal/logic/common/token.go
Normal file
24
apps/base/passport/internal/logic/common/token.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/crypto/token"
|
||||
"git.apinb.com/bsm-sdk/core/env"
|
||||
)
|
||||
|
||||
// GenerateTokenAes .
|
||||
func GenerateTokenAes(id uint, identity, client, role string, extend map[string]string) (string, error) {
|
||||
token, err := token.New(env.Runtime.JwtSecretKey).GenerateJwt(id, identity, client, role, nil, extend)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func VerifyMapKeys(m map[string]string, keys []string) bool {
|
||||
for _, key := range keys {
|
||||
if _, ok := m[key]; !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
41
apps/base/passport/internal/logic/forget/reset.go
Normal file
41
apps/base/passport/internal/logic/forget/reset.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package forget
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
"git.apinb.com/bsm-apps/passport/internal/vars"
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
)
|
||||
|
||||
// 重罢密码
|
||||
func Reset(ctx context.Context, in *pb.ForgetResetRequest) (reply *pb.StatusReply, err error) {
|
||||
if in.Identity == "" || in.Password == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
pa, err := models.GetPassportAccountByField("identity", in.Identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if pa.Status == vars.Status_Disable {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
err = impl.DBService.Model(&models.PassportAccount{}).Where("identity = ? ", pa.Identity).Update("password", utils.Md5(in.Password)).Error
|
||||
if err != nil {
|
||||
printer.Error("Update passport account password by identity %+v error:%v", in.Identity, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
32
apps/base/passport/internal/logic/forget/verify.go
Normal file
32
apps/base/passport/internal/logic/forget/verify.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package forget
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
"git.apinb.com/bsm-apps/passport/internal/vars"
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
)
|
||||
|
||||
// 验证手机号和验证码
|
||||
func Verify(ctx context.Context, in *pb.ForgetVerifyRequest) (reply *pb.StatusReply, err error) {
|
||||
if in.Phone == "" || in.Code == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
pa, err := models.GetPassportAccountByField("phone", in.Phone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if pa.Status == vars.Status_Disable {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
60
apps/base/passport/internal/logic/login/code.go
Normal file
60
apps/base/passport/internal/logic/login/code.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/logic/common"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
"git.apinb.com/bsm-apps/passport/internal/vars"
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 通过验证码登录
|
||||
func Code(ctx context.Context, in *pb.LoginByCodeRequest) (reply *pb.LoginReply, err error) {
|
||||
if in.Phone == "" || in.Code == "" || in.Country == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
pa, err := models.GetPassportAccountByField("phone", in.Phone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := models.CheckPassportData(pa.ID, pa.Identity)
|
||||
if err != nil {
|
||||
printer.Error("Get passport data by passport_id %v error:%v", pa.ID, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
paExtend := map[string]string{
|
||||
"rights": data.Rights,
|
||||
}
|
||||
token, err := common.GenerateTokenAes(uint(pa.ID), pa.Identity, "", data.Rights, paExtend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//save token to cache.
|
||||
err = impl.RedisService.Client.Set(impl.RedisService.Ctx, vars.TokenPrefix+pa.Identity, token, 0).Err()
|
||||
if err != nil {
|
||||
printer.Error("Set redis cache by key %v error:%v", vars.TokenPrefix+pa.Identity, err)
|
||||
return nil, errcode.ErrInternal
|
||||
}
|
||||
|
||||
return &pb.LoginReply{
|
||||
Id: int64(pa.ID),
|
||||
Identity: pa.Identity,
|
||||
Token: token,
|
||||
Extend: paExtend,
|
||||
VerifyStatus: &pb.VerifyStatus{
|
||||
EmailVerify: data.EmailVerify,
|
||||
PhoneVerify: data.PhoneVerify,
|
||||
FaceVerify: data.FaceVerify,
|
||||
DocumentVerify: data.DocumentVerify,
|
||||
KycVerify: data.KycVerify,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
67
apps/base/passport/internal/logic/login/pwd.go
Normal file
67
apps/base/passport/internal/logic/login/pwd.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/logic/common"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
"git.apinb.com/bsm-apps/passport/internal/vars"
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
_vars "git.apinb.com/bsm-sdk/core/vars"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// 通过密码登录
|
||||
func Pwd(ctx context.Context, in *pb.LoginByPwdRequest) (reply *pb.LoginReply, err error) {
|
||||
if in.Account == "" || in.Password == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
pa, err := models.GetPassportAccountByField("account", in.Account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword([]byte(pa.Password), []byte(in.Password+pa.Salt))
|
||||
if err != nil {
|
||||
return nil, errcode.NewError(187, "Passport Error")
|
||||
}
|
||||
|
||||
data, err := models.CheckPassportData(pa.ID, pa.Identity)
|
||||
if err != nil {
|
||||
printer.Error("Get passport data by passport_id %v error:%v", pa.ID, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
paExtend := map[string]string{
|
||||
"rights": data.Rights,
|
||||
}
|
||||
|
||||
token, err := common.GenerateTokenAes(uint(pa.ID), pa.Identity, "", data.Rights, paExtend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//save token to cache.
|
||||
err = impl.RedisService.Client.Set(impl.RedisService.Ctx, vars.TokenPrefix+pa.Identity, token, _vars.JwtExpire).Err()
|
||||
if err != nil {
|
||||
printer.Error("Set redis cache by key %v error:%v", vars.TokenPrefix+pa.Identity, err)
|
||||
return nil, errcode.ErrInternal
|
||||
}
|
||||
|
||||
return &pb.LoginReply{
|
||||
Id: int64(pa.ID),
|
||||
Identity: pa.Identity,
|
||||
Token: token,
|
||||
Extend: paExtend,
|
||||
VerifyStatus: &pb.VerifyStatus{
|
||||
EmailVerify: data.EmailVerify,
|
||||
PhoneVerify: data.PhoneVerify,
|
||||
FaceVerify: data.FaceVerify,
|
||||
DocumentVerify: data.DocumentVerify,
|
||||
KycVerify: data.KycVerify,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
99
apps/base/passport/internal/logic/login/quick.go
Normal file
99
apps/base/passport/internal/logic/login/quick.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/logic/common"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
"git.apinb.com/bsm-apps/passport/internal/vars"
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 通过验证码快捷登录并注册
|
||||
func Quick(ctx context.Context, in *pb.LoginByCodeRequest) (reply *pb.LoginReply, err error) {
|
||||
if in.Phone == "" || in.Code == "" || in.Country == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
var pa models.PassportAccount
|
||||
err = impl.DBService.Where("phone = ?", in.Phone).First(&pa).Error
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
printer.Error("Get passport account by phone %v error:%v", in.Phone, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
pa = models.PassportAccount{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: utils.UUID(),
|
||||
Status: vars.Status_Normal,
|
||||
},
|
||||
Account: in.Phone,
|
||||
Phone: in.Phone,
|
||||
}
|
||||
|
||||
err = impl.DBService.Create(&pa).Error
|
||||
if err != nil {
|
||||
printer.Error("create passport account and password extend by data %+v error:%v", pa, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
data := models.PassportData{
|
||||
Std_Passport: types.Std_Passport{
|
||||
PassportID: pa.ID,
|
||||
PassportIdentity: pa.Identity,
|
||||
},
|
||||
Country: in.Country,
|
||||
AgencyId: uint(in.AgencyId),
|
||||
StaffId: uint(in.StaffId),
|
||||
OwnerId: uint(in.OwnerId),
|
||||
OwnerIdentity: in.OwnerIdentity,
|
||||
}
|
||||
|
||||
err = impl.DBService.Create(&data).Error
|
||||
if err != nil {
|
||||
printer.Error("create passport data by data %+v error:%v", data, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
}
|
||||
|
||||
if pa.Status == vars.Status_Disable {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
var data models.PassportData
|
||||
err = impl.DBService.Where("passport_id = ?", pa.ID).First(&data).Error
|
||||
if err != nil {
|
||||
printer.Error("Get passport data by passport_id %v error:%v", pa.ID, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
paExtend := map[string]string{
|
||||
"rights": data.Rights,
|
||||
}
|
||||
|
||||
token, err := common.GenerateTokenAes(uint(pa.ID), pa.Identity, "", data.Rights, paExtend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.LoginReply{
|
||||
Id: int64(pa.ID),
|
||||
Identity: pa.Identity,
|
||||
Token: token,
|
||||
Extend: paExtend,
|
||||
VerifyStatus: &pb.VerifyStatus{
|
||||
EmailVerify: data.EmailVerify,
|
||||
PhoneVerify: data.PhoneVerify,
|
||||
FaceVerify: data.FaceVerify,
|
||||
DocumentVerify: data.DocumentVerify,
|
||||
KycVerify: data.KycVerify,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
31
apps/base/passport/internal/logic/register/code.go
Normal file
31
apps/base/passport/internal/logic/register/code.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package register
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
)
|
||||
|
||||
// 手机验证码注册
|
||||
func Code(ctx context.Context, in *pb.RegisterRequest) (reply *pb.RegisterReply, err error) {
|
||||
if in.Phone == "" || in.Code == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
//手机号验证
|
||||
matched, err := regexp.MatchString("^1[3456789]{1}\\d{9}$", in.Phone)
|
||||
if err != nil || !matched {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
//账号唯一验证
|
||||
found := models.PassportAccountExists("phone", in.Phone)
|
||||
if found {
|
||||
return nil, errcode.ErrAlreadyExists
|
||||
}
|
||||
|
||||
return Do(in)
|
||||
}
|
||||
103
apps/base/passport/internal/logic/register/do.go
Normal file
103
apps/base/passport/internal/logic/register/do.go
Normal file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* @Author: ZhaoYadong
|
||||
* @Date: 2024-02-27 21:09:45
|
||||
* @LastEditors: ZhaoYadong
|
||||
* @LastEditTime: 2024-02-28 11:50:05
|
||||
* @FilePath: /server/Users/edy/go/src/passport/internal/logic/register/do.go
|
||||
*/
|
||||
package register
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-apps/passport/internal/config"
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/logic/common"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
"git.apinb.com/bsm-apps/passport/internal/vars"
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
_vars "git.apinb.com/bsm-sdk/core/vars"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// Do .
|
||||
func Do(in *pb.RegisterRequest) (*pb.RegisterReply, error) {
|
||||
var salt string
|
||||
if in.Password != "" {
|
||||
salt = utils.UUID()
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(in.Password+salt), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrInternal
|
||||
}
|
||||
in.Password = string(hashedPassword)
|
||||
}
|
||||
|
||||
var account = in.Account
|
||||
if account == "" {
|
||||
account = in.Phone
|
||||
}
|
||||
pa := models.PassportAccount{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: utils.UUID(),
|
||||
Status: vars.Status_Normal,
|
||||
},
|
||||
Account: account,
|
||||
Phone: in.Phone,
|
||||
Password: in.Password,
|
||||
Salt: salt,
|
||||
}
|
||||
|
||||
// 插入passport 基础表
|
||||
if err := impl.DBService.Create(&pa).Error; err != nil {
|
||||
printer.Error("create passport account and password extend by data %+v error:%v", pa, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
data := models.PassportData{
|
||||
Std_Passport: types.Std_Passport{
|
||||
PassportID: pa.ID,
|
||||
PassportIdentity: pa.Identity,
|
||||
},
|
||||
Country: in.Country,
|
||||
AgencyId: uint(in.AgencyId),
|
||||
StaffId: uint(in.StaffId),
|
||||
OwnerId: uint(in.OwnerId),
|
||||
OwnerIdentity: in.OwnerIdentity,
|
||||
}
|
||||
|
||||
if err := impl.DBService.Create(&data).Error; err != nil {
|
||||
printer.Error("create passport data by data %+v error:%v", data, err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
extend := map[string]string{
|
||||
"rights": data.Rights,
|
||||
}
|
||||
token, err := common.GenerateTokenAes(uint(pa.ID), pa.Identity, "", data.Rights, extend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//save token to cache.
|
||||
err = impl.RedisService.Client.Set(impl.RedisService.Ctx, config.Spec.Token.Prefix+pa.Identity, token, _vars.JwtExpire).Err()
|
||||
if err != nil {
|
||||
printer.Error("Set redis cache by key %v error:%v", vars.TokenPrefix+pa.Identity, err)
|
||||
return nil, errcode.ErrInternal
|
||||
}
|
||||
|
||||
return &pb.RegisterReply{
|
||||
Id: int64(pa.ID),
|
||||
Identity: pa.Identity,
|
||||
Token: token,
|
||||
Extend: extend,
|
||||
VerifyStatus: &pb.VerifyStatus{
|
||||
EmailVerify: 0,
|
||||
PhoneVerify: 0,
|
||||
FaceVerify: 0,
|
||||
DocumentVerify: 0,
|
||||
KycVerify: 0,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
24
apps/base/passport/internal/logic/register/pwd.go
Normal file
24
apps/base/passport/internal/logic/register/pwd.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package register
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
)
|
||||
|
||||
// 帐号密码注册
|
||||
func Pwd(ctx context.Context, in *pb.RegisterRequest) (reply *pb.RegisterReply, err error) {
|
||||
if in.Account == "" || in.Password == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
//账号唯一验证
|
||||
found := models.PassportAccountExists("account", in.Account)
|
||||
if found {
|
||||
return nil, errcode.ErrAlreadyExists
|
||||
}
|
||||
|
||||
return Do(in)
|
||||
}
|
||||
35
apps/base/passport/internal/logic/verify/jumio_callback.go
Normal file
35
apps/base/passport/internal/logic/verify/jumio_callback.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package verify
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"time"
|
||||
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// KYC 认证回调
|
||||
func JumioCallback(ctx context.Context, in *pb.JumioCallbackPayload) (reply *pb.StatusReply, err error) {
|
||||
// Validate callback payload
|
||||
if in == nil {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// Log the callback for audit purposes
|
||||
printer.Info("Received Jumio KYC callback: %+v", in)
|
||||
|
||||
// Process the KYC callback based on the payload
|
||||
// This is where you would typically:
|
||||
// 1. Verify the callback signature/authenticity
|
||||
// 2. Update user verification status in database
|
||||
// 3. Send notifications if needed
|
||||
// 4. Log the verification result
|
||||
|
||||
// For now, return success
|
||||
// In production, implement proper callback handling logic
|
||||
return &pb.StatusReply{
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
140
apps/base/passport/internal/logic/verify/request.go
Normal file
140
apps/base/passport/internal/logic/verify/request.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package verify
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/passport/internal/config"
|
||||
"git.apinb.com/bsm-apps/passport/internal/impl"
|
||||
"git.apinb.com/bsm-apps/passport/internal/logic/common"
|
||||
"git.apinb.com/bsm-apps/passport/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
type JumioInitRequest struct {
|
||||
CustomerInternalReference string `json:"customerInternalReference"`
|
||||
UserReference string `json:"userReference"`
|
||||
SuccessURL string `json:"successUrl"`
|
||||
ErrorURL string `json:"errorUrl"`
|
||||
CallbackURL string `json:"callbackUrl"`
|
||||
}
|
||||
|
||||
type JumioInitResponse struct {
|
||||
Timestamp string `json:"timestamp"`
|
||||
ScanReference string `json:"scanReference"`
|
||||
ClientRedirectURL string `json:"clientRedirectUrl"`
|
||||
}
|
||||
|
||||
func Request(ctx context.Context, in *pb.VerifyRequest) (reply *pb.StatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var data string
|
||||
switch strings.ToLower(in.Provider) {
|
||||
case "jumio":
|
||||
id := fmt.Sprintf("ID_%d", auth.ID)
|
||||
resp, err := InitiateJumioScan(id, auth.Identity)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrInternal
|
||||
}
|
||||
data = resp.ClientRedirectURL
|
||||
case "local":
|
||||
if !common.VerifyMapKeys(in.Args, []string{"type", "name", "number", "front", "back"}) {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err = LocalVerify(auth.ID, in.Args)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
default:
|
||||
return nil, errcode.ErrNotFound(404, "provider")
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: data,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func LocalVerify(authID uint, args map[string]string) error {
|
||||
err := impl.DBService.Model(&models.PassportData{}).Where("passport_id = ?", authID).Update("document_verify", 1).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = impl.DBService.Model(&models.PassportVerify{}).Where("passport_id = ?", authID).Updates(map[string]any{
|
||||
"document_verify_at": time.Now(),
|
||||
"document_type": args["type"],
|
||||
"document_name": args["name"],
|
||||
"document_number": args["number"],
|
||||
"document_front": args["front"],
|
||||
"document_back": args["back"],
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitiateJumioScan(internalRef, userRef string) (*JumioInitResponse, error) {
|
||||
if config.Spec.Kyc == nil {
|
||||
return nil, fmt.Errorf("kyc config is missing")
|
||||
}
|
||||
|
||||
reqBody := JumioInitRequest{
|
||||
CustomerInternalReference: internalRef,
|
||||
UserReference: userRef,
|
||||
CallbackURL: config.Spec.Kyc.ApiArgs,
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
req, err := http.NewRequest(http.MethodPost, config.Spec.Kyc.BaseUrl, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.SetBasicAuth(config.Spec.Kyc.ApiToken, config.Spec.Kyc.ApiSecret)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", "BSM-Passport/1.0")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("jumio api error: status=%d body=%s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var jumioResp JumioInitResponse
|
||||
err = json.Unmarshal(body, &jumioResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &jumioResp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user