91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/redis"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/services/equipment/internal/dao"
|
|
"lone-services/services/equipment/internal/model"
|
|
"lone-services/services/equipment/validator"
|
|
"strconv"
|
|
|
|
"lone-services/rpc/equipment/pb"
|
|
"lone-services/services/equipment/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type AuthorizationBindLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewAuthorizationBindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthorizationBindLogic {
|
|
return &AuthorizationBindLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *AuthorizationBindLogic) AuthorizationBind(in *equipment.AuthorizationBindRequest) (*equipment.Response, error) {
|
|
var v validator.CodeBindValidator
|
|
if fail := l.checkParams(in, &v); fail != nil {
|
|
return fail, nil
|
|
}
|
|
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
return l.fail(utils.ErrorNoLoginInfo)
|
|
}
|
|
|
|
key := dao.CodeName + strconv.FormatInt(in.StoreId, utils.NumberTen)
|
|
|
|
redisCode, err := redis.Client.Get(l.ctx, key).Result()
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.out(utils.Fail, "获取Code失败")
|
|
}
|
|
if redisCode != in.Code {
|
|
return l.out(utils.Fail, "Code不匹配")
|
|
}
|
|
|
|
w := modelbase.Params{Eq: map[string]string{
|
|
"store_id": strconv.Itoa(int(in.StoreId)), "user_id": strconv.Itoa(int(adminInfo.ID))}}
|
|
var info dao.AuthorizationRecordCreate
|
|
err = model.AuthorizationRecordModel{}.Init().GetOne(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
if info.Id > utils.NumberZero {
|
|
info.Status = utils.StatusOk
|
|
info.Datetime = utils.Now()
|
|
_, err = model.AuthorizationRecordModel{}.Init().Edit(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
} else {
|
|
info.StoreId = in.StoreId
|
|
info.UserId = adminInfo.ID
|
|
err = model.AuthorizationRecordModel{}.Init().Create(&info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
}
|
|
|
|
_, err = redis.Client.Del(l.ctx, key).Result()
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
return l.ok(utils.Ok)
|
|
}
|