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/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type sAgentTApiResource struct{}
|
||||
|
||||
// NewAgentTApiResource 创建开放接口资源后台服务实例。
|
||||
func NewAgentTApiResource() *sAgentTApiResource {
|
||||
return &sAgentTApiResource{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterAgentTApiResource(NewAgentTApiResource())
|
||||
}
|
||||
|
||||
// Model 返回开放接口资源 ORM 模型,默认启用租户和数据权限过滤。
|
||||
func (s *sAgentTApiResource) 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.TApiResource.Ctx(ctx), option...)
|
||||
}
|
||||
|
||||
// List 分页查询开放接口资源列表。
|
||||
func (s *sAgentTApiResource) List(ctx context.Context, in *model.TApiResourceListParam) (list []*model.TApiResourceListModel, totalCount int, err error) {
|
||||
deptId := contexts.GetDeptId(ctx)
|
||||
if deptId <= 0 {
|
||||
return []*model.TApiResourceListModel{}, 0, nil
|
||||
}
|
||||
|
||||
mod := s.Model(ctx, &handler.Option{FilterTenant: true, FilterAuth: false}).
|
||||
Fields(model.TApiResourceListModel{})
|
||||
|
||||
isTop, dept, err := service.AgentDept().IsTopDept(ctx)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if dept != nil && (dept.DeptId == 100 || dept.ParentId == 100) {
|
||||
isTop = true
|
||||
}
|
||||
|
||||
if !isTop {
|
||||
var apiInfo struct {
|
||||
GroupId int64 `orm:"group_id"`
|
||||
}
|
||||
if err = dao.TApi.Ctx(ctx).
|
||||
Fields(dao.TApi.Columns().GroupId).
|
||||
Where(dao.TApi.Columns().DeptId, deptId).
|
||||
Where(dao.TApi.Columns().TenantId, contexts.GetTenantId(ctx)).
|
||||
OrderDesc(dao.TApi.Columns().Id).
|
||||
Scan(&apiInfo); err != nil {
|
||||
return nil, 0, gerror.Wrap(err, "get current dept open api group failed")
|
||||
}
|
||||
if apiInfo.GroupId <= 0 {
|
||||
return []*model.TApiResourceListModel{}, 0, nil
|
||||
}
|
||||
|
||||
apiIds, err := dao.TApiGroupResource.Ctx(ctx).
|
||||
Fields(dao.TApiGroupResource.Columns().ApiId).
|
||||
Where(dao.TApiGroupResource.Columns().GroupId, apiInfo.GroupId).
|
||||
Array()
|
||||
if err != nil {
|
||||
return nil, 0, gerror.Wrap(err, "get open api group resources failed")
|
||||
}
|
||||
if len(apiIds) == 0 {
|
||||
return []*model.TApiResourceListModel{}, 0, nil
|
||||
}
|
||||
mod = mod.WhereIn(dao.TApiResource.Columns().Id, gconv.Int64s(apiIds))
|
||||
}
|
||||
|
||||
if in.Id > 0 {
|
||||
mod = mod.Where(dao.TApiResource.Columns().Id, in.Id)
|
||||
}
|
||||
if in.Status > 0 {
|
||||
mod = mod.Where(dao.TApiResource.Columns().Status, in.Status)
|
||||
}
|
||||
if in.ApiName != "" {
|
||||
mod = mod.Where(dao.TApiResource.Columns().ApiName+" LIKE ?", "%"+in.ApiName+"%")
|
||||
}
|
||||
if in.Version != "" {
|
||||
mod = mod.Where(dao.TApiResource.Columns().Version+" LIKE ?", "%"+in.Version+"%")
|
||||
}
|
||||
|
||||
if len(in.CreatedAt) == 2 {
|
||||
in.CreatedAt[1] = in.CreatedAt[1].EndOfDay()
|
||||
mod = mod.WhereBetween(dao.TApiResource.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
|
||||
}
|
||||
|
||||
mod = mod.Page(in.Page, in.PageSize).
|
||||
OrderDesc(dao.TApiResource.Columns().Id).
|
||||
Hook(hook.MemberSummary)
|
||||
|
||||
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
||||
return nil, 0, gerror.Wrap(err, "获取开放接口资源列表失败,请稍后重试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Export 导出开放接口资源列表。
|
||||
func (s *sAgentTApiResource) Export(ctx context.Context, in *model.TApiResourceListParam) (err error) {
|
||||
list, totalCount, err := s.List(ctx, in)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tags, err := convert.GetEntityDescTags(model.TApiResourceExportModel{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fileName := "export-open-api-resource-" + gctx.CtxId(ctx)
|
||||
totalPage := 0
|
||||
if in.PageSize > 0 {
|
||||
totalPage = (totalCount + in.PageSize - 1) / in.PageSize
|
||||
}
|
||||
sheetName := fmt.Sprintf("total %v pages %v current %v count %v", totalCount, totalPage, in.Page, len(list))
|
||||
|
||||
var exports []*model.TApiResourceExportModel
|
||||
if err = gconv.Scan(list, &exports); err != nil {
|
||||
return
|
||||
}
|
||||
return excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
|
||||
}
|
||||
|
||||
// Edit 新增或修改开放接口资源。
|
||||
func (s *sAgentTApiResource) Edit(ctx context.Context, in *model.TApiResourceEditParam) (err error) {
|
||||
if err = in.Filter(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
if in.Status == nil {
|
||||
status := 0
|
||||
in.Status = &status
|
||||
}
|
||||
if in.Method != nil {
|
||||
method := *in.Method
|
||||
in.Method = &method
|
||||
}
|
||||
|
||||
if in.ApiCode != nil && gstr.Trim(*in.ApiCode) != "" {
|
||||
if err = xgorm.IsUnique(ctx, &dao.TApiResource, g.Map{dao.TApiResource.Columns().ApiCode: gstr.Trim(*in.ApiCode)}, "接口权限标识已存在", in.Id); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
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.TApiResourceUpdateFields{}).
|
||||
WherePri(in.Id).
|
||||
Data(in).
|
||||
Update(); err != nil {
|
||||
return gerror.Wrap(err, "修改开放接口资源失败,请稍后重试!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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()
|
||||
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
|
||||
Fields(model.TApiResourceInsertFields{}).
|
||||
Data(in).
|
||||
Insert(); err != nil {
|
||||
return gerror.Wrap(err, "新增开放接口资源失败,请稍后重试!")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Delete 删除开放接口资源,并清理权限组资源关联。
|
||||
func (s *sAgentTApiResource) Delete(ctx context.Context, in *model.TApiResourceDeleteParam) (err error) {
|
||||
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
apiIds := gconv.Int64s(in.Id)
|
||||
if len(apiIds) > 0 {
|
||||
if _, err = tx.Ctx(ctx).Model(dao.TApiGroupResource.Table()).
|
||||
WhereIn(dao.TApiGroupResource.Columns().ApiId, apiIds).
|
||||
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 查询开放接口资源详情。
|
||||
func (s *sAgentTApiResource) View(ctx context.Context, in *model.TApiResourceViewParam) (res *model.TApiResourceViewModel, err error) {
|
||||
if err = s.Model(ctx).WherePri(in.Id).Hook(hook.MemberSummary).Scan(&res); err != nil {
|
||||
return nil, gerror.Wrap(err, "获取开放接口资源信息失败,请稍后重试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Status 更新开放接口资源启停状态。
|
||||
func (s *sAgentTApiResource) Status(ctx context.Context, in *model.TApiResourceStatusParam) (err error) {
|
||||
if _, err = s.Model(ctx).WherePri(in.Id).Data(g.Map{
|
||||
dao.TApiResource.Columns().Status: in.Status,
|
||||
dao.TApiResource.Columns().UpdatedBy: contexts.GetUserId(ctx),
|
||||
dao.TApiResource.Columns().UpdatedAt: gtime.Now(),
|
||||
}).Update(); err != nil {
|
||||
return gerror.Wrap(err, "更新开放接口资源状态失败,请稍后重试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user