1
This commit is contained in:
29
internal/app/common/service/cache.go
Normal file
29
internal/app/common/service/cache.go
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* @desc:缓存处理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu
|
||||
* @Date: 2022/3/9 11:15
|
||||
*/
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/tiger1103/gfast-cache/cache"
|
||||
)
|
||||
|
||||
type ICache interface {
|
||||
cache.IGCache
|
||||
}
|
||||
|
||||
var c ICache
|
||||
|
||||
func Cache() ICache {
|
||||
if c == nil {
|
||||
panic("implement not found for interface ICache, forgot register?")
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func RegisterCache(che ICache) {
|
||||
c = che
|
||||
}
|
||||
28
internal/app/common/service/captcha.go
Normal file
28
internal/app/common/service/captcha.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// ================================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type ICaptcha interface {
|
||||
GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string, err error)
|
||||
VerifyString(id, answer string) bool
|
||||
}
|
||||
|
||||
var localCaptcha ICaptcha
|
||||
|
||||
func Captcha() ICaptcha {
|
||||
if localCaptcha == nil {
|
||||
panic("implement not found for interface ICaptcha, forgot register?")
|
||||
}
|
||||
return localCaptcha
|
||||
}
|
||||
|
||||
func RegisterCaptcha(i ICaptcha) {
|
||||
localCaptcha = i
|
||||
}
|
||||
238
internal/app/common/service/casbin.go
Normal file
238
internal/app/common/service/casbin.go
Normal file
@@ -0,0 +1,238 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/casbin/casbin/v2"
|
||||
"github.com/casbin/casbin/v2/model"
|
||||
"github.com/casbin/casbin/v2/persist"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type adapterCasbin struct {
|
||||
Enforcer *casbin.SyncedEnforcer
|
||||
EnforcerErr error
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
en *casbin.SyncedEnforcer
|
||||
enErr error
|
||||
)
|
||||
|
||||
// CasbinEnforcer 获取adapter单例对象
|
||||
func CasbinEnforcer(ctx context.Context) (enforcer *casbin.SyncedEnforcer, err error) {
|
||||
ac := newAdapter(ctx)
|
||||
enforcer = ac.Enforcer
|
||||
err = ac.EnforcerErr
|
||||
return
|
||||
}
|
||||
|
||||
// 初始化adapter操作
|
||||
func newAdapter(ctx context.Context) (a *adapterCasbin) {
|
||||
a = new(adapterCasbin)
|
||||
a.ctx = ctx
|
||||
once.Do(func() {
|
||||
en, enErr = initPolicy(ctx, a)
|
||||
})
|
||||
if enErr == nil && en != nil {
|
||||
en.SetAdapter(a)
|
||||
}
|
||||
a.Enforcer, a.EnforcerErr = en, enErr
|
||||
return
|
||||
}
|
||||
|
||||
func initPolicy(ctx context.Context, a *adapterCasbin) (e *casbin.SyncedEnforcer, err error) {
|
||||
// Because the DB is empty at first,
|
||||
// so we need to load the policy from the file adapter (.CSV) first.
|
||||
e, err = casbin.NewSyncedEnforcer(g.Cfg().MustGet(ctx, "casbin.modelFile").String(), a)
|
||||
return
|
||||
}
|
||||
|
||||
// SavePolicy saves policy to database.
|
||||
func (a *adapterCasbin) SavePolicy(model model.Model) (err error) {
|
||||
err = a.dropTable()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = a.createTable()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for ptype, ast := range model["p"] {
|
||||
for _, rule := range ast.Policy {
|
||||
line := savePolicyLine(ptype, rule)
|
||||
_, err := dao.CasbinRule.Ctx(a.ctx).Data(line).Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for ptype, ast := range model["g"] {
|
||||
for _, rule := range ast.Policy {
|
||||
line := savePolicyLine(ptype, rule)
|
||||
_, err := dao.CasbinRule.Ctx(a.ctx).Data(line).Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (a *adapterCasbin) dropTable() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (a *adapterCasbin) createTable() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// LoadPolicy loads policy from database.
|
||||
func (a *adapterCasbin) LoadPolicy(model model.Model) error {
|
||||
var lines []*entity.CasbinRule
|
||||
if err := dao.CasbinRule.Ctx(a.ctx).Scan(&lines); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, line := range lines {
|
||||
loadPolicyLine(line, model)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddPolicy adds a policy rule to the storage.
|
||||
func (a *adapterCasbin) AddPolicy(sec string, ptype string, rule []string) error {
|
||||
line := savePolicyLine(ptype, rule)
|
||||
_, err := dao.CasbinRule.Ctx(a.ctx).Data(line).Insert()
|
||||
return err
|
||||
}
|
||||
|
||||
// RemovePolicy removes a policy rule from the storage.
|
||||
func (a *adapterCasbin) RemovePolicy(sec string, ptype string, rule []string) error {
|
||||
line := savePolicyLine(ptype, rule)
|
||||
err := rawDelete(a, line)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *adapterCasbin) AddPolicies(sec string, ptype string, rules [][]string) error {
|
||||
lines := make([]*entity.CasbinRule, len(rules))
|
||||
for k, rule := range rules {
|
||||
lines[k] = savePolicyLine(ptype, rule)
|
||||
}
|
||||
_, err := dao.CasbinRule.Ctx(a.ctx).Data(lines).Insert()
|
||||
return err
|
||||
}
|
||||
|
||||
// RemovePolicies removes policy rules from the storage.
|
||||
// This is part of the Auto-Save feature.
|
||||
func (a *adapterCasbin) RemovePolicies(sec string, ptype string, rules [][]string) error {
|
||||
for _, rule := range rules {
|
||||
err := a.RemovePolicy(sec, ptype, rule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
|
||||
func (a *adapterCasbin) RemoveFilteredPolicy(sec string, ptype string,
|
||||
fieldIndex int, fieldValues ...string) error {
|
||||
line := &entity.CasbinRule{}
|
||||
line.Ptype = ptype
|
||||
if fieldIndex <= 0 && 0 < fieldIndex+len(fieldValues) {
|
||||
line.V0 = fieldValues[0-fieldIndex]
|
||||
}
|
||||
if fieldIndex <= 1 && 1 < fieldIndex+len(fieldValues) {
|
||||
line.V1 = fieldValues[1-fieldIndex]
|
||||
}
|
||||
if fieldIndex <= 2 && 2 < fieldIndex+len(fieldValues) {
|
||||
line.V2 = fieldValues[2-fieldIndex]
|
||||
}
|
||||
if fieldIndex <= 3 && 3 < fieldIndex+len(fieldValues) {
|
||||
line.V3 = fieldValues[3-fieldIndex]
|
||||
}
|
||||
if fieldIndex <= 4 && 4 < fieldIndex+len(fieldValues) {
|
||||
line.V4 = fieldValues[4-fieldIndex]
|
||||
}
|
||||
if fieldIndex <= 5 && 5 < fieldIndex+len(fieldValues) {
|
||||
line.V5 = fieldValues[5-fieldIndex]
|
||||
}
|
||||
err := rawDelete(a, line)
|
||||
return err
|
||||
}
|
||||
|
||||
func loadPolicyLine(line *entity.CasbinRule, model model.Model) {
|
||||
lineText := line.Ptype
|
||||
if line.V0 != "" {
|
||||
lineText += ", " + line.V0
|
||||
}
|
||||
if line.V1 != "" {
|
||||
lineText += ", " + line.V1
|
||||
}
|
||||
if line.V2 != "" {
|
||||
lineText += ", " + line.V2
|
||||
}
|
||||
if line.V3 != "" {
|
||||
lineText += ", " + line.V3
|
||||
}
|
||||
if line.V4 != "" {
|
||||
lineText += ", " + line.V4
|
||||
}
|
||||
if line.V5 != "" {
|
||||
lineText += ", " + line.V5
|
||||
}
|
||||
persist.LoadPolicyLine(lineText, model)
|
||||
}
|
||||
|
||||
func savePolicyLine(ptype string, rule []string) *entity.CasbinRule {
|
||||
line := &entity.CasbinRule{}
|
||||
line.Ptype = ptype
|
||||
if len(rule) > 0 {
|
||||
line.V0 = rule[0]
|
||||
}
|
||||
if len(rule) > 1 {
|
||||
line.V1 = rule[1]
|
||||
}
|
||||
if len(rule) > 2 {
|
||||
line.V2 = rule[2]
|
||||
}
|
||||
if len(rule) > 3 {
|
||||
line.V3 = rule[3]
|
||||
}
|
||||
if len(rule) > 4 {
|
||||
line.V4 = rule[4]
|
||||
}
|
||||
if len(rule) > 5 {
|
||||
line.V5 = rule[5]
|
||||
}
|
||||
return line
|
||||
}
|
||||
|
||||
func rawDelete(a *adapterCasbin, line *entity.CasbinRule) error {
|
||||
db := dao.CasbinRule.Ctx(a.ctx).Where("ptype = ?", line.Ptype)
|
||||
if line.V0 != "" {
|
||||
db = db.Where("v0 = ?", line.V0)
|
||||
}
|
||||
if line.V1 != "" {
|
||||
db = db.Where("v1 = ?", line.V1)
|
||||
}
|
||||
if line.V2 != "" {
|
||||
db = db.Where("v2 = ?", line.V2)
|
||||
}
|
||||
if line.V3 != "" {
|
||||
db = db.Where("v3 = ?", line.V3)
|
||||
}
|
||||
if line.V4 != "" {
|
||||
db = db.Where("v4 = ?", line.V4)
|
||||
}
|
||||
if line.V5 != "" {
|
||||
db = db.Where("v5 = ?", line.V5)
|
||||
}
|
||||
_, err := db.Delete()
|
||||
return err
|
||||
}
|
||||
23
internal/app/common/service/event_bus.go
Normal file
23
internal/app/common/service/event_bus.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// ================================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
eventBus "github.com/asaskevich/EventBus"
|
||||
)
|
||||
|
||||
var localEventBus eventBus.Bus
|
||||
|
||||
func EventBus() eventBus.Bus {
|
||||
if localEventBus == nil {
|
||||
panic("implement not found for interface EventBus, forgot register?")
|
||||
}
|
||||
return localEventBus
|
||||
}
|
||||
|
||||
func RegisterEventBus(i eventBus.Bus) {
|
||||
localEventBus = i
|
||||
}
|
||||
27
internal/app/common/service/middleware.go
Normal file
27
internal/app/common/service/middleware.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// ================================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
type IMiddleware interface {
|
||||
MiddlewareCORS(r *ghttp.Request)
|
||||
}
|
||||
|
||||
var localMiddleware IMiddleware
|
||||
|
||||
func Middleware() IMiddleware {
|
||||
if localMiddleware == nil {
|
||||
panic("implement not found for interface IMiddleware, forgot register?")
|
||||
}
|
||||
return localMiddleware
|
||||
}
|
||||
|
||||
func RegisterMiddleware(i IMiddleware) {
|
||||
localMiddleware = i
|
||||
}
|
||||
37
internal/app/common/service/sys_config.go
Normal file
37
internal/app/common/service/sys_config.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// ================================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
|
||||
)
|
||||
|
||||
type ISysConfig interface {
|
||||
List(ctx context.Context, req *system.ConfigSearchReq) (res *system.ConfigSearchRes, err error)
|
||||
Add(ctx context.Context, req *system.ConfigAddReq, userId uint64) (err error)
|
||||
CheckConfigKeyUnique(ctx context.Context, configKey string, configId ...int64) (err error)
|
||||
Get(ctx context.Context, id int) (res *system.ConfigGetRes, err error)
|
||||
Edit(ctx context.Context, req *system.ConfigEditReq, userId uint64) (err error)
|
||||
Delete(ctx context.Context, ids []int) (err error)
|
||||
GetConfigByKey(ctx context.Context, key string) (config *entity.SysConfig, err error)
|
||||
GetByKey(ctx context.Context, key string) (config *entity.SysConfig, err error)
|
||||
}
|
||||
|
||||
var localSysConfig ISysConfig
|
||||
|
||||
func SysConfig() ISysConfig {
|
||||
if localSysConfig == nil {
|
||||
panic("implement not found for interface ISysConfig, forgot register?")
|
||||
}
|
||||
return localSysConfig
|
||||
}
|
||||
|
||||
func RegisterSysConfig(i ISysConfig) {
|
||||
localSysConfig = i
|
||||
}
|
||||
35
internal/app/common/service/sys_dict_data.go
Normal file
35
internal/app/common/service/sys_dict_data.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// ================================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
)
|
||||
|
||||
type ISysDictData interface {
|
||||
GetDictDataTree(ctx context.Context, dictType string) (res *system.GetDictTreeRes, err error)
|
||||
GetDictWithDataByType(ctx context.Context, req *system.GetDictReq) (dict *system.GetDictRes, err error)
|
||||
List(ctx context.Context, req *system.DictDataSearchReq) (res *system.DictDataSearchRes, err error)
|
||||
Add(ctx context.Context, req *system.DictDataAddReq, userId uint64) (err error)
|
||||
Get(ctx context.Context, dictCode uint) (res *system.DictDataGetRes, err error)
|
||||
Edit(ctx context.Context, req *system.DictDataEditReq, userId uint64) (err error)
|
||||
Delete(ctx context.Context, ids []int) (err error)
|
||||
}
|
||||
|
||||
var localSysDictData ISysDictData
|
||||
|
||||
func SysDictData() ISysDictData {
|
||||
if localSysDictData == nil {
|
||||
panic("implement not found for interface ISysDictData, forgot register?")
|
||||
}
|
||||
return localSysDictData
|
||||
}
|
||||
|
||||
func RegisterSysDictData(i ISysDictData) {
|
||||
localSysDictData = i
|
||||
}
|
||||
36
internal/app/common/service/sys_dict_type.go
Normal file
36
internal/app/common/service/sys_dict_type.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// ================================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
|
||||
)
|
||||
|
||||
type ISysDictType interface {
|
||||
List(ctx context.Context, req *system.DictTypeSearchReq) (res *system.DictTypeSearchRes, err error)
|
||||
Add(ctx context.Context, req *system.DictTypeAddReq, userId uint64) (err error)
|
||||
Edit(ctx context.Context, req *system.DictTypeEditReq, userId uint64) (err error)
|
||||
Get(ctx context.Context, req *system.DictTypeGetReq) (dictType *entity.SysDictType, err error)
|
||||
ExistsDictType(ctx context.Context, dictType string, dictId ...int64) (err error)
|
||||
Delete(ctx context.Context, dictIds []int) (err error)
|
||||
GetAllDictType(ctx context.Context) (list []*entity.SysDictType, err error)
|
||||
}
|
||||
|
||||
var localSysDictType ISysDictType
|
||||
|
||||
func SysDictType() ISysDictType {
|
||||
if localSysDictType == nil {
|
||||
panic("implement not found for interface ISysDictType, forgot register?")
|
||||
}
|
||||
return localSysDictType
|
||||
}
|
||||
|
||||
func RegisterSysDictType(i ISysDictType) {
|
||||
localSysDictType = i
|
||||
}
|
||||
Reference in New Issue
Block a user