refactor: reorganize modules and add Linux build tooling
This commit is contained in:
76
module/ec/mall/internal/logic/staff/create.go
Normal file
76
module/ec/mall/internal/logic/staff/create.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/mall/internal/impl"
|
||||
"bsm/full/module/ec/mall/internal/models"
|
||||
pb "bsm/full/module/ec/mall/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
)
|
||||
|
||||
// 创建工作人员
|
||||
func Create(ctx context.Context, in *pb.StaffItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.GetAccount() == "" || in.GetPassword() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
salt := utils.RandomString(8)
|
||||
data := models.MallStaff{
|
||||
Name: in.Name,
|
||||
Account: in.Account,
|
||||
Password: utils.Md5(in.Password + salt),
|
||||
Profile: in.Profile,
|
||||
Phone: in.Phone,
|
||||
Email: in.Email,
|
||||
Avatar: in.Avatar,
|
||||
Salt: salt,
|
||||
Role: "staff",
|
||||
Std_IICUDS: types.Std_IICUDS{Status: 1},
|
||||
}
|
||||
|
||||
ownerStore := auth.Owner.(map[string]any)
|
||||
storeId := uint(ownerStore["store_id"].(float64))
|
||||
storeIdentity := ownerStore["store_identity"].(string)
|
||||
|
||||
data.Identity = utils.UUID()
|
||||
data.Store_ID = storeId
|
||||
data.Store_Identity = storeIdentity
|
||||
|
||||
var cnt int64
|
||||
err = impl.DBService.Model(&models.MallStaff{}).Where("store_identity = ?", storeIdentity).Where("account = ?", in.Account).Count(&cnt).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 账号已存在
|
||||
if cnt > 0 {
|
||||
return nil, errcode.ErrAlreadyExists
|
||||
}
|
||||
|
||||
err = impl.DBService.Create(&data).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Identity: data.Identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
40
module/ec/mall/internal/logic/staff/delete.go
Normal file
40
module/ec/mall/internal/logic/staff/delete.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/mall/internal/impl"
|
||||
"bsm/full/module/ec/mall/internal/models"
|
||||
pb "bsm/full/module/ec/mall/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 删除工作人员
|
||||
func Delete(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = impl.DBService.Where("id=? or identity=?", in.GetId(), in.GetIdentity()).Delete(&models.MallStaff{}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
79
module/ec/mall/internal/logic/staff/fetch.go
Normal file
79
module/ec/mall/internal/logic/staff/fetch.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/mall/internal/impl"
|
||||
"bsm/full/module/ec/mall/internal/models"
|
||||
pb "bsm/full/module/ec/mall/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 获取工作人员列表
|
||||
func Fetch(ctx context.Context, in *pb.FetchRequest) (reply *pb.StaffListReply, err error) {
|
||||
// parse authorization meta.
|
||||
// _, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
var (
|
||||
cnt int64 = 0
|
||||
pageNo int64 = in.GetPageNo()
|
||||
pageSize int64 = in.GetPageSize()
|
||||
offset int64 = (pageNo - 1) * pageSize
|
||||
params = map[string]string{
|
||||
"store_identity": in.GetStoreIdentity(),
|
||||
}
|
||||
)
|
||||
|
||||
// vlidate request page_no,page_size.
|
||||
if pageNo < 1 {
|
||||
pageNo = 1
|
||||
offset = 0
|
||||
}
|
||||
if pageSize < 50 {
|
||||
pageSize = 50
|
||||
offset = (pageNo - 1) * pageSize
|
||||
}
|
||||
|
||||
var data []*models.MallStaff
|
||||
if err := impl.DBService.Model(&models.MallStaff{}).Where(params).Order("created_at desc").Count(&cnt).
|
||||
Offset(int(offset)).Limit(int(pageSize)).Find(&data).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return &pb.StaffListReply{
|
||||
Count: cnt,
|
||||
Data: ModelToReply(data),
|
||||
}, nil
|
||||
}
|
||||
func ModelToReply(data []*models.MallStaff) (list []*pb.StaffItem) {
|
||||
if len(data) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, in := range data {
|
||||
row := pb.StaffItem{
|
||||
Id: int64(in.ID),
|
||||
Identity: in.Identity,
|
||||
Name: in.Name,
|
||||
Account: in.Account,
|
||||
Profile: in.Profile,
|
||||
Role: in.Role,
|
||||
Phone: in.Phone,
|
||||
Email: in.Email,
|
||||
Status: int32(in.Status),
|
||||
Avatar: in.Avatar,
|
||||
CreatedAt: in.CreatedAt.Format(time.DateTime),
|
||||
}
|
||||
|
||||
list = append(list, &row)
|
||||
}
|
||||
return
|
||||
}
|
||||
48
module/ec/mall/internal/logic/staff/get_profile.go
Normal file
48
module/ec/mall/internal/logic/staff/get_profile.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/mall/internal/impl"
|
||||
"bsm/full/module/ec/mall/internal/models"
|
||||
pb "bsm/full/module/ec/mall/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// GetProfile 获取个人信息
|
||||
func GetProfile(ctx context.Context, in *pb.IdentRequest) (reply *pb.StaffItem, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
var data models.MallStaff
|
||||
err = impl.DBService.Where("id=? or identity=?", in.GetId(), in.GetIdentity()).First(&data).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StaffItem{
|
||||
Id: int64(data.ID),
|
||||
Identity: data.Identity,
|
||||
Name: data.Name,
|
||||
Account: data.Account,
|
||||
Profile: data.Profile,
|
||||
Phone: data.Phone,
|
||||
Email: data.Email,
|
||||
Avatar: data.Avatar,
|
||||
Role: data.Role,
|
||||
Status: int32(data.Status),
|
||||
CreatedAt: data.CreatedAt.Format(time.DateTime),
|
||||
}, nil
|
||||
}
|
||||
122
module/ec/mall/internal/logic/staff/login.go
Normal file
122
module/ec/mall/internal/logic/staff/login.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/mall/internal/excode"
|
||||
"bsm/full/module/ec/mall/internal/impl"
|
||||
"bsm/full/module/ec/mall/internal/models"
|
||||
pb "bsm/full/module/ec/mall/pb"
|
||||
"git.apinb.com/bsm-sdk/core/crypto/encipher"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Login 员工登录验证
|
||||
func Login(ctx context.Context, in *pb.LoginRequest) (reply *pb.LoginReply, err error) {
|
||||
var record models.MallStaff
|
||||
|
||||
// 根据登录类型进行不同的验证
|
||||
switch in.LoginGenre {
|
||||
case 1: // 账号密码登录
|
||||
if in.Account == "" || in.Password == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err := impl.DBService.Where("account = ?", in.Account).First(&record).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrAccountNotFound
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
// 密码校验 - 使用MD5加盐验证
|
||||
if record.Password != utils.Md5(in.Password+record.Salt) {
|
||||
return nil, errcode.ErrPassword
|
||||
}
|
||||
|
||||
case 2: // 手机验证码登录
|
||||
if in.Phone == "" || in.VerifyCode == "" || ValidatePhone(in.Phone) != nil {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err := impl.DBService.Where("phone = ?", in.Phone).First(&record).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrAccountNotFound
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
// TODO: 添加验证码校验逻辑
|
||||
|
||||
default:
|
||||
return nil, errcode.ErrUnimplemented
|
||||
}
|
||||
|
||||
// 账号状态校验
|
||||
if record.Status == models.DisabledStatus {
|
||||
return nil, errcode.ErrAccountDisabled
|
||||
}
|
||||
|
||||
// 检查店铺是否正常运营
|
||||
var storeData = models.MallStore{}
|
||||
err = impl.DBService.Model(&models.MallStore{}).Where("id = ? AND status = 1", record.Store_ID).First(&storeData).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, excode.ErrStoreNotFound
|
||||
}
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if storeData.Status == 0 {
|
||||
return nil, excode.ErrStoreDisabled
|
||||
}
|
||||
|
||||
store := models.Std_Store{
|
||||
Store_ID: record.Store_ID,
|
||||
Store_Identity: record.Store_Identity,
|
||||
}
|
||||
|
||||
token, err := encipher.GenerateTokenAes(record.ID, record.Identity, "", record.Role, store, map[string]string{"role": record.Role})
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 更新登录时间
|
||||
record.LastLoginAt = time.Now()
|
||||
err = impl.DBService.Where("id = ?", record.ID).Select("last_login_at").Updates(record).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.LoginReply{
|
||||
Token: token,
|
||||
Identity: record.Identity,
|
||||
Name: record.Name,
|
||||
Account: record.Account,
|
||||
Role: record.Role,
|
||||
StoreName: storeData.Title,
|
||||
StoreIdentity: storeData.Identity,
|
||||
Status: int32(record.Status),
|
||||
CreatedAt: record.CreatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ValidatePhone 验证手机号格式
|
||||
func ValidatePhone(phone string) error {
|
||||
// 中国大陆手机号验证:1开头,第二位为3-9,总共11位数字
|
||||
matched, err := regexp.MatchString("^1[3-9]\\d{9}$", phone)
|
||||
if err != nil || !matched {
|
||||
return errcode.ErrInvalidArgument
|
||||
}
|
||||
return nil
|
||||
}
|
||||
32
module/ec/mall/internal/logic/staff/ref.go
Normal file
32
module/ec/mall/internal/logic/staff/ref.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/mall/internal/models"
|
||||
pb "bsm/full/module/ec/mall/pb"
|
||||
)
|
||||
|
||||
func ref(data []*models.MallStaff) (list []*pb.StaffItem) {
|
||||
if len(data) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, in := range data {
|
||||
row := pb.StaffItem{
|
||||
Identity: in.Identity,
|
||||
Name: in.Name,
|
||||
Account: in.Account,
|
||||
Password: "***",
|
||||
Profile: in.Profile,
|
||||
Phone: in.Phone,
|
||||
Email: in.Email,
|
||||
Avatar: in.Avatar,
|
||||
Role: "staff",
|
||||
CreatedAt: in.CreatedAt.Format(time.DateTime),
|
||||
}
|
||||
|
||||
list = append(list, &row)
|
||||
}
|
||||
return
|
||||
}
|
||||
42
module/ec/mall/internal/logic/staff/set_password.go
Normal file
42
module/ec/mall/internal/logic/staff/set_password.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/mall/internal/impl"
|
||||
"bsm/full/module/ec/mall/internal/models"
|
||||
pb "bsm/full/module/ec/mall/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
)
|
||||
|
||||
// SetPassword 设置账号密码
|
||||
func SetPassword(ctx context.Context, in *pb.SetAccountRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.Password == "" || in.Password != in.PasswordConfirmed {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
salt := utils.RandomString(8)
|
||||
pwd := utils.Md5(in.Password + salt)
|
||||
err = impl.DBService.Where("id = ?", auth.ID).Updates(&models.MallStaff{Salt: salt, Password: pwd}).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
43
module/ec/mall/internal/logic/staff/set_profile.go
Normal file
43
module/ec/mall/internal/logic/staff/set_profile.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/ec/mall/internal/impl"
|
||||
"bsm/full/module/ec/mall/internal/models"
|
||||
pb "bsm/full/module/ec/mall/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// SetProfile 设置个人信息
|
||||
func SetProfile(ctx context.Context, in *pb.StaffItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := &models.MallStaff{
|
||||
Name: in.Name,
|
||||
Account: in.Account,
|
||||
Profile: in.Profile,
|
||||
Phone: in.Phone,
|
||||
Email: in.Email,
|
||||
Avatar: in.Avatar,
|
||||
}
|
||||
err = impl.DBService.Debug().Where("identity = ?", auth.Identity).Updates(data).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user