实现商户注册、管理员审核商户申请接口

This commit is contained in:
2025-06-04 15:10:56 +08:00
parent 00b889cfcc
commit caf3d42fe5
63 changed files with 1195 additions and 294 deletions

View File

@ -37,7 +37,7 @@ func checkAdmin() {
Name: "系统管理员",
Code: consts.AdminRoleCode,
Description: "管理员角色",
Status: 1,
Status: consts.RoleEnable,
IsDeletable: false,
})
} else {

View File

@ -7,9 +7,8 @@ package logic
import (
_ "server/internal/logic/admin"
_ "server/internal/logic/menu"
_ "server/internal/logic/merchant"
_ "server/internal/logic/merchantAdmin"
_ "server/internal/logic/role"
_ "server/internal/logic/roleMenu"
_ "server/internal/logic/storeAdmin"
_ "server/internal/logic/user"
)

View File

@ -0,0 +1,83 @@
package merchant
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/os/gtime"
"server/internal/consts"
"server/internal/dao"
"server/internal/model"
"server/internal/model/do"
"server/internal/service"
"server/utility/ecode"
)
type sMerchant struct {
}
func New() service.IMerchant {
return &sMerchant{}
}
func init() {
service.RegisterMerchant(New())
}
func (s *sMerchant) List(ctx context.Context, in *model.MerchantListIn) (out *model.MerchantListOut, err error) {
list := make([]model.Merchant, 0)
var total int
orm := dao.Merchants.Ctx(ctx)
if in.Status != 0 {
orm = orm.Where(dao.Merchants.Columns().Status, in.Status)
}
if in.AuditStatus != 0 {
orm = orm.Where(dao.Merchants.Columns().AuditStatus, in.AuditStatus)
}
if err = orm.Page(in.Page, in.Size).ScanAndCount(&list, &total, false); err != nil {
return nil, ecode.Fail.Sub("查询商户列表失败")
}
return &model.MerchantListOut{
List: list,
Total: total,
}, nil
}
func (s *sMerchant) Audit(ctx context.Context, in *model.MerchantAuditIn) (out *model.MerchantAuditOut, err error) {
merchant, err := dao.Merchants.Ctx(ctx).WherePri(in.Id).One()
if err != nil {
return nil, ecode.Fail.Sub("查询商户失败")
}
if merchant.IsEmpty() {
return nil, ecode.Params.Sub("商户不存在")
}
if merchant[dao.Merchants.Columns().AuditStatus].Int() != 0 {
return nil, ecode.Params.Sub("商户已审核")
}
if err = dao.Merchants.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
if _, err = tx.Model(dao.Merchants.Table()).WherePri(in.Id).Data(do.Merchants{
AuditBy: in.AdminId,
AuditRemark: in.AuditRemark,
AuditStatus: in.AuditStatus,
AuditAt: gtime.Now(),
Status: in.AuditStatus, // 暂定审核通过商户即可使用
RejectReason: in.RejectReason,
}).OmitEmptyData().Update(); err != nil {
return ecode.Fail.Sub("审核商户失败")
}
if _, err = tx.Model(dao.MerchantAdmins.Table()).Where(do.MerchantAdmins{MerchantId: in.Id, IsPrimary: true}).Data(do.MerchantAdmins{
Status: consts.MerchantAdministratorEnable,
}).Update(); err != nil {
return ecode.Fail.Sub("审核商户失败")
}
return nil
}); err != nil {
return nil, err
}
return
}
func (s *sMerchant) Create(ctx context.Context, in *model.MerchantCreateIn) (out *model.CreateOut, err error) {
return
}

View File

@ -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
}

View File

@ -1,28 +0,0 @@
package roleMenu
import (
"context"
"server/internal/model"
"server/internal/service"
)
type sRoleMenu struct {
}
func New() service.IRoleMenu {
return &sRoleMenu{}
}
func init() {
service.RegisterRoleMenu(New())
}
func init() {
}
func (s *sRoleMenu) List(ctx context.Context, in *model.RoleMenuListInput) (out *model.MenuListOutput, err error) {
return
}
func (s *sRoleMenu) Save(ctx context.Context, in *model.RoleMenuSaveInput) (out *model.UpdateOut, err error) {
return
}

View File

@ -1 +0,0 @@
package storeAdmin

View File

@ -33,7 +33,7 @@ func checkUserRole() {
Name: "用户",
Code: consts.UserRoleCode,
Description: "用户角色",
Status: 1,
Status: consts.RoleEnable,
IsDeletable: false,
}).Insert()
if err != nil {