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,292 @@
|
||||
// package system
|
||||
// @Link https://github.com/xiujiecn/xiu-admin
|
||||
// @Copyright Copyright (c) 2025 LiXiujie
|
||||
// @Author Lxj <li@xiujie.cn>
|
||||
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
|
||||
"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/event"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/rolepolicy"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/rolesync"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/handler"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/do"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model/entity"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type sSysConfig struct {
|
||||
}
|
||||
|
||||
func NewSysConfig() *sSysConfig {
|
||||
return &sSysConfig{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterSysConfig(NewSysConfig())
|
||||
}
|
||||
|
||||
func (l *sSysConfig) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
|
||||
if len(option) == 0 {
|
||||
option = append(option, &handler.Option{
|
||||
FilterTenant: true,
|
||||
FilterAuth: false,
|
||||
})
|
||||
}
|
||||
return handler.Model(dao.SysConfig.Ctx(ctx), option...)
|
||||
}
|
||||
|
||||
func (l *sSysConfig) ModelQuery(ctx context.Context, option ...*handler.Option) *gdb.Model {
|
||||
if len(option) == 0 {
|
||||
option = append(option, &handler.Option{
|
||||
FilterTenant: true,
|
||||
FilterAuth: false,
|
||||
})
|
||||
}
|
||||
return handler.Model(service.MemoryDB().DB(ctx).Ctx(ctx).Model(dao.SysConfig.Table()), option...)
|
||||
}
|
||||
|
||||
func (s *sSysConfig) List(ctx context.Context, param *model.SysConfigListParam) (items []*model.SysConfigListModel, total int, err error) {
|
||||
m := s.ModelQuery(ctx)
|
||||
|
||||
if param.ConfigName != "" {
|
||||
m = m.WhereLike(dao.SysConfig.Columns().ConfigName, "%"+param.ConfigName+"%")
|
||||
}
|
||||
|
||||
if param.ConfigKey != "" {
|
||||
m = m.WhereLike(dao.SysConfig.Columns().ConfigKey, "%"+param.ConfigKey+"%")
|
||||
}
|
||||
|
||||
if param.ConfigType != "" {
|
||||
m = m.Where(dao.SysConfig.Columns().ConfigType, param.ConfigType)
|
||||
}
|
||||
|
||||
if param.ConfigValue != "" {
|
||||
m = m.WhereLike(dao.SysConfig.Columns().ConfigValue, "%"+param.ConfigValue+"%")
|
||||
}
|
||||
|
||||
if len(param.CreatedAt) > 0 {
|
||||
startTime := gtime.NewFromStr(param.CreatedAt[0])
|
||||
endTime := gtime.NewFromStr(param.CreatedAt[1])
|
||||
m = m.WhereBetween(dao.SysConfig.Columns().CreatedAt, startTime, endTime.EndOfDay())
|
||||
}
|
||||
|
||||
total, err = m.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err = m.Page(param.Page, param.PageSize).Order(dao.SysConfig.Columns().ConfigId, "ASC").Scan(&items)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysConfig) Add(ctx context.Context, param *model.SysConfigAddParam) (output *model.SysConfigAddModel, err error) {
|
||||
if param == nil || param.ConfigName == "" {
|
||||
return nil, errors.New("配置名称不能为空")
|
||||
}
|
||||
if param.ConfigKey == rolepolicy.TemplateConfigKey {
|
||||
if !contexts.IsSuperAdmin(ctx) || contexts.GetTenantId(ctx) != consts.DefaultSystemTenantCode {
|
||||
return nil, errors.New("仅平台超级管理员可配置代理模板")
|
||||
}
|
||||
if _, err := rolepolicy.ValidateTemplate(ctx, param.ConfigValue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
count, err := dao.SysConfig.Ctx(ctx).Where("tenant_id", consts.DefaultSystemTenantCode).Where("config_key", rolepolicy.TemplateConfigKey).Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count > 0 {
|
||||
return nil, errors.New("代理模板参数已存在")
|
||||
}
|
||||
}
|
||||
m := s.Model(ctx)
|
||||
|
||||
data := &do.SysConfig{}
|
||||
gconv.Struct(param, data)
|
||||
data.TenantId = contexts.GetTenantId(ctx)
|
||||
data.CreatedDept = contexts.GetDeptId(ctx)
|
||||
data.CreatedBy = contexts.GetUserId(ctx)
|
||||
data.CreatedAt = gtime.Now()
|
||||
data.UpdatedBy = contexts.GetUserId(ctx)
|
||||
data.UpdatedAt = gtime.Now()
|
||||
|
||||
var lastInsertId int64
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
var insertErr error
|
||||
lastInsertId, insertErr = m.Ctx(ctx).Data(data).InsertAndGetId()
|
||||
if insertErr != nil {
|
||||
return insertErr
|
||||
}
|
||||
if param.ConfigKey == rolepolicy.TemplateConfigKey {
|
||||
template, resolveErr := rolepolicy.Template(ctx)
|
||||
if resolveErr != nil {
|
||||
return resolveErr
|
||||
}
|
||||
_ = template
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if param.ConfigKey == rolepolicy.TemplateConfigKey {
|
||||
template, resolveErr := rolepolicy.Template(ctx)
|
||||
if resolveErr != nil {
|
||||
return nil, resolveErr
|
||||
}
|
||||
if err = rolesync.EnqueueRole(ctx, template.RoleId, contexts.GetUserId(ctx)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
output = &model.SysConfigAddModel{
|
||||
ConfigId: lastInsertId,
|
||||
}
|
||||
event.EventsInstance().Emit(ctx, consts.EventKeyDBSysConfigCreate, lastInsertId, data.TenantId, data.ConfigKey)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysConfig) Edit(ctx context.Context, param *model.SysConfigEditParam) (output *model.SysConfigEditModel, err error) {
|
||||
if param == nil || param.ConfigId == 0 {
|
||||
return nil, errors.New("配置ID不能为空")
|
||||
}
|
||||
|
||||
var current *entity.SysConfig
|
||||
if err := s.Model(ctx).Where("config_id", param.ConfigId).Scan(¤t); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if current == nil {
|
||||
return nil, errors.New("参数不存在")
|
||||
}
|
||||
nextKey, nextValue := current.ConfigKey, current.ConfigValue
|
||||
if param.ConfigKey != nil {
|
||||
nextKey = *param.ConfigKey
|
||||
}
|
||||
if param.ConfigValue != nil {
|
||||
nextValue = *param.ConfigValue
|
||||
}
|
||||
if current.ConfigKey == rolepolicy.TemplateConfigKey || nextKey == rolepolicy.TemplateConfigKey {
|
||||
if !contexts.IsSuperAdmin(ctx) || current.TenantId != consts.DefaultSystemTenantCode {
|
||||
return nil, errors.New("仅平台超级管理员可配置代理模板")
|
||||
}
|
||||
if current.ConfigKey != nextKey {
|
||||
return nil, errors.New("代理模板参数键不允许更改,请修改角色ID")
|
||||
}
|
||||
nextTemplate, err := rolepolicy.ValidateTemplate(ctx, nextValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
previousTemplate, previousErr := rolepolicy.ValidateTemplate(ctx, current.ConfigValue)
|
||||
if previousErr == nil && previousTemplate.DeptId != nextTemplate.DeptId {
|
||||
return nil, errors.New("模板角色必须属于同一机构,跨机构切换需要迁移下级授权")
|
||||
}
|
||||
}
|
||||
data := &do.SysConfig{}
|
||||
gconv.Struct(param, data)
|
||||
data.UpdatedBy = contexts.GetUserId(ctx)
|
||||
data.UpdatedAt = gtime.Now()
|
||||
data.ConfigId = nil
|
||||
|
||||
m := s.Model(ctx)
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
if _, updateErr := m.Ctx(ctx).Data(data).Where(dao.SysConfig.Columns().ConfigId, param.ConfigId).Update(); updateErr != nil {
|
||||
return updateErr
|
||||
}
|
||||
if nextKey == rolepolicy.TemplateConfigKey {
|
||||
template, resolveErr := rolepolicy.Template(ctx)
|
||||
if resolveErr != nil {
|
||||
return resolveErr
|
||||
}
|
||||
return rolesync.EnqueueRole(ctx, template.RoleId, contexts.GetUserId(ctx))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if nextKey == rolepolicy.TemplateConfigKey {
|
||||
template, resolveErr := rolepolicy.Template(ctx)
|
||||
if resolveErr != nil {
|
||||
return nil, resolveErr
|
||||
}
|
||||
if err = rolesync.EnqueueRole(ctx, template.RoleId, contexts.GetUserId(ctx)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
output = &model.SysConfigEditModel{
|
||||
ConfigId: param.ConfigId,
|
||||
}
|
||||
event.EventsInstance().Emit(ctx, consts.EventKeyDBSysConfigUpdate, param.ConfigId, contexts.GetTenantId(ctx), nextKey)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysConfig) Delete(ctx context.Context, param *model.SysConfigDeleteParam) (output *model.SysConfigDeleteModel, err error) {
|
||||
m := s.Model(ctx)
|
||||
|
||||
if len(param.ConfigIds) == 0 {
|
||||
return nil, errors.New("请选择要删除的配置")
|
||||
}
|
||||
count, checkErr := dao.SysConfig.Ctx(ctx).WhereIn("config_id", param.ConfigIds).Where("tenant_id", consts.DefaultSystemTenantCode).Where("config_key", rolepolicy.TemplateConfigKey).Count()
|
||||
if checkErr != nil {
|
||||
return nil, checkErr
|
||||
}
|
||||
if count > 0 {
|
||||
return nil, errors.New("代理模板参数不能删除,请通过修改角色ID切换模板")
|
||||
}
|
||||
m = m.WhereIn(dao.SysConfig.Columns().ConfigId, param.ConfigIds)
|
||||
data := make([]entity.SysConfig, 0)
|
||||
err = m.Scan(&data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range data {
|
||||
if v.ConfigType == "Y" {
|
||||
return nil, errors.New("系统内置配置不能删除")
|
||||
}
|
||||
}
|
||||
|
||||
_, err = m.Delete()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.EventsInstance().Emit(ctx, consts.EventKeyDBSysConfigDelete, param.ConfigIds, contexts.GetTenantId(ctx))
|
||||
return &model.SysConfigDeleteModel{
|
||||
ConfigIds: param.ConfigIds,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sSysConfig) View(ctx context.Context, param *model.SysConfigViewParam) (output *model.SysConfigViewModel, err error) {
|
||||
m := s.ModelQuery(ctx)
|
||||
|
||||
err = m.Where(dao.SysConfig.Columns().ConfigId, param.ConfigId).Scan(&output)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysConfig) GetConfigByKey(ctx context.Context, configKey string) (config *entity.SysConfig, err error) {
|
||||
m := s.ModelQuery(ctx)
|
||||
err = m.Where(dao.SysConfig.Columns().ConfigKey, configKey).Scan(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user