simple_admin

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2020 License: MIT Imports: 25 Imported by: 0

README

about simple_admin

First time , very thanks for iris framework and developer to helps~

Now talk something , this library just resolve a small and simple problem :

Xorm or gorm Why not provide a simple or small struct model visible dashboard

My trust 99% golang uses people need this

Wait long time not has , start with me , but i time and ability is small and limited !

So I'm just use i familiar technology :

  • iris
  • xorm
  • casbin -> rbac
  • react -> ant.design

Hope to help everyone !

use

Install

go get https://github.com/23233/simple_admin

Examples -> https://github.com/23233/simple_admin/tree/master/_examples


Ready -> Defined you struct

type TestModelA struct {
	Id   uint64 `xorm:"autoincr pk unique" json:"id"`
	Name string `xorm:"varchar(20)"`
}

type TestModelB struct {
	Id   uint64 `xorm:"autoincr pk unique" json:"id"`
	Desc string `xorm:"varchar(60)"`
}

Ready -> Defined you xorm engine


Engine, err = xorm.NewEngine("mysql", dbUrl)

Ready -> Defined iris application

app := iris.New()

Go -> Register simple_admin

modelList := []interface{}{new(TestModelA), new(TestModelB)}

_, err := simple_admin.New(simple_admin.Config{
    Engine:    engine,
    App:       app,
    ModelList: modelList,
    Name:      "app name",
    RunSync:   true, // this is xorm sync2
    Prefix:    "/admin", // path prefix like app.Prefix("/admin")
})

Go -> Run you app open browser http://127.0.0.1:8080/admin Yes god job

app.Listen(":8080")

warning

  • first, now don't support xorm deleted tag
  • the best you do not use custom usermodel , admin isolation is good!

model custom tags (sp) now support

  • autogen -> mark columns is code auto generate not handle
  • lineTo -> if custom action , default value use this define for select row

todo features

  • [] full test
  • [] gorm support
  • [] add websocket dashboard
  • custom action
  • [] fine permission manage
  • [] beat more features

Documentation

Overview

Code generated for package main by go-bindata DO NOT EDIT. (@generated) sources: simple_admin_templates/simple_admin.template simple_admin_templates/umi.css simple_admin_templates/umi.js

Index

Constants

This section is empty.

Variables

View Source
var (
	NowSpAdmin    *SpAdmin
	SvKey         = sv.GlobalContextKey
	DefaultPrefix = "/SP_PREFIX"
)
View Source
var CustomJwt = jwt.New(jwt.Config{
	ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
		return MySecret, nil
	},
	Expiration:    true,
	SigningMethod: jwt.SigningMethodHS256,
})

自定义JWT 使用办法 中间层 handler.CustomJwt.Serve, handler.TokenToUserUidMiddleware,user handler

View Source
var MySecret = []byte("8657684ae02840ead423e0d781a7a0c5")

Functions

func AddRouterData

func AddRouterData(ctx iris.Context)

新增数据

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func ChangeUserPassword

func ChangeUserPassword(ctx iris.Context)

变更用户密码

func ChangeUserRoles

func ChangeUserRoles(ctx iris.Context)

变更用户群组

func Configuration

func Configuration(ctx iris.Context)

获取配置信息

func EditRouterData

func EditRouterData(ctx iris.Context)

修改数据 -> 全量更新

func GenJwtToken

func GenJwtToken(userUid, userName string) string

生成token

func GetCurrentUser

func GetCurrentUser(ctx iris.Context)

获取当前用户

func GetMapValues

func GetMapValues(a map[string]string) []string

func GetRouterCustomAction added in v1.0.8

func GetRouterCustomAction(ctx iris.Context)

获取单个表的自定义操作

func GetRouterData

func GetRouterData(ctx iris.Context)

获取表数据

func GetRouterFields

func GetRouterFields(ctx iris.Context)

获取单个表的列信息

func GetRouterSingleData

func GetRouterSingleData(ctx iris.Context)

获取表单条数据

func GetRouters

func GetRouters(ctx iris.Context)

获取所有表名

func Index

func Index(ctx iris.Context)

func IsNum

func IsNum(s string) bool

func Login

func Login(ctx iris.Context)

登录

func MsgLog

func MsgLog(msg string) error

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func PolicyRequireAdminMiddleware

func PolicyRequireAdminMiddleware(ctx iris.Context)

必须admin权限middleware

func PolicyValidMiddleware

func PolicyValidMiddleware(ctx iris.Context)

权限Middleware

func RandStringBytes

func RandStringBytes(n int) string

func Reg

func Reg(ctx iris.Context)

注册

func RemoveRouterData

func RemoveRouterData(ctx iris.Context)

删除数据 -> 可以批量

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

func StringsContains

func StringsContains(a []string, x string) bool

func TokenToUserUidMiddleware

