chore: 初始开源发布
iotlcm-admin-svr:管理后台服务。 - Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件) - 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、 MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位; 私网 IP → 文档地址段;库名 xiujie_admin → iotlcm - 真实手机号/邮箱 → 合法 11 位编造号 / example.com - 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、 私有依赖 go get 行、明文 registry 登录口令 - 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名 - Dockerfile 改用公共基础镜像 - 拼错的 RADEME.md → README.md,README 重写 - 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用 - 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
This commit is contained in:
@@ -0,0 +1,239 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/contexts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/handler"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/hook"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/convert"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/excel"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type sAgentTApiGroup struct{}
|
||||
|
||||
// NewAgentTApiGroup 创建开放接口权限组后台服务实例。
|
||||
func NewAgentTApiGroup() *sAgentTApiGroup {
|
||||
return &sAgentTApiGroup{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterAgentTApiGroup(NewAgentTApiGroup())
|
||||
}
|
||||
|
||||
// Model 返回开放接口权限组 ORM 模型,默认启用租户和数据权限过滤。
|
||||
func (s *sAgentTApiGroup) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
|
||||
if len(option) == 0 {
|
||||
option = append(option, &handler.Option{
|
||||
FilterTenant: true,
|
||||
FilterAuth: true,
|
||||
})
|
||||
}
|
||||
return handler.Model(dao.TApiGroup.Ctx(ctx), option...)
|
||||
}
|
||||
|
||||
// List 分页查询开放接口权限组列表。
|
||||
func (s *sAgentTApiGroup) List(ctx context.Context, in *model.TApiGroupListParam) (list []*model.TApiGroupListModel, totalCount int, err error) {
|
||||
mod := s.Model(ctx).Fields(model.TApiGroupListModel{})
|
||||
|
||||
if in.Id > 0 {
|
||||
mod = mod.Where(dao.TApiGroup.Columns().Id, in.Id)
|
||||
}
|
||||
if in.Status > 0 {
|
||||
mod = mod.Where(dao.TApiGroup.Columns().Status, in.Status)
|
||||
}
|
||||
if in.GroupName != "" {
|
||||
mod = mod.Where(dao.TApiGroup.Columns().GroupName+" LIKE ?", "%"+in.GroupName+"%")
|
||||
}
|
||||
if len(in.CreatedAt) == 2 {
|
||||
in.CreatedAt[1] = in.CreatedAt[1].EndOfDay()
|
||||
mod = mod.WhereBetween(dao.TApiGroup.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
|
||||
}
|
||||
|
||||
mod = mod.Page(in.Page, in.PageSize).
|
||||
OrderDesc(dao.TApiGroup.Columns().Id).
|
||||
Hook(hook.MemberSummary)
|
||||
|
||||
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
||||
return nil, 0, gerror.Wrap(err, "获取开放接口权限组列表失败,请稍后重试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Export 导出开放接口权限组列表。
|
||||
func (s *sAgentTApiGroup) Export(ctx context.Context, in *model.TApiGroupListParam) (err error) {
|
||||
list, totalCount, err := s.List(ctx, in)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tags, err := convert.GetEntityDescTags(model.TApiGroupExportModel{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fileName := "导出开放接口权限组-" + gctx.CtxId(ctx)
|
||||
sheetName := fmt.Sprintf(
|
||||
"搜索条件共%v条 共%v页 当前导出第%v页 本页共%v条",
|
||||
totalCount,
|
||||
(totalCount+in.PageSize-1)/in.PageSize,
|
||||
in.Page,
|
||||
len(list),
|
||||
)
|
||||
var exports []model.TApiGroupExportModel
|
||||
if err = gconv.Scan(list, &exports); err != nil {
|
||||
return
|
||||
}
|
||||
return excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
|
||||
}
|
||||
|
||||
// Edit 新增或修改开放接口权限组,并同步维护权限组与接口资源的关联。
|
||||
func (s *sAgentTApiGroup) Edit(ctx context.Context, in *model.TApiGroupEditParam) (err error) {
|
||||
if in.GroupCode != nil {
|
||||
if err = xgorm.IsUnique(ctx, &dao.TApiGroup, g.Map{dao.TApiGroup.Columns().GroupCode: *in.GroupCode}, "权限组编码已存在", in.Id); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
groupId := in.Id
|
||||
|
||||
if in.Id > 0 {
|
||||
updatedBy := contexts.GetUserId(ctx)
|
||||
in.UpdatedBy = &updatedBy
|
||||
in.UpdatedAt = gtime.Now()
|
||||
tenantId := contexts.GetTenantId(ctx)
|
||||
in.TenantId = &tenantId
|
||||
if _, err = s.Model(ctx).
|
||||
Fields(model.TApiGroupUpdateFields{}).
|
||||
WherePri(in.Id).
|
||||
Data(in).
|
||||
Update(); err != nil {
|
||||
return gerror.Wrap(err, "修改开放接口权限组失败,请稍后重试!")
|
||||
}
|
||||
return s.replaceGroupResources(ctx, tx, groupId, in.ApiIds)
|
||||
}
|
||||
|
||||
tenantId := contexts.GetTenantId(ctx)
|
||||
in.TenantId = &tenantId
|
||||
createdDept := contexts.GetDeptId(ctx)
|
||||
in.CreatedDept = &createdDept
|
||||
createdBy := contexts.GetUserId(ctx)
|
||||
in.CreatedBy = &createdBy
|
||||
in.CreatedAt = gtime.Now()
|
||||
groupId, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
|
||||
Fields(model.TApiGroupInsertFields{}).
|
||||
Data(in).
|
||||
InsertAndGetId()
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "新增开放接口权限组失败,请稍后重试!")
|
||||
}
|
||||
return s.replaceGroupResources(ctx, tx, groupId, in.ApiIds)
|
||||
})
|
||||
}
|
||||
|
||||
// Delete 删除开放接口权限组,并删除该权限组下的资源关联。
|
||||
func (s *sAgentTApiGroup) Delete(ctx context.Context, in *model.TApiGroupDeleteParam) (err error) {
|
||||
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
groupIds := gconv.Int64s(in.Id)
|
||||
if len(groupIds) > 0 {
|
||||
if _, err = tx.Ctx(ctx).Model(dao.TApiGroupResource.Table()).
|
||||
WhereIn(dao.TApiGroupResource.Columns().GroupId, groupIds).
|
||||
Delete(); err != nil {
|
||||
return gerror.Wrap(err, "删除开放接口权限组资源关联失败,请稍后重试!")
|
||||
}
|
||||
}
|
||||
if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
|
||||
return gerror.Wrap(err, "删除开放接口权限组失败,请稍后重试!")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// View returns the API group detail with bound resource IDs and resource list.
|
||||
func (s *sAgentTApiGroup) View(ctx context.Context, in *model.TApiGroupViewParam) (res *model.TApiGroupViewModel, err error) {
|
||||
if err = s.Model(ctx).WherePri(in.Id).Hook(hook.MemberSummary).Scan(&res); err != nil {
|
||||
return nil, gerror.Wrap(err, "get open api group failed")
|
||||
}
|
||||
if res == nil {
|
||||
return
|
||||
}
|
||||
apiIds, err := dao.TApiGroupResource.Ctx(ctx).
|
||||
Fields(dao.TApiGroupResource.Columns().ApiId).
|
||||
Where(dao.TApiGroupResource.Columns().GroupId, in.Id).
|
||||
Array()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "get open api group resource relations failed")
|
||||
}
|
||||
res.ApiIds = gconv.Int64s(apiIds)
|
||||
res.Apis = []*model.TApiResourceListModel{}
|
||||
if len(res.ApiIds) > 0 {
|
||||
if err = handler.Model(dao.TApiResource.Ctx(ctx), &handler.Option{FilterTenant: true, FilterAuth: false}).
|
||||
Fields(model.TApiResourceListModel{}).
|
||||
WhereIn(dao.TApiResource.Columns().Id, res.ApiIds).
|
||||
OrderAsc(dao.TApiResource.Columns().Id).
|
||||
Hook(hook.MemberSummary).
|
||||
Scan(&res.Apis); err != nil {
|
||||
return nil, gerror.Wrap(err, "get open api group resources failed")
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Status 更新开放接口权限组启停状态。
|
||||
func (s *sAgentTApiGroup) Status(ctx context.Context, in *model.TApiGroupStatusParam) (err error) {
|
||||
if _, err = s.Model(ctx).WherePri(in.Id).Data(g.Map{
|
||||
dao.TApiGroup.Columns().Status: in.Status,
|
||||
dao.TApiGroup.Columns().UpdatedBy: contexts.GetUserId(ctx),
|
||||
dao.TApiGroup.Columns().UpdatedAt: gtime.Now(),
|
||||
}).Update(); err != nil {
|
||||
return gerror.Wrap(err, "更新开放接口权限组状态失败,请稍后重试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// replaceGroupResources 替换权限组绑定的接口资源,先清空旧关联再批量写入新关联。
|
||||
func (s *sAgentTApiGroup) replaceGroupResources(ctx context.Context, tx gdb.TX, groupId int64, apiIds []int64) (err error) {
|
||||
if groupId <= 0 {
|
||||
return gerror.New("权限组ID不能为空")
|
||||
}
|
||||
if _, err = tx.Ctx(ctx).Model(dao.TApiGroupResource.Table()).
|
||||
Where(dao.TApiGroupResource.Columns().GroupId, groupId).
|
||||
Delete(); err != nil {
|
||||
return gerror.Wrap(err, "删除开放接口权限组资源关联失败,请稍后重试!")
|
||||
}
|
||||
|
||||
seen := make(map[int64]struct{}, len(apiIds))
|
||||
data := g.List{}
|
||||
for _, apiId := range apiIds {
|
||||
if apiId <= 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[apiId]; ok {
|
||||
continue
|
||||
}
|
||||
seen[apiId] = struct{}{}
|
||||
data = append(data, g.Map{
|
||||
dao.TApiGroupResource.Columns().GroupId: groupId,
|
||||
dao.TApiGroupResource.Columns().ApiId: apiId,
|
||||
})
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err = tx.Ctx(ctx).Model(dao.TApiGroupResource.Table()).Data(data).Insert(); err != nil {
|
||||
return gerror.Wrap(err, "保存开放接口权限组资源关联失败,请稍后重试!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user