修复生产商地址受控展示
在平台管理列表、详情和编辑场景中恢复生产商自身企业地址,同时保留全局敏感字段保护。未填写地址时统一显示为‘未填写’,并补充单元测试、操作日志和项目文档。
This commit is contained in:
@@ -37,8 +37,65 @@ type producerAccountUpdateRequest struct {
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
func ListProducerAccount(ctx *gin.Context) { common.ListResource(ctx, &models.ProducerAccount{}) }
|
||||
func GetProducerAccount(ctx *gin.Context) { common.GetResource(ctx, &models.ProducerAccount{}) }
|
||||
// ListProducerAccount 查询生产商分页列表,并向已鉴权的平台管理端恢复企业地址。
|
||||
func ListProducerAccount(ctx *gin.Context) {
|
||||
page, size := common.PageSize(ctx)
|
||||
var list []models.ProducerAccount
|
||||
var total int64
|
||||
query := common.ApplyKeywordFilter(ctx, common.ActiveRecords(impl.DBService.Model(&models.ProducerAccount{})), &models.ProducerAccount{})
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := query.Order("created_at desc").Offset((page - 1) * size).Limit(size).Find(&list).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
response, err := common.PublicResourceResponse(list)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
// 先执行全局敏感字段保护,再仅为本管理资源恢复生产商自身地址。
|
||||
protected := common.ProtectPreciseLocation(ctx, &models.ProducerAccount{}, response)
|
||||
infra.Response.Success(ctx, gin.H{"total": total, "list": restoreProducerAccountAddresses(protected, list)})
|
||||
}
|
||||
|
||||
// GetProducerAccount 查询单个生产商,并返回详情及编辑表单所需的企业地址。
|
||||
func GetProducerAccount(ctx *gin.Context) {
|
||||
var producer models.ProducerAccount
|
||||
if err := common.ActiveRecords(impl.DBService).Where("identity = ?", ctx.Param("identity")).First(&producer).Error; err != nil {
|
||||
common.RespondRecordError(ctx, err)
|
||||
return
|
||||
}
|
||||
response, err := common.PublicResourceResponse(producer)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
protected := common.ProtectPreciseLocation(ctx, &models.ProducerAccount{}, response)
|
||||
infra.Response.Success(ctx, restoreProducerAccountAddresses(protected, []models.ProducerAccount{producer}))
|
||||
}
|
||||
|
||||
// restoreProducerAccountAddresses 将已鉴权生产商记录的企业地址恢复到安全响应中。
|
||||
func restoreProducerAccountAddresses(response any, producers []models.ProducerAccount) any {
|
||||
switch data := response.(type) {
|
||||
case []any:
|
||||
for index, item := range data {
|
||||
if index >= len(producers) {
|
||||
break
|
||||
}
|
||||
if record, ok := item.(map[string]any); ok {
|
||||
record["address"] = producers[index].Address
|
||||
}
|
||||
}
|
||||
case map[string]any:
|
||||
if len(producers) > 0 {
|
||||
data["address"] = producers[0].Address
|
||||
}
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
func CreateProducerAccount(ctx *gin.Context) {
|
||||
var request producerAccountCreateRequest
|
||||
|
||||
50
backend/api/internal/logic/platform/product/producer_test.go
Normal file
50
backend/api/internal/logic/platform/product/producer_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package product
|
||||
|
||||
// 功能描述:验证生产商管理响应仅恢复生产商自身的企业地址。
|
||||
// 版本:v1.0
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
)
|
||||
|
||||
// TestRestoreProducerAccountAddresses 验证生产商列表按原顺序恢复企业地址。
|
||||
func TestRestoreProducerAccountAddresses(t *testing.T) {
|
||||
response := []any{
|
||||
map[string]any{"identity": "producer-1"},
|
||||
map[string]any{"identity": "producer-2"},
|
||||
}
|
||||
producers := []models.ProducerAccount{
|
||||
{Address: "上海市浦东新区生产路 1 号"},
|
||||
{Address: "上海市闵行区生产路 2 号"},
|
||||
}
|
||||
|
||||
restored := restoreProducerAccountAddresses(response, producers).([]any)
|
||||
for index, producer := range producers {
|
||||
if restored[index].(map[string]any)["address"] != producer.Address {
|
||||
t.Fatalf("第 %d 条生产商地址未正确恢复:%#v", index+1, restored[index])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestoreProducerAccountAddress 验证生产商详情恢复编辑所需的企业地址。
|
||||
func TestRestoreProducerAccountAddress(t *testing.T) {
|
||||
response := map[string]any{"identity": "producer-1"}
|
||||
producer := models.ProducerAccount{Address: "上海市徐汇区生产路 18 号"}
|
||||
|
||||
restored := restoreProducerAccountAddresses(response, []models.ProducerAccount{producer}).(map[string]any)
|
||||
if restored["address"] != producer.Address {
|
||||
t.Fatalf("生产商详情地址未正确恢复:%#v", restored)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestoreProducerAccountEmptyAddress 验证未填写地址时保留空值,由前端显示“未填写”。
|
||||
func TestRestoreProducerAccountEmptyAddress(t *testing.T) {
|
||||
response := map[string]any{"identity": "producer-1"}
|
||||
|
||||
restored := restoreProducerAccountAddresses(response, []models.ProducerAccount{{}}).(map[string]any)
|
||||
if restored["address"] != "" {
|
||||
t.Fatalf("未填写地址应保留空值:%#v", restored)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user