Files
2026-05-20 16:13:58 +08:00

208 lines
4.5 KiB
Go

package adminSer
import (
"context"
"encoding/base32"
"fmt"
"stat/middleware/define"
"time"
"stat/api/proto"
"stat/common"
"stat/common/log"
"stat/common/stdLog"
"stat/models/adminMod"
"stat/models/authorityMod"
)
func AdminLogin(ctx context.Context, param proto.AdminVerifyReq) (code stdLog.CodeType, data adminMod.Admin, err error) {
res, err := adminMod.FindOneByAccount(ctx, *param.Account)
if err != nil || res.Account == "" {
code = stdLog.AdminAccountNotExists
return
}
// 绑定两步验证
if !res.HasBind {
_ = adminMod.TwoStepBind(ctx, res.Account)
}
// 首次登陆,生成token
roleInfo, err := authorityMod.GetAuthorityById(ctx, res.RoleId)
if err != nil {
log.Error("GetAuthorityById failed", log.E(err))
return stdLog.Failure, data, err
}
// 生成token
claims := define.Claims{
Type: define.Admin,
Name: res.Account,
Role: roleInfo.Role,
CID: res.MerchantId,
}
newToken, err := define.GenToken(&claims)
if err != nil {
log.Error("GenerateUserToken failed", log.E(err))
code = stdLog.Failure
return code, data, err
}
res.Token = newToken
// 刷新token
if err = adminMod.RefreshToken(ctx, res.Account, newToken); err != nil {
code = stdLog.Failure
return code, data, err
}
data = res
code = stdLog.Success
return
}
// 管理员列表
func AdminList(ctx context.Context, param proto.AdminListReq) (code stdLog.CodeType, data interface{}, err error) {
skip := (param.PageNum - 1) * param.PageSize
limit := param.PageSize
resp := proto.AdminListRes{
Total: 0,
List: make([]proto.AdminInfo, 0),
}
count, adminList, err := adminMod.FindAdminList(ctx, param.BlocRole, skip, limit)
if err != nil {
code = stdLog.Failure
return
}
authIds := make([]int64, 0)
for _, v := range adminList {
authIds = append(authIds, v.RoleId)
}
// 获取权限角色
authList, err := authorityMod.GetAuthorityByIds(ctx, authIds)
if err != nil {
code = stdLog.Failure
return
}
authMap := make(map[int64]*authorityMod.Authority)
for _, v := range authList {
_v := v
authMap[v.Id] = &_v
}
for _, v := range adminList {
role := ""
if roleInfo, ok := authMap[v.RoleId]; ok {
role = roleInfo.Role
}
tmp := proto.AdminInfo{
RoleId: v.RoleId,
Role: role,
MerchantId: v.MerchantId,
Account: v.Account,
Password: v.Password,
Name: v.Name,
CreatedAt: v.CreatedAt,
UpdatedAt: v.UpdatedAt,
LastTime: v.LastTime,
}
resp.List = append(resp.List, tmp)
}
resp.Total = count
data = resp
code = stdLog.Success
return
}
// 添加管理员账号
func AddAdmin(ctx context.Context, param proto.AddAdminReq) (code stdLog.CodeType, data interface{}, err error) {
secret := base32.StdEncoding.EncodeToString([]byte(fmt.Sprintf("admin%s", common.RandStr(10))))
now := time.Now()
doc := adminMod.Admin{
Account: *param.Account,
Password: *param.Password,
Name: *param.Name,
RoleId: *param.RoleId,
HasBind: false,
Secret: secret,
CreatedAt: now,
UpdatedAt: now,
}
if param.MerchantId != nil {
doc.MerchantId = *param.MerchantId
}
if param.BlocRole != nil {
doc.BlocRole = *param.BlocRole
}
roleInfo, err := authorityMod.GetAuthorityById(ctx, doc.RoleId)
if err != nil {
log.Error("GetAuthorityById failed", log.E(err))
return
}
// 生成token
claims := define.Claims{
Type: define.Admin,
Name: doc.Account,
Role: roleInfo.Role,
CID: doc.MerchantId,
}
newToken, err := define.GenToken(&claims)
if err != nil {
log.Error("GenerateUserToken failed", log.E(err))
code = stdLog.Failure
return code, data, err
}
doc.Token = newToken
if err = adminMod.AddAdmin(ctx, &doc); err != nil {
code = stdLog.Failure
return
}
code = stdLog.Success
return
}
// 修改管理员账号
func UpdateAdmin(ctx context.Context, param proto.UpdateAdminReq) (code stdLog.CodeType, data interface{}, err error) {
now := time.Now()
doc := adminMod.UpdateDoc{
Account: param.Account,
Password: param.Password,
Name: param.Name,
RoleId: param.RoleId,
Status: param.Status,
MerchantId: param.MerchantId,
UpdatedAt: &now,
}
if err = adminMod.UpdateAdmin(ctx, *param.Account, doc); err != nil {
code = stdLog.Failure
return
}
code = stdLog.Success
return
}
// 修改管理员账号
func DelAdmin(ctx context.Context, param proto.DelAdminReq) (code stdLog.CodeType, data interface{}, err error) {
if err = adminMod.DelAdmin(ctx, *param.Account); err != nil {
code = stdLog.Failure
return
}
code = stdLog.Success
return
}