add admin
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"admin/validator"
|
||||
"context"
|
||||
|
||||
"admin/admin"
|
||||
"admin/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"pkg.local/utils"
|
||||
validateService "pkg.local/validate"
|
||||
)
|
||||
|
||||
type AddLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
|
||||
return &AddLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AddLogic) Add(in *admin.AddRequest) (*admin.Response, error) {
|
||||
var req validator.AdminAddValidator
|
||||
if resp := validateService.ValidateFromProto(in, &req); resp != utils.StringEmpty {
|
||||
return &admin.Response{Code: utils.Ok.Code, Msg: resp}, nil
|
||||
}
|
||||
|
||||
return &admin.Response{}, nil
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"admin/admin"
|
||||
"admin/internal/dao"
|
||||
"admin/internal/model"
|
||||
"admin/internal/svc"
|
||||
"admin/validator"
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"pkg.local/modelbase"
|
||||
"pkg.local/utils"
|
||||
)
|
||||
|
||||
type AdminAddLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
BaseLogic
|
||||
}
|
||||
|
||||
func NewAdminAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminAddLogic {
|
||||
return &AdminAddLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminAddLogic) AdminAdd(in *admin.AdminEditRequest) (*admin.Response, error) {
|
||||
var v validator.AdminEditValidator
|
||||
if fail := l.checkParams(in, &v); fail != nil {
|
||||
return fail, nil
|
||||
}
|
||||
|
||||
encryPhone, cErr := utils.EncryptPhone(in.Phone)
|
||||
if cErr != nil {
|
||||
l.Logger.Error(cErr)
|
||||
return l.fail(utils.ErrorEncryptAesError), nil
|
||||
}
|
||||
|
||||
info, check := l.checkPhone(encryPhone)
|
||||
if !check {
|
||||
return l.fail(utils.ErrorEncryptAesError), nil
|
||||
}
|
||||
modelObj := model.AdminModel{}.Init()
|
||||
if in.Id > utils.NumberZero && info.Id < utils.NumberOne {
|
||||
w := modelbase.Params{Eq: map[string]string{"id": strconv.Itoa(int(in.Id))}}
|
||||
info = dao.Admin{}
|
||||
err := modelObj.GetOne(w, &info)
|
||||
if err != nil {
|
||||
l.Logger.Error(cErr)
|
||||
return l.fail(utils.Fail), nil
|
||||
|
||||
}
|
||||
if info.Id < utils.NumberZero {
|
||||
return l.fail(utils.ErrorNotFund), nil
|
||||
}
|
||||
}
|
||||
//编辑
|
||||
if in.Id > utils.NumberZero {
|
||||
if info.Id < utils.NumberOne {
|
||||
return l.fail(utils.ErrorNotFund), nil
|
||||
}
|
||||
if info.Id != in.Id {
|
||||
return l.fail(utils.ErrorExistUser), nil
|
||||
}
|
||||
} else {
|
||||
if len(in.Password) < utils.NumberOne {
|
||||
return l.out(utils.ErrorParams, "密码不能为空"), nil
|
||||
|
||||
}
|
||||
if info.Id > utils.NumberZero {
|
||||
return l.fail(utils.ErrorExistUser), nil
|
||||
}
|
||||
}
|
||||
//adminId, _ := c.Get(utils.USERID)
|
||||
//adminName, _ := c.Get(utils.USERNAME)
|
||||
var roles []string
|
||||
if len(in.Roles) > utils.NumberZero {
|
||||
for _, role := range in.Roles {
|
||||
roles = append(roles, strconv.Itoa(int(role)))
|
||||
}
|
||||
}
|
||||
|
||||
info.Name = in.Name
|
||||
info.Phone = encryPhone
|
||||
info.Roles = strings.Join(roles, ",")
|
||||
info.Email = in.Email
|
||||
info.Avatar = in.Avatar
|
||||
var err error
|
||||
if info.Id > utils.NumberZero {
|
||||
w := modelbase.Params{Eq: map[string]string{"id": strconv.Itoa(int(info.Id))}}
|
||||
_, err = modelObj.Edit(w, info)
|
||||
} else {
|
||||
salt := utils.GetRandstring(utils.NumberFive)
|
||||
pwd := fmt.Sprintf("%s%s", salt, in.Password)
|
||||
pwd, _ = utils.EncryptPassword(pwd)
|
||||
info.Password = pwd
|
||||
info.Salt = salt
|
||||
err = modelObj.Init().Create(&info)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
l.Logger.Error(err)
|
||||
return l.fail(utils.Fail), nil
|
||||
}
|
||||
return l.ok(info.Id), nil
|
||||
}
|
||||
|
||||
func (l *AdminAddLogic) checkPhone(phone string) (dao.Admin, bool) {
|
||||
w := modelbase.Params{Eq: map[string]string{"phone": phone}}
|
||||
info := dao.Admin{}
|
||||
err := model.AdminModel{}.Init().GetOne(w, &info)
|
||||
if err != nil {
|
||||
l.Logger.Error(err)
|
||||
return info, false
|
||||
}
|
||||
|
||||
return info, true
|
||||
}
|
||||
@@ -9,24 +9,22 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type EditLogic struct {
|
||||
type AdminEditLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditLogic {
|
||||
return &EditLogic{
|
||||
func NewAdminEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminEditLogic {
|
||||
return &AdminEditLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *EditLogic) Edit(in *admin.EditRequest) (*admin.Response, error) {
|
||||
func (l *AdminEditLogic) AdminEdit(in *admin.AdminEditRequest) (*admin.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &admin.Response{
|
||||
Msg: in.Name + " " + in.Pwd,
|
||||
}, nil
|
||||
return &admin.Response{}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"admin/admin"
|
||||
"admin/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminInfoLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAdminInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminInfoLogic {
|
||||
return &AdminInfoLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminInfoLogic) AdminInfo(in *admin.AdminInfoRequest) (*admin.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &admin.Response{}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"admin/admin"
|
||||
"admin/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminItemsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAdminItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminItemsLogic {
|
||||
return &AdminItemsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminItemsLogic) AdminItems(in *admin.AdminItemsRequest) (*admin.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &admin.Response{}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"admin/admin"
|
||||
"admin/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminLoginLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAdminLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminLoginLogic {
|
||||
return &AdminLoginLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminLoginLogic) AdminLogin(in *admin.AdminLoginRequest) (*admin.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &admin.Response{}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"admin/admin"
|
||||
"admin/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminStatusLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAdminStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminStatusLogic {
|
||||
return &AdminStatusLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminStatusLogic) AdminStatus(in *admin.StatusRequest) (*admin.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &admin.Response{}, nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"admin/admin"
|
||||
|
||||
"pkg.local/utils"
|
||||
validateService "pkg.local/validate"
|
||||
)
|
||||
|
||||
type BaseLogic struct {
|
||||
}
|
||||
|
||||
func (l *BaseLogic) checkParams(in *admin.AdminEditRequest, v validateService.IValidator) *admin.Response {
|
||||
|
||||
if resp := validateService.ValidateFromProto(in, v); resp != utils.StringEmpty {
|
||||
return &admin.Response{Code: utils.ErrorParams.Code, Msg: resp}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *AdminAddLogic) fail(status utils.Status) *admin.Response {
|
||||
return &admin.Response{Code: status.Code, Msg: status.Msg}
|
||||
}
|
||||
|
||||
func (l *AdminAddLogic) out(status utils.Status, msg string) *admin.Response {
|
||||
return &admin.Response{Code: status.Code, Msg: msg}
|
||||
}
|
||||
|
||||
func (l *AdminAddLogic) ok(data any) *admin.Response {
|
||||
return &admin.Response{Code: utils.Ok.Code, Msg: utils.Ok.Msg}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"admin/admin"
|
||||
"admin/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type OwnPasswordLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewOwnPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OwnPasswordLogic {
|
||||
return &OwnPasswordLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *OwnPasswordLogic) OwnPassword(in *admin.OwnPasswordRequest) (*admin.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &admin.Response{}, nil
|
||||
}
|
||||
@@ -9,21 +9,21 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type StatusLogic struct {
|
||||
type PasswordLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StatusLogic {
|
||||
return &StatusLogic{
|
||||
func NewPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PasswordLogic {
|
||||
return &PasswordLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *StatusLogic) Status(in *admin.StatusRequest) (*admin.Response, error) {
|
||||
func (l *PasswordLogic) Password(in *admin.PasswordRequest) (*admin.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &admin.Response{}, nil
|
||||
Reference in New Issue
Block a user