实现商户注册、管理员审核商户申请接口
This commit is contained in:
@ -1 +1,168 @@
|
||||
package merchantAdmin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
"server/internal/consts"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
utility "server/utility/encrypt"
|
||||
"server/utility/jwt"
|
||||
)
|
||||
|
||||
type sMerchantAdmin struct {
|
||||
}
|
||||
|
||||
func New() service.IMerchantAdmin {
|
||||
return &sMerchantAdmin{}
|
||||
}
|
||||
func init() {
|
||||
service.RegisterMerchantAdmin(New())
|
||||
go checkMerchantRole()
|
||||
}
|
||||
|
||||
func checkMerchantRole() {
|
||||
ctx := context.Background()
|
||||
exist, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: consts.MerchantRoleCode}).Exist()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !exist {
|
||||
_, err = dao.Roles.Ctx(ctx).Insert(do.Roles{
|
||||
Name: "商户管理员",
|
||||
Code: consts.MerchantRoleCode,
|
||||
Description: "商户管理员角色",
|
||||
Status: consts.RoleEnable,
|
||||
IsDeletable: false,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *sMerchantAdmin) Login(ctx context.Context, in *model.MerchantLoginIn) (out *model.MerchantLoginOut, err error) {
|
||||
mAdmin, err := dao.MerchantAdmins.Ctx(ctx).Where(do.MerchantAdmins{Username: in.Username}).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
||||
}
|
||||
|
||||
if mAdmin.IsEmpty() {
|
||||
return nil, ecode.Params.Sub("该用户不存在")
|
||||
}
|
||||
|
||||
if mAdmin[dao.MerchantAdmins.Columns().Status].Int() == consts.MerchantAdministratorDisable {
|
||||
return nil, ecode.Params.Sub("该用户已被禁用")
|
||||
}
|
||||
if !utility.ComparePassword(mAdmin[dao.MerchantAdmins.Columns().PasswordHash].String(), in.Password) {
|
||||
return nil, ecode.Params.Sub("密码错误")
|
||||
}
|
||||
value, err := dao.Roles.Ctx(ctx).WherePri(mAdmin[dao.MerchantAdmins.Columns().RoleId].Int()).Fields(dao.Roles.Columns().Code).Value()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
mAdminId := mAdmin[dao.MerchantAdmins.Columns().Id].Int64()
|
||||
token, err := jwt.GenerateToken(&jwt.TokenIn{UserId: mAdminId, Role: value.String()})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("生成token失败")
|
||||
}
|
||||
out = &model.MerchantLoginOut{
|
||||
Token: token,
|
||||
}
|
||||
go func(ctx context.Context, merchantAdminId int64) {
|
||||
// 更新商户管理员登录时间
|
||||
dao.MerchantAdmins.Ctx(ctx).WherePri(merchantAdminId).Update(do.MerchantAdmins{
|
||||
LastLoginAt: gtime.Now(),
|
||||
LastLoginIp: ghttp.RequestFromCtx(ctx).RemoteAddr,
|
||||
})
|
||||
}(ctx, mAdminId)
|
||||
return
|
||||
}
|
||||
func (s *sMerchantAdmin) Info(ctx context.Context, in *model.MerchantAdminInfoIn) (out *model.MerchantAdminInfoOut, err error) {
|
||||
return
|
||||
}
|
||||
func (s *sMerchantAdmin) Code(ctx context.Context, in *model.MerchantAdminCodeIn) (out *model.MerchantAdminCodeOut, err error) {
|
||||
exist, err := dao.MerchantAdmins.Ctx(ctx).Where(do.MerchantAdmins{Phone: in.Phone}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Fail.Sub("该手机号已被注册")
|
||||
}
|
||||
// TODO 调用验证码服务发送验证码
|
||||
// 插入缓存,过期时间为 5 分钟
|
||||
if err = g.Redis().SetEX(ctx, "merchant_admin_code:"+in.Phone, grand.Digits(6), 5*60); err != nil {
|
||||
return nil, ecode.Fail.Sub("插入验证码缓存失败")
|
||||
}
|
||||
|
||||
return &model.MerchantAdminCodeOut{}, nil
|
||||
}
|
||||
|
||||
func (s *sMerchantAdmin) VertifyPhone(ctx context.Context, in *model.MerchantAdminVertifyPhoneIn) (out *model.MerchantAdminVertifyPhoneOut, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sMerchantAdmin) Register(ctx context.Context, in *model.MerchantAdminRegisterIn) (out *model.MerchantAdminRegisterOut, err error) {
|
||||
exist, err := dao.MerchantAdmins.Ctx(ctx).Where(do.MerchantAdmins{Username: in.Username}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Fail.Sub("该用户名已被注册")
|
||||
}
|
||||
// 验证码校验
|
||||
code, err := g.Redis().Get(ctx, "merchant_admin_code:"+in.Phone)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("获取验证码缓存失败")
|
||||
}
|
||||
if code.IsEmpty() {
|
||||
return nil, ecode.Fail.Sub("验证码已过期")
|
||||
}
|
||||
if code.String() != in.Code {
|
||||
return nil, ecode.Fail.Sub("验证码错误")
|
||||
}
|
||||
hashPass, err := utility.EncryptPassword(in.Password)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("密码加密失败")
|
||||
}
|
||||
if err = dao.MerchantAdmins.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
// 插入商户数据
|
||||
id, err := tx.Model(dao.Merchants.Table()).Data(do.Merchants{
|
||||
Name: fmt.Sprintf("%s的商铺", in.Username),
|
||||
CreatedByType: consts.MerchantRegisterByAdmin,
|
||||
Status: consts.MerchantDisabledStatus,
|
||||
AuditStatus: consts.MerchantPendingReview,
|
||||
ContactPhone: in.Phone,
|
||||
ApplicationReason: in.ApplicationReason,
|
||||
}).InsertAndGetId()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("插入商户数据失败")
|
||||
}
|
||||
|
||||
// 插入商户管理员数据
|
||||
if _, err = tx.Model(dao.MerchantAdmins.Table()).Data(do.MerchantAdmins{
|
||||
MerchantId: id,
|
||||
PasswordHash: hashPass,
|
||||
Phone: in.Phone,
|
||||
IsPrimary: true,
|
||||
Username: in.Username,
|
||||
Status: consts.MerchantAdministratorEnable,
|
||||
}).Insert(); err != nil {
|
||||
return ecode.Fail.Sub("插入商户管理员数据失败")
|
||||
}
|
||||
return
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g.Redis().Del(ctx, "merchant_admin_code:"+in.Phone)
|
||||
return &model.MerchantAdminRegisterOut{Success: true}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user