fadd
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminAppendQueryWhitelistLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminAppendQueryWhitelistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminAppendQueryWhitelistLogic {
|
||||
return &AdminAppendQueryWhitelistLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminAppendQueryWhitelistLogic) AdminAppendQueryWhitelist(req *types.AdminAppendQueryWhitelistReq) (resp *types.AdminAppendQueryWhitelistResp, err error) {
|
||||
if err = validateQueryWhitelistReq(req.IdCard, req.ApiCodes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiResp, err := callQueryWhitelistMgmt(
|
||||
l.svcCtx,
|
||||
"/api/v1/query-whitelist/entries/append",
|
||||
req.Name,
|
||||
req.IdCard,
|
||||
req.ApiCodes,
|
||||
req.Remark,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
recordQueryWhitelistOpLog(l.ctx, l.svcCtx, "append", req.Name, req.IdCard, req.ApiCodes, req.Remark, apiResp)
|
||||
|
||||
return toAppendResp(apiResp), nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminCreateQueryWhitelistLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminCreateQueryWhitelistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminCreateQueryWhitelistLogic {
|
||||
return &AdminCreateQueryWhitelistLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminCreateQueryWhitelistLogic) AdminCreateQueryWhitelist(req *types.AdminCreateQueryWhitelistReq) (resp *types.AdminCreateQueryWhitelistResp, err error) {
|
||||
if err = validateQueryWhitelistReq(req.IdCard, req.ApiCodes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiResp, err := callQueryWhitelistMgmt(
|
||||
l.svcCtx,
|
||||
"/api/v1/query-whitelist/entries",
|
||||
req.Name,
|
||||
req.IdCard,
|
||||
req.ApiCodes,
|
||||
req.Remark,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
recordQueryWhitelistOpLog(l.ctx, l.svcCtx, "create", req.Name, req.IdCard, req.ApiCodes, req.Remark, apiResp)
|
||||
|
||||
return toCreateResp(apiResp), nil
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminGetQueryWhitelistOpLogListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminGetQueryWhitelistOpLogListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetQueryWhitelistOpLogListLogic {
|
||||
return &AdminGetQueryWhitelistOpLogListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminGetQueryWhitelistOpLogListLogic) AdminGetQueryWhitelistOpLogList(req *types.AdminGetQueryWhitelistOpLogListReq) (resp *types.AdminGetQueryWhitelistOpLogListResp, err error) {
|
||||
builder := l.svcCtx.QueryWhitelistOpLogModel.SelectBuilder()
|
||||
|
||||
if req.IdCard != nil && *req.IdCard != "" {
|
||||
builder = builder.Where("id_card LIKE ?", "%"+*req.IdCard+"%")
|
||||
}
|
||||
if req.Action != nil && *req.Action != "" {
|
||||
builder = builder.Where("action = ?", *req.Action)
|
||||
}
|
||||
if req.TianyuanCode != nil {
|
||||
builder = builder.Where("tianyuan_code = ?", *req.TianyuanCode)
|
||||
}
|
||||
|
||||
total, err := l.svcCtx.QueryWhitelistOpLogModel.FindCount(l.ctx, builder, "id")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录总数失败, %v", err)
|
||||
}
|
||||
|
||||
list, err := l.svcCtx.QueryWhitelistOpLogModel.FindPageListByPage(l.ctx, builder, req.Page, req.PageSize, "create_time DESC")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录列表失败, %v", err)
|
||||
}
|
||||
|
||||
adminNameMap := l.buildAdminUsernameMap(list)
|
||||
items := make([]types.AdminQueryWhitelistOpLogItem, 0, len(list))
|
||||
for _, row := range list {
|
||||
items = append(items, types.AdminQueryWhitelistOpLogItem{
|
||||
Id: row.Id,
|
||||
AdminUserId: row.AdminUserId,
|
||||
AdminUsername: adminNameMap[row.AdminUserId],
|
||||
Action: row.Action,
|
||||
ActionText: queryWhitelistActionText(row.Action),
|
||||
Name: row.Name,
|
||||
IdCard: row.IdCard,
|
||||
IdCardMasked: row.IdCardMasked.String,
|
||||
ApiCodes: parseApiCodesJSON(row.ApiCodes),
|
||||
Remark: row.Remark.String,
|
||||
TianyuanCode: row.TianyuanCode,
|
||||
TianyuanMessage: row.TianyuanMessage.String,
|
||||
TransactionId: row.TransactionId.String,
|
||||
EntryId: row.EntryId.String,
|
||||
EntryStatus: row.EntryStatus.String,
|
||||
EntryApiCodes: parseApiCodesJSON(row.EntryApiCodes.String),
|
||||
CreateTime: row.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &types.AdminGetQueryWhitelistOpLogListResp{
|
||||
Total: total,
|
||||
Items: items,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *AdminGetQueryWhitelistOpLogListLogic) buildAdminUsernameMap(list []*model.QueryWhitelistOpLog) map[string]string {
|
||||
nameMap := make(map[string]string)
|
||||
if len(list) == 0 {
|
||||
return nameMap
|
||||
}
|
||||
|
||||
adminIds := make([]string, 0, len(list))
|
||||
seen := make(map[string]struct{}, len(list))
|
||||
for _, row := range list {
|
||||
if row.AdminUserId == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[row.AdminUserId]; ok {
|
||||
continue
|
||||
}
|
||||
seen[row.AdminUserId] = struct{}{}
|
||||
adminIds = append(adminIds, row.AdminUserId)
|
||||
}
|
||||
if len(adminIds) == 0 {
|
||||
return nameMap
|
||||
}
|
||||
|
||||
builder := l.svcCtx.AdminUserModel.SelectBuilder().Where(squirrel.Eq{"id": adminIds})
|
||||
users, err := l.svcCtx.AdminUserModel.FindAll(l.ctx, builder, "")
|
||||
if err != nil {
|
||||
l.Errorf("批量查询管理员名称失败: %v", err)
|
||||
return nameMap
|
||||
}
|
||||
for _, user := range users {
|
||||
displayName := user.RealName
|
||||
if displayName == "" {
|
||||
displayName = user.Username
|
||||
}
|
||||
nameMap[user.Id] = displayName
|
||||
}
|
||||
return nameMap
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
tianyuanapi "ycc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/ctxdata"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
var idCardPattern = regexp.MustCompile(`^\d{17}[\dXx]$`)
|
||||
|
||||
func validateQueryWhitelistReq(idCard string, apiCodes []string) error {
|
||||
if strings.TrimSpace(idCard) == "" {
|
||||
return errors.Wrapf(xerr.NewErrMsg("身份证号不能为空"), "")
|
||||
}
|
||||
if !idCardPattern.MatchString(strings.TrimSpace(idCard)) {
|
||||
return errors.Wrapf(xerr.NewErrMsg("身份证号格式不正确,需为18位中国大陆身份证号"), "")
|
||||
}
|
||||
if len(apiCodes) == 0 {
|
||||
return errors.Wrapf(xerr.NewErrMsg("请至少选择一个产品编码"), "")
|
||||
}
|
||||
for _, code := range apiCodes {
|
||||
if strings.TrimSpace(code) == "" {
|
||||
return errors.Wrapf(xerr.NewErrMsg("产品编码不能为空"), "")
|
||||
}
|
||||
if code == "*" {
|
||||
return errors.Wrapf(xerr.NewErrMsg("产品编码不支持通配符 *"), "")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildQueryWhitelistPayload(name, idCard string, apiCodes []string, remark string) map[string]interface{} {
|
||||
payload := map[string]interface{}{
|
||||
"name": name,
|
||||
"id_card": strings.TrimSpace(idCard),
|
||||
"api_codes": apiCodes,
|
||||
}
|
||||
if strings.TrimSpace(remark) != "" {
|
||||
payload["remark"] = strings.TrimSpace(remark)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func callQueryWhitelistMgmt(
|
||||
svcCtx *svc.ServiceContext,
|
||||
apiPath string,
|
||||
name, idCard string,
|
||||
apiCodes []string,
|
||||
remark string,
|
||||
) (*tianyuanapi.QueryWhitelistMgmtResponse, error) {
|
||||
mgmtKey := svcCtx.Config.Tianyuanapi.WhitelistMgmtKey
|
||||
if mgmtKey == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("查询白名单管理密钥未配置,请在服务端配置 Tianyuanapi.WhitelistMgmtKey"), "")
|
||||
}
|
||||
if svcCtx.TianyuanapiClient == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("天远 API 客户端未初始化"), "")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(name) == "" {
|
||||
name = "*"
|
||||
}
|
||||
|
||||
payload := buildQueryWhitelistPayload(name, idCard, apiCodes, remark)
|
||||
return svcCtx.TianyuanapiClient.CallQueryWhitelistMgmt(apiPath, payload, mgmtKey)
|
||||
}
|
||||
|
||||
func toAdminEntry(entry *tianyuanapi.QueryWhitelistEntry) *types.AdminQueryWhitelistEntry {
|
||||
if entry == nil {
|
||||
return nil
|
||||
}
|
||||
return &types.AdminQueryWhitelistEntry{
|
||||
Id: entry.ID,
|
||||
Name: entry.Name,
|
||||
IdCardMasked: entry.IdCardMasked,
|
||||
ApiCodes: entry.ApiCodes,
|
||||
Status: entry.Status,
|
||||
Remark: entry.Remark,
|
||||
CreatedAt: entry.CreatedAt,
|
||||
UpdatedAt: entry.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func toCreateResp(resp *tianyuanapi.QueryWhitelistMgmtResponse) *types.AdminCreateQueryWhitelistResp {
|
||||
return &types.AdminCreateQueryWhitelistResp{
|
||||
Code: resp.Code,
|
||||
Message: resp.Message,
|
||||
TransactionId: resp.TransactionID,
|
||||
Entry: toAdminEntry(resp.Entry),
|
||||
}
|
||||
}
|
||||
|
||||
func toAppendResp(resp *tianyuanapi.QueryWhitelistMgmtResponse) *types.AdminAppendQueryWhitelistResp {
|
||||
return &types.AdminAppendQueryWhitelistResp{
|
||||
Code: resp.Code,
|
||||
Message: resp.Message,
|
||||
TransactionId: resp.TransactionID,
|
||||
Entry: toAdminEntry(resp.Entry),
|
||||
}
|
||||
}
|
||||
|
||||
func resolveName(name string) string {
|
||||
if strings.TrimSpace(name) == "" {
|
||||
return "*"
|
||||
}
|
||||
return strings.TrimSpace(name)
|
||||
}
|
||||
|
||||
func parseApiCodesJSON(raw string) []string {
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
var codes []string
|
||||
if err := json.Unmarshal([]byte(raw), &codes); err != nil {
|
||||
return nil
|
||||
}
|
||||
return codes
|
||||
}
|
||||
|
||||
func queryWhitelistActionText(action string) string {
|
||||
switch action {
|
||||
case "create":
|
||||
return "创建规则"
|
||||
case "append":
|
||||
return "追加接口"
|
||||
default:
|
||||
return action
|
||||
}
|
||||
}
|
||||
|
||||
func recordQueryWhitelistOpLog(
|
||||
ctx context.Context,
|
||||
svcCtx *svc.ServiceContext,
|
||||
action, name, idCard string,
|
||||
apiCodes []string,
|
||||
remark string,
|
||||
apiResp *tianyuanapi.QueryWhitelistMgmtResponse,
|
||||
) {
|
||||
adminUserId, err := ctxdata.GetUidFromCtx(ctx)
|
||||
if err != nil {
|
||||
logx.WithContext(ctx).Errorf("记录查询白名单操作日志失败: 获取管理员ID失败, %v", err)
|
||||
adminUserId = ""
|
||||
}
|
||||
|
||||
apiCodesJSON, marshalErr := json.Marshal(apiCodes)
|
||||
if marshalErr != nil {
|
||||
logx.WithContext(ctx).Errorf("记录查询白名单操作日志失败: 序列化 api_codes 失败, %v", marshalErr)
|
||||
return
|
||||
}
|
||||
|
||||
log := &model.QueryWhitelistOpLog{
|
||||
AdminUserId: adminUserId,
|
||||
Action: action,
|
||||
Name: resolveName(name),
|
||||
IdCard: strings.TrimSpace(idCard),
|
||||
ApiCodes: string(apiCodesJSON),
|
||||
TianyuanCode: int64(apiResp.Code),
|
||||
}
|
||||
|
||||
if strings.TrimSpace(remark) != "" {
|
||||
log.Remark = sql.NullString{String: strings.TrimSpace(remark), Valid: true}
|
||||
}
|
||||
if apiResp.Message != "" {
|
||||
log.TianyuanMessage = sql.NullString{String: apiResp.Message, Valid: true}
|
||||
}
|
||||
if apiResp.TransactionID != "" {
|
||||
log.TransactionId = sql.NullString{String: apiResp.TransactionID, Valid: true}
|
||||
}
|
||||
if apiResp.Entry != nil {
|
||||
if apiResp.Entry.IdCardMasked != "" {
|
||||
log.IdCardMasked = sql.NullString{String: apiResp.Entry.IdCardMasked, Valid: true}
|
||||
}
|
||||
if apiResp.Entry.ID != "" {
|
||||
log.EntryId = sql.NullString{String: apiResp.Entry.ID, Valid: true}
|
||||
}
|
||||
if apiResp.Entry.Status != "" {
|
||||
log.EntryStatus = sql.NullString{String: apiResp.Entry.Status, Valid: true}
|
||||
}
|
||||
if len(apiResp.Entry.ApiCodes) > 0 {
|
||||
entryCodesJSON, _ := json.Marshal(apiResp.Entry.ApiCodes)
|
||||
log.EntryApiCodes = sql.NullString{String: string(entryCodesJSON), Valid: true}
|
||||
}
|
||||
}
|
||||
|
||||
if _, insertErr := svcCtx.QueryWhitelistOpLogModel.Insert(ctx, nil, log); insertErr != nil {
|
||||
logx.WithContext(ctx).Errorf("记录查询白名单操作日志失败: %v", insertErr)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user