func TokenToUserUidMiddleware(ctx iris.Context)

登录token存储信息 记录到上下文中

Types

type Config

type Config struct {
	Name                       string            // 后台显示名称
	Engine                     *xorm.Engine      // xorm engine实例
	App                        *iris.Application // iris实例
	ModelList                  []interface{}     // 模型列表
	UserModel                  interface{}       // 用户模型
	RunSync                    bool              // 是否进行sync
	PageSize                   int
	AbridgeName                string
	Prefix                     string
	InitAdminUserName          string
	InitAdminPassword          string
	UserModelSpecialUniqueName string
	CustomAction               []CustomAction
}

type ConfigResp added in v1.0.2

type ConfigResp struct {
	Name          string `json:"name"`
	Prefix        string `json:"prefix"`
	UserModelName string `json:"user_model_name"`
}

type CustomAction added in v1.0.8

type CustomAction struct {
	Name    string        `json:"name"`    // action display name
	Methods string        `json:"methods"` // request run methods
	Valid   interface{}   `json:"valid"`   // request valid struct
	Path    string        `json:"path"`    // request path
	Scope   []interface{} `json:"scope"`   // show where
	Func    func(ctx context.Context)
}

自定义action

type CustomActionResp added in v1.0.8

type CustomActionResp struct {
	Name    string       `json:"name"`    // action display name
	Methods string       `json:"methods"` // request run methods
	Fields  []structInfo `json:"fields"`  // request valid struct
	Path    string       `json:"path"`    // request path
}

type DeleteReq added in v1.0.2

type DeleteReq struct {
	Ids string `json:"ids" form:"ids" comment:"标识符列表" validate:"required,min=1"`
}

type GetCurrentUserResp added in v1.0.2

type GetCurrentUserResp struct {
	Name   string   `json:"name"`
	Avatar string   `json:"avatar"`
	UserId string   `json:"userid"`
	Roles  []string `json:"roles"`
}

type GlobalResp added in v1.0.2

type GlobalResp struct {
	Code    uint16      `json:"code"`    // 状态码
	Message string      `json:"message"` // 消息
	Data    interface{} `json:"data"`
}

所有返回必须包裹一层resp

type PagResult

type PagResult struct {
	All      int64               `json:"all"`
	Page     int                 `json:"page"`
	PageSize int                 `json:"page_size"`
	Data     []map[string]string `json:"data"`
}

type Policy

type Policy struct {
	Path   string `json:"path"`
	Method string `json:"method"`
}

type SpAdmin

type SpAdmin struct {
	// contains filtered or unexported fields
}

func New

func New(c Config) (*SpAdmin, error)

func (*SpAdmin) Pagination

func (lib *SpAdmin) Pagination(routerName string, page int) (PagResult, error)

分页

func (*SpAdmin) Register

func (lib *SpAdmin) Register()

注册视图

func (*SpAdmin) Router

func (lib *SpAdmin) Router(router iris.Party)

在这里注册主路由

func (*SpAdmin) SingleData

func (lib *SpAdmin) SingleData(routerName string, id uint64) (map[string]string, error)

单条数据获取

type TableFieldsResp added in v1.0.3

type TableFieldsResp struct {
	Fields   []structInfo `json:"fields"`
	Autoincr string       `json:"autoincr"`
	Updated  string       `json:"updated"`
	Deleted  string       `json:"deleted"`
	Version  string       `json:"version"`
}

type UserChangePasswordReq added in v1.0.2

type UserChangePasswordReq struct {
	Id       uint64 `json:"id" comment:"id" validate:"required"`
	UserName string `json:"user_name" comment:"用户名" validate:"required,max=20,min=3"`
	Password string `json:"password" comment:"密码" validate:"required,min=3,max=20"`
}

用户变更密码

type UserChangeRolesReq added in v1.0.2

type UserChangeRolesReq struct {
	Id   uint64 `json:"id" comment:"id" validate:"required"`
	Role string `json:"role" comment:"群组名" validate:"required"`
	Add  bool   `json:"add" comment:"添加"`
}

admin 变更用户群组

type UserLoginReq added in v1.0.2

type UserLoginReq struct {
	UserName string `json:"user_name" comment:"用户名" validate:"required,max=20,min=3"`
	Password string `json:"password" comment:"密码" validate:"required,min=3,max=20"`
}

用户登录

type UserLoginResp added in v1.0.2

type UserLoginResp struct {
	Token    string             `json:"token"`
	UserInfo GetCurrentUserResp `json:"user_info"`
}

type UserModel

type UserModel struct {
	Id       uint64 `xorm:"autoincr pk unique" json:"id"`
	UserName string `xorm:"varchar(60) notnull unique index" json:"username"`
	Password string `xorm:"varchar(100) notnull" json:"password"`
	Salt     string `xorm:"varchar(40) notnull" json:"salt"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL