refactor: reorganize modules and add Linux build tooling
This commit is contained in:
67
module/base/passport/internal/config/config.go
Normal file
67
module/base/passport/internal/config/config.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
_vars "git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
var (
|
||||
Spec SrvConfig
|
||||
)
|
||||
|
||||
type SrvConfig struct {
|
||||
conf.Base `yaml:",inline"`
|
||||
Databases *conf.DBConf `yaml:"Databases"`
|
||||
MicroService *conf.MicroServiceConf `yaml:"MicroService"`
|
||||
Rpc map[string]conf.RpcConf `yaml:"Rpc"`
|
||||
Gateway *conf.GatewayConf `yaml:"Gateway"`
|
||||
Apm *conf.ApmConf `yaml:"APM"`
|
||||
Etcd *conf.EtcdConf `yaml:"Etcd"`
|
||||
WeChat *WeChatConf `yaml:"WeChatConf"`
|
||||
Token *TokenConf `yaml:"Token"`
|
||||
Kyc *KycConf `yaml:"Kyc"`
|
||||
}
|
||||
|
||||
type KycConf struct {
|
||||
Provider string `yaml:"Provider"`
|
||||
BaseUrl string `yaml:"BaseUrl"`
|
||||
ApiSecret string `yaml:"ApiSecret"`
|
||||
ApiToken string `yaml:"ApiToken"`
|
||||
ApiArgs string `yaml:"ApiArgs"`
|
||||
}
|
||||
|
||||
type TokenConf struct {
|
||||
Prefix string `yaml:"Prefix"`
|
||||
Expire int `yaml:"Expire"`
|
||||
}
|
||||
|
||||
type WeChatConf struct {
|
||||
AppID string `json:"app_id"`
|
||||
AppSecret string `json:"app_secret"`
|
||||
}
|
||||
|
||||
func New(srvKey string) {
|
||||
// 初始化配置 创建一个新的配置实例,用于服务配置
|
||||
conf.New(srvKey, &Spec)
|
||||
|
||||
// 配置校验 服务IP,端口; 端口如果不合规,则随机分配端口
|
||||
Spec.Port = conf.CheckPort(Spec.Port)
|
||||
Spec.BindIP = conf.CheckIP(Spec.BindIP)
|
||||
Spec.Addr = net.JoinHostPort(Spec.BindIP, Spec.Port)
|
||||
|
||||
// 配置校验 服务名称地址及监听地址不能为空
|
||||
conf.NotNil(Spec.Service, Spec.Cache)
|
||||
|
||||
// 配置JWT过期时间
|
||||
if Spec.Token != nil && Spec.Token.Expire > 0 {
|
||||
_vars.JwtExpire = time.Duration(Spec.Token.Expire * int(time.Second))
|
||||
} else {
|
||||
// 默认24小时过期
|
||||
_vars.JwtExpire = 24 * time.Hour
|
||||
}
|
||||
|
||||
conf.PrintInfo(Spec.Addr)
|
||||
}
|
||||
1
module/base/passport/internal/excode/ex.go
Normal file
1
module/base/passport/internal/excode/ex.go
Normal file
@@ -0,0 +1 @@
|
||||
package excode
|
||||
28
module/base/passport/internal/impl/impl.go
Normal file
28
module/base/passport/internal/impl/impl.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"bsm/full/module/base/passport/internal/config"
|
||||
"git.apinb.com/bsm-sdk/core/cache/redis"
|
||||
"git.apinb.com/bsm-sdk/core/with"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
RedisService *redis.RedisClient
|
||||
EtcdService *clientv3.Client
|
||||
DBService *gorm.DB
|
||||
MemoryService *cache.Cache
|
||||
)
|
||||
|
||||
func NewImpl() {
|
||||
// with memory cache
|
||||
MemoryService = with.Memory(nil)
|
||||
// with redis cache
|
||||
RedisService = with.RedisCache(config.Spec.Cache)
|
||||
// with databases
|
||||
DBService = with.Databases(config.Spec.Databases, nil)
|
||||
// with etcd
|
||||
EtcdService = with.Etcd(config.Spec.Etcd)
|
||||
}
|
||||
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
|
||||
|
||||
}
|
||||
24
module/base/passport/internal/logic/common/token.go
Normal file
24
module/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
module/base/passport/internal/logic/forget/reset.go
Normal file
41
module/base/passport/internal/logic/forget/reset.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package forget
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
"bsm/full/module/base/passport/internal/vars"
|
||||
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/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
module/base/passport/internal/logic/forget/verify.go
Normal file
32
module/base/passport/internal/logic/forget/verify.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package forget
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
"bsm/full/module/base/passport/internal/vars"
|
||||
pb "bsm/full/module/base/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
module/base/passport/internal/logic/login/code.go
Normal file
60
module/base/passport/internal/logic/login/code.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/logic/common"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
"bsm/full/module/base/passport/internal/vars"
|
||||
pb "bsm/full/module/base/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
module/base/passport/internal/logic/login/pwd.go
Normal file
67
module/base/passport/internal/logic/login/pwd.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/logic/common"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
"bsm/full/module/base/passport/internal/vars"
|
||||
pb "bsm/full/module/base/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
module/base/passport/internal/logic/login/quick.go
Normal file
99
module/base/passport/internal/logic/login/quick.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/logic/common"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
"bsm/full/module/base/passport/internal/vars"
|
||||
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/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
module/base/passport/internal/logic/register/code.go
Normal file
31
module/base/passport/internal/logic/register/code.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package register
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
pb "bsm/full/module/base/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
module/base/passport/internal/logic/register/do.go
Normal file
103
module/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 (
|
||||
"bsm/full/module/base/passport/internal/config"
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/logic/common"
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
"bsm/full/module/base/passport/internal/vars"
|
||||
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/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
module/base/passport/internal/logic/register/pwd.go
Normal file
24
module/base/passport/internal/logic/register/pwd.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package register
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/passport/internal/models"
|
||||
pb "bsm/full/module/base/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
module/base/passport/internal/logic/verify/jumio_callback.go
Normal file
35
module/base/passport/internal/logic/verify/jumio_callback.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package verify
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"time"
|
||||
|
||||
pb "bsm/full/module/base/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
module/base/passport/internal/logic/verify/request.go
Normal file
140
module/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"
|
||||
|
||||
"bsm/full/module/base/passport/internal/config"
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/logic/common"
|
||||
"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"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
172
module/base/passport/internal/models/cache.go
Normal file
172
module/base/passport/internal/models/cache.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
)
|
||||
|
||||
var (
|
||||
// 默认缓存TTL
|
||||
DefaultTTL = 30 * time.Minute
|
||||
// 用户信息缓存TTL
|
||||
UserCacheTTL = 1 * time.Hour
|
||||
// Token缓存TTL
|
||||
TokenCacheTTL = 24 * time.Hour
|
||||
)
|
||||
|
||||
// GetAccountByCache 通过缓存获取账户信息
|
||||
func GetAccountByCache(ctx context.Context, field, value string) (*PassportAccount, error) {
|
||||
var account *PassportAccount
|
||||
|
||||
key := impl.RedisService.BuildKey("account", field, value)
|
||||
|
||||
// 尝试从缓存获取
|
||||
err := impl.RedisService.Get(key, &account)
|
||||
if err == nil && account != nil {
|
||||
return account, nil
|
||||
}
|
||||
|
||||
// 缓存未命中,从数据库获取
|
||||
account, err = GetPassportAccountByField(field, value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 存入缓存
|
||||
err = impl.RedisService.Set(key, account, UserCacheTTL)
|
||||
return account, err
|
||||
}
|
||||
|
||||
// GetUserDataByCache 通过缓存获取用户扩展数据
|
||||
func GetUserDataByCache(ctx context.Context, passportID uint) (*PassportData, error) {
|
||||
var data *PassportData
|
||||
|
||||
key := impl.RedisService.BuildKey("userdata", passportID)
|
||||
|
||||
// 尝试从缓存获取
|
||||
err := impl.RedisService.Get(key, &data)
|
||||
if err == nil && data != nil {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// 缓存未命中,从数据库获取
|
||||
err = impl.DBService.Where("passport_id = ?", passportID).First(&data).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 存入缓存
|
||||
err = impl.RedisService.Set(key, data, UserCacheTTL)
|
||||
return data, err
|
||||
}
|
||||
|
||||
// GetUserTagsByCache 通过缓存获取用户标签
|
||||
func GetUserTagsByCache(ctx context.Context, passportID uint) ([]*PassportTags, error) {
|
||||
var tags []*PassportTags
|
||||
|
||||
key := impl.RedisService.BuildKey("usertags", passportID)
|
||||
|
||||
// 尝试从缓存获取
|
||||
err := impl.RedisService.Get(key, &tags)
|
||||
if err == nil && tags != nil {
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
// 缓存未命中,从数据库获取
|
||||
err = impl.DBService.Where("passport_id = ?", passportID).Find(&tags).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 存入缓存
|
||||
err = impl.RedisService.Set(key, tags, DefaultTTL)
|
||||
return tags, err
|
||||
}
|
||||
|
||||
// InvalidateUserCache 清除用户相关缓存
|
||||
func InvalidateUserCache(ctx context.Context, passportID uint, identity string) error {
|
||||
keys := []string{
|
||||
impl.RedisService.BuildKey("account", "id", passportID),
|
||||
impl.RedisService.BuildKey("account", "identity", identity),
|
||||
impl.RedisService.BuildKey("userdata", passportID),
|
||||
impl.RedisService.BuildKey("usertags", passportID),
|
||||
}
|
||||
|
||||
for _, key := range keys {
|
||||
impl.RedisService.Client.Del(impl.RedisService.Ctx, key)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetTokenCache 设置Token缓存
|
||||
func SetTokenCache(ctx context.Context, identity, token string) error {
|
||||
key := impl.RedisService.BuildKey("token", identity)
|
||||
return impl.RedisService.Set(key, token, TokenCacheTTL)
|
||||
}
|
||||
|
||||
// GetTokenCache 获取Token缓存
|
||||
func GetTokenCache(ctx context.Context, identity string) (string, error) {
|
||||
var token string
|
||||
key := impl.RedisService.BuildKey("token", identity)
|
||||
err := impl.RedisService.Get(key, &token)
|
||||
return token, err
|
||||
}
|
||||
|
||||
// InvalidateTokenCache 清除Token缓存
|
||||
func InvalidateTokenCache(ctx context.Context, identity string) error {
|
||||
key := impl.RedisService.BuildKey("token", identity)
|
||||
return impl.RedisService.Client.Del(impl.RedisService.Ctx, key).Err()
|
||||
}
|
||||
|
||||
// SetVerificationCodeCache 设置验证码缓存
|
||||
func SetVerificationCodeCache(ctx context.Context, phone, code string, ttl time.Duration) error {
|
||||
key := impl.RedisService.BuildKey("verifycode", phone)
|
||||
return impl.RedisService.Set(key, code, ttl)
|
||||
}
|
||||
|
||||
// GetVerificationCodeCache 获取验证码缓存
|
||||
func GetVerificationCodeCache(ctx context.Context, phone string) (string, error) {
|
||||
var code string
|
||||
key := impl.RedisService.BuildKey("verifycode", phone)
|
||||
err := impl.RedisService.Get(key, &code)
|
||||
return code, err
|
||||
}
|
||||
|
||||
// InvalidateVerificationCodeCache 清除验证码缓存
|
||||
func InvalidateVerificationCodeCache(ctx context.Context, phone string) error {
|
||||
key := impl.RedisService.BuildKey("verifycode", phone)
|
||||
return impl.RedisService.Client.Del(impl.RedisService.Ctx, key).Err()
|
||||
}
|
||||
|
||||
// IncrementLoginAttempts 增加登录尝试次数
|
||||
func IncrementLoginAttempts(ctx context.Context, account string) (int64, error) {
|
||||
key := impl.RedisService.BuildKey("loginattempts", account)
|
||||
result := impl.RedisService.Client.Incr(impl.RedisService.Ctx, key)
|
||||
if result.Err() != nil {
|
||||
return 0, result.Err()
|
||||
}
|
||||
|
||||
// 设置过期时间(15分钟)
|
||||
impl.RedisService.Client.Expire(impl.RedisService.Ctx, key, 15*time.Minute)
|
||||
|
||||
return result.Val(), nil
|
||||
}
|
||||
|
||||
// GetLoginAttempts 获取登录尝试次数
|
||||
func GetLoginAttempts(ctx context.Context, account string) (int64, error) {
|
||||
key := impl.RedisService.BuildKey("loginattempts", account)
|
||||
result := impl.RedisService.Client.Get(impl.RedisService.Ctx, key)
|
||||
if result.Err() != nil {
|
||||
return 0, nil // 如果key不存在,返回0
|
||||
}
|
||||
return result.Int64()
|
||||
}
|
||||
|
||||
// ClearLoginAttempts 清除登录尝试次数
|
||||
func ClearLoginAttempts(ctx context.Context, account string) error {
|
||||
key := impl.RedisService.BuildKey("loginattempts", account)
|
||||
return impl.RedisService.Client.Del(impl.RedisService.Ctx, key).Err()
|
||||
}
|
||||
65
module/base/passport/internal/models/passport_account.go
Normal file
65
module/base/passport/internal/models/passport_account.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* @Author: ZhaoYadong
|
||||
* @Date: 2024-02-27 21:09:45
|
||||
* @LastEditors: ZhaoYadong
|
||||
* @LastEditTime: 2024-02-28 09:13:35
|
||||
* @FilePath: /server/Users/edy/go/src/passport/internal/models/passport_account.go
|
||||
*/
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/vars"
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
* PassportAccount
|
||||
* Comment: 通行证帐号表
|
||||
* Version: 10
|
||||
* Created: 2022-04-12 10:44:51 , Updated:0001-01-01 00:00:00
|
||||
*/
|
||||
type PassportAccount struct {
|
||||
types.Std_IICUDS
|
||||
Account string `gorm:"column:account;type:varchar(255);default:'';" json:"account"` // 帐号
|
||||
Phone string `gorm:"column:phone;type:varchar(20);default:'';" json:"phone"` // 手机号
|
||||
Email string `gorm:"column:email;type:varchar(255);default:'';" json:"email"` // Email
|
||||
Password string `gorm:"column:password;type:varchar(255);not null;" json:"password"` // 密码
|
||||
Salt string `gorm:"column:salt;type:varchar(255);not null;" json:"salt"` // 密码盐
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&PassportAccount{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *PassportAccount) TableName() string {
|
||||
return "passport_account" //对应数据库表名
|
||||
}
|
||||
|
||||
// GetPassportAccountByField 根据特定字段值获取PassportAccount对象
|
||||
func GetPassportAccountByField(field string, value any) (*PassportAccount, error) {
|
||||
var (
|
||||
data PassportAccount
|
||||
condition = map[string]any{field: value}
|
||||
)
|
||||
err := impl.DBService.Where(condition).First(&data).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrNotFound(404, "Account not found")
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
if data.Status == vars.Status_Disable {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
return &data, nil
|
||||
}
|
||||
75
module/base/passport/internal/models/passport_data.go
Normal file
75
module/base/passport/internal/models/passport_data.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/*
|
||||
* PassportExtend
|
||||
* Comment: 通行证帐号扩展表
|
||||
* Version: 10
|
||||
* Created: 2022-04-12 10:50:54 , Updated:0001-01-01 00:00:00
|
||||
*/
|
||||
type PassportData struct {
|
||||
types.Std_ID
|
||||
types.Std_Passport
|
||||
Nickname string `gorm:"column:nickname;type:varchar(64);default:'';" json:"nickname"` // 昵称
|
||||
Avatar string `gorm:"column:avatar;type:varchar(255);default:'';" json:"avatar"` // 头像
|
||||
Birthday time.Time `gorm:"column:birthday;" json:"birthday"` // 生日
|
||||
Sex int8 `gorm:"column:sex;default:0;" json:"sex"` // 性别,1为女性,2为男性
|
||||
Country string `gorm:"column:country;default:'';" json:"country"` // 国家
|
||||
Province string `gorm:"column:province;default:'';" json:"province"` // 省
|
||||
City string `gorm:"column:city;default:'';" json:"city"` // 市
|
||||
Area string `gorm:"column:area;default:'';" json:"area"` // 区
|
||||
Sign string `gorm:"column:sign;type:varchar(500);default:'';" json:"sign"` // 签名
|
||||
Cover string `gorm:"column:cover;type:varchar(255);default:'';" json:"cover"` // 背景&封面
|
||||
Score int32 `gorm:"column:score;default:0;" json:"score"` // 积分
|
||||
Level int32 `gorm:"column:level;default:0;" json:"level"` // 等级
|
||||
Rights string `gorm:"column:rights;type:varchar(255);default:'';" json:"rights"` // 权限
|
||||
AgencyId uint `gorm:"column:agency_id;default:0;" json:"agency_id"` // 分销代理id
|
||||
StaffId uint `gorm:"column:staff_id;default:0;" json:"staff_id"` // 工作人员id
|
||||
OwnerId uint `gorm:"column:owner_id;default:0;" json:"owner_id"` // 所属唯一id
|
||||
OwnerIdentity string `gorm:"column:owner_identity;type:varchar(64);default:'';" json:"owner_identity"` // 所属唯一码
|
||||
EmailVerify int32 `gorm:"default:0"` // 邮件验证状态:0 未验证 1 验证中,2 验证成功,-1 验证失败
|
||||
PhoneVerify int32 `gorm:"default:0"` // 手机验证状态:0 未验证 1 验证中,2 验证成功,-1 验证失败
|
||||
FaceVerify int32 `gorm:"default:0"` // 人脸或照片验证状态:0 未验证 1 验证中,2 验证成功,-1 验证失败
|
||||
DocumentVerify int32 `gorm:"default:0"` // 证件验证状态:0 未验证 1 验证中,2 验证成功,-1 验证失败
|
||||
KycVerify int32 `gorm:"default:0"` // KYC验证状态:0 未验证 1 验证中,2 验证成功,-1 验证失败
|
||||
|
||||
UpdatedAt time.Time // 最后更新时间
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&PassportData{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *PassportData) TableName() string {
|
||||
return "passport_data" //对应数据库表名
|
||||
}
|
||||
|
||||
func CheckPassportData(id uint, identity string) (*PassportData, error) {
|
||||
var data PassportData
|
||||
err := impl.DBService.Where("passport_id=? AND passport_identity=?", id, identity).First(&data).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
data = PassportData{
|
||||
Std_Passport: types.Std_Passport{
|
||||
PassportID: id,
|
||||
PassportIdentity: identity,
|
||||
},
|
||||
}
|
||||
impl.DBService.Create(&data)
|
||||
return &data, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &data, nil
|
||||
}
|
||||
31
module/base/passport/internal/models/passport_notify.go
Normal file
31
module/base/passport/internal/models/passport_notify.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
/*
|
||||
* PassportNotify
|
||||
* Comment: 会员消息通知表
|
||||
* Version: 10
|
||||
* Created: 2022-04-12 10:37:50 , Updated:0001-01-01 00:00:00
|
||||
*/
|
||||
type PassportNotify struct {
|
||||
types.Std_IICUDS
|
||||
types.Std_Passport
|
||||
Type int8 `gorm:"column:type;default:0;" json:"type"` // 类型
|
||||
Body string `gorm:"column:body;type:text;default:'';" json:"body"` // 正文
|
||||
Title string `gorm:"column:title;type:varchar(255);default:'';" json:"title"` // 标题
|
||||
From string `gorm:"column:from;type:varchar(36);default:'';" json:"from"` // 发信人
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&PassportNotify{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *PassportNotify) TableName() string {
|
||||
return "passport_notify" //对应数据库表名
|
||||
}
|
||||
26
module/base/passport/internal/models/passport_provider.go
Normal file
26
module/base/passport/internal/models/passport_provider.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 第三方登录配置表
|
||||
type PassportProvider struct {
|
||||
gorm.Model
|
||||
types.Std_Passport
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Provider string `gorm:"type:varchar(20);index;not null" json:"provider"` // google, twitter, facebook, wechat, apple, custom
|
||||
ProviderID string `gorm:"type:varchar(255);index;not null" json:"provider_id"` // 第三方平台的用户ID
|
||||
Email string `gorm:"type:varchar(100)" json:"email"`
|
||||
AccessToken string `gorm:"type:text" json:"-"`
|
||||
RefreshToken string `gorm:"type:text" json:"-"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&PassportProvider{})
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
/*
|
||||
* PassportRightsExpiry
|
||||
* Comment: 通行证特权有效期记录表
|
||||
* Version: 10
|
||||
* Created: 2022-04-11 17:33:50 , Updated:0001-01-01 00:00:00
|
||||
*/
|
||||
type PassportRightsExpiry struct {
|
||||
types.Std_IICUDS
|
||||
types.Std_Passport
|
||||
Rights string `gorm:"default:'';" json:"rights"` // 特权名称
|
||||
StartDate time.Time `json:"start_date"` // 生效日期
|
||||
EndDate time.Time `json:"end_date"` // 结束日期
|
||||
Remark string `gorm:"default:'';" json:"remark"` // 备注
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&PassportRightsExpiry{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *PassportRightsExpiry) TableName() string {
|
||||
return "passport_rights_expiry" //对应数据库表名
|
||||
}
|
||||
31
module/base/passport/internal/models/passport_score.go
Normal file
31
module/base/passport/internal/models/passport_score.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
/*
|
||||
* PassportScore
|
||||
* Comment: 会员积分记录表
|
||||
* Version: 10
|
||||
* Created: 2022-04-12 10:38:56 , Updated:0001-01-01 00:00:00
|
||||
*/
|
||||
type PassportScore struct {
|
||||
types.Std_IICUDS
|
||||
types.Std_Passport
|
||||
Score int64 `gorm:"default:0;" json:"score"` // 积分
|
||||
Action string `gorm:"default:'';" json:"action"` // 动作
|
||||
Remark string `gorm:"default:'';" json:"remark"` // 描述
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&PassportScore{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *PassportScore) TableName() string {
|
||||
return "passport_score" //对应数据库表名
|
||||
}
|
||||
29
module/base/passport/internal/models/passport_statistics.go
Normal file
29
module/base/passport/internal/models/passport_statistics.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
/*
|
||||
* PassportStatistics
|
||||
* Comment:
|
||||
* Version: 10
|
||||
* Created: 2022-04-12 10:41:13 , Updated:0001-01-01 00:00:00
|
||||
*/
|
||||
type PassportStatistics struct {
|
||||
types.Std_IICUDS
|
||||
types.Std_Passport
|
||||
Item string `gorm:"column:item;type:varchar(255);default:'';" json:"item"` // 统计项KEY
|
||||
Value int64 `gorm:"default:0;" json:"value"` // 统计项数
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&PassportStatistics{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *PassportStatistics) TableName() string {
|
||||
return "passport_statistics" //对应数据库表名
|
||||
}
|
||||
29
module/base/passport/internal/models/passport_tags.go
Normal file
29
module/base/passport/internal/models/passport_tags.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
/*
|
||||
* PassportTags
|
||||
* Comment:
|
||||
* Version: 10
|
||||
* Created: 2022-04-12 10:39:29 , Updated:0001-01-01 00:00:00
|
||||
*/
|
||||
type PassportTags struct {
|
||||
types.Std_IICUDS
|
||||
types.Std_Passport
|
||||
Name string `gorm:"column:name;type:varchar(255);not null;" json:"name"` // 标签标题
|
||||
Icon string `gorm:"column:icon;type:varchar(255);" json:"icon"` // 标签ICON
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&PassportTags{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (table *PassportTags) TableName() string {
|
||||
return "passport_tags" //对应数据库表名
|
||||
}
|
||||
54
module/base/passport/internal/models/passport_verify.go
Normal file
54
module/base/passport/internal/models/passport_verify.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
/*
|
||||
* PassportExtend
|
||||
* Comment: 通行证帐号认证表
|
||||
* Version: 10
|
||||
* Created: 2022-04-12 10:50:54 , Updated:0001-01-01 00:00:00
|
||||
*/
|
||||
type PassportVerify struct {
|
||||
types.Std_ID
|
||||
types.Std_Passport
|
||||
|
||||
// 验证时间戳
|
||||
EmailVerifyAt *time.Time
|
||||
PhoneVerifyAt *time.Time
|
||||
FaceVerifyAt *time.Time
|
||||
DocumentVerifyAt *time.Time
|
||||
KycVerifyAt *time.Time
|
||||
|
||||
// 验证相关 token/代码
|
||||
EmailVerifyToken string `gorm:"type:varchar(100)"`
|
||||
PhoneVerifyCode string `gorm:"type:varchar(10)"`
|
||||
FaceVerifyReject string `gorm:"type:text"`
|
||||
PhoneVerifyExpiresAt *time.Time
|
||||
|
||||
// 证件相关字段
|
||||
DocumentType string `gorm:"type:varchar(50)"` // 如: 'id_card', 'passport'
|
||||
DocumentName string `gorm:"type:varchar(100)"`
|
||||
DocumentNumber string `gorm:"type:varchar(100)"`
|
||||
DocumentFront string `gorm:"type:varchar(255)"` // 证件正面
|
||||
DocumentBack string `gorm:"type:varchar(255)"` // 证件反面
|
||||
|
||||
// KYC 相关字段
|
||||
KycDocumentBack string `gorm:"type:varchar(255)"`
|
||||
KycStatus string `gorm:"type:varchar(20);default:'pending'"` // pending, approved, rejected
|
||||
KycRejectReason string `gorm:"type:text"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.AppendMigrate(&PassportVerify{})
|
||||
}
|
||||
|
||||
// TableName .
|
||||
func (pv *PassportVerify) TableName() string {
|
||||
return "passport_verify" //对应数据库表名
|
||||
}
|
||||
65
module/base/passport/internal/models/query.go
Normal file
65
module/base/passport/internal/models/query.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"bsm/full/module/base/passport/internal/impl"
|
||||
"bsm/full/module/base/passport/internal/vars"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func InitData() error {
|
||||
var cnt int64
|
||||
var err error
|
||||
err = impl.DBService.Model(&PassportAccount{}).Where("account=?", "demo").Count(&cnt).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cnt == 0 {
|
||||
salt := utils.ULID()
|
||||
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("welcome"+salt), bcrypt.MinCost)
|
||||
pa := PassportAccount{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: utils.UUID(),
|
||||
Status: vars.Status_Normal,
|
||||
},
|
||||
Account: "demo",
|
||||
Phone: "",
|
||||
Password: string(hashedPassword),
|
||||
Salt: salt,
|
||||
}
|
||||
err = impl.DBService.Create(&pa).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func PassportAccountExists(key, val string) bool {
|
||||
var count int64
|
||||
err := impl.DBService.Model(&PassportAccount{}).Where(key+" = ?", val).Count(&count).Error
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func CreateAccount(pa *PassportAccount, nickname string) (err error) {
|
||||
return impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
// 插入扩展表
|
||||
pe := new(PassportData)
|
||||
pe.PassportID = pa.ID
|
||||
pe.PassportIdentity = pa.Identity
|
||||
pe.Nickname = nickname
|
||||
|
||||
if err := tx.Create(pe).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
46
module/base/passport/internal/server/account_server.go
Normal file
46
module/base/passport/internal/server/account_server.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/passport/internal/logic/account"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
)
|
||||
|
||||
type AccountServer struct {
|
||||
pb.UnimplementedAccountServer
|
||||
}
|
||||
|
||||
func NewAccountServer() *AccountServer {
|
||||
return &AccountServer{}
|
||||
}
|
||||
|
||||
// 通过会员所有信息
|
||||
func (s *AccountServer) Get(ctx context.Context, in *pb.Empty) (*pb.GetFullReply, error) {
|
||||
return account.Get(ctx, in)
|
||||
}
|
||||
|
||||
// 更新会员的信息数据,字段值为空或是0,将不更新此数据
|
||||
func (s *AccountServer) SetData(ctx context.Context, in *pb.SetDataRequest) (*pb.StatusReply, error) {
|
||||
return account.SetData(ctx, in)
|
||||
}
|
||||
|
||||
// 更新会员的密码
|
||||
func (s *AccountServer) SetPassword(ctx context.Context, in *pb.SetPasswordRequest) (*pb.StatusReply, error) {
|
||||
return account.SetPassword(ctx, in)
|
||||
}
|
||||
|
||||
// 新增标签
|
||||
func (s *AccountServer) TagCreate(ctx context.Context, in *pb.TagItem) (*pb.StatusReply, error) {
|
||||
return account.TagCreate(ctx, in)
|
||||
}
|
||||
|
||||
// 删除标签
|
||||
func (s *AccountServer) TagRemove(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
return account.TagRemove(ctx, in)
|
||||
}
|
||||
|
||||
// 获取会员的相关统计数据
|
||||
func (s *AccountServer) Statistics(ctx context.Context, in *pb.StatisticsRequest) (*pb.StatisticsReply, error) {
|
||||
return account.Statistics(ctx, in)
|
||||
}
|
||||
26
module/base/passport/internal/server/forget_server.go
Normal file
26
module/base/passport/internal/server/forget_server.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/passport/internal/logic/forget"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
)
|
||||
|
||||
type ForgetServer struct {
|
||||
pb.UnimplementedForgetServer
|
||||
}
|
||||
|
||||
func NewForgetServer() *ForgetServer {
|
||||
return &ForgetServer{}
|
||||
}
|
||||
|
||||
// 验证手机号和验证码
|
||||
func (s *ForgetServer) Verify(ctx context.Context, in *pb.ForgetVerifyRequest) (*pb.StatusReply, error) {
|
||||
return forget.Verify(ctx, in)
|
||||
}
|
||||
|
||||
// 重罢密码
|
||||
func (s *ForgetServer) Reset(ctx context.Context, in *pb.ForgetResetRequest) (*pb.StatusReply, error) {
|
||||
return forget.Reset(ctx, in)
|
||||
}
|
||||
31
module/base/passport/internal/server/login_server.go
Normal file
31
module/base/passport/internal/server/login_server.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/passport/internal/logic/login"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
)
|
||||
|
||||
type LoginServer struct {
|
||||
pb.UnimplementedLoginServer
|
||||
}
|
||||
|
||||
func NewLoginServer() *LoginServer {
|
||||
return &LoginServer{}
|
||||
}
|
||||
|
||||
// 通过密码登录
|
||||
func (s *LoginServer) Pwd(ctx context.Context, in *pb.LoginByPwdRequest) (*pb.LoginReply, error) {
|
||||
return login.Pwd(ctx, in)
|
||||
}
|
||||
|
||||
// 通过验证码登录
|
||||
func (s *LoginServer) Code(ctx context.Context, in *pb.LoginByCodeRequest) (*pb.LoginReply, error) {
|
||||
return login.Code(ctx, in)
|
||||
}
|
||||
|
||||
// 通过验证码快捷登录并注册
|
||||
func (s *LoginServer) Quick(ctx context.Context, in *pb.LoginByCodeRequest) (*pb.LoginReply, error) {
|
||||
return login.Quick(ctx, in)
|
||||
}
|
||||
111
module/base/passport/internal/server/new.go
Normal file
111
module/base/passport/internal/server/new.go
Normal file
@@ -0,0 +1,111 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Grpc *grpc.Server
|
||||
Ctx context.Context
|
||||
Mux *gwRuntime.ServeMux
|
||||
grpcConns map[string]*grpc.ClientConn // 连接池
|
||||
}
|
||||
|
||||
func New(addr string) *Server {
|
||||
srv := &Server{
|
||||
Ctx: context.Background(),
|
||||
Grpc: grpc.NewServer(),
|
||||
Mux: gwRuntime.NewServeMux(gwRuntime.WithForwardResponseRewriter(responseEnvelope)),
|
||||
grpcConns: make(map[string]*grpc.ClientConn),
|
||||
}
|
||||
|
||||
// register service to grpc.Server
|
||||
pb.RegisterAccountServer(srv.Grpc, NewAccountServer())
|
||||
pb.RegisterForgetServer(srv.Grpc, NewForgetServer())
|
||||
pb.RegisterLoginServer(srv.Grpc, NewLoginServer())
|
||||
pb.RegisterRegisterServer(srv.Grpc, NewRegisterServer())
|
||||
pb.RegisterVerifyServer(srv.Grpc, NewVerifyServer())
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 连接池: 只创建一次连接并复用
|
||||
conn, ok := srv.grpcConns[addr]
|
||||
if !ok {
|
||||
var err error
|
||||
conn, err = grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
panic("failed to dial grpc server: " + err.Error())
|
||||
}
|
||||
srv.grpcConns[addr] = conn
|
||||
}
|
||||
|
||||
// 将服务注册到Gateway
|
||||
|
||||
if err := pb.RegisterAccountHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Account handler: " + err.Error())
|
||||
}
|
||||
|
||||
if err := pb.RegisterForgetHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Forget handler: " + err.Error())
|
||||
}
|
||||
|
||||
if err := pb.RegisterLoginHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Login handler: " + err.Error())
|
||||
}
|
||||
|
||||
if err := pb.RegisterRegisterHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Register handler: " + err.Error())
|
||||
}
|
||||
|
||||
if err := pb.RegisterVerifyHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Verify handler: " + err.Error())
|
||||
}
|
||||
|
||||
// Register services swagger
|
||||
srv.RegisterSwagger()
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
// RegisterSwagger 注册swagger
|
||||
func (s *Server) RegisterSwagger() {
|
||||
srvKey := strings.ToLower(vars.ServiceKey)
|
||||
s.Mux.HandlePath("GET", "/"+srvKey+".swagger.json", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
bytes, err := os.ReadFile("./swagger/" + srvKey + ".swagger.json")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
w.Write(bytes)
|
||||
return
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// response envelope
|
||||
func responseEnvelope(_ context.Context, response proto.Message) (interface{}, error) {
|
||||
name := string(response.ProtoReflect().Descriptor().Name())
|
||||
if name == "Status" || name == "Error" || name == "StatusReply" {
|
||||
return response, nil
|
||||
}
|
||||
return map[string]any{
|
||||
"code": 0,
|
||||
"message": vars.OK,
|
||||
"details": response,
|
||||
"timeseq": time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
26
module/base/passport/internal/server/register_server.go
Normal file
26
module/base/passport/internal/server/register_server.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/passport/internal/logic/register"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
)
|
||||
|
||||
type RegisterServer struct {
|
||||
pb.UnimplementedRegisterServer
|
||||
}
|
||||
|
||||
func NewRegisterServer() *RegisterServer {
|
||||
return &RegisterServer{}
|
||||
}
|
||||
|
||||
// 帐号密码注册
|
||||
func (s *RegisterServer) Pwd(ctx context.Context, in *pb.RegisterRequest) (*pb.RegisterReply, error) {
|
||||
return register.Pwd(ctx, in)
|
||||
}
|
||||
|
||||
// 手机验证码注册
|
||||
func (s *RegisterServer) Code(ctx context.Context, in *pb.RegisterRequest) (*pb.RegisterReply, error) {
|
||||
return register.Code(ctx, in)
|
||||
}
|
||||
26
module/base/passport/internal/server/verify_server.go
Normal file
26
module/base/passport/internal/server/verify_server.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/passport/internal/logic/verify"
|
||||
pb "bsm/full/module/base/passport/pb"
|
||||
)
|
||||
|
||||
type VerifyServer struct {
|
||||
pb.UnimplementedVerifyServer
|
||||
}
|
||||
|
||||
func NewVerifyServer() *VerifyServer {
|
||||
return &VerifyServer{}
|
||||
}
|
||||
|
||||
// 认证请求
|
||||
func (s *VerifyServer) Request(ctx context.Context, in *pb.VerifyRequest) (*pb.StatusReply, error) {
|
||||
return verify.Request(ctx, in)
|
||||
}
|
||||
|
||||
// KYC 认证回调
|
||||
func (s *VerifyServer) JumioCallback(ctx context.Context, in *pb.JumioCallbackPayload) (*pb.StatusReply, error) {
|
||||
return verify.JumioCallback(ctx, in)
|
||||
}
|
||||
10
module/base/passport/internal/vars/provider.go
Normal file
10
module/base/passport/internal/vars/provider.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package vars
|
||||
|
||||
const (
|
||||
Provider_Google = "google"
|
||||
Provider_Twitter = "twitter"
|
||||
Provider_Facebook = "facebook"
|
||||
Provider_Wechat = "wechat"
|
||||
Provider_Apple = "apple"
|
||||
Provider_Custom = "custom"
|
||||
)
|
||||
8
module/base/passport/internal/vars/status.go
Normal file
8
module/base/passport/internal/vars/status.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package vars
|
||||
|
||||
const (
|
||||
Status_UnApproved int8 = -1
|
||||
Status_Disable int8 = -9
|
||||
Status_Normal int8 = 0
|
||||
Status_Approved int8 = 1
|
||||
)
|
||||
3
module/base/passport/internal/vars/token.go
Normal file
3
module/base/passport/internal/vars/token.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package vars
|
||||
|
||||
var TokenPrefix string = "/TOKEN/"
|
||||
Reference in New Issue
Block a user