All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m45s
主要变更: 1. OpenAPI 文档契约对齐 - 统一错误响应字段名为 msg(非 message) - 规范 envelope 响应结构(code, msg, data, timestamp) - 个人客户路由纳入文档体系(使用 Register 机制) - 新增 BuildDocHandlers() 统一管理 handler 构造 - 确保文档生成的幂等性 2. Service 层错误处理统一 - 全面替换 fmt.Errorf 为 errors.New/Wrap - 统一错误码使用规范 - Handler 层参数校验不泄露底层细节 - 新增错误码验证集成测试 3. 代码质量提升 - 删除未使用的 Task handler 和路由 - 新增代码规范检查脚本(check-service-errors.sh) - 新增注释路径一致性检查(check-comment-paths.sh) - 更新 API 文档生成指南 4. OpenSpec 归档 - 归档 openapi-contract-alignment 变更(63 tasks) - 归档 service-error-unify-core 变更 - 归档 service-error-unify-support 变更 - 归档 code-cleanup-docs-update 变更 - 归档 handler-validation-security 变更 - 同步 delta specs 到主规范文件 影响范围: - pkg/openapi: 新增 handlers.go,优化 generator.go - internal/service/*: 48 个 service 文件错误处理统一 - internal/handler/admin: 优化参数校验错误提示 - internal/routes: 个人客户路由改造,删除 task 路由 - scripts: 新增 3 个代码检查脚本 - docs: 更新 OpenAPI 文档(15750+ 行) - openspec/specs: 同步 3 个主规范文件 破坏性变更:无 向后兼容:是
266 lines
7.9 KiB
Go
266 lines
7.9 KiB
Go
package shop_account
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
|
"github.com/break/junhong_cmp_fiber/internal/store"
|
|
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Service struct {
|
|
accountStore *postgres.AccountStore
|
|
shopStore *postgres.ShopStore
|
|
}
|
|
|
|
func New(accountStore *postgres.AccountStore, shopStore *postgres.ShopStore) *Service {
|
|
return &Service{
|
|
accountStore: accountStore,
|
|
shopStore: shopStore,
|
|
}
|
|
}
|
|
|
|
func (s *Service) List(ctx context.Context, req *dto.ShopAccountListRequest) ([]*dto.ShopAccountResponse, int64, error) {
|
|
opts := &store.QueryOptions{
|
|
Page: req.Page,
|
|
PageSize: req.PageSize,
|
|
OrderBy: "created_at DESC",
|
|
}
|
|
if opts.Page == 0 {
|
|
opts.Page = 1
|
|
}
|
|
if opts.PageSize == 0 {
|
|
opts.PageSize = constants.DefaultPageSize
|
|
}
|
|
|
|
filters := make(map[string]interface{})
|
|
filters["user_type"] = constants.UserTypeAgent
|
|
if req.Username != "" {
|
|
filters["username"] = req.Username
|
|
}
|
|
if req.Phone != "" {
|
|
filters["phone"] = req.Phone
|
|
}
|
|
if req.Status != nil {
|
|
filters["status"] = *req.Status
|
|
}
|
|
|
|
var accounts []*model.Account
|
|
var total int64
|
|
var err error
|
|
|
|
if req.ShopID != nil {
|
|
accounts, total, err = s.accountStore.ListByShopID(ctx, *req.ShopID, opts, filters)
|
|
} else {
|
|
filters["user_type"] = constants.UserTypeAgent
|
|
accounts, total, err = s.accountStore.List(ctx, opts, filters)
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, 0, errors.Wrap(errors.CodeInternalError, err, "查询代理商账号列表失败")
|
|
}
|
|
|
|
shopMap := make(map[uint]string)
|
|
for _, account := range accounts {
|
|
if account.ShopID != nil {
|
|
if _, exists := shopMap[*account.ShopID]; !exists {
|
|
shop, err := s.shopStore.GetByID(ctx, *account.ShopID)
|
|
if err == nil {
|
|
shopMap[*account.ShopID] = shop.ShopName
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
responses := make([]*dto.ShopAccountResponse, 0, len(accounts))
|
|
for _, account := range accounts {
|
|
resp := &dto.ShopAccountResponse{
|
|
ID: account.ID,
|
|
Username: account.Username,
|
|
Phone: account.Phone,
|
|
UserType: account.UserType,
|
|
Status: account.Status,
|
|
CreatedAt: account.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: account.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
if account.ShopID != nil {
|
|
resp.ShopID = *account.ShopID
|
|
if shopName, ok := shopMap[*account.ShopID]; ok {
|
|
resp.ShopName = shopName
|
|
}
|
|
}
|
|
responses = append(responses, resp)
|
|
}
|
|
|
|
return responses, total, nil
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, req *dto.CreateShopAccountRequest) (*dto.ShopAccountResponse, error) {
|
|
currentUserID := middleware.GetUserIDFromContext(ctx)
|
|
if currentUserID == 0 {
|
|
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
|
}
|
|
|
|
shop, err := s.shopStore.GetByID(ctx, req.ShopID)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
|
|
}
|
|
return nil, errors.Wrap(errors.CodeInternalError, err, "获取店铺失败")
|
|
}
|
|
|
|
existing, err := s.accountStore.GetByUsername(ctx, req.Username)
|
|
if err == nil && existing != nil {
|
|
return nil, errors.New(errors.CodeUsernameExists, "用户名已存在")
|
|
}
|
|
|
|
existing, err = s.accountStore.GetByPhone(ctx, req.Phone)
|
|
if err == nil && existing != nil {
|
|
return nil, errors.New(errors.CodePhoneExists, "手机号已存在")
|
|
}
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return nil, errors.Wrap(errors.CodeInternalError, err, "密码哈希失败")
|
|
}
|
|
|
|
account := &model.Account{
|
|
Username: req.Username,
|
|
Phone: req.Phone,
|
|
Password: string(hashedPassword),
|
|
UserType: constants.UserTypeAgent,
|
|
ShopID: &req.ShopID,
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
account.Creator = currentUserID
|
|
account.Updater = currentUserID
|
|
|
|
if err := s.accountStore.Create(ctx, account); err != nil {
|
|
return nil, errors.Wrap(errors.CodeInternalError, err, "创建代理商账号失败")
|
|
}
|
|
|
|
return &dto.ShopAccountResponse{
|
|
ID: account.ID,
|
|
ShopID: *account.ShopID,
|
|
ShopName: shop.ShopName,
|
|
Username: account.Username,
|
|
Phone: account.Phone,
|
|
UserType: account.UserType,
|
|
Status: account.Status,
|
|
CreatedAt: account.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: account.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, id uint, req *dto.UpdateShopAccountRequest) (*dto.ShopAccountResponse, error) {
|
|
currentUserID := middleware.GetUserIDFromContext(ctx)
|
|
if currentUserID == 0 {
|
|
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
|
}
|
|
|
|
account, err := s.accountStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.New(errors.CodeAccountNotFound, "账号不存在")
|
|
}
|
|
return nil, errors.Wrap(errors.CodeInternalError, err, "获取账号失败")
|
|
}
|
|
|
|
if account.UserType != constants.UserTypeAgent {
|
|
return nil, errors.New(errors.CodeInvalidParam, "只能更新代理商账号")
|
|
}
|
|
|
|
existingAccount, err := s.accountStore.GetByUsername(ctx, req.Username)
|
|
if err == nil && existingAccount != nil && existingAccount.ID != id {
|
|
return nil, errors.New(errors.CodeUsernameExists, "用户名已存在")
|
|
}
|
|
|
|
account.Username = req.Username
|
|
account.Updater = currentUserID
|
|
|
|
if err := s.accountStore.Update(ctx, account); err != nil {
|
|
return nil, errors.Wrap(errors.CodeInternalError, err, "更新代理商账号失败")
|
|
}
|
|
|
|
var shopName string
|
|
if account.ShopID != nil {
|
|
shop, err := s.shopStore.GetByID(ctx, *account.ShopID)
|
|
if err == nil {
|
|
shopName = shop.ShopName
|
|
}
|
|
}
|
|
|
|
return &dto.ShopAccountResponse{
|
|
ID: account.ID,
|
|
ShopID: *account.ShopID,
|
|
ShopName: shopName,
|
|
Username: account.Username,
|
|
Phone: account.Phone,
|
|
UserType: account.UserType,
|
|
Status: account.Status,
|
|
CreatedAt: account.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: account.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) UpdatePassword(ctx context.Context, id uint, req *dto.UpdateShopAccountPasswordRequest) error {
|
|
currentUserID := middleware.GetUserIDFromContext(ctx)
|
|
if currentUserID == 0 {
|
|
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
|
}
|
|
|
|
account, err := s.accountStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return errors.New(errors.CodeAccountNotFound, "账号不存在")
|
|
}
|
|
return errors.Wrap(errors.CodeInternalError, err, "获取账号失败")
|
|
}
|
|
|
|
if account.UserType != constants.UserTypeAgent {
|
|
return errors.New(errors.CodeInvalidParam, "只能更新代理商账号密码")
|
|
}
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return errors.Wrap(errors.CodeInternalError, err, "密码哈希失败")
|
|
}
|
|
|
|
if err := s.accountStore.UpdatePassword(ctx, id, string(hashedPassword), currentUserID); err != nil {
|
|
return errors.Wrap(errors.CodeInternalError, err, "更新密码失败")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) UpdateStatus(ctx context.Context, id uint, req *dto.UpdateShopAccountStatusRequest) error {
|
|
currentUserID := middleware.GetUserIDFromContext(ctx)
|
|
if currentUserID == 0 {
|
|
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
|
}
|
|
|
|
account, err := s.accountStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return errors.New(errors.CodeAccountNotFound, "账号不存在")
|
|
}
|
|
return errors.Wrap(errors.CodeInternalError, err, "获取账号失败")
|
|
}
|
|
|
|
if account.UserType != constants.UserTypeAgent {
|
|
return errors.New(errors.CodeInvalidParam, "只能更新代理商账号状态")
|
|
}
|
|
|
|
if err := s.accountStore.UpdateStatus(ctx, id, req.Status, currentUserID); err != nil {
|
|
return errors.Wrap(errors.CodeInternalError, err, "更新账号状态失败")
|
|
}
|
|
|
|
return nil
|
|
}
|