f
This commit is contained in:
80
app/main/api/desc/admin/admin_query_whitelist.api
Normal file
80
app/main/api/desc/admin/admin_query_whitelist.api
Normal file
@@ -0,0 +1,80 @@
|
||||
syntax = "v1"
|
||||
|
||||
info (
|
||||
title: "查询白名单管理"
|
||||
desc: "代理天远查询白名单接口,并记录操作日志"
|
||||
version: "v1"
|
||||
)
|
||||
|
||||
@server (
|
||||
prefix: /api/v1/admin/query-whitelist
|
||||
group: admin_query_whitelist
|
||||
middleware: AdminAuthInterceptor
|
||||
)
|
||||
service main {
|
||||
// 创建规则(代理天远 /entries)
|
||||
@handler AdminQueryWhitelistCreate
|
||||
post /create (AdminQueryWhitelistCreateReq) returns (AdminQueryWhitelistOpResp)
|
||||
|
||||
// 追加接口(代理天远 /entries/append)
|
||||
@handler AdminQueryWhitelistAppend
|
||||
post /append (AdminQueryWhitelistAppendReq) returns (AdminQueryWhitelistOpResp)
|
||||
|
||||
// 操作记录分页列表
|
||||
@handler AdminQueryWhitelistOpLogList
|
||||
get /op-log/list (AdminQueryWhitelistOpLogListReq) returns (AdminQueryWhitelistOpLogListResp)
|
||||
}
|
||||
|
||||
type (
|
||||
AdminQueryWhitelistCreateReq {
|
||||
Name string `json:"name"` // 姓名,* 表示只按身份证匹配
|
||||
IdCard string `json:"id_card"` // 18位身份证号
|
||||
ApiCodes []string `json:"api_codes"` // 产品编码列表
|
||||
Remark string `json:"remark,optional"` // 备注
|
||||
}
|
||||
AdminQueryWhitelistAppendReq {
|
||||
Name string `json:"name"` // 姓名,* 表示只按身份证匹配
|
||||
IdCard string `json:"id_card"` // 18位身份证号
|
||||
ApiCodes []string `json:"api_codes"` // 产品编码列表
|
||||
Remark string `json:"remark,optional"` // 备注
|
||||
}
|
||||
AdminQueryWhitelistOpResp {
|
||||
Success bool `json:"success"` // 天远是否业务成功
|
||||
TianyuanCode int `json:"tianyuan_code"` // 天远业务码
|
||||
TianyuanMessage string `json:"tianyuan_message"` // 天远返回描述
|
||||
TransactionId string `json:"transaction_id,optional"` // 天远流水号
|
||||
EntryId string `json:"entry_id,optional"` // 规则ID
|
||||
IdCardMasked string `json:"id_card_masked,optional"` // 脱敏身份证号
|
||||
EntryApiCodes []string `json:"entry_api_codes,optional"` // 规则当前产品编码
|
||||
EntryStatus string `json:"entry_status,optional"` // 规则状态
|
||||
}
|
||||
AdminQueryWhitelistOpLogListReq {
|
||||
Page int64 `form:"page,default=1"`
|
||||
PageSize int64 `form:"pageSize,default=20"`
|
||||
IdCard *string `form:"id_card,optional"`
|
||||
Action *string `form:"action,optional"`
|
||||
TianyuanCode *int `form:"tianyuan_code,optional"`
|
||||
}
|
||||
QueryWhitelistOpLogItem {
|
||||
Id int64 `json:"id"`
|
||||
AdminUserId int64 `json:"admin_user_id"`
|
||||
AdminUserName string `json:"admin_user_name"`
|
||||
Action string `json:"action"`
|
||||
Name string `json:"name"`
|
||||
IdCard string `json:"id_card"`
|
||||
IdCardMasked string `json:"id_card_masked"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark"`
|
||||
TianyuanCode int `json:"tianyuan_code"`
|
||||
TianyuanMessage string `json:"tianyuan_message"`
|
||||
TransactionId string `json:"transaction_id"`
|
||||
EntryId string `json:"entry_id"`
|
||||
EntryStatus string `json:"entry_status"`
|
||||
EntryApiCodes []string `json:"entry_api_codes"`
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
AdminQueryWhitelistOpLogListResp {
|
||||
Total int64 `json:"total"`
|
||||
Items []QueryWhitelistOpLogItem `json:"items"`
|
||||
}
|
||||
)
|
||||
@@ -30,3 +30,4 @@ import "./admin/admin_agent.api"
|
||||
import "./admin/admin_api.api"
|
||||
import "./admin/admin_role_api.api"
|
||||
import "./admin/admin_queue.api"
|
||||
import "./admin/admin_query_whitelist.api"
|
||||
|
||||
@@ -94,6 +94,7 @@ Tianyuanapi:
|
||||
Key: "04c6b4c559be6d5ba5351c04c8713a64"
|
||||
BaseURL: "https://api.tianyuanapi.com"
|
||||
Timeout: 60
|
||||
WhitelistMgmtKey: "2R12TmEc1e8P3p69RdIoN5Ykjk%H@4orPy7DZv7MXpGByoEL"
|
||||
Authorization:
|
||||
FileBaseURL: "https://www.tianyuandb.com/api/v1/auth-docs" # 授权书文件访问基础URL
|
||||
ExtensionTime: 24 # 佣金解冻延迟时间,单位:24小时
|
||||
|
||||
@@ -99,6 +99,7 @@ Tianyuanapi:
|
||||
Key: "04c6b4c559be6d5ba5351c04c8713a64"
|
||||
BaseURL: "https://api.tianyuanapi.com"
|
||||
Timeout: 60
|
||||
WhitelistMgmtKey: "2R12TmEc1e8P3p69RdIoN5Ykjk%H@4orPy7DZv7MXpGByoEL"
|
||||
Authorization:
|
||||
FileBaseURL: "https://www.tianyuandb.com/api/v1/auth-docs" # 授权书文件访问基础URL
|
||||
ExtensionTime: 24 # 佣金解冻延迟时间,单位:24小时
|
||||
@@ -129,10 +129,11 @@ type TaxConfig struct {
|
||||
TaxExemptionAmount float64
|
||||
}
|
||||
type TianyuanapiConfig struct {
|
||||
AccessID string
|
||||
Key string
|
||||
BaseURL string
|
||||
Timeout int64
|
||||
AccessID string
|
||||
Key string
|
||||
BaseURL string
|
||||
Timeout int64
|
||||
WhitelistMgmtKey string
|
||||
}
|
||||
|
||||
type AuthorizationConfig struct {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tydata-server/app/main/api/internal/logic/admin_query_whitelist"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/result"
|
||||
"tydata-server/pkg/lzkit/validator"
|
||||
)
|
||||
|
||||
func AdminQueryWhitelistAppendHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.AdminQueryWhitelistAppendReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
l := admin_query_whitelist.NewAdminQueryWhitelistAppendLogic(r.Context(), svcCtx)
|
||||
resp, err := l.AdminQueryWhitelistAppend(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tydata-server/app/main/api/internal/logic/admin_query_whitelist"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/result"
|
||||
"tydata-server/pkg/lzkit/validator"
|
||||
)
|
||||
|
||||
func AdminQueryWhitelistCreateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.AdminQueryWhitelistCreateReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
l := admin_query_whitelist.NewAdminQueryWhitelistCreateLogic(r.Context(), svcCtx)
|
||||
resp, err := l.AdminQueryWhitelistCreate(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tydata-server/app/main/api/internal/logic/admin_query_whitelist"
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/result"
|
||||
"tydata-server/pkg/lzkit/validator"
|
||||
)
|
||||
|
||||
func AdminQueryWhitelistOpLogListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.AdminQueryWhitelistOpLogListReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate(req); err != nil {
|
||||
result.ParamValidateErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
l := admin_query_whitelist.NewAdminQueryWhitelistOpLogListLogic(r.Context(), svcCtx)
|
||||
resp, err := l.AdminQueryWhitelistOpLogList(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
admin_product "tydata-server/app/main/api/internal/handler/admin_product"
|
||||
admin_promotion "tydata-server/app/main/api/internal/handler/admin_promotion"
|
||||
admin_query "tydata-server/app/main/api/internal/handler/admin_query"
|
||||
admin_query_whitelist "tydata-server/app/main/api/internal/handler/admin_query_whitelist"
|
||||
admin_role "tydata-server/app/main/api/internal/handler/admin_role"
|
||||
admin_role_api "tydata-server/app/main/api/internal/handler/admin_role_api"
|
||||
admin_user "tydata-server/app/main/api/internal/handler/admin_user"
|
||||
@@ -603,6 +604,30 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
rest.WithPrefix("/api/v1/admin/query"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.AdminAuthInterceptor},
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/append",
|
||||
Handler: admin_query_whitelist.AdminQueryWhitelistAppendHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/create",
|
||||
Handler: admin_query_whitelist.AdminQueryWhitelistCreateHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/op-log/list",
|
||||
Handler: admin_query_whitelist.AdminQueryWhitelistOpLogListHandler(serverCtx),
|
||||
},
|
||||
}...,
|
||||
),
|
||||
rest.WithPrefix("/api/v1/admin/query-whitelist"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.AdminAuthInterceptor},
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
tianyuanapi "tydata-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminQueryWhitelistAppendLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminQueryWhitelistAppendLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminQueryWhitelistAppendLogic {
|
||||
return &AdminQueryWhitelistAppendLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminQueryWhitelistAppendLogic) AdminQueryWhitelistAppend(req *types.AdminQueryWhitelistAppendReq) (resp *types.AdminQueryWhitelistOpResp, err error) {
|
||||
return l.executeWhitelistOp(whitelistOpInput{
|
||||
Action: actionAppend,
|
||||
Name: req.Name,
|
||||
IdCard: req.IdCard,
|
||||
ApiCodes: req.ApiCodes,
|
||||
Remark: req.Remark,
|
||||
}, func(client *tianyuanapi.Client, payload tianyuanapi.WhitelistEntryPayload, mgmtKey string) (*tianyuanapi.WhitelistResponse, error) {
|
||||
return client.AppendWhitelistEntry(payload, mgmtKey)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
tianyuanapi "tydata-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminQueryWhitelistCreateLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminQueryWhitelistCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminQueryWhitelistCreateLogic {
|
||||
return &AdminQueryWhitelistCreateLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminQueryWhitelistCreateLogic) AdminQueryWhitelistCreate(req *types.AdminQueryWhitelistCreateReq) (resp *types.AdminQueryWhitelistOpResp, err error) {
|
||||
return l.executeWhitelistOp(whitelistOpInput{
|
||||
Action: actionCreate,
|
||||
Name: req.Name,
|
||||
IdCard: req.IdCard,
|
||||
ApiCodes: req.ApiCodes,
|
||||
Remark: req.Remark,
|
||||
}, func(client *tianyuanapi.Client, payload tianyuanapi.WhitelistEntryPayload, mgmtKey string) (*tianyuanapi.WhitelistResponse, error) {
|
||||
return client.CreateWhitelistEntry(payload, mgmtKey)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/globalkey"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/mr"
|
||||
)
|
||||
|
||||
type AdminQueryWhitelistOpLogListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminQueryWhitelistOpLogListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminQueryWhitelistOpLogListLogic {
|
||||
return &AdminQueryWhitelistOpLogListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminQueryWhitelistOpLogListLogic) AdminQueryWhitelistOpLogList(req *types.AdminQueryWhitelistOpLogListReq) (resp *types.AdminQueryWhitelistOpLogListResp, err error) {
|
||||
builder := l.svcCtx.QueryWhitelistOpLogModel.SelectBuilder().
|
||||
Where("del_state = ?", globalkey.DelStateNo)
|
||||
|
||||
if req.IdCard != nil && *req.IdCard != "" {
|
||||
builder = builder.Where("id_card = ?", *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)
|
||||
}
|
||||
|
||||
var total int64
|
||||
var logs []*model.QueryWhitelistOpLog
|
||||
err = mr.Finish(func() error {
|
||||
var err error
|
||||
total, err = l.svcCtx.QueryWhitelistOpLogModel.FindCount(l.ctx, builder, "id")
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录总数失败 err: %v", err)
|
||||
}
|
||||
return nil
|
||||
}, func() error {
|
||||
var err error
|
||||
logs, err = l.svcCtx.QueryWhitelistOpLogModel.FindPageListByPage(l.ctx, builder, req.Page, req.PageSize, "id DESC")
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询操作记录列表失败 err: %v", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
adminNameMap := make(map[int64]string)
|
||||
resp = &types.AdminQueryWhitelistOpLogListResp{
|
||||
Total: total,
|
||||
Items: make([]types.QueryWhitelistOpLogItem, 0, len(logs)),
|
||||
}
|
||||
|
||||
for _, log := range logs {
|
||||
adminName := ""
|
||||
if cached, ok := adminNameMap[log.AdminUserId]; ok {
|
||||
adminName = cached
|
||||
} else {
|
||||
adminUser, findErr := l.svcCtx.AdminUserModel.FindOne(l.ctx, log.AdminUserId)
|
||||
if findErr == nil && adminUser != nil {
|
||||
adminName = adminUser.RealName
|
||||
if adminName == "" {
|
||||
adminName = adminUser.Username
|
||||
}
|
||||
}
|
||||
adminNameMap[log.AdminUserId] = adminName
|
||||
}
|
||||
|
||||
item := types.QueryWhitelistOpLogItem{
|
||||
Id: log.Id,
|
||||
AdminUserId: log.AdminUserId,
|
||||
AdminUserName: adminName,
|
||||
Action: log.Action,
|
||||
Name: log.Name,
|
||||
IdCard: log.IdCard,
|
||||
IdCardMasked: log.IdCardMasked.String,
|
||||
ApiCodes: parseJSONStringArray(log.ApiCodes),
|
||||
Remark: log.Remark.String,
|
||||
TianyuanCode: int(log.TianyuanCode),
|
||||
TianyuanMessage: log.TianyuanMessage.String,
|
||||
TransactionId: log.TransactionId.String,
|
||||
EntryId: log.EntryId.String,
|
||||
EntryStatus: log.EntryStatus.String,
|
||||
EntryApiCodes: parseJSONStringArray(log.EntryApiCodes.String),
|
||||
CreateTime: log.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
resp.Items = append(resp.Items, item)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
157
app/main/api/internal/logic/admin_query_whitelist/ops_helper.go
Normal file
157
app/main/api/internal/logic/admin_query_whitelist/ops_helper.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package admin_query_whitelist
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
tianyuanapi "tydata-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"tydata-server/common/ctxdata"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
actionCreate = "create"
|
||||
actionAppend = "append"
|
||||
)
|
||||
|
||||
var idCardPattern = regexp.MustCompile(`^\d{17}[\dXx]$`)
|
||||
|
||||
type whitelistOpInput struct {
|
||||
Action string
|
||||
Name string
|
||||
IdCard string
|
||||
ApiCodes []string
|
||||
Remark string
|
||||
}
|
||||
|
||||
func validateWhitelistInput(input whitelistOpInput) error {
|
||||
name := strings.TrimSpace(input.Name)
|
||||
if name == "" {
|
||||
return xerr.NewErrMsg("姓名不能为空")
|
||||
}
|
||||
idCard := strings.TrimSpace(input.IdCard)
|
||||
if !idCardPattern.MatchString(idCard) {
|
||||
return xerr.NewErrMsg("身份证号格式不正确")
|
||||
}
|
||||
if len(input.ApiCodes) == 0 {
|
||||
return xerr.NewErrMsg("产品编码不能为空")
|
||||
}
|
||||
for _, code := range input.ApiCodes {
|
||||
if strings.TrimSpace(code) == "" {
|
||||
return xerr.NewErrMsg("产品编码不能为空")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *AdminQueryWhitelistCreateLogic) executeWhitelistOp(input whitelistOpInput, call func(client *tianyuanapi.Client, payload tianyuanapi.WhitelistEntryPayload, mgmtKey string) (*tianyuanapi.WhitelistResponse, error)) (*types.AdminQueryWhitelistOpResp, error) {
|
||||
return executeWhitelistOp(l.ctx, l.svcCtx, input, call)
|
||||
}
|
||||
|
||||
func (l *AdminQueryWhitelistAppendLogic) executeWhitelistOp(input whitelistOpInput, call func(client *tianyuanapi.Client, payload tianyuanapi.WhitelistEntryPayload, mgmtKey string) (*tianyuanapi.WhitelistResponse, error)) (*types.AdminQueryWhitelistOpResp, error) {
|
||||
return executeWhitelistOp(l.ctx, l.svcCtx, input, call)
|
||||
}
|
||||
|
||||
func executeWhitelistOp(
|
||||
ctx context.Context,
|
||||
svcCtx *svc.ServiceContext,
|
||||
input whitelistOpInput,
|
||||
call func(client *tianyuanapi.Client, payload tianyuanapi.WhitelistEntryPayload, mgmtKey string) (*tianyuanapi.WhitelistResponse, error),
|
||||
) (*types.AdminQueryWhitelistOpResp, error) {
|
||||
if err := validateWhitelistInput(input); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
adminUserId, err := ctxdata.GetUidFromCtx(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取管理员信息失败, %+v", err)
|
||||
}
|
||||
|
||||
mgmtKey := strings.TrimSpace(svcCtx.Config.Tianyuanapi.WhitelistMgmtKey)
|
||||
if mgmtKey == "" {
|
||||
return nil, xerr.NewErrMsg("未配置天远白名单管理密钥")
|
||||
}
|
||||
if svcCtx.TianyuanapiClient == nil {
|
||||
return nil, xerr.NewErrMsg("天远API客户端未初始化")
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(input.Name)
|
||||
idCard := strings.TrimSpace(strings.ToUpper(input.IdCard))
|
||||
apiCodes := make([]string, 0, len(input.ApiCodes))
|
||||
for _, code := range input.ApiCodes {
|
||||
apiCodes = append(apiCodes, strings.TrimSpace(code))
|
||||
}
|
||||
|
||||
payload := tianyuanapi.WhitelistEntryPayload{
|
||||
Name: name,
|
||||
IdCard: idCard,
|
||||
ApiCodes: apiCodes,
|
||||
Remark: strings.TrimSpace(input.Remark),
|
||||
}
|
||||
|
||||
apiResp, err := call(svcCtx.TianyuanapiClient, payload, mgmtKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "调用天远白名单接口失败, %+v", err)
|
||||
}
|
||||
|
||||
apiCodesJSON, _ := json.Marshal(apiCodes)
|
||||
opLog := &model.QueryWhitelistOpLog{
|
||||
AdminUserId: adminUserId,
|
||||
Action: input.Action,
|
||||
Name: name,
|
||||
IdCard: idCard,
|
||||
ApiCodes: string(apiCodesJSON),
|
||||
TianyuanCode: int64(apiResp.Code),
|
||||
TianyuanMessage: sql.NullString{String: apiResp.Message, Valid: apiResp.Message != ""},
|
||||
TransactionId: sql.NullString{String: apiResp.TransactionID, Valid: apiResp.TransactionID != ""},
|
||||
}
|
||||
if payload.Remark != "" {
|
||||
opLog.Remark = sql.NullString{String: payload.Remark, Valid: true}
|
||||
}
|
||||
if apiResp.Data != nil {
|
||||
opLog.IdCardMasked = sql.NullString{String: apiResp.Data.IdCardMasked, Valid: apiResp.Data.IdCardMasked != ""}
|
||||
opLog.EntryId = sql.NullString{String: apiResp.Data.Id, Valid: apiResp.Data.Id != ""}
|
||||
opLog.EntryStatus = sql.NullString{String: apiResp.Data.Status, Valid: apiResp.Data.Status != ""}
|
||||
if len(apiResp.Data.ApiCodes) > 0 {
|
||||
entryCodesJSON, _ := json.Marshal(apiResp.Data.ApiCodes)
|
||||
opLog.EntryApiCodes = sql.NullString{String: string(entryCodesJSON), Valid: true}
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = svcCtx.QueryWhitelistOpLogModel.Insert(ctx, nil, opLog); err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "保存操作记录失败, %+v", err)
|
||||
}
|
||||
|
||||
resp := &types.AdminQueryWhitelistOpResp{
|
||||
Success: apiResp.Code == 0,
|
||||
TianyuanCode: apiResp.Code,
|
||||
TianyuanMessage: apiResp.Message,
|
||||
TransactionId: apiResp.TransactionID,
|
||||
}
|
||||
if apiResp.Data != nil {
|
||||
resp.EntryId = apiResp.Data.Id
|
||||
resp.IdCardMasked = apiResp.Data.IdCardMasked
|
||||
resp.EntryApiCodes = apiResp.Data.ApiCodes
|
||||
resp.EntryStatus = apiResp.Data.Status
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func parseJSONStringArray(raw string) []string {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return []string{}
|
||||
}
|
||||
var items []string
|
||||
if err := json.Unmarshal([]byte(raw), &items); err != nil {
|
||||
return []string{raw}
|
||||
}
|
||||
return items
|
||||
}
|
||||
134
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
134
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package tianyuanapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
whitelistEntriesPath = "query-whitelist/entries"
|
||||
whitelistEntriesAppendPath = "query-whitelist/entries/append"
|
||||
)
|
||||
|
||||
// WhitelistEntryPayload 查询白名单请求明文
|
||||
type WhitelistEntryPayload struct {
|
||||
Name string `json:"name"`
|
||||
IdCard string `json:"id_card"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark,omitempty"`
|
||||
}
|
||||
|
||||
// WhitelistEntryData 查询白名单响应解密数据
|
||||
type WhitelistEntryData struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IdCardMasked string `json:"id_card_masked"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Status string `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// WhitelistResponse 查询白名单调用结果(保留天远原始业务码)
|
||||
type WhitelistResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionID string `json:"transaction_id"`
|
||||
Data *WhitelistEntryData `json:"data,omitempty"`
|
||||
Timeout int64 `json:"timeout"`
|
||||
}
|
||||
|
||||
// CreateWhitelistEntry 创建查询白名单规则
|
||||
func (c *Client) CreateWhitelistEntry(payload WhitelistEntryPayload, mgmtKey string) (*WhitelistResponse, error) {
|
||||
return c.callWhitelist(whitelistEntriesPath, payload, mgmtKey)
|
||||
}
|
||||
|
||||
// AppendWhitelistEntry 向已有规则追加产品编码
|
||||
func (c *Client) AppendWhitelistEntry(payload WhitelistEntryPayload, mgmtKey string) (*WhitelistResponse, error) {
|
||||
return c.callWhitelist(whitelistEntriesAppendPath, payload, mgmtKey)
|
||||
}
|
||||
|
||||
func (c *Client) callWhitelist(path string, payload WhitelistEntryPayload, mgmtKey string) (*WhitelistResponse, error) {
|
||||
startTime := time.Now()
|
||||
|
||||
jsonData, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("参数序列化失败: %v", err)
|
||||
}
|
||||
|
||||
encryptedData, err := c.encrypt(string(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("数据加密失败: %v", err)
|
||||
}
|
||||
|
||||
requestBody := map[string]interface{}{
|
||||
"data": encryptedData,
|
||||
}
|
||||
requestBodyBytes, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求体序列化失败: %v", err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/api/v1/%s", c.baseURL, path)
|
||||
httpReq, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(requestBodyBytes))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建HTTP请求失败: %v", err)
|
||||
}
|
||||
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Access-Id", c.accessID)
|
||||
httpReq.Header.Set("Whitelist-Mgmt-Key", mgmtKey)
|
||||
httpReq.Header.Set("User-Agent", "TianyuanAPI-Go-SDK/1.0.0")
|
||||
|
||||
resp, err := c.client.Do(httpReq)
|
||||
if err != nil {
|
||||
return &WhitelistResponse{
|
||||
Code: -1,
|
||||
Message: "请求失败: " + err.Error(),
|
||||
Timeout: time.Since(startTime).Milliseconds(),
|
||||
}, nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return &WhitelistResponse{
|
||||
Code: -1,
|
||||
Message: "读取响应失败: " + err.Error(),
|
||||
Timeout: time.Since(startTime).Milliseconds(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
var apiResp ApiResponse
|
||||
if err := json.Unmarshal(body, &apiResp); err != nil {
|
||||
return &WhitelistResponse{
|
||||
Code: -1,
|
||||
Message: "响应解析失败: " + err.Error(),
|
||||
Timeout: time.Since(startTime).Milliseconds(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
result := &WhitelistResponse{
|
||||
Code: apiResp.Code,
|
||||
Message: apiResp.Message,
|
||||
TransactionID: apiResp.TransactionID,
|
||||
Timeout: time.Since(startTime).Milliseconds(),
|
||||
}
|
||||
|
||||
if apiResp.Data != "" {
|
||||
decryptedData, err := c.decrypt(apiResp.Data)
|
||||
if err == nil {
|
||||
var entry WhitelistEntryData
|
||||
if json.Unmarshal([]byte(decryptedData), &entry) == nil {
|
||||
result.Data = &entry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -85,6 +85,7 @@ type ServiceContext struct {
|
||||
ExampleModel model.ExampleModel
|
||||
GlobalNotificationsModel model.GlobalNotificationsModel
|
||||
AuthorizationDocumentModel model.AuthorizationDocumentModel
|
||||
QueryWhitelistOpLogModel model.QueryWhitelistOpLogModel
|
||||
|
||||
// 服务
|
||||
AlipayService *service.AliPayService
|
||||
@@ -100,6 +101,7 @@ type ServiceContext struct {
|
||||
AdminPromotionLinkStatsService *service.AdminPromotionLinkStatsService
|
||||
ImageService *service.ImageService
|
||||
AuthorizationService *service.AuthorizationService
|
||||
TianyuanapiClient *tianyuanapi.Client
|
||||
}
|
||||
|
||||
// NewServiceContext 创建服务上下文
|
||||
@@ -175,6 +177,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
exampleModel := model.NewExampleModel(db, cacheConf)
|
||||
globalNotificationsModel := model.NewGlobalNotificationsModel(db, cacheConf)
|
||||
authorizationDocumentModel := model.NewAuthorizationDocumentModel(db, cacheConf)
|
||||
queryWhitelistOpLogModel := model.NewQueryWhitelistOpLogModel(db, cacheConf)
|
||||
|
||||
// ============================== 第三方服务初始化 ==============================
|
||||
tianyuanapi, err := tianyuanapi.NewClient(tianyuanapi.Config{
|
||||
@@ -287,6 +290,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
ExampleModel: exampleModel,
|
||||
GlobalNotificationsModel: globalNotificationsModel,
|
||||
AuthorizationDocumentModel: authorizationDocumentModel,
|
||||
QueryWhitelistOpLogModel: queryWhitelistOpLogModel,
|
||||
|
||||
// 服务
|
||||
AlipayService: alipayService,
|
||||
@@ -302,6 +306,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
AdminPromotionLinkStatsService: adminPromotionLinkStatsService,
|
||||
ImageService: imageService,
|
||||
AuthorizationService: authorizationService,
|
||||
TianyuanapiClient: tianyuanapi,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -768,6 +768,44 @@ type AdminQueryItem struct {
|
||||
Data interface{} `json:"data"` // 这里可以是 map 或 具体的 struct
|
||||
}
|
||||
|
||||
type AdminQueryWhitelistAppendReq struct {
|
||||
Name string `json:"name"` // 姓名,* 表示只按身份证匹配
|
||||
IdCard string `json:"id_card"` // 18位身份证号
|
||||
ApiCodes []string `json:"api_codes"` // 产品编码列表
|
||||
Remark string `json:"remark,optional"` // 备注
|
||||
}
|
||||
|
||||
type AdminQueryWhitelistCreateReq struct {
|
||||
Name string `json:"name"` // 姓名,* 表示只按身份证匹配
|
||||
IdCard string `json:"id_card"` // 18位身份证号
|
||||
ApiCodes []string `json:"api_codes"` // 产品编码列表
|
||||
Remark string `json:"remark,optional"` // 备注
|
||||
}
|
||||
|
||||
type AdminQueryWhitelistOpLogListReq struct {
|
||||
Page int64 `form:"page,default=1"`
|
||||
PageSize int64 `form:"pageSize,default=20"`
|
||||
IdCard *string `form:"id_card,optional"`
|
||||
Action *string `form:"action,optional"`
|
||||
TianyuanCode *int `form:"tianyuan_code,optional"`
|
||||
}
|
||||
|
||||
type AdminQueryWhitelistOpLogListResp struct {
|
||||
Total int64 `json:"total"`
|
||||
Items []QueryWhitelistOpLogItem `json:"items"`
|
||||
}
|
||||
|
||||
type AdminQueryWhitelistOpResp struct {
|
||||
Success bool `json:"success"` // 天远是否业务成功
|
||||
TianyuanCode int `json:"tianyuan_code"` // 天远业务码
|
||||
TianyuanMessage string `json:"tianyuan_message"` // 天远返回描述
|
||||
TransactionId string `json:"transaction_id,optional"` // 天远流水号
|
||||
EntryId string `json:"entry_id,optional"` // 规则ID
|
||||
IdCardMasked string `json:"id_card_masked,optional"` // 脱敏身份证号
|
||||
EntryApiCodes []string `json:"entry_api_codes,optional"` // 规则当前产品编码
|
||||
EntryStatus string `json:"entry_status,optional"` // 规则状态
|
||||
}
|
||||
|
||||
type AdminRefundOrderReq struct {
|
||||
Id int64 `path:"id"` // 订单ID
|
||||
RefundAmount float64 `json:"refund_amount"` // 退款金额
|
||||
@@ -2075,6 +2113,25 @@ type QuerySingleTestResp struct {
|
||||
Api string `json:"api"`
|
||||
}
|
||||
|
||||
type QueryWhitelistOpLogItem struct {
|
||||
Id int64 `json:"id"`
|
||||
AdminUserId int64 `json:"admin_user_id"`
|
||||
AdminUserName string `json:"admin_user_name"`
|
||||
Action string `json:"action"`
|
||||
Name string `json:"name"`
|
||||
IdCard string `json:"id_card"`
|
||||
IdCardMasked string `json:"id_card_masked"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark"`
|
||||
TianyuanCode int `json:"tianyuan_code"`
|
||||
TianyuanMessage string `json:"tianyuan_message"`
|
||||
TransactionId string `json:"transaction_id"`
|
||||
EntryId string `json:"entry_id"`
|
||||
EntryStatus string `json:"entry_status"`
|
||||
EntryApiCodes []string `json:"entry_api_codes"`
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
|
||||
type RecordLinkClickReq struct {
|
||||
Path string `path:"path"` // 链接路径
|
||||
}
|
||||
@@ -2222,5 +2279,5 @@ type GetAppVersionResp struct {
|
||||
type SendSmsReq struct {
|
||||
Mobile string `json:"mobile" validate:"required,mobile"`
|
||||
CaptchaVerifyParam string `json:"captchaVerifyParam"`
|
||||
ActionType string `json:"actionType" validate:"omitempty,oneof=login register query agentApply realName bindMobile"`
|
||||
ActionType string `json:"actionType,optional" validate:"oneof=login register query agentApply realName bindMobile"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user