fix version 1
This commit is contained in:
@@ -3,13 +3,16 @@ package method
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/feedback/internal/impl"
|
||||
"bsm/full/module/base/feedback/internal/models"
|
||||
pb "bsm/full/module/base/feedback/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Delete 删除反馈记录
|
||||
@@ -17,12 +20,30 @@ import (
|
||||
// in: 删除请求参数,包含记录ID
|
||||
// 返回: 删除操作结果
|
||||
func Delete(ctx context.Context, in *pb.DeleteRequest) (reply *pb.StatusReply, err error) {
|
||||
// 解析用户认证信息
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证请求参数
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 删除记录(GORM会自动处理关联的图片和附件删除)
|
||||
// 归属校验:仅本人或管理端可删除
|
||||
record := new(models.FeedbackItem)
|
||||
if err = impl.DBService.Where("identity = ?", in.GetIdentity()).First(record).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !canAccess(auth, record.PassportIdentity) {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// 删除反馈主记录
|
||||
err = impl.DBService.Where("identity = ?", in.GetIdentity()).Delete(new(models.FeedbackItem)).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"bsm/full/module/base/feedback/internal/models"
|
||||
pb "bsm/full/module/base/feedback/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -17,6 +18,12 @@ import (
|
||||
// in: 查询请求参数,包含记录ID
|
||||
// 返回: 反馈记录详情
|
||||
func Get(ctx context.Context, in *pb.GetRequest) (reply *pb.GetReply, err error) {
|
||||
// 解析用户认证信息
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证请求参数
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
@@ -34,8 +41,14 @@ func Get(ctx context.Context, in *pb.GetRequest) (reply *pb.GetReply, err error)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 归属校验:仅本人或管理端可查看
|
||||
allowed := canAccess(auth, record.PassportIdentity)
|
||||
if !allowed {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// 转换为响应格式
|
||||
item := convert(*record)
|
||||
item := convert(*record, !allowed)
|
||||
out.Record = item
|
||||
out.Exists = true
|
||||
return out, nil
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"bsm/full/module/base/feedback/internal/impl"
|
||||
"bsm/full/module/base/feedback/internal/models"
|
||||
pb "bsm/full/module/base/feedback/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
@@ -34,14 +35,16 @@ func List(ctx context.Context, in *pb.ListRequest) (reply *pb.ListReply, err err
|
||||
// 构建查询会话,预加载图片信息
|
||||
sess := impl.DBService.Preload("Images")
|
||||
|
||||
// 根据机构筛选或用户身份筛选
|
||||
if in.GetAgency() != "" {
|
||||
sess = sess.Where("agency = ?", in.GetAgency())
|
||||
} else {
|
||||
userIdentity := auth.Identity
|
||||
if userIdentity != "" {
|
||||
sess = sess.Where("passport_identity = ?", userIdentity)
|
||||
// 权限收敛:仅管理端可按机构跨用户查询,普通用户只能查看本人反馈
|
||||
if isAdmin(auth) {
|
||||
if in.GetAgency() != "" {
|
||||
sess = sess.Where("agency = ?", in.GetAgency())
|
||||
}
|
||||
} else {
|
||||
if auth.Identity == "" {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
sess = sess.Where("passport_identity = ?", auth.Identity)
|
||||
}
|
||||
|
||||
// 用户名筛选
|
||||
@@ -80,7 +83,7 @@ func List(ctx context.Context, in *pb.ListRequest) (reply *pb.ListReply, err err
|
||||
|
||||
// 转换数据格式
|
||||
for _, v := range list {
|
||||
item := convert(v)
|
||||
item := convert(v, !canAccess(auth, v.PassportIdentity))
|
||||
out.List = append(out.List, item)
|
||||
}
|
||||
return out, nil
|
||||
|
||||
@@ -3,6 +3,7 @@ package method
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/feedback/internal/impl"
|
||||
@@ -13,6 +14,8 @@ import (
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// Modify 修改反馈记录
|
||||
@@ -31,33 +34,37 @@ func Modify(ctx context.Context, in *pb.ModifyRequest) (reply *pb.StatusReply, e
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 构建更新记录
|
||||
// 归属校验:仅本人或管理端可修改
|
||||
exist := new(models.FeedbackItem)
|
||||
if err = impl.DBService.Where("identity = ?", in.GetIdentity()).First(exist).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !canAccess(auth, exist.PassportIdentity) {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// 构建更新字段:不改写 identity 与归属,避免记录"消失"或归属被转移
|
||||
record := &models.FeedbackItem{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: utils.UUID(),
|
||||
},
|
||||
Std_Passport: types.Std_Passport{
|
||||
PassportID: auth.ID, // 用户ID
|
||||
PassportIdentity: auth.Identity, // 用户身份标识
|
||||
},
|
||||
UserName: in.GetUserName(), // 用户名
|
||||
Email: in.GetEmail(), // 邮箱
|
||||
Phone: in.GetPhone(), // 手机号
|
||||
Status: in.GetStatus(), // 状态
|
||||
Title: in.GetTitle(), // 标题
|
||||
Content: in.GetContent(), // 内容
|
||||
Category: in.GetCategory(), // 分类
|
||||
Images: make([]models.FeedbackImage, 0, len(in.GetImages())), // 图片列表
|
||||
Accessories: make([]models.FeedbackAccessory, 0, len(in.GetAccessories())), // 附件列表
|
||||
UserName: in.GetUserName(), // 用户名
|
||||
Email: in.GetEmail(), // 邮箱
|
||||
Phone: in.GetPhone(), // 手机号
|
||||
Status: in.GetStatus(), // 状态
|
||||
Title: in.GetTitle(), // 标题
|
||||
Content: in.GetContent(), // 内容
|
||||
Category: in.GetCategory(), // 分类
|
||||
}
|
||||
|
||||
// 处理图片信息
|
||||
images := make([]models.FeedbackImage, 0, len(in.GetImages()))
|
||||
for _, v := range in.GetImages() {
|
||||
identity := v.GetIdentity()
|
||||
if identity == "" {
|
||||
identity = utils.UUID() // 生成新的图片ID
|
||||
}
|
||||
record.Images = append(record.Images, models.FeedbackImage{
|
||||
images = append(images, models.FeedbackImage{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: identity,
|
||||
},
|
||||
@@ -66,43 +73,56 @@ func Modify(ctx context.Context, in *pb.ModifyRequest) (reply *pb.StatusReply, e
|
||||
})
|
||||
}
|
||||
|
||||
// 处理附件信息
|
||||
// 处理附件信息:子表 identity 为唯一索引,必须逐个赋值,否则多条附件必然唯一键冲突
|
||||
accessories := make([]models.FeedbackAccessory, 0, len(in.GetAccessories()))
|
||||
for _, v := range in.GetAccessories() {
|
||||
record.Accessories = append(record.Accessories, models.FeedbackAccessory{
|
||||
identity := v.GetIdentity()
|
||||
if identity == "" {
|
||||
identity = utils.UUID() // 生成新的附件ID
|
||||
}
|
||||
accessories = append(accessories, models.FeedbackAccessory{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: identity,
|
||||
},
|
||||
ItemIdentity: in.GetIdentity(), // 关联的反馈记录ID
|
||||
Title: v.Title, // 附件标题
|
||||
FilePath: v.FilePath, // 附件文件路径
|
||||
})
|
||||
}
|
||||
|
||||
// 先删除关联的图片和附件
|
||||
err = impl.DBService.Where("item_identity = ?", in.GetIdentity()).Delete(&models.FeedbackImage{}).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = impl.DBService.Where("item_identity = ?", in.GetIdentity()).Delete(&models.FeedbackAccessory{}).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 更新主记录
|
||||
err = impl.DBService.Where("identity = ?", in.GetIdentity()).Updates(record).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 重新创建关联的图片和附件
|
||||
if len(record.Images) > 0 {
|
||||
err = impl.DBService.Create(&record.Images).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// 主表字段与子表重建放在同一事务内,任一步失败整体回滚
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 只更新主表标量字段;Omit 关联后由下面显式重建子表,避免同一次修改写入两遍
|
||||
if err := tx.Model(&models.FeedbackItem{}).
|
||||
Where("identity = ?", in.GetIdentity()).
|
||||
Omit(clause.Associations).
|
||||
Updates(record).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(record.Accessories) > 0 {
|
||||
err = impl.DBService.Create(&record.Accessories).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
// 先删除关联的图片和附件
|
||||
if err := tx.Where("item_identity = ?", in.GetIdentity()).Delete(&models.FeedbackImage{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("item_identity = ?", in.GetIdentity()).Delete(&models.FeedbackAccessory{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 重新创建关联的图片和附件
|
||||
if len(images) > 0 {
|
||||
if err := tx.Create(&images).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(accessories) > 0 {
|
||||
if err := tx.Create(&accessories).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
|
||||
@@ -2,15 +2,50 @@
|
||||
package method
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"bsm/full/module/base/feedback/internal/models"
|
||||
pb "bsm/full/module/base/feedback/pb"
|
||||
"git.apinb.com/bsm-sdk/core/crypto/token"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// isAdmin 判断是否为管理端角色,沿用仓库内 <模块>_Admin 的角色命名约定
|
||||
func isAdmin(auth *token.Claims) bool {
|
||||
role := strings.ToLower(auth.Role)
|
||||
return role == "admin" || strings.HasSuffix(role, "_admin")
|
||||
}
|
||||
|
||||
// canAccess 判断调用方是否为记录归属人或管理端
|
||||
func canAccess(auth *token.Claims, passportIdentity string) bool {
|
||||
if isAdmin(auth) {
|
||||
return true
|
||||
}
|
||||
return auth.Identity != "" && passportIdentity != "" && passportIdentity == auth.Identity
|
||||
}
|
||||
|
||||
// maskEmail 邮箱脱敏,保留首字符与域名
|
||||
func maskEmail(email string) string {
|
||||
at := strings.Index(email, "@")
|
||||
if at <= 0 {
|
||||
return "***"
|
||||
}
|
||||
return email[:1] + "***" + email[at:]
|
||||
}
|
||||
|
||||
// maskPhone 手机号脱敏,保留前3位与后4位
|
||||
func maskPhone(phone string) string {
|
||||
if len(phone) < 7 {
|
||||
return "***"
|
||||
}
|
||||
return phone[:3] + "****" + phone[len(phone)-4:]
|
||||
}
|
||||
|
||||
// convert 将数据库模型转换为protobuf响应格式
|
||||
// item: 数据库中的反馈记录模型
|
||||
// maskPII: 是否对邮箱与手机号脱敏(非本人且非管理端时为 true)
|
||||
// 返回: protobuf格式的反馈记录指针
|
||||
func convert(item models.FeedbackItem) *pb.FeedbackItem {
|
||||
func convert(item models.FeedbackItem, maskPII bool) *pb.FeedbackItem {
|
||||
reply := &pb.FeedbackItem{
|
||||
Identity: item.Identity, // 记录唯一标识
|
||||
UserName: item.UserName, // 用户名
|
||||
@@ -25,6 +60,12 @@ func convert(item models.FeedbackItem) *pb.FeedbackItem {
|
||||
UpdatedAt: item.UpdatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS), // 更新时间
|
||||
}
|
||||
|
||||
// 非本人且非管理端时抹除联系方式的完整内容
|
||||
if maskPII {
|
||||
reply.Email = maskEmail(item.Email)
|
||||
reply.Phone = maskPhone(item.Phone)
|
||||
}
|
||||
|
||||
// 转换图片信息
|
||||
for _, image := range item.Images {
|
||||
reply.Images = append(reply.Images, &pb.FeedbackImage{
|
||||
|
||||
@@ -3,13 +3,16 @@ package method
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/feedback/internal/impl"
|
||||
"bsm/full/module/base/feedback/internal/models"
|
||||
pb "bsm/full/module/base/feedback/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Remark 添加或更新反馈记录的备注和状态
|
||||
@@ -17,11 +20,29 @@ import (
|
||||
// in: 备注请求参数,包含记录ID、备注内容和状态
|
||||
// 返回: 操作结果
|
||||
func Remark(ctx context.Context, in *pb.RemarkRequest) (reply *pb.StatusReply, err error) {
|
||||
// 解析用户认证信息
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证请求参数
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 归属校验:仅本人或管理端可修改备注与状态
|
||||
exist := new(models.FeedbackItem)
|
||||
if err = impl.DBService.Where("identity = ?", in.GetIdentity()).First(exist).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !canAccess(auth, exist.PassportIdentity) {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// 构建更新记录
|
||||
record := &models.FeedbackItem{
|
||||
Remark: in.GetRemark(), // 备注内容
|
||||
|
||||
Reference in New Issue
Block a user