├── test ├── 记录.txt ├── casbin │ ├── policy.csv │ ├── model.conf │ ├── casbin_rule.sql │ └── casbin.go ├── goctl │ └── 1.6.0 │ │ ├── model │ │ ├── tag.tpl │ │ ├── field.tpl │ │ ├── interface-delete.tpl │ │ ├── interface-insert.tpl │ │ ├── table-name.tpl │ │ ├── interface-find-one-by-field.tpl │ │ ├── interface-find-one.tpl │ │ ├── interface-update.tpl │ │ ├── err.tpl │ │ ├── model-gen.tpl │ │ ├── types.tpl │ │ ├── model-new.tpl │ │ ├── import-no-cache.tpl │ │ ├── import.tpl │ │ ├── find-one-by-field-extra-method.tpl │ │ ├── insert.tpl │ │ ├── delete.tpl │ │ ├── var.tpl │ │ ├── update.tpl │ │ ├── find-one.tpl │ │ ├── find-one-by-field.tpl │ │ └── model.tpl │ │ ├── api │ │ ├── etc.tpl │ │ ├── route-addition.tpl │ │ ├── types.tpl │ │ ├── config.tpl │ │ ├── routes.tpl │ │ ├── context.tpl │ │ ├── middleware.tpl │ │ ├── logic.tpl │ │ ├── template.tpl │ │ ├── handler.tpl │ │ └── main.tpl │ │ ├── rpc │ │ ├── config.tpl │ │ ├── etc.tpl │ │ ├── svc.tpl │ │ ├── template.tpl │ │ ├── logic-func.tpl │ │ ├── server.tpl │ │ ├── server-func.tpl │ │ ├── logic.tpl │ │ ├── call.tpl │ │ └── main.tpl │ │ ├── mongo │ │ ├── err.tpl │ │ ├── model_types.tpl │ │ └── model_custom.tpl │ │ ├── newapi │ │ └── newtemplate.tpl │ │ ├── gateway │ │ ├── main.tpl │ │ └── etc.tpl │ │ ├── docker │ │ └── docker.tpl │ │ └── kube │ │ └── job.tpl ├── casbin_RBAC-with-domainste_nants │ ├── policy.csv │ ├── model.conf │ ├── casbin_rule.sql │ └── casbin.go ├── testMain │ ├── cron │ │ └── cron.go │ ├── openai │ │ ├── nginx.conf │ │ ├── chatGPT │ │ │ └── chatGPT.go │ │ ├── nginx--.conf │ │ └── gpt3.5 │ │ │ └── gpt3.5.go │ ├── findkey │ │ └── findkey.go │ ├── interface.go │ └── interface_struct │ │ └── interface_struct.go ├── sh │ ├── rpc.bat │ ├── api.bat │ └── model.bat └── gormTest │ └── gormTest.go ├── application └── applet │ ├── api │ ├── desc │ │ └── applet.api │ ├── internal │ │ ├── handler │ │ │ ├── menu │ │ │ │ ├── get_menu_handler.go │ │ │ │ ├── add_base_menu_handler.go │ │ │ │ ├── get_menu_list_handler.go │ │ │ │ ├── delete_base_menu_handler.go │ │ │ │ ├── update_base_menu_handler.go │ │ │ │ ├── get_base_menu_by_id_handler.go │ │ │ │ ├── get_base_menu_tree_handler.go │ │ │ │ └── get_menu_authority_handler.go │ │ │ ├── api │ │ │ │ ├── create_api_handler.go │ │ │ │ ├── delete_api_handler.go │ │ │ │ ├── update_api_handler.go │ │ │ │ ├── get_api_list_handler.go │ │ │ │ ├── get_all_api_list_handler.go │ │ │ │ └── delete_apis_by_ids_handler.go │ │ │ ├── user │ │ │ │ ├── register_handler.go │ │ │ │ ├── delete_user_handler.go │ │ │ │ ├── get_user_list_handler.go │ │ │ │ ├── update_user_info_handler.go │ │ │ │ └── reset_user_password_handler.go │ │ │ ├── base │ │ │ │ ├── send_email_code_handler.go │ │ │ │ └── upload_file_img_handler.go │ │ │ ├── casbin │ │ │ │ ├── update_casbin_data_handler.go │ │ │ │ ├── get_path_by_authority_id_handler.go │ │ │ │ └── update_casbin_data_by_api_ids_handler.go │ │ │ ├── authority │ │ │ │ ├── create_authority_handler.go │ │ │ │ ├── delete_authority_handler.go │ │ │ │ ├── update_authority_handler.go │ │ │ │ ├── add_authority_menu_handler.go │ │ │ │ └── get_authority_list_handler.go │ │ │ ├── dictionary │ │ │ │ ├── create_sys_dictionary_handler.go │ │ │ │ ├── delete_sys_dictionary_handler.go │ │ │ │ ├── update_sys_dictionary_handler.go │ │ │ │ ├── get_sys_dictionary_list_handler.go │ │ │ │ ├── create_sys_dictionary_info_handler.go │ │ │ │ ├── delete_sys_dictionary_info_handler.go │ │ │ │ ├── get_sys_dictionary_details_handler.go │ │ │ │ ├── update_sys_dictionary_info_handler.go │ │ │ │ ├── get_sys_dictionary_info_list_handler.go │ │ │ │ └── get_sys_dictionary_info_list_details_by_id_handler.go │ │ │ └── usernocasbin │ │ │ │ ├── login_handler.go │ │ │ │ └── random_image_handler.go │ │ ├── config │ │ │ └── config.go │ │ └── logic │ │ │ ├── menu │ │ │ ├── add_base_menu_logic.go │ │ │ ├── delete_base_menu_logic.go │ │ │ ├── update_base_menu_logic.go │ │ │ ├── get_base_menu_by_id_logic.go │ │ │ ├── get_base_menu_tree_logic.go │ │ │ ├── get_menu_logic.go │ │ │ ├── get_menu_authority_logic.go │ │ │ └── get_menu_list_logic.go │ │ │ ├── api │ │ │ ├── delete_apis_by_ids_logic.go │ │ │ ├── delete_api_logic.go │ │ │ ├── update_api_logic.go │ │ │ ├── get_all_api_list_logic.go │ │ │ └── create_api_logic.go │ │ │ ├── authority │ │ │ ├── delete_authority_logic.go │ │ │ ├── add_authority_menu_logic.go │ │ │ ├── create_authority_logic.go │ │ │ ├── update_authority_logic.go │ │ │ └── get_authority_list_logic.go │ │ │ ├── user │ │ │ ├── register_logic.go │ │ │ ├── reset_user_password_logic.go │ │ │ ├── delete_user_logic.go │ │ │ ├── get_user_list_logic.go │ │ │ └── update_user_info_logic.go │ │ │ ├── dictionary │ │ │ ├── delete_sys_dictionary_logic.go │ │ │ ├── delete_sys_dictionary_info_logic.go │ │ │ ├── create_sys_dictionary_logic.go │ │ │ ├── update_sys_dictionary_logic.go │ │ │ ├── update_sys_dictionary_info_logic.go │ │ │ ├── create_sys_dictionary_info_logic.go │ │ │ ├── get_sys_dictionary_list_logic.go │ │ │ ├── get_sys_dictionary_info_list_details_by_id_logic.go │ │ │ ├── get_sys_dictionary_details_logic.go │ │ │ └── get_sys_dictionary_info_list_logic.go │ │ │ ├── casbin │ │ │ └── update_casbin_data_logic.go │ │ │ ├── base │ │ │ └── upload_file_img_logic.go │ │ │ └── usernocasbin │ │ │ └── random_image_logic.go │ ├── etc │ │ └── applet-api.yaml │ └── applet.go │ └── rpc │ ├── internal │ ├── model │ │ ├── sys_authority_btn.go │ │ ├── sys_user_authority.go │ │ ├── sys_api.go │ │ ├── sys_authority_menu.go │ │ ├── sys_dictionary.go │ │ ├── sys_dictionary_info.go │ │ └── sys_authority.go │ ├── config │ │ └── config.go │ ├── svc │ │ └── service_context.go │ ├── logic │ │ ├── dictionary │ │ │ ├── delete_sys_dictionary_info_logic.go │ │ │ ├── get_sys_dictionary_list_logic.go │ │ │ ├── create_sys_dictionary_info_logic.go │ │ │ ├── get_sys_dictionary_details_logic.go │ │ │ ├── create_sys_dictionary_logic.go │ │ │ ├── delete_sys_dictionary_logic.go │ │ │ ├── update_sys_dictionary_logic.go │ │ │ ├── get_sys_dictionary_info_list_details_by_id_logic.go │ │ │ ├── update_sys_dictionary_info_logic.go │ │ │ └── get_sys_dictionary_info_list_logic.go │ │ ├── user │ │ │ ├── reset_user_password_logic.go │ │ │ ├── delete_user_logic.go │ │ │ ├── get_user_list_logic.go │ │ │ └── get_user_toke_logic.go │ │ ├── api │ │ │ ├── get_all_api_list_logic.go │ │ │ ├── create_api_logic.go │ │ │ ├── delete_api_logic.go │ │ │ ├── delete_apis_by_ids_logic.go │ │ │ └── update_api_logic.go │ │ ├── menu │ │ │ ├── get_base_menu_by_id_logic.go │ │ │ ├── add_menu_base_logic.go │ │ │ ├── delete_base_menu_logic.go │ │ │ └── get_menu_authority_logic.go │ │ ├── casbin │ │ │ ├── get_path_by_authority_id_logic.go │ │ │ └── update_casbin_data_logic.go │ │ └── authority │ │ │ └── update_authority_logic.go │ └── server │ │ ├── casbin │ │ └── casbin_server.go │ │ ├── api │ │ └── api_server.go │ │ └── authority │ │ └── authority_server.go │ ├── etc │ └── applet.yaml │ └── applet.go ├── data └── doc │ ├── api.png │ ├── menu.png │ ├── user.png │ ├── authority.png │ ├── dictionary.png │ └── dictionary-usage.png ├── .idea ├── .gitignore ├── vcs.xml ├── modules.xml └── go-zero-admin.iml ├── pkg ├── model │ └── model.go ├── hash │ └── hash.go ├── result │ ├── xerr │ │ ├── errCode.go │ │ ├── errors.go │ │ └── errMsg.go │ ├── responseBean.go │ └── httpResult.go └── orm │ └── metric.go ├── .gitignore └── README.en.md /test/记录.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /test/casbin/policy.csv: -------------------------------------------------------------------------------- 1 | p,zhangsan,data1,read -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/tag.tpl: -------------------------------------------------------------------------------- 1 | `db:"{{.field}}"` -------------------------------------------------------------------------------- /application/applet/api/desc/applet.api: -------------------------------------------------------------------------------- 1 | import "./sys_base/user.api" -------------------------------------------------------------------------------- /test/casbin_RBAC-with-domainste_nants/policy.csv: -------------------------------------------------------------------------------- 1 | p,zhangsan,data1,read -------------------------------------------------------------------------------- /data/doc/api.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yh-zero/go-zero-admin/HEAD/data/doc/api.png -------------------------------------------------------------------------------- /data/doc/menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yh-zero/go-zero-admin/HEAD/data/doc/menu.png -------------------------------------------------------------------------------- /data/doc/user.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yh-zero/go-zero-admin/HEAD/data/doc/user.png -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/etc.tpl: -------------------------------------------------------------------------------- 1 | Name: {{.serviceName}} 2 | Host: {{.host}} 3 | Port: {{.port}} 4 | -------------------------------------------------------------------------------- /data/doc/authority.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yh-zero/go-zero-admin/HEAD/data/doc/authority.png -------------------------------------------------------------------------------- /data/doc/dictionary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yh-zero/go-zero-admin/HEAD/data/doc/dictionary.png -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/field.tpl: -------------------------------------------------------------------------------- 1 | {{.name}} {{.type}} {{.tag}} {{if .hasComment}}// {{.comment}}{{end}} -------------------------------------------------------------------------------- /data/doc/dictionary-usage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yh-zero/go-zero-admin/HEAD/data/doc/dictionary-usage.png -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/interface-delete.tpl: -------------------------------------------------------------------------------- 1 | Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/interface-insert.tpl: -------------------------------------------------------------------------------- 1 | Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/table-name.tpl: -------------------------------------------------------------------------------- 1 | func (m *default{{.upperStartCamelObject}}Model) tableName() string { 2 | return m.table 3 | } 4 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/interface-find-one-by-field.tpl: -------------------------------------------------------------------------------- 1 | FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/route-addition.tpl: -------------------------------------------------------------------------------- 1 | 2 | server.AddRoutes( 3 | {{.routes}} {{.jwt}}{{.signature}} {{.prefix}} {{.timeout}} {{.maxBytes}} 4 | ) 5 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/interface-find-one.tpl: -------------------------------------------------------------------------------- 1 | FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/interface-update.tpl: -------------------------------------------------------------------------------- 1 | Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/err.tpl: -------------------------------------------------------------------------------- 1 | package {{.pkg}} 2 | 3 | import "github.com/zeromicro/go-zero/core/stores/sqlx" 4 | 5 | var ErrNotFound = sqlx.ErrNotFound 6 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/config.tpl: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import "github.com/zeromicro/go-zero/zrpc" 4 | 5 | type Config struct { 6 | zrpc.RpcServerConf 7 | } 8 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/types.tpl: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT. 2 | package types{{if .containsTime}} 3 | import ( 4 | "time" 5 | ){{end}} 6 | {{.types}} 7 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/etc.tpl: -------------------------------------------------------------------------------- 1 | Name: {{.serviceName}}.rpc 2 | ListenOn: 0.0.0.0:8080 3 | Etcd: 4 | Hosts: 5 | - 127.0.0.1:2379 6 | Key: {{.serviceName}}.rpc 7 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/config.tpl: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import {{.authImport}} 4 | 5 | type Config struct { 6 | rest.RestConf 7 | {{.auth}} 8 | {{.jwtTrans}} 9 | } 10 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # 默认忽略的文件 2 | /shelf/ 3 | /workspace.xml 4 | # 基于编辑器的 HTTP 客户端请求 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/mongo/err.tpl: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import ( 4 | "errors" 5 | 6 | "github.com/zeromicro/go-zero/core/stores/mon" 7 | ) 8 | 9 | var ( 10 | ErrNotFound = mon.ErrNotFound 11 | ErrInvalidObjectId = errors.New("invalid objectId") 12 | ) 13 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/model-gen.tpl: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT. 2 | 3 | package {{.pkg}} 4 | {{.imports}} 5 | {{.vars}} 6 | {{.types}} 7 | {{.new}} 8 | {{.delete}} 9 | {{.find}} 10 | {{.insert}} 11 | {{.update}} 12 | {{.extraMethod}} 13 | {{.tableName}} 14 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/svc.tpl: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import {{.imports}} 4 | 5 | type ServiceContext struct { 6 | Config config.Config 7 | } 8 | 9 | func NewServiceContext(c config.Config) *ServiceContext { 10 | return &ServiceContext{ 11 | Config:c, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/routes.tpl: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT. 2 | package handler 3 | 4 | import ( 5 | "net/http"{{if .hasTimeout}} 6 | "time"{{end}} 7 | 8 | {{.importPackages}} 9 | ) 10 | 11 | func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { 12 | {{.routesAdditions}} 13 | } 14 | -------------------------------------------------------------------------------- /pkg/model/model.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import ( 4 | "gorm.io/gorm" 5 | "time" 6 | ) 7 | 8 | type MODEL_BASE struct { 9 | ID int64 `gorm:"primarykey"` // 主键ID 10 | CreatedAt time.Time // 创建时间 11 | UpdatedAt time.Time // 更新时间 12 | DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` // 删除时间 13 | } 14 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/newapi/newtemplate.tpl: -------------------------------------------------------------------------------- 1 | syntax = "v1" 2 | 3 | type Request { 4 | Name string `path:"name,options=you|me"` 5 | } 6 | 7 | type Response { 8 | Message string `json:"message"` 9 | } 10 | 11 | service {{.name}}-api { 12 | @handler {{.handler}}Handler 13 | get /from/:name(Request) returns (Response) 14 | } 15 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/template.tpl: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package {{.package}}; 4 | option go_package="./{{.package}}"; 5 | 6 | message Request { 7 | string ping = 1; 8 | } 9 | 10 | message Response { 11 | string pong = 1; 12 | } 13 | 14 | service {{.serviceName}} { 15 | rpc Ping(Request) returns(Response); 16 | } 17 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/context.tpl: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import ( 4 | {{.configImport}} 5 | ) 6 | 7 | type ServiceContext struct { 8 | Config {{.config}} 9 | {{.middleware}} 10 | } 11 | 12 | func NewServiceContext(c {{.config}}) *ServiceContext { 13 | return &ServiceContext{ 14 | Config: c, 15 | {{.middlewareAssignment}} 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/model/sys_authority_btn.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | type SysAuthorityBtn struct { 4 | AuthorityId int64 `gorm:"comment:角色ID"` 5 | SysMenuID int64 `gorm:"comment:菜单ID"` 6 | SysBaseMenuBtnID int64 `gorm:"comment:菜单按钮ID"` 7 | SysBaseMenuBtn SysBaseMenuBtn ` gorm:"comment:按钮详情"` 8 | } 9 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/types.tpl: -------------------------------------------------------------------------------- 1 | type ( 2 | {{.lowerStartCamelObject}}Model interface{ 3 | {{.method}} 4 | } 5 | 6 | default{{.upperStartCamelObject}}Model struct { 7 | {{if .withCache}}sqlc.CachedConn{{else}}conn sqlx.SqlConn{{end}} 8 | table string 9 | } 10 | 11 | {{.upperStartCamelObject}} struct { 12 | {{.fields}} 13 | } 14 | ) 15 | -------------------------------------------------------------------------------- /.idea/go-zero-admin.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /test/casbin_RBAC-with-domainste_nants/model.conf: -------------------------------------------------------------------------------- 1 | [request_definition] 2 | r = sub, dom, obj, act 3 | 4 | [policy_definition] 5 | p = sub, dom, obj, act 6 | 7 | [role_definition] 8 | g = _, _, _ 9 | 10 | [policy_effect] 11 | e = some(where (p.eft == allow)) 12 | 13 | [matchers] 14 | m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act -------------------------------------------------------------------------------- /test/casbin/model.conf: -------------------------------------------------------------------------------- 1 | [request_definition] 2 | r = sub, obj, act 3 | 4 | [policy_definition] 5 | p = sub, obj, act 6 | 7 | [role_definition] 8 | g = _, _ 9 | 10 | [policy_effect] 11 | e = some(where (p.eft == allow)) 12 | 13 | [matchers] 14 | ; m = g( r.sub, p.sub) && r.obj == p.obj && r.act == p.act 15 | m = g( r.sub, p.sub) && my_func(r.obj, p.obj) && r.act == p.act -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/model-new.tpl: -------------------------------------------------------------------------------- 1 | func new{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf, opts ...cache.Option{{end}}) *default{{.upperStartCamelObject}}Model { 2 | return &default{{.upperStartCamelObject}}Model{ 3 | {{if .withCache}}CachedConn: sqlc.NewConn(conn, c, opts...){{else}}conn:conn{{end}}, 4 | table: {{.table}}, 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/logic-func.tpl: -------------------------------------------------------------------------------- 1 | {{if .hasComment}}{{.comment}}{{end}} 2 | func (l *{{.logicName}}) {{.method}} ({{if .hasReq}}in {{.request}}{{if .stream}},stream {{.streamBody}}{{end}}{{else}}stream {{.streamBody}}{{end}}) ({{if .hasReply}}{{.response}},{{end}} error) { 3 | // todo: add your logic here and delete this line 4 | 5 | return {{if .hasReply}}&{{.responseType}}{},{{end}} nil 6 | } 7 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/model/sys_user_authority.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | // SysUserAuthority 是 sysUser 和 sysAuthority 的连接表 4 | type SysUserAuthority struct { 5 | SysUserId int64 `gorm:"column:sys_user_id"` 6 | SysAuthorityAuthorityId int64 `gorm:"column:sys_authority_authority_id"` 7 | } 8 | 9 | func (s *SysUserAuthority) TableName() string { 10 | return "sys_user_authority" 11 | } 12 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/import-no-cache.tpl: -------------------------------------------------------------------------------- 1 | import ( 2 | "context" 3 | "database/sql" 4 | "fmt" 5 | "strings" 6 | {{if .time}}"time"{{end}} 7 | 8 | {{if .containsPQ}}"github.com/lib/pq"{{end}} 9 | "github.com/zeromicro/go-zero/core/stores/builder" 10 | "github.com/zeromicro/go-zero/core/stores/sqlc" 11 | "github.com/zeromicro/go-zero/core/stores/sqlx" 12 | "github.com/zeromicro/go-zero/core/stringx" 13 | ) 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build and Release Folders 2 | bin-debug/ 3 | bin-release/ 4 | [Oo]bj/ 5 | [Bb]in/ 6 | 7 | # Other files and folders 8 | .settings/ 9 | 10 | # Executables 11 | *.swf 12 | *.air 13 | *.ipa 14 | *.apk 15 | 16 | # Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties` 17 | # should NOT be excluded as they contain compiler settings and other important 18 | # information for Eclipse / Flash Builder. 19 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/server.tpl: -------------------------------------------------------------------------------- 1 | {{.head}} 2 | 3 | package server 4 | 5 | import ( 6 | {{if .notStream}}"context"{{end}} 7 | 8 | {{.imports}} 9 | ) 10 | 11 | type {{.server}}Server struct { 12 | svcCtx *svc.ServiceContext 13 | {{.unimplementedServer}} 14 | } 15 | 16 | func New{{.server}}Server(svcCtx *svc.ServiceContext) *{{.server}}Server { 17 | return &{{.server}}Server{ 18 | svcCtx: svcCtx, 19 | } 20 | } 21 | 22 | {{.funcs}} 23 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/mongo/model_types.tpl: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import ( 4 | "time" 5 | 6 | "go.mongodb.org/mongo-driver/bson/primitive" 7 | ) 8 | 9 | type {{.Type}} struct { 10 | ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` 11 | // TODO: Fill your own fields 12 | UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"` 13 | CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"` 14 | } 15 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/gateway/main.tpl: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | 6 | "github.com/zeromicro/go-zero/core/conf" 7 | "github.com/zeromicro/go-zero/gateway" 8 | ) 9 | 10 | var configFile = flag.String("f", "etc/gateway.yaml", "config file") 11 | 12 | func main() { 13 | flag.Parse() 14 | 15 | var c gateway.GatewayConf 16 | conf.MustLoad(*configFile, &c) 17 | gw := gateway.MustNewServer(c) 18 | defer gw.Stop() 19 | gw.Start() 20 | } 21 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/import.tpl: -------------------------------------------------------------------------------- 1 | import ( 2 | "context" 3 | "database/sql" 4 | "fmt" 5 | "strings" 6 | {{if .time}}"time"{{end}} 7 | 8 | {{if .containsPQ}}"github.com/lib/pq"{{end}} 9 | "github.com/zeromicro/go-zero/core/stores/builder" 10 | "github.com/zeromicro/go-zero/core/stores/cache" 11 | "github.com/zeromicro/go-zero/core/stores/sqlc" 12 | "github.com/zeromicro/go-zero/core/stores/sqlx" 13 | "github.com/zeromicro/go-zero/core/stringx" 14 | ) 15 | -------------------------------------------------------------------------------- /pkg/hash/hash.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import "golang.org/x/crypto/bcrypt" 4 | 5 | // BcryptHash 使用 bcrypt 对密码进行加密 6 | func BcryptHash(password string) string { 7 | bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) 8 | return string(bytes) 9 | } 10 | 11 | // BcryptCheck 对比明文密码和数据库的哈希值 12 | func BcryptCheck(password, hash string) bool { 13 | err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) 14 | return err == nil 15 | } 16 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/middleware.tpl: -------------------------------------------------------------------------------- 1 | package middleware 2 | 3 | import "net/http" 4 | 5 | type {{.name}} struct { 6 | } 7 | 8 | func New{{.name}}() *{{.name}} { 9 | return &{{.name}}{} 10 | } 11 | 12 | func (m *{{.name}})Handle(next http.HandlerFunc) http.HandlerFunc { 13 | return func(w http.ResponseWriter, r *http.Request) { 14 | // TODO generate middleware implement function, delete after code implementation 15 | 16 | // Passthrough to next handler if need 17 | next(w, r) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/testMain/cron/cron.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/robfig/cron/v3" 6 | ) 7 | 8 | func myScheduledTask() { 9 | fmt.Println("定时任务执行了!") 10 | } 11 | 12 | func main() { 13 | // 创建 cron 实例 14 | c := cron.New() 15 | 16 | // 添加定时任务,每分钟执行一次 17 | _, err := c.AddFunc("@every 1s", myScheduledTask) 18 | if err != nil { 19 | fmt.Println("添加定时任务失败:", err) 20 | return 21 | } 22 | 23 | // 启动 cron 24 | c.Start() 25 | 26 | // 等待程序运行,以便定时任务有足够时间执行 27 | select {} 28 | } 29 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/server-func.tpl: -------------------------------------------------------------------------------- 1 | 2 | {{if .hasComment}}{{.comment}}{{end}} 3 | func (s *{{.server}}Server) {{.method}} ({{if .notStream}}ctx context.Context,{{if .hasReq}} in {{.request}}{{end}}{{else}}{{if .hasReq}} in {{.request}},{{end}}stream {{.streamBody}}{{end}}) ({{if .notStream}}{{.response}},{{end}}error) { 4 | l := {{.logicPkg}}.New{{.logicName}}({{if .notStream}}ctx,{{else}}stream.Context(),{{end}}s.svcCtx) 5 | return l.{{.method}}({{if .hasReq}}in{{if .stream}} ,stream{{end}}{{else}}{{if .stream}}stream{{end}}{{end}}) 6 | } 7 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/logic.tpl: -------------------------------------------------------------------------------- 1 | package {{.packageName}} 2 | 3 | import ( 4 | "context" 5 | 6 | {{.imports}} 7 | 8 | "github.com/zeromicro/go-zero/core/logx" 9 | ) 10 | 11 | type {{.logicName}} struct { 12 | ctx context.Context 13 | svcCtx *svc.ServiceContext 14 | logx.Logger 15 | } 16 | 17 | func New{{.logicName}}(ctx context.Context,svcCtx *svc.ServiceContext) *{{.logicName}} { 18 | return &{{.logicName}}{ 19 | ctx: ctx, 20 | svcCtx: svcCtx, 21 | Logger: logx.WithContext(ctx), 22 | } 23 | } 24 | {{.functions}} 25 | -------------------------------------------------------------------------------- /test/sh/rpc.bat: -------------------------------------------------------------------------------- 1 | @REM 项目根目录 运行例子: .\test\sh\rpc.bat applet applet 2 | 3 | 4 | set name=%1 5 | set rpc=%2 6 | 7 | goctl rpc protoc application\%name%\rpc\desc\%rpc%.proto --go_out=application\%name%\rpc --go-grpc_out=application\%name%\rpc --zrpc_out=application\%name%\rpc\ -m --style=go_zero 8 | 9 | @REM goctl rpc protoc application\usre\rpc\usre.proto --go_out=application\usre\rpc --go-grpc_out=application\usre\rpc --zrpc_out=application\usre\rpc\ 10 | 11 | 12 | @REM goctl rpc protoc ./user.proto --go_out=. --go-grpc_out=. --zrpc_out=./ 13 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/find-one-by-field-extra-method.tpl: -------------------------------------------------------------------------------- 1 | func (m *default{{.upperStartCamelObject}}Model) formatPrimary(primary any) string { 2 | return fmt.Sprintf("%s%v", {{.primaryKeyLeft}}, primary) 3 | } 4 | 5 | func (m *default{{.upperStartCamelObject}}Model) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { 6 | query := fmt.Sprintf("select %s from %s where {{.originalPrimaryField}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table ) 7 | return conn.QueryRowCtx(ctx, v, query, primary) 8 | } 9 | -------------------------------------------------------------------------------- /test/testMain/openai/nginx.conf: -------------------------------------------------------------------------------- 1 | # nginx -c G:\yanghao\code\github_yh\go-zero-admin\test\openai\nginx.conf 2 | 3 | events { 4 | worker_connections 1024; 5 | } 6 | 7 | http { 8 | server { 9 | listen 8001; 10 | 11 | location /openai-api { 12 | proxy_pass https://api.openai.com/v1/engines/davinci-codex/completions; 13 | proxy_set_header Host $http_host; 14 | proxy_set_header Authorization "Bearer sk-1m"; 15 | proxy_set_header Content-Type "application/json"; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/model/sys_api.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import "go-zero-admin/pkg/model" 4 | 5 | type SysApi struct { 6 | model.MODEL_BASE 7 | Path string `json:"path" gorm:"comment:api路径"` // api路径 8 | Description string `json:"description" gorm:"comment:api中文描述"` // api中文描述 9 | ApiGroup string `json:"apiGroup" gorm:"comment:api组"` // api组 10 | Method string `json:"method" gorm:"default:POST;comment:方法"` // 方法:创建POST(默认)|查看GET|更新PUT|删除DELETE 11 | } 12 | 13 | func (SysApi) TableName() string { 14 | return "sys_apis" 15 | } 16 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/logic.tpl: -------------------------------------------------------------------------------- 1 | package {{.pkgName}} 2 | 3 | import ( 4 | {{.imports}} 5 | ) 6 | 7 | type {{.logic}} struct { 8 | logx.Logger 9 | ctx context.Context 10 | svcCtx *svc.ServiceContext 11 | } 12 | 13 | func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) *{{.logic}} { 14 | return &{{.logic}}{ 15 | Logger: logx.WithContext(ctx), 16 | ctx: ctx, 17 | svcCtx: svcCtx, 18 | } 19 | } 20 | 21 | func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} { 22 | // todo: add your logic here and delete this line 23 | 24 | {{.returnString}} 25 | } 26 | -------------------------------------------------------------------------------- /test/testMain/findkey/findkey.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type m map[string]struct{} 6 | 7 | func (m m) findKey(key string) bool { 8 | if _, ok := m[key]; ok { 9 | return true 10 | } 11 | return false 12 | } 13 | 14 | //func (m m) insert(key string) {} 15 | 16 | func main() { 17 | var mm m = make(m, 10) 18 | mm["result"] = struct{}{} 19 | 20 | fmt.Println(isContainKey(mm, "result")) 21 | fmt.Println(isContainKey(mm, "xxx")) 22 | } 23 | 24 | func isContainKey(intf interface { 25 | findKey(key string) bool 26 | }, key string) bool { 27 | return intf.findKey(key) 28 | } 29 | -------------------------------------------------------------------------------- /test/sh/api.bat: -------------------------------------------------------------------------------- 1 | @REM 注意 运用了自定义模板 2 | 3 | @REM 项目根目录 运行例子1: .\test\sh\api.bat applet 4 | 5 | set name=%1 6 | 7 | goctl api go -api application\%name%\api\desc\%name%.api -dir application\%name%\api\ -home test\goctl\ -style=go_zero 8 | 9 | 10 | @REM if "%api%" == "api" ( 11 | @REM goctl api go -api application\%dir%\api\%dir%.api -dir application\%dir%\api\ 12 | @REM ) else ( 13 | @REM goctl api go -api application\%dir%\%dir%.api -dir application\%dir%\ 14 | @REM ) 15 | 16 | 17 | 18 | @REM goctl api go -api application\%dir%\%api% -dir %dir% -home ./dev/goctl 19 | @REM goctl api go --dir=./ --api applet.api -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/call.tpl: -------------------------------------------------------------------------------- 1 | {{.head}} 2 | 3 | package {{.filePackage}} 4 | 5 | import ( 6 | "context" 7 | 8 | {{.pbPackage}} 9 | {{if ne .pbPackage .protoGoPackage}}{{.protoGoPackage}}{{end}} 10 | 11 | "github.com/zeromicro/go-zero/zrpc" 12 | "google.golang.org/grpc" 13 | ) 14 | 15 | type ( 16 | {{.alias}} 17 | 18 | {{.serviceName}} interface { 19 | {{.interface}} 20 | } 21 | 22 | default{{.serviceName}} struct { 23 | cli zrpc.Client 24 | } 25 | ) 26 | 27 | func New{{.serviceName}}(cli zrpc.Client) {{.serviceName}} { 28 | return &default{{.serviceName}}{ 29 | cli: cli, 30 | } 31 | } 32 | 33 | {{.functions}} 34 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/template.tpl: -------------------------------------------------------------------------------- 1 | syntax = "v1" 2 | 3 | info ( 4 | title: // TODO: add title 5 | desc: // TODO: add description 6 | author: "{{.gitUser}}" 7 | email: "{{.gitEmail}}" 8 | ) 9 | 10 | type request { 11 | // TODO: add members here and delete this comment 12 | } 13 | 14 | type response { 15 | // TODO: add members here and delete this comment 16 | } 17 | 18 | service {{.serviceName}} { 19 | @handler GetUser // TODO: set handler name and delete this comment 20 | get /users/id/:userId(request) returns(response) 21 | 22 | @handler CreateUser // TODO: set handler name and delete this comment 23 | post /users/create(request) 24 | } 25 | -------------------------------------------------------------------------------- /test/testMain/interface.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | ) 7 | 8 | func main() { 9 | var data interface{} 10 | 11 | data = map[string]interface{}{ 12 | "res": "111", 13 | } 14 | 15 | if containsResultKey(data) { 16 | fmt.Println("包含了") 17 | } else { 18 | fmt.Println("不包含") 19 | 20 | } 21 | } 22 | 23 | // containsResultKey 检查interface]类型的数据是否包含key“result" 24 | func containsResultKey(data interface{}) bool { 25 | val := reflect.ValueOf(data) 26 | 27 | if val.Kind() == reflect.Map { 28 | for _, key := range val.MapKeys() { 29 | if key.String() == "res" { 30 | return true 31 | } 32 | } 33 | } 34 | return false 35 | } 36 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/zeromicro/go-zero/core/stores/redis" 5 | "github.com/zeromicro/go-zero/zrpc" 6 | "go-zero-admin/pkg/middlecasbin" 7 | ) 8 | 9 | type Config struct { 10 | zrpc.RpcServerConf 11 | BizRedis redis.RedisConf 12 | DB struct { 13 | DataSource string 14 | MaxOpenConns int `json:",default=10"` 15 | MaxIdleConns int `json:",default=100"` 16 | MaxLifetime int `json:",default=3600"` 17 | } 18 | JwtAuth struct { 19 | AccessSecret string 20 | AccessExpire int64 21 | } 22 | CasbinConf middlecasbin.CasbinConf 23 | Default struct { 24 | UserPassword string 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /test/testMain/interface_struct/interface_struct.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type IsResult struct { 8 | Result string `json:"result"` 9 | } 10 | 11 | type RandomImageResponse struct { 12 | Result string `json:"result"` 13 | } 14 | 15 | func Success(data interface{}) { 16 | fmt.Printf("data: %+v\n", data) // 使用 %+v 输出结构体的完整 key-value 对 17 | 18 | if d, ok := data.(*IsResult); ok { 19 | fmt.Printf("Result: %+v\n", d) // 使用 %+v 输出结构体的完整 key-value 对 20 | } else { 21 | fmt.Println("类型断言失败,期望 *IsResult 类型") 22 | } 23 | 24 | } 25 | 26 | func main() { 27 | // 传递指针类型的数据 28 | var data interface{} = &RandomImageResponse{ 29 | Result: "111", 30 | } 31 | 32 | Success(data) 33 | } 34 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/handler.tpl: -------------------------------------------------------------------------------- 1 | package {{.PkgName}} 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/pkg/result" 7 | {{.ImportPackages}} 8 | 9 | "github.com/zeromicro/go-zero/rest/httpx" 10 | ) 11 | 12 | func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc { 13 | return func(w http.ResponseWriter, r *http.Request) { 14 | {{if .HasRequest}}var req types.{{.RequestType}} 15 | if err := httpx.Parse(r, &req); err != nil { 16 | httpx.ErrorCtx(r.Context(), w, err) 17 | return 18 | } 19 | 20 | {{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx) 21 | {{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}}) 22 | result.HttpResult(r, w, resp, err) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /test/sh/model.bat: -------------------------------------------------------------------------------- 1 | @REM 项目根目录 运行例子: .\pkg\test\model.bat user user 2 | @REM 第一个参数的数据库名 第二个参数是表名 3 | set baseName=%1 4 | set tableName=%2 5 | set mysql=root:123456@tcp(127.0.0.1:3306)/beyond_goZero_%baseName% 6 | 7 | 8 | goctl model mysql datasource --dir application/%baseName%/rpc/internal/model --table %tableName% --cache true --url=%mysql% 9 | 10 | @REM goctl model mysql datasource --dir application/%baseName%/mq/internal/model --table %tableName% --url=%mysql% 11 | 12 | 13 | 14 | 15 | 16 | 17 | @REM goctl model mysql datasource -url="%mysql%" -table="%table%" -c -dir %dir% -home ./dev/goctl 18 | @REM goctl model mysql datasource --dir ./internal/model --table user --cache true --url "root:Zsg123456@tcp(127.0.0.1:3306)/beyond_user" -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/insert.tpl: -------------------------------------------------------------------------------- 1 | func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) { 2 | {{if .withCache}}{{.keys}} 3 | ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { 4 | query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet) 5 | return conn.ExecCtx(ctx, query, {{.expressionValues}}) 6 | }, {{.keyValues}}){{else}}query := fmt.Sprintf("insert into %s (%s) values ({{.expression}})", m.table, {{.lowerStartCamelObject}}RowsExpectAutoSet) 7 | ret,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}} 8 | return ret,err 9 | } 10 | -------------------------------------------------------------------------------- /pkg/result/xerr/errCode.go: -------------------------------------------------------------------------------- 1 | package xerr 2 | 3 | // 成功返回 4 | const OK uint32 = 200 5 | 6 | /**(前3位代表业务,后三位代表具体功能)**/ 7 | 8 | // 全局错误码 9 | const SERVER_COMMON_ERROR uint32 = 100001 10 | const REUQEST_PARAM_ERROR uint32 = 100002 11 | const TOKEN_EXPIRE_ERROR uint32 = 100003 12 | const TOKEN_GENERATE_ERROR uint32 = 100004 13 | const DB_ERROR uint32 = 100005 14 | const DB_UPDATE_AFFECTED_ZERO_ERROR uint32 = 100006 15 | 16 | // sys 模块 17 | const CAPTCHA_ERROR uint32 = 200007 18 | const USER_PASSWORD_ERROR = 200008 19 | const REPEAT_NAME_ERROR = 200009 20 | 21 | // sys_base 模块 22 | const REPEAT_OSS_GET_BUCKET_ERROR = 300001 23 | const REPEAT_OSS_PUT_BUCKET_ERROR = 300002 24 | const EMAIL_CANNOT_BE_EMPTY_ERROR = 300003 25 | const ALREADY_EXISTS_ERROR = 300004 26 | -------------------------------------------------------------------------------- /pkg/orm/metric.go: -------------------------------------------------------------------------------- 1 | package orm 2 | 3 | import "github.com/zeromicro/go-zero/core/metric" 4 | 5 | const gormNamespace = "gorm_client" 6 | 7 | var ( 8 | metricClientReqDur = metric.NewHistogramVec(&metric.HistogramVecOpts{ 9 | Namespace: gormNamespace, 10 | Subsystem: "requests", 11 | Name: "duration_ms", 12 | Help: "gorm client requests duration(ms).", 13 | Labels: []string{"table", "method"}, 14 | Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000}, 15 | }) 16 | 17 | metricClientReqErrTotal = metric.NewCounterVec(&metric.CounterVecOpts{ 18 | Namespace: gormNamespace, 19 | Subsystem: "requests", 20 | Name: "error_total", 21 | Help: "gorm client requests error count.", 22 | Labels: []string{"table", "method", "is_error"}, 23 | }) 24 | ) 25 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/menu/get_menu_handler.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/menu" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetMenuHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetMenuRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := menu.NewGetMenuLogic(r.Context(), svcCtx) 23 | resp, err := l.GetMenu(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/model/sys_authority_menu.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | type SysMenu struct { 4 | SysBaseMenu 5 | MenuId string `json:"menuId" gorm:"comment:菜单ID"` 6 | AuthorityId int64 `json:"-" gorm:"comment:角色ID"` 7 | Children []SysMenu `json:"children" gorm:"-"` 8 | Parameters []SysBaseMenuParameter `json:"parameters" gorm:"foreignKey:SysBaseMenuID;references:MenuId"` 9 | Btns map[string]int64 `json:"btns" gorm:"-"` 10 | } 11 | 12 | type SysAuthorityMenu struct { 13 | MenuId string `json:"menuId" gorm:"comment:菜单ID;column:sys_base_menu_id"` 14 | AuthorityId string `json:"-" gorm:"comment:角色ID;column:sys_authority_authority_id"` 15 | } 16 | 17 | func (s SysAuthorityMenu) TableName() string { 18 | return "sys_authority_menus" 19 | } 20 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/api/create_api_handler.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/api" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func CreateApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.CreateApiRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := api.NewCreateApiLogic(r.Context(), svcCtx) 23 | resp, err := l.CreateApi(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/api/delete_api_handler.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/api" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func DeleteApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.DeleteApiRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := api.NewDeleteApiLogic(r.Context(), svcCtx) 23 | resp, err := l.DeleteApi(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/api/update_api_handler.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/api" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func UpdateApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.UpdateApiRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := api.NewUpdateApiLogic(r.Context(), svcCtx) 23 | resp, err := l.UpdateApi(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/user/register_handler.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/user" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func RegisterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.RegisterRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := user.NewRegisterLogic(r.Context(), svcCtx) 23 | resp, err := l.Register(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/api/get_api_list_handler.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/api" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetApiListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetApiListRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := api.NewGetApiListLogic(r.Context(), svcCtx) 23 | resp, err := l.GetApiList(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/user/delete_user_handler.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/user" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func DeleteUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.DeleteUserRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := user.NewDeleteUserLogic(r.Context(), svcCtx) 23 | resp, err := l.DeleteUser(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/menu/add_base_menu_handler.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/menu" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func AddBaseMenuHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.AddBaseMenuRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := menu.NewAddBaseMenuLogic(r.Context(), svcCtx) 23 | resp, err := l.AddBaseMenu(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/menu/get_menu_list_handler.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/menu" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetMenuListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetMenuListRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := menu.NewGetMenuListLogic(r.Context(), svcCtx) 23 | resp, err := l.GetMenuList(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/user/get_user_list_handler.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/user" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetUserListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetUserListRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := user.NewGetUserListLogic(r.Context(), svcCtx) 23 | resp, err := l.GetUserList(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/api/get_all_api_list_handler.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/api" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetAllApiListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetAllApiListRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := api.NewGetAllApiListLogic(r.Context(), svcCtx) 23 | resp, err := l.GetAllApiList(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/base/send_email_code_handler.go: -------------------------------------------------------------------------------- 1 | package base 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/base" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func SendEmailCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.SendEmailCodeRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := base.NewSendEmailCodeLogic(r.Context(), svcCtx) 23 | resp, err := l.SendEmailCode(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/model/sys_dictionary.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import ( 4 | "go-zero-admin/pkg/model" 5 | ) 6 | 7 | type SysDictionary struct { 8 | model.MODEL_BASE 9 | Name string `json:"name" form:"name" gorm:"column:name;comment:字典名(中)"` // 字典名(中) 10 | Type string `json:"type" form:"type" gorm:"column:type;comment:字典名(英)"` // 字典名(英) 11 | Status int64 `json:"status" form:"status" gorm:"column:status;comment:状态"` // 状态 1开启 0关闭 12 | Desc string `json:"desc" form:"desc" gorm:"column:desc;comment:描述"` // 描述 13 | SysDictionaryInfoList []SysDictionaryInfo `json:"sysDictionaryInfoList" form:"sysDictionaryInfoList"` 14 | } 15 | 16 | func (SysDictionary) TableName() string { 17 | return "sys_dictionaries" 18 | } 19 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/gateway/etc.tpl: -------------------------------------------------------------------------------- 1 | Name: gateway-example # gateway name 2 | Host: localhost # gateway host 3 | Port: 8888 # gateway port 4 | Upstreams: # upstreams 5 | - Grpc: # grpc upstream 6 | Target: 0.0.0.0:8080 # grpc target,the direct grpc server address,for only one node 7 | # Endpoints: [0.0.0.0:8080,192.168.120.1:8080] # grpc endpoints, the grpc server address list, for multiple nodes 8 | # Etcd: # etcd config, if you want to use etcd to discover the grpc server address 9 | # Hosts: [127.0.0.1:2378,127.0.0.1:2379] # etcd hosts 10 | # Key: greet.grpc # the discovery key 11 | # protoset mode 12 | ProtoSets: 13 | - hello.pb 14 | # Mappings can also be written in proto options 15 | # Mappings: # routes mapping 16 | # - Method: get 17 | # Path: /ping 18 | # RpcPath: hello.Hello/Ping 19 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/api/delete_apis_by_ids_handler.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/api" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func DeleteApisByIdsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.DeleteApisByIdsRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := api.NewDeleteApisByIdsLogic(r.Context(), svcCtx) 23 | resp, err := l.DeleteApisByIds(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/base/upload_file_img_handler.go: -------------------------------------------------------------------------------- 1 | package base 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/base" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func UploadFileImgHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.UploadFileImgRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := base.NewUploadFileImgLogic(r.Context(), svcCtx) 23 | resp, err := l.UploadFileImg(&req, r) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/menu/delete_base_menu_handler.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/menu" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func DeleteBaseMenuHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.DeleteBaseMenuRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := menu.NewDeleteBaseMenuLogic(r.Context(), svcCtx) 23 | resp, err := l.DeleteBaseMenu(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/menu/update_base_menu_handler.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/menu" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func UpdateBaseMenuHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.UpdateBaseMenuRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := menu.NewUpdateBaseMenuLogic(r.Context(), svcCtx) 23 | resp, err := l.UpdateBaseMenu(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/user/update_user_info_handler.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/user" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func UpdateUserInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.UpdateUserInfoRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := user.NewUpdateUserInfoLogic(r.Context(), svcCtx) 23 | resp, err := l.UpdateUserInfo(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/api/main.tpl: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "net/http" 7 | 8 | 9 | "go-zero-admin/pkg/result" 10 | "go-zero-admin/pkg/result/xerr" 11 | {{.importPackages}} 12 | ) 13 | 14 | var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file") 15 | 16 | func main() { 17 | flag.Parse() 18 | 19 | var c config.Config 20 | conf.MustLoad(*configFile, &c) 21 | 22 | server := rest.MustNewServer(c.RestConf, rest.WithUnauthorizedCallback(func(w http.ResponseWriter, r *http.Request, err error) { 23 | result.HttpResult(r, w, "", xerr.NewErrCode(xerr.TOKEN_EXPIRE_ERROR)) 24 | })) 25 | defer server.Stop() 26 | 27 | ctx := svc.NewServiceContext(c) 28 | handler.RegisterHandlers(server, ctx) 29 | 30 | fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) 31 | server.Start() 32 | } 33 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/menu/get_base_menu_by_id_handler.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/menu" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetBaseMenuByIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetBaseMenuByIdRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := menu.NewGetBaseMenuByIdLogic(r.Context(), svcCtx) 23 | resp, err := l.GetBaseMenuById(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/menu/get_base_menu_tree_handler.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/menu" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetBaseMenuTreeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetBaseMenuTreeRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := menu.NewGetBaseMenuTreeLogic(r.Context(), svcCtx) 23 | resp, err := l.GetBaseMenuTree(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/menu/get_menu_authority_handler.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/menu" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetMenuAuthorityHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetMenuAuthorityRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := menu.NewGetMenuAuthorityLogic(r.Context(), svcCtx) 23 | resp, err := l.GetMenuAuthority(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/user/reset_user_password_handler.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/user" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func ResetUserPasswordHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.ResetUserPasswordRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := user.NewResetUserPasswordLogic(r.Context(), svcCtx) 23 | resp, err := l.ResetUserPassword(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/casbin/update_casbin_data_handler.go: -------------------------------------------------------------------------------- 1 | package casbin 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/casbin" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func UpdateCasbinDataHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.UpdateCasbinDataRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := casbin.NewUpdateCasbinDataLogic(r.Context(), svcCtx) 23 | resp, err := l.UpdateCasbinData(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/authority/create_authority_handler.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/authority" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func CreateAuthorityHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.CreateAuthorityRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := authority.NewCreateAuthorityLogic(r.Context(), svcCtx) 23 | resp, err := l.CreateAuthority(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/authority/delete_authority_handler.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/authority" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func DeleteAuthorityHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.DeleteAuthorityRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := authority.NewDeleteAuthorityLogic(r.Context(), svcCtx) 23 | resp, err := l.DeleteAuthority(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/authority/update_authority_handler.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/authority" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func UpdateAuthorityHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.UpdateAuthorityRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := authority.NewUpdateAuthorityLogic(r.Context(), svcCtx) 23 | resp, err := l.UpdateAuthority(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/rpc/etc/applet.yaml: -------------------------------------------------------------------------------- 1 | Name: applet.rpc 2 | ListenOn: 0.0.0.0:6001 3 | Etcd: 4 | Hosts: 5 | - 127.0.0.1:2379 6 | Key: applet.rpc 7 | DB: 8 | DataSource: root:123456@tcp(127.0.0.1:3306)/goZero-admin?parseTime=true&loc=Local 9 | MaxIdleConns: 10 10 | MaxOpenConns: 100 11 | ConnMaxLifetime: 3600 12 | BizRedis: 13 | Host: 127.0.0.1:6379 14 | Pass: 15 | Type: node 16 | JwtAuth: 17 | AccessSecret: 3fG7-go-zero-admin_p2hE 18 | AccessExpire: 31536000 19 | CasbinConf: 20 | ModelText: | 21 | [request_definition] 22 | r = sub, obj, act 23 | [policy_definition] 24 | p = sub, obj, act 25 | [role_definition] 26 | g = _, _ 27 | [policy_effect] 28 | e = some(where (p.eft == allow)) 29 | [matchers] 30 | m = r.sub == p.sub && keyMatch2(r.obj,p.obj) && r.act == p.act 31 | Default: # 默认值 32 | UserPassword: goZero # 默认user重置密码 -------------------------------------------------------------------------------- /application/applet/api/internal/handler/authority/add_authority_menu_handler.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/authority" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func AddAuthorityMenuHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.AddAuthorityMenuRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := authority.NewAddAuthorityMenuLogic(r.Context(), svcCtx) 23 | resp, err := l.AddAuthorityMenu(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/authority/get_authority_list_handler.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/authority" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetAuthorityListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetAuthorityListRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := authority.NewGetAuthorityListLogic(r.Context(), svcCtx) 23 | resp, err := l.GetAuthorityList(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/svc/service_context.go: -------------------------------------------------------------------------------- 1 | package svc 2 | 3 | import ( 4 | "go-zero-admin/application/applet/rpc/internal/config" 5 | "go-zero-admin/pkg/orm" 6 | 7 | "github.com/zeromicro/go-zero/core/stores/redis" 8 | ) 9 | 10 | type ServiceContext struct { 11 | Config config.Config 12 | DB *orm.DB 13 | BizRedis *redis.Redis 14 | } 15 | 16 | func NewServiceContext(c config.Config) *ServiceContext { 17 | db := orm.MustNewMysql(&orm.Config{ 18 | DSN: c.DB.DataSource, 19 | MaxOpenConns: c.DB.MaxOpenConns, 20 | MaxIdleCnns: c.DB.MaxIdleConns, 21 | MaxLifetime: c.DB.MaxLifetime, 22 | }) 23 | rds := redis.MustNewRedis(redis.RedisConf{ 24 | Host: c.BizRedis.Host, 25 | Type: c.BizRedis.Type, 26 | Pass: c.BizRedis.Pass, 27 | }) 28 | 29 | return &ServiceContext{ 30 | Config: c, 31 | DB: db, 32 | BizRedis: rds, 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/casbin/get_path_by_authority_id_handler.go: -------------------------------------------------------------------------------- 1 | package casbin 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/casbin" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetPathByAuthorityIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetPathByAuthorityIdRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := casbin.NewGetPathByAuthorityIdLogic(r.Context(), svcCtx) 23 | resp, err := l.GetPathByAuthorityId(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/create_sys_dictionary_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func CreateSysDictionaryHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.CreateSysDictionaryRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewCreateSysDictionaryLogic(r.Context(), svcCtx) 23 | resp, err := l.CreateSysDictionary(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/delete_sys_dictionary_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func DeleteSysDictionaryHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.DeleteSysDictionaryRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewDeleteSysDictionaryLogic(r.Context(), svcCtx) 23 | resp, err := l.DeleteSysDictionary(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/update_sys_dictionary_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func UpdateSysDictionaryHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.UpdateSysDictionaryRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewUpdateSysDictionaryLogic(r.Context(), svcCtx) 23 | resp, err := l.UpdateSysDictionary(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/get_sys_dictionary_list_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetSysDictionaryListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetSysDictionaryListRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewGetSysDictionaryListLogic(r.Context(), svcCtx) 23 | resp, err := l.GetSysDictionaryList(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/casbin/update_casbin_data_by_api_ids_handler.go: -------------------------------------------------------------------------------- 1 | package casbin 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/casbin" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func UpdateCasbinDataByApiIdsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.UpdateCasbinDataByApiIdsRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := casbin.NewUpdateCasbinDataByApiIdsLogic(r.Context(), svcCtx) 23 | resp, err := l.UpdateCasbinDataByApiIds(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/delete.tpl: -------------------------------------------------------------------------------- 1 | func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error { 2 | {{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, {{.lowerStartCamelPrimaryKey}}) 3 | if err!=nil{ 4 | return err 5 | } 6 | 7 | {{end}} {{.keys}} 8 | _, err {{if .containsIndexCache}}={{else}}:={{end}} m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { 9 | query := fmt.Sprintf("delete from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table) 10 | return conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}) 11 | }, {{.keyValues}}){{else}}query := fmt.Sprintf("delete from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table) 12 | _,err:=m.conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}){{end}} 13 | return err 14 | } 15 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/create_sys_dictionary_info_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func CreateSysDictionaryInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.CreateSysDictionaryInfoRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewCreateSysDictionaryInfoLogic(r.Context(), svcCtx) 23 | resp, err := l.CreateSysDictionaryInfo(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/delete_sys_dictionary_info_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func DeleteSysDictionaryInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.DeleteSysDictionaryInfoRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewDeleteSysDictionaryInfoLogic(r.Context(), svcCtx) 23 | resp, err := l.DeleteSysDictionaryInfo(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/get_sys_dictionary_details_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetSysDictionaryDetailsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetSysDictionaryDetailsRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewGetSysDictionaryDetailsLogic(r.Context(), svcCtx) 23 | resp, err := l.GetSysDictionaryDetails(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/update_sys_dictionary_info_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func UpdateSysDictionaryInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.UpdateSysDictionaryInfoRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewUpdateSysDictionaryInfoLogic(r.Context(), svcCtx) 23 | resp, err := l.UpdateSysDictionaryInfo(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/get_sys_dictionary_info_list_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetSysDictionaryInfoListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetSysDictionaryInfoListRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewGetSysDictionaryInfoListLogic(r.Context(), svcCtx) 23 | resp, err := l.GetSysDictionaryInfoList(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /pkg/result/xerr/errors.go: -------------------------------------------------------------------------------- 1 | package xerr 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | /** 8 | 常用通用固定错误 9 | */ 10 | 11 | type CodeError struct { 12 | errCode uint32 13 | errMsg string 14 | } 15 | 16 | // 返回给前端的错误码 17 | func (e *CodeError) GetErrCode() uint32 { 18 | return e.errCode 19 | } 20 | 21 | // 返回给前端显示端错误信息 22 | func (e *CodeError) GetErrMsg() string { 23 | return e.errMsg 24 | } 25 | 26 | func (e *CodeError) Error() string { 27 | return fmt.Sprintf("ErrCode:%d,ErrMsg:%s", e.errCode, e.errMsg) 28 | } 29 | 30 | func NewErrCode(errCode uint32) *CodeError { 31 | return &CodeError{errCode: errCode, errMsg: MapErrMsg(errCode)} 32 | } 33 | 34 | // 暂时屏蔽 都用一个 errCode 在errCode、errMsg 定义就好 35 | func NewErrCodeMsg(errCode uint32, errMsg string) *CodeError { 36 | return &CodeError{errCode: errCode, errMsg: errMsg} 37 | } 38 | func NewErrMsg(errMsg string) *CodeError { 39 | return &CodeError{errCode: SERVER_COMMON_ERROR, errMsg: errMsg} 40 | } 41 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/dictionary/get_sys_dictionary_info_list_details_by_id_handler.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "net/http" 5 | 6 | "go-zero-admin/application/applet/api/internal/logic/dictionary" 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/pkg/result" 10 | 11 | "github.com/zeromicro/go-zero/rest/httpx" 12 | ) 13 | 14 | func GetSysDictionaryInfoListDetailsByIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 | return func(w http.ResponseWriter, r *http.Request) { 16 | var req types.GetSysDictionaryInfoListDetailsByIdRequest 17 | if err := httpx.Parse(r, &req); err != nil { 18 | httpx.ErrorCtx(r.Context(), w, err) 19 | return 20 | } 21 | 22 | l := dictionary.NewGetSysDictionaryInfoListDetailsByIdLogic(r.Context(), svcCtx) 23 | resp, err := l.GetSysDictionaryInfoListDetailsById(&req) 24 | result.HttpResult(r, w, resp, err) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/var.tpl: -------------------------------------------------------------------------------- 1 | var ( 2 | {{.lowerStartCamelObject}}FieldNames = builder.RawFieldNames(&{{.upperStartCamelObject}}{}{{if .postgreSql}}, true{{end}}) 3 | {{.lowerStartCamelObject}}Rows = strings.Join({{.lowerStartCamelObject}}FieldNames, ",") 4 | {{.lowerStartCamelObject}}RowsExpectAutoSet = {{if .postgreSql}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}", {{end}} {{.ignoreColumns}}), ","){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}", {{end}} {{.ignoreColumns}}), ","){{end}} 5 | {{.lowerStartCamelObject}}RowsWithPlaceHolder = {{if .postgreSql}}builder.PostgreSqlJoin(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", {{.ignoreColumns}})){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", {{.ignoreColumns}}), "=?,") + "=?"{{end}} 6 | 7 | {{if .withCache}}{{.cacheKeys}}{{end}} 8 | ) 9 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/rpc/main.tpl: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | 7 | {{.imports}} 8 | 9 | "github.com/zeromicro/go-zero/core/conf" 10 | "github.com/zeromicro/go-zero/core/service" 11 | "github.com/zeromicro/go-zero/zrpc" 12 | "google.golang.org/grpc" 13 | "google.golang.org/grpc/reflection" 14 | ) 15 | 16 | var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file") 17 | 18 | func main() { 19 | flag.Parse() 20 | 21 | var c config.Config 22 | conf.MustLoad(*configFile, &c) 23 | ctx := svc.NewServiceContext(c) 24 | 25 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 26 | {{range .serviceNames}} {{.Pkg}}.Register{{.Service}}Server(grpcServer, {{.ServerPkg}}.New{{.Service}}Server(ctx)) 27 | {{end}} 28 | if c.Mode == service.DevMode || c.Mode == service.TestMode { 29 | reflection.Register(grpcServer) 30 | } 31 | }) 32 | defer s.Stop() 33 | 34 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 35 | s.Start() 36 | } 37 | -------------------------------------------------------------------------------- /pkg/result/responseBean.go: -------------------------------------------------------------------------------- 1 | package result 2 | 3 | import ( 4 | "time" 5 | ) 6 | 7 | type ResponseSuccessBean struct { 8 | Code uint32 `json:"code"` 9 | Message string `json:"message"` 10 | Result interface{} `json:"result"` 11 | ReturnData interface{} `json:"returnData"` 12 | Success bool `json:"success"` 13 | Timestamp int64 `json:"timestamp"` 14 | } 15 | 16 | type ResponseErrorBean struct { 17 | Code uint32 `json:"code"` 18 | Message string `json:"message"` 19 | } 20 | 21 | var Res ResponseSuccessBean 22 | 23 | func Success(data interface{}) *ResponseSuccessBean { 24 | if Res.Message == "" { 25 | Res.Message = "操作成功!" 26 | } 27 | 28 | return &ResponseSuccessBean{ 29 | Code: 200, 30 | Message: Res.Message, 31 | Result: data, 32 | ReturnData: nil, 33 | Success: true, 34 | Timestamp: time.Now().Unix(), 35 | } 36 | } 37 | 38 | func Error(errCode uint32, errMsg string) *ResponseErrorBean { 39 | return &ResponseErrorBean{errCode, errMsg} 40 | } 41 | -------------------------------------------------------------------------------- /application/applet/api/internal/config/config.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/zeromicro/go-zero/core/stores/redis" 5 | "github.com/zeromicro/go-zero/rest" 6 | "github.com/zeromicro/go-zero/zrpc" 7 | "go-zero-admin/pkg/middlecasbin" 8 | ) 9 | 10 | type Config struct { 11 | rest.RestConf 12 | JwtAuth struct { 13 | AccessSecret string 14 | AccessExpire int 15 | } 16 | BizRedis redis.RedisConf 17 | Page struct { 18 | PageNo int64 19 | PageSize int64 20 | } 21 | AppletRPC zrpc.RpcClientConf 22 | CasbinConf middlecasbin.CasbinConf 23 | DB struct { 24 | DataSource string 25 | MaxOpenConns int `json:",default=10"` 26 | MaxIdleConns int `json:",default=100"` 27 | MaxLifetime int `json:",default=3600"` 28 | } 29 | Oss struct { 30 | Endpoint string 31 | AccessKeyId string 32 | AccessKeySecret string 33 | BucketName string 34 | ConnectTimeout int64 `json:",optional"` 35 | ReadWriteTimeout int64 `json:",optional"` 36 | } 37 | Isdev bool 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/usernocasbin/login_handler.go: -------------------------------------------------------------------------------- 1 | package usernocasbin 2 | 3 | import ( 4 | "context" 5 | "net/http" 6 | 7 | "go-zero-admin/application/applet/api/internal/logic/usernocasbin" 8 | "go-zero-admin/application/applet/api/internal/svc" 9 | "go-zero-admin/application/applet/api/internal/types" 10 | "go-zero-admin/pkg/result" 11 | 12 | "github.com/zeromicro/go-zero/rest/httpx" 13 | ) 14 | 15 | func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 16 | return func(w http.ResponseWriter, r *http.Request) { 17 | var req types.LoginRequest 18 | if err := httpx.Parse(r, &req); err != nil { 19 | httpx.ErrorCtx(r.Context(), w, err) 20 | return 21 | } 22 | 23 | ctx := context.WithValue(r.Context(), "RemoteAddr", r.RemoteAddr) 24 | ctx = context.WithValue(ctx, "Isdev", r.Header.Get("Isdev")) 25 | r = r.WithContext(ctx) 26 | 27 | l := usernocasbin.NewLoginLogic(r.Context(), svcCtx) 28 | resp, err := l.Login(&req) 29 | result.HttpResult(r, w, resp, err) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/update.tpl: -------------------------------------------------------------------------------- 1 | func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error { 2 | {{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, newData.{{.upperStartCamelPrimaryKey}}) 3 | if err!=nil{ 4 | return err 5 | } 6 | 7 | {{end}} {{.keys}} 8 | _, {{if .containsIndexCache}}err{{else}}err:{{end}}= m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { 9 | query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder) 10 | return conn.ExecCtx(ctx, query, {{.expressionValues}}) 11 | }, {{.keyValues}}){{else}}query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder) 12 | _,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}} 13 | return err 14 | } 15 | -------------------------------------------------------------------------------- /application/applet/api/internal/handler/usernocasbin/random_image_handler.go: -------------------------------------------------------------------------------- 1 | package usernocasbin 2 | 3 | import ( 4 | "context" 5 | "net/http" 6 | 7 | "go-zero-admin/application/applet/api/internal/logic/usernocasbin" 8 | "go-zero-admin/application/applet/api/internal/svc" 9 | "go-zero-admin/application/applet/api/internal/types" 10 | "go-zero-admin/pkg/result" 11 | 12 | "github.com/zeromicro/go-zero/rest/httpx" 13 | ) 14 | 15 | func RandomImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 16 | return func(w http.ResponseWriter, r *http.Request) { 17 | var req types.RandomImageRequest 18 | if err := httpx.Parse(r, &req); err != nil { 19 | httpx.ErrorCtx(r.Context(), w, err) 20 | return 21 | } 22 | 23 | ctx := context.WithValue(r.Context(), "RemoteAddr", r.RemoteAddr) 24 | ctx = context.WithValue(ctx, "Isdev", r.Header.Get("Isdev")) 25 | r = r.WithContext(ctx) 26 | 27 | l := usernocasbin.NewRandomImageLogic(r.Context(), svcCtx) 28 | resp, err := l.RandomImage(&req) 29 | result.HttpResult(r, w, resp, err) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/delete_sys_dictionary_info_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/rpc/internal/model" 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type DeleteSysDictionaryInfoLogic struct { 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | logx.Logger 17 | } 18 | 19 | func NewDeleteSysDictionaryInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteSysDictionaryInfoLogic { 20 | return &DeleteSysDictionaryInfoLogic{ 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | Logger: logx.WithContext(ctx), 24 | } 25 | } 26 | 27 | // 删除SysDictionaryInfo 28 | func (l *DeleteSysDictionaryInfoLogic) DeleteSysDictionaryInfo(in *pb.DeleteSysDictionaryInfoRequest) (*pb.NoDataResponse, error) { 29 | err := l.svcCtx.DB.Where("id = ?", in.ID).Delete(&model.SysDictionaryInfo{}).Error 30 | 31 | return &pb.NoDataResponse{}, err 32 | } 33 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/model/sys_dictionary_info.go: -------------------------------------------------------------------------------- 1 | // 自动生成模板SysDictionaryDetail 2 | package model 3 | 4 | import "go-zero-admin/pkg/model" 5 | 6 | type SysDictionaryInfo struct { 7 | model.MODEL_BASE 8 | Label string `json:"label" form:"label" gorm:"column:label;comment:展示值"` // 展示值 9 | Value int64 `json:"value" form:"value" gorm:"column:value;comment:字典值"` // 字典值 10 | Extend string `json:"extend" form:"extend" gorm:"column:extend;comment:扩展值"` // 扩展值 11 | Status int64 `json:"status" form:"status" gorm:"column:status;comment:启用状态"` // 启用状态 状态 1开启 2关闭 12 | Sort int64 `json:"sort" form:"sort" gorm:"column:sort;comment:排序标记"` // 排序标记 13 | SysDictionaryID int64 `json:"sysDictionaryID" form:"sysDictionaryID" gorm:"column:sys_dictionary_id;comment:关联标记"` // 关联标记 14 | } 15 | 16 | func (SysDictionaryInfo) TableName() string { 17 | return "sys_dictionary_info" 18 | } 19 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/menu/add_base_menu_logic.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type AddBaseMenuLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewAddBaseMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddBaseMenuLogic { 21 | return &AddBaseMenuLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *AddBaseMenuLogic) AddBaseMenu(req *types.AddBaseMenuRequest) (resp *types.AddBaseMenuResponse, err error) { 29 | 30 | var pbSysBaseMenu pb.SysBaseMenu 31 | _ = copier.Copy(&pbSysBaseMenu, req) 32 | 33 | _, err = l.svcCtx.AppletMenuRPC.AddMenuBase(l.ctx, &pb.AddMenuBaseRequest{SysBaseMenu: &pbSysBaseMenu}) 34 | return &types.AddBaseMenuResponse{}, err 35 | } 36 | -------------------------------------------------------------------------------- /README.en.md: -------------------------------------------------------------------------------- 1 | # go-zero-admin 2 | 3 | #### Description 4 | 5 | {**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} 6 | 7 | #### Software Architecture 8 | 9 | Software architecture description 10 | 11 | #### Installation 12 | 13 | 1. xxxx 14 | 2. xxxx 15 | 3. xxxx 16 | 17 | #### Instructions 18 | 19 | 1. xxxx 20 | 2. xxxx 21 | 3. xxxx 22 | 23 | #### Contribution 24 | 25 | 1. Fork the repository 26 | 2. Create Feat_xxx branch 27 | 3. Commit your code 28 | 4. Create Pull Request 29 | 30 | #### Gitee Feature 31 | 32 | 1. You can use Readme_XXX.md to support different languages, such as Readme_en.md, Readme_zh.md 33 | 2. Gitee blog [blog.gitee.com](https://blog.gitee.com) 34 | 3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) 35 | 4. The most valuable open source project [GVP](https://gitee.com/gvp) 36 | 5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) 37 | 6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) 38 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/menu/delete_base_menu_logic.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type DeleteBaseMenuLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewDeleteBaseMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteBaseMenuLogic { 20 | return &DeleteBaseMenuLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *DeleteBaseMenuLogic) DeleteBaseMenu(req *types.DeleteBaseMenuRequest) (resp *types.MessageResponse, err error) { 28 | _, err = l.svcCtx.AppletMenuRPC.DeleteBaseMenu(l.ctx, &pb.DeleteBaseMenuRequest{ID: req.ID}) 29 | if err != nil { 30 | logx.Errorf("l.svcCtx.AppletMenuRPC.DeleteBaseMenu err:%v", err) 31 | return nil, err 32 | } 33 | 34 | return &types.MessageResponse{Message: "删除成功!"}, nil 35 | } 36 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/docker/docker.tpl: -------------------------------------------------------------------------------- 1 | FROM golang:{{.Version}}alpine AS builder 2 | 3 | LABEL stage=gobuilder 4 | 5 | ENV CGO_ENABLED 0 6 | {{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct 7 | RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories 8 | {{end}}{{if .HasTimezone}} 9 | RUN apk update --no-cache && apk add --no-cache tzdata 10 | {{end}} 11 | WORKDIR /build 12 | 13 | ADD go.mod . 14 | ADD go.sum . 15 | RUN go mod download 16 | COPY . . 17 | {{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc 18 | {{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoMainFrom}} 19 | 20 | 21 | FROM {{.BaseImage}} 22 | 23 | COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt 24 | {{if .HasTimezone}}COPY --from=builder /usr/share/zoneinfo/{{.Timezone}} /usr/share/zoneinfo/{{.Timezone}} 25 | ENV TZ {{.Timezone}} 26 | {{end}} 27 | WORKDIR /app 28 | COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}} 29 | COPY --from=builder /app/etc /app/etc{{end}} 30 | {{if .HasPort}} 31 | EXPOSE {{.Port}} 32 | {{end}} 33 | CMD ["./{{.ExeFile}}"{{.Argument}}] 34 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/api/delete_apis_by_ids_logic.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "context" 5 | "go-zero-admin/application/applet/api/internal/svc" 6 | "go-zero-admin/application/applet/api/internal/types" 7 | "go-zero-admin/application/applet/rpc/pb" 8 | 9 | "github.com/zeromicro/go-zero/core/logx" 10 | ) 11 | 12 | type DeleteApisByIdsLogic struct { 13 | logx.Logger 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | } 17 | 18 | func NewDeleteApisByIdsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteApisByIdsLogic { 19 | return &DeleteApisByIdsLogic{ 20 | Logger: logx.WithContext(ctx), 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | } 24 | } 25 | 26 | func (l *DeleteApisByIdsLogic) DeleteApisByIds(req *types.DeleteApisByIdsRequest) (resp *types.MessageResponse, err error) { 27 | _, err = l.svcCtx.AppletAPIRPC.DeleteApisByIds(l.ctx, &pb.DeleteApisByIdsRequest{Ids: req.Ids}) 28 | if err != nil { 29 | logx.Errorf("l.svcCtx.AppletAPIRPC.DeleteApisByIds err:%v", err) 30 | return nil, err 31 | } 32 | 33 | return &types.MessageResponse{Message: "删除成功!"}, nil 34 | } 35 | -------------------------------------------------------------------------------- /application/applet/api/etc/applet-api.yaml: -------------------------------------------------------------------------------- 1 | Name: applet-api 2 | Host: 0.0.0.0 3 | Port: 7001 4 | BizRedis: 5 | Host: 127.0.0.1:6379 6 | Pass: 7 | Type: node 8 | Page: 9 | PageNo: 1 10 | PageSize: 10 11 | AppletRPC: 12 | Etcd: 13 | Hosts: 14 | - 127.0.0.1:2379 15 | Key: applet.rpc 16 | NonBlock: true 17 | JwtAuth: 18 | AccessSecret: 3fG7-go-zero-admin_p2hE 19 | AccessExpire: 31536000 20 | DB: 21 | DataSource: root:123456@tcp(127.0.0.1:3306)/goZero-admin?parseTime=true&loc=Local 22 | MaxIdleConns: 10 23 | MaxOpenConns: 100 24 | ConnMaxLifetime: 3600 25 | CasbinConf: 26 | ModelText: | 27 | [request_definition] 28 | r = sub, obj, act 29 | [policy_definition] 30 | p = sub, obj, act 31 | [role_definition] 32 | g = _, _ 33 | [policy_effect] 34 | e = some(where (p.eft == allow)) 35 | [matchers] 36 | m = r.sub == p.sub && myFun(r.obj,p.obj) && r.act == p.act 37 | # m = r.sub == p.sub && r.obj == p.obj && r.act == p.act 38 | Oss: # oss相关数据 安全问题 存到本地环境中了 39 | Endpoint: 40 | AccessKeyId: 41 | AccessKeySecret: 42 | BucketName: 43 | Isdev: true # true的时候不验证 验证码 -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/user/reset_user_password_logic.go: -------------------------------------------------------------------------------- 1 | package userlogic 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/rpc/internal/model" 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | "go-zero-admin/pkg/hash" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type ResetUserPasswordLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewResetUserPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetUserPasswordLogic { 21 | return &ResetUserPasswordLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | // 重置用户密码 默认密码:goZero 29 | func (l *ResetUserPasswordLogic) ResetUserPassword(in *pb.ResetUserPasswordRequest) (*pb.NoDataResponse, error) { 30 | hashedPassword := hash.BcryptHash(l.svcCtx.Config.Default.UserPassword) 31 | 32 | err := l.svcCtx.DB.Model(&model.SysUser{}).Where("id = ?", in.UserID).Update("password", hashedPassword).Error 33 | return &pb.NoDataResponse{}, err 34 | } 35 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/authority/delete_authority_logic.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type DeleteAuthorityLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewDeleteAuthorityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteAuthorityLogic { 20 | return &DeleteAuthorityLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *DeleteAuthorityLogic) DeleteAuthority(req *types.DeleteAuthorityRequest) (resp *types.MessageResponse, err error) { 28 | _, err = l.svcCtx.AppletAuthorityRPC.DeleteAuthority(l.ctx, &pb.DeleteAuthorityRequest{ID: req.ID}) 29 | if err != nil { 30 | logx.Errorf("l.svcCtx.AppletAuthorityRPC.DeleteAuthority 错误 error: %v", err) 31 | return nil, err 32 | } 33 | 34 | return &types.MessageResponse{Message: "删除成功!"}, nil 35 | } 36 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/user/register_logic.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type RegisterLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic { 21 | return &RegisterLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *RegisterLogic) Register(req *types.RegisterRequest) (resp *types.RegisterResponse, err error) { 29 | var pbUserInfo pb.UserInfo 30 | _ = copier.Copy(&pbUserInfo, req) 31 | _, err = l.svcCtx.AppletUserRPC.Register(l.ctx, &pb.RegisterRequest{UserInfo: &pbUserInfo, AuthorityIds: req.AuthorityIds}) 32 | if err != nil { 33 | logx.Errorf("l.svcCtx.AppletUserRPC.Register err: %v", err) 34 | return nil, err 35 | } 36 | 37 | return 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/user/reset_user_password_logic.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type ResetUserPasswordLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewResetUserPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetUserPasswordLogic { 20 | return &ResetUserPasswordLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *ResetUserPasswordLogic) ResetUserPassword(req *types.ResetUserPasswordRequest) (resp *types.MessageResponse, err error) { 28 | _, err = l.svcCtx.AppletUserRPC.ResetUserPassword(l.ctx, &pb.ResetUserPasswordRequest{UserID: req.UserID}) 29 | if err != nil { 30 | logx.Errorf("l.svcCtx.AppletUserRPC.ResetUserPassword err: %v", err) 31 | return nil, err 32 | } 33 | 34 | return &types.MessageResponse{Message: "重置成功!"}, err 35 | } 36 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/model/sys_authority.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | import ( 4 | "time" 5 | ) 6 | 7 | type SysAuthority struct { 8 | CreatedAt time.Time // 创建时间 9 | UpdatedAt time.Time // 更新时间 10 | DeletedAt *time.Time `sql:"index"` 11 | AuthorityId int64 `json:"authorityId" gorm:"not null;unique;primary_key;comment:角色ID;"` // 角色ID 12 | AuthorityName string `json:"authorityName" gorm:"comment:角色名"` // 角色名 13 | ParentId *int64 `json:"parentId" gorm:"comment:父角色ID"` // 父角色ID 14 | DataAuthorityId []*SysAuthority `json:"dataAuthorityId" gorm:"many2many:sys_data_authority_id;"` 15 | Children []SysAuthority `json:"children" gorm:"-"` 16 | SysBaseMenus []SysBaseMenu `json:"menus" gorm:"many2many:sys_authority_menus;"` 17 | Users []SysUser `json:"-" gorm:"many2many:sys_user_authority;"` 18 | DefaultRouter string `json:"defaultRouter" gorm:"comment:默认菜单;default:index"` // 默认菜单(默认dashboard) 19 | } 20 | 21 | func (SysAuthority) TableName() string { 22 | return "sys_authorities" 23 | } 24 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/api/delete_api_logic.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type DeleteApiLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewDeleteApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteApiLogic { 21 | return &DeleteApiLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *DeleteApiLogic) DeleteApi(req *types.DeleteApiRequest) (resp *types.MessageResponse, err error) { 29 | var sysApi pb.SysApi 30 | _ = copier.Copy(&sysApi, req.SysApi) 31 | _, err = l.svcCtx.AppletAPIRPC.DeleteApi(l.ctx, &pb.DeleteApiRequest{SysApi: &sysApi}) 32 | if err != nil { 33 | logx.Errorf("l.svcCtx.AppletAPIRPC.DeleteApi err:%v", err) 34 | return nil, err 35 | } 36 | 37 | return &types.MessageResponse{Message: "删除成功"}, nil 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/api/update_api_logic.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type UpdateApiLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewUpdateApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateApiLogic { 21 | return &UpdateApiLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *UpdateApiLogic) UpdateApi(req *types.UpdateApiRequest) (resp *types.MessageResponse, err error) { 29 | var pbSysApi pb.SysApi 30 | _ = copier.Copy(&pbSysApi, req) 31 | _, err = l.svcCtx.AppletAPIRPC.UpdateApi(l.ctx, &pb.UpdateApiRequest{SysApi: &pbSysApi}) 32 | if err != nil { 33 | logx.Errorf("l.svcCtx.AppletAPIRPC.UpdateApi err:%v", err) 34 | return nil, err 35 | } 36 | 37 | return &types.MessageResponse{Message: "修改成功!"}, nil 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/api/get_all_api_list_logic.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetAllApiListLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewGetAllApiListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAllApiListLogic { 21 | return &GetAllApiListLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *GetAllApiListLogic) GetAllApiList(req *types.GetAllApiListRequest) (resp *types.GetAllApiListResponse, err error) { 29 | apiList, err := l.svcCtx.AppletAPIRPC.GetAllApiList(l.ctx, &pb.NoDataResponse{}) 30 | if err != nil { 31 | logx.Errorf("----------- 获取失败! err:%v", err) 32 | return nil, err 33 | } 34 | resp = &types.GetAllApiListResponse{} 35 | _ = copier.Copy(&resp.ApiList, apiList.ApiList) 36 | 37 | return resp, err 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/api/get_all_api_list_logic.go: -------------------------------------------------------------------------------- 1 | package apilogic 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/jinzhu/copier" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type GetAllApiListLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewGetAllApiListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAllApiListLogic { 22 | return &GetAllApiListLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 获取全部API列表 30 | func (l *GetAllApiListLogic) GetAllApiList(in *pb.NoDataResponse) (*pb.GetAllApiListResponse, error) { 31 | fmt.Println("========= GetAllApiList ======= ") 32 | var apiList []model.SysApi 33 | err := l.svcCtx.DB.Find(&apiList).Error 34 | //var pbSysApi []pb.SysApi 35 | var AllApiListRes pb.GetAllApiListResponse 36 | _ = copier.Copy(&AllApiListRes.ApiList, apiList) 37 | 38 | return &AllApiListRes, err 39 | } 40 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/delete_sys_dictionary_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type DeleteSysDictionaryLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewDeleteSysDictionaryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteSysDictionaryLogic { 20 | return &DeleteSysDictionaryLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *DeleteSysDictionaryLogic) DeleteSysDictionary(req *types.DeleteSysDictionaryRequest) (resp *types.MessageResponse, err error) { 28 | _, err = l.svcCtx.AppletDictionaryRPC.DeleteSysDictionary(l.ctx, &pb.DeleteSysDictionaryRequest{ID: req.ID}) 29 | if err != nil { 30 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.DeleteSysDictionary err:%v", err) 31 | return nil, err 32 | } 33 | return &types.MessageResponse{Message: "删除成功!"}, nil 34 | } 35 | -------------------------------------------------------------------------------- /pkg/result/xerr/errMsg.go: -------------------------------------------------------------------------------- 1 | package xerr 2 | 3 | var message map[uint32]string 4 | 5 | func init() { 6 | message = make(map[uint32]string) 7 | message[OK] = "SUCCESS" 8 | message[SERVER_COMMON_ERROR] = "服务器开小差啦,稍后再来试一试" 9 | message[REUQEST_PARAM_ERROR] = "参数错误" 10 | message[TOKEN_EXPIRE_ERROR] = "token失效,请重新登陆" 11 | message[TOKEN_GENERATE_ERROR] = "生成token失败" 12 | message[DB_ERROR] = "数据库繁忙,请稍后再试" 13 | message[DB_UPDATE_AFFECTED_ZERO_ERROR] = "更新数据影响行数为0" 14 | 15 | message[CAPTCHA_ERROR] = "验证码错误" 16 | message[USER_PASSWORD_ERROR] = "账号或者密码错误" 17 | 18 | message[REPEAT_NAME_ERROR] = "添加失败,不能添加同样的name" 19 | 20 | // sys_base 模块 21 | message[REPEAT_OSS_GET_BUCKET_ERROR] = "获取oss_bucket实例失败" 22 | message[REPEAT_OSS_PUT_BUCKET_ERROR] = "上传oss_bucket失败" 23 | message[EMAIL_CANNOT_BE_EMPTY_ERROR] = "邮箱不能为空错误" 24 | message[ALREADY_EXISTS_ERROR] = "邮箱已发送" 25 | } 26 | 27 | func MapErrMsg(errcode uint32) string { 28 | if msg, ok := message[errcode]; ok { 29 | return msg 30 | } else { 31 | return "服务器开小差啦,稍后再来试一试" 32 | } 33 | } 34 | 35 | func IsCodeErr(errcode uint32) bool { 36 | if _, ok := message[errcode]; ok { 37 | return true 38 | } else { 39 | return false 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/api/create_api_logic.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | "go-zero-admin/pkg/result/xerr" 10 | 11 | "github.com/jinzhu/copier" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type CreateApiLogic struct { 16 | logx.Logger 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | } 20 | 21 | func NewCreateApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateApiLogic { 22 | return &CreateApiLogic{ 23 | Logger: logx.WithContext(ctx), 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | } 27 | } 28 | 29 | func (l *CreateApiLogic) CreateApi(req *types.CreateApiRequest) (resp *types.MessageResponse, err error) { 30 | var sysApi pb.SysApi 31 | _ = copier.Copy(&sysApi, req.SysApi) 32 | _, err = l.svcCtx.AppletAPIRPC.CreateApi(l.ctx, &pb.CreateApiRequest{SysApi: &sysApi}) 33 | if err != nil { 34 | logx.Errorf("创建失败 err: %v", err) 35 | return nil, xerr.NewErrMsg("创建失败不能拥有同样的属性") 36 | } 37 | 38 | return &types.MessageResponse{Message: "创建成功"}, nil 39 | } 40 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/authority/add_authority_menu_logic.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type AddAuthorityMenuLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewAddAuthorityMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddAuthorityMenuLogic { 20 | return &AddAuthorityMenuLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *AddAuthorityMenuLogic) AddAuthorityMenu(req *types.AddAuthorityMenuRequest) (resp *types.MessageResponse, err error) { 28 | var authorityMenu pb.AddAuthorityMenuRequest 29 | authorityMenu.AuthorityId = req.AuthorityId 30 | authorityMenu.MenuIds = req.MenuIds 31 | 32 | _, err = l.svcCtx.AppletAuthorityRPC.AddAuthorityMenu(l.ctx, &authorityMenu) 33 | if err != nil { 34 | return nil, err 35 | } 36 | 37 | return &types.MessageResponse{Message: "添加成功"}, nil 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/menu/get_base_menu_by_id_logic.go: -------------------------------------------------------------------------------- 1 | package menulogic 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/rpc/internal/model" 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetBaseMenuByIdLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewGetBaseMenuByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBaseMenuByIdLogic { 21 | return &GetBaseMenuByIdLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | // 根据id获取菜单 29 | func (l *GetBaseMenuByIdLogic) GetBaseMenuById(in *pb.GetBaseMenuByIdRequest) (*pb.GetBaseMenuByIdResponse, error) { 30 | var sysBaseMenu model.SysBaseMenu 31 | err := l.svcCtx.DB.Preload("MenuBtn").Preload("Parameters").Where("id = ?", in.ID).First(&sysBaseMenu).Error 32 | var pbSysBaseMenu pb.SysBaseMenu 33 | _ = copier.Copy(&pbSysBaseMenu, sysBaseMenu) 34 | return &pb.GetBaseMenuByIdResponse{SysBaseMenu: &pbSysBaseMenu}, err 35 | } 36 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/user/delete_user_logic.go: -------------------------------------------------------------------------------- 1 | package userlogic 2 | 3 | import ( 4 | "context" 5 | "gorm.io/gorm" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type DeleteUserLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewDeleteUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserLogic { 21 | return &DeleteUserLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | // 删除用户 29 | func (l *DeleteUserLogic) DeleteUser(in *pb.DeleteUserRequest) (*pb.NoDataResponse, error) { 30 | err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error { 31 | if err := tx.Where("id = ?", in.UserID).Delete(&model.SysUser{}).Error; err != nil { 32 | return err 33 | } 34 | if err := tx.Delete(&[]model.SysUserAuthority{}, "sys_user_id = ?", in.UserID).Error; err != nil { 35 | return err 36 | } 37 | return nil 38 | }) 39 | 40 | return &pb.NoDataResponse{}, err 41 | } 42 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/delete_sys_dictionary_info_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type DeleteSysDictionaryInfoLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewDeleteSysDictionaryInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteSysDictionaryInfoLogic { 20 | return &DeleteSysDictionaryInfoLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *DeleteSysDictionaryInfoLogic) DeleteSysDictionaryInfo(req *types.DeleteSysDictionaryInfoRequest) (resp *types.MessageResponse, err error) { 28 | _, err = l.svcCtx.AppletDictionaryRPC.DeleteSysDictionaryInfo(l.ctx, &pb.DeleteSysDictionaryInfoRequest{ID: req.ID}) 29 | if err != nil { 30 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.DeleteSysDictionaryInfo err:%v", err) 31 | return nil, err 32 | } 33 | return &types.MessageResponse{Message: "删除成功!"}, nil 34 | } 35 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/kube/job.tpl: -------------------------------------------------------------------------------- 1 | apiVersion: batch/v1 2 | kind: CronJob 3 | metadata: 4 | name: {{.Name}} 5 | namespace: {{.Namespace}} 6 | spec: 7 | successfulJobsHistoryLimit: {{.SuccessfulJobsHistoryLimit}} 8 | schedule: "{{.Schedule}}" 9 | jobTemplate: 10 | spec: 11 | template: 12 | spec:{{if .ServiceAccount}} 13 | serviceAccountName: {{.ServiceAccount}}{{end}} 14 | {{end}}containers: 15 | - name: {{.Name}} 16 | image: # todo image url 17 | resources: 18 | requests: 19 | cpu: {{.RequestCpu}}m 20 | memory: {{.RequestMem}}Mi 21 | limits: 22 | cpu: {{.LimitCpu}}m 23 | memory: {{.LimitMem}}Mi 24 | command: 25 | - ./{{.ServiceName}} 26 | - -f 27 | - ./{{.Name}}.yaml 28 | volumeMounts: 29 | - name: timezone 30 | mountPath: /etc/localtime 31 | imagePullSecrets: 32 | - name: # registry secret, if no, remove this 33 | restartPolicy: OnFailure 34 | volumes: 35 | - name: timezone 36 | hostPath: 37 | path: /usr/share/zoneinfo/Asia/Shanghai 38 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/menu/update_base_menu_logic.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type UpdateBaseMenuLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewUpdateBaseMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateBaseMenuLogic { 21 | return &UpdateBaseMenuLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *UpdateBaseMenuLogic) UpdateBaseMenu(req *types.UpdateBaseMenuRequest) (resp *types.MessageResponse, err error) { 29 | var pbSysBaseMenu pb.SysBaseMenu 30 | _ = copier.Copy(&pbSysBaseMenu, req) 31 | _, err = l.svcCtx.AppletMenuRPC.UpdateBaseMenu(l.ctx, &pb.UpdateBaseMenuRequest{SysBaseMenu: &pbSysBaseMenu}) 32 | if err != nil { 33 | logx.Errorf("l.svcCtx.AppletMenuRPC.UpdateBaseMenu err: %v", err) 34 | return nil, err 35 | 36 | } 37 | 38 | return &types.MessageResponse{Message: "更新成功!"}, nil 39 | } 40 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/find-one.tpl: -------------------------------------------------------------------------------- 1 | func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) { 2 | {{if .withCache}}{{.cacheKey}} 3 | var resp {{.upperStartCamelObject}} 4 | err := m.QueryRowCtx(ctx, &resp, {{.cacheKeyVariable}}, func(ctx context.Context, conn sqlx.SqlConn, v any) error { 5 | query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) 6 | return conn.QueryRowCtx(ctx, v, query, {{.lowerStartCamelPrimaryKey}}) 7 | }) 8 | switch err { 9 | case nil: 10 | return &resp, nil 11 | case sqlc.ErrNotFound: 12 | return nil, ErrNotFound 13 | default: 14 | return nil, err 15 | }{{else}}query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) 16 | var resp {{.upperStartCamelObject}} 17 | err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelPrimaryKey}}) 18 | switch err { 19 | case nil: 20 | return &resp, nil 21 | case sqlc.ErrNotFound: 22 | return nil, ErrNotFound 23 | default: 24 | return nil, err 25 | }{{end}} 26 | } 27 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/get_sys_dictionary_list_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/rpc/internal/model" 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetSysDictionaryListLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewGetSysDictionaryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSysDictionaryListLogic { 21 | return &GetSysDictionaryListLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | // 获取SysDictionary列表 - 全部 29 | func (l *GetSysDictionaryListLogic) GetSysDictionaryList(in *pb.NoDataResponse) (*pb.DictionaryListResponse, error) { 30 | var modelSysDictionaryList []model.SysDictionary 31 | err := l.svcCtx.DB.Find(&modelSysDictionaryList).Error 32 | var sysDictionaryList []*pb.SysDictionary 33 | _ = copier.Copy(&sysDictionaryList, modelSysDictionaryList) 34 | 35 | return &pb.DictionaryListResponse{ 36 | SysDictionaryList: sysDictionaryList, 37 | }, err 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/casbin/update_casbin_data_logic.go: -------------------------------------------------------------------------------- 1 | package casbin 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type UpdateCasbinDataLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewUpdateCasbinDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCasbinDataLogic { 21 | return &UpdateCasbinDataLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *UpdateCasbinDataLogic) UpdateCasbinData(req *types.UpdateCasbinDataRequest) (resp *types.MessageResponse, err error) { 29 | var pbUpdateCasbinData pb.UpdateCasbinDataRequest 30 | _ = copier.Copy(&pbUpdateCasbinData.CasbinInfoList, req.CasbinInfoList) 31 | pbUpdateCasbinData.AuthorityId = req.AuthorityId 32 | _, err = l.svcCtx.AppletCasbinRPC.UpdateCasbinData(l.ctx, &pbUpdateCasbinData) 33 | if err != nil { 34 | return nil, err 35 | } 36 | 37 | return &types.MessageResponse{ 38 | Message: "修改成功!", 39 | }, nil 40 | } 41 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/menu/get_base_menu_by_id_logic.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetBaseMenuByIdLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewGetBaseMenuByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBaseMenuByIdLogic { 21 | return &GetBaseMenuByIdLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *GetBaseMenuByIdLogic) GetBaseMenuById(req *types.GetBaseMenuByIdRequest) (resp *types.GetBaseMenuByIdResponse, err error) { 29 | baseMenuById, err := l.svcCtx.AppletMenuRPC.GetBaseMenuById(l.ctx, &pb.GetBaseMenuByIdRequest{ID: req.Id}) 30 | 31 | if err != nil { 32 | logx.Errorf("l.svcCtx.AppletMenuRPC.GetBaseMenuById err: %v", err) 33 | return nil, err 34 | } 35 | var typesSysBaseMenu types.SysBaseMenu 36 | _ = copier.Copy(&typesSysBaseMenu, baseMenuById.SysBaseMenu) 37 | return &types.GetBaseMenuByIdResponse{SysBaseMenu: typesSysBaseMenu}, err 38 | } 39 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/find-one-by-field.tpl: -------------------------------------------------------------------------------- 1 | func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) { 2 | {{if .withCache}}{{.cacheKey}} 3 | var resp {{.upperStartCamelObject}} 4 | err := m.QueryRowIndexCtx(ctx, &resp, {{.cacheKeyVariable}}, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { 5 | query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) 6 | if err := conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}}); err != nil { 7 | return nil, err 8 | } 9 | return resp.{{.upperStartCamelPrimaryKey}}, nil 10 | }, m.queryPrimary) 11 | switch err { 12 | case nil: 13 | return &resp, nil 14 | case sqlc.ErrNotFound: 15 | return nil, ErrNotFound 16 | default: 17 | return nil, err 18 | } 19 | }{{else}}var resp {{.upperStartCamelObject}} 20 | query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table ) 21 | err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}}) 22 | switch err { 23 | case nil: 24 | return &resp, nil 25 | case sqlc.ErrNotFound: 26 | return nil, ErrNotFound 27 | default: 28 | return nil, err 29 | } 30 | }{{end}} 31 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/menu/get_base_menu_tree_logic.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetBaseMenuTreeLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewGetBaseMenuTreeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBaseMenuTreeLogic { 21 | return &GetBaseMenuTreeLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *GetBaseMenuTreeLogic) GetBaseMenuTree(req *types.GetBaseMenuTreeRequest) (resp *types.GetBaseMenuTreeResponse, err error) { 29 | baseMenuTree, err := l.svcCtx.AppletMenuRPC.GetBaseMenuTree(l.ctx, &pb.NoDataResponse{}) 30 | if err != nil { 31 | logx.Errorf("l.svcCtx.AppletMenuRPC.GetBaseMenuTree err: %v", err) 32 | return nil, err 33 | } 34 | 35 | var sysBaseMenuList []types.SysBaseMenu 36 | _ = copier.Copy(&sysBaseMenuList, baseMenuTree.SysBaseMenuList) 37 | 38 | return &types.GetBaseMenuTreeResponse{ 39 | SysBaseMenuList: sysBaseMenuList, 40 | }, err 41 | } 42 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/user/delete_user_logic.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | "go-zero-admin/pkg/ctxJwt" 10 | "go-zero-admin/pkg/result/xerr" 11 | 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type DeleteUserLogic struct { 16 | logx.Logger 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | } 20 | 21 | func NewDeleteUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserLogic { 22 | return &DeleteUserLogic{ 23 | Logger: logx.WithContext(ctx), 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | } 27 | } 28 | 29 | func (l *DeleteUserLogic) DeleteUser(req *types.DeleteUserRequest) (resp *types.MessageResponse, err error) { 30 | jwtUserId := ctxJwt.GetJwtDataID(l.ctx) 31 | if jwtUserId == req.UserID { 32 | logx.Errorf("不能自己删除自己") 33 | return nil, xerr.NewErrCodeMsg(900000, "不能自己删除自己") 34 | } 35 | _, err = l.svcCtx.AppletUserRPC.DeleteUser(l.ctx, &pb.DeleteUserRequest{UserID: req.UserID}) 36 | if err != nil { 37 | logx.Errorf("l.svcCtx.AppletUserRPC.DeleteUser err: %v", err) 38 | return nil, err 39 | } 40 | 41 | return &types.MessageResponse{Message: "删除成功"}, nil 42 | } 43 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/api/create_api_logic.go: -------------------------------------------------------------------------------- 1 | package apilogic 2 | 3 | import ( 4 | "context" 5 | "gorm.io/gorm" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/jinzhu/copier" 12 | "github.com/pkg/errors" 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type CreateApiLogic struct { 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | logx.Logger 20 | } 21 | 22 | func NewCreateApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateApiLogic { 23 | return &CreateApiLogic{ 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | Logger: logx.WithContext(ctx), 27 | } 28 | } 29 | 30 | // 创建/添加 API列表 31 | func (l *CreateApiLogic) CreateApi(in *pb.CreateApiRequest) (*pb.NoDataResponse, error) { 32 | if !errors.Is(l.svcCtx.DB.Where("path = ? AND method = ?", in.SysApi.Path, in.SysApi.Method).First(&model.SysApi{}).Error, gorm.ErrRecordNotFound) { 33 | return nil, errors.New("存在相同api") 34 | } 35 | var sysApi model.SysApi 36 | _ = copier.Copy(&sysApi, in.SysApi) 37 | //err := l.svcCtx.DB.Omit("deleted_at").Create(&in.SysApi).Error 38 | err := l.svcCtx.DB.Omit("deleted_at").Create(&sysApi).Error 39 | 40 | return &pb.NoDataResponse{}, err 41 | } 42 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/create_sys_dictionary_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type CreateSysDictionaryLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewCreateSysDictionaryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateSysDictionaryLogic { 21 | return &CreateSysDictionaryLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *CreateSysDictionaryLogic) CreateSysDictionary(req *types.CreateSysDictionaryRequest) (resp *types.MessageResponse, err error) { 29 | var pbSysDictionary pb.SysDictionary 30 | _ = copier.Copy(&pbSysDictionary, req) 31 | _, err = l.svcCtx.AppletDictionaryRPC.CreateSysDictionary(l.ctx, &pb.CreateSysDictionaryRequest{SysDictionary: &pbSysDictionary}) 32 | if err != nil { 33 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.CreateSysDictionary err:%v", err) 34 | return nil, err 35 | } 36 | 37 | return &types.MessageResponse{Message: "创建成功!"}, nil 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/update_sys_dictionary_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type UpdateSysDictionaryLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewUpdateSysDictionaryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateSysDictionaryLogic { 21 | return &UpdateSysDictionaryLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *UpdateSysDictionaryLogic) UpdateSysDictionary(req *types.UpdateSysDictionaryRequest) (resp *types.MessageResponse, err error) { 29 | var pbSysDictionary pb.SysDictionary 30 | _ = copier.Copy(&pbSysDictionary, req) 31 | _, err = l.svcCtx.AppletDictionaryRPC.UpdateSysDictionary(l.ctx, &pb.UpdateSysDictionaryRequest{SysDictionary: &pbSysDictionary}) 32 | if err != nil { 33 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.UpdateSysDictionary err:%v", err) 34 | return nil, err 35 | } 36 | 37 | return &types.MessageResponse{Message: "修改成功!"}, nil 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/menu/get_menu_logic.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | "go-zero-admin/pkg/ctxJwt" 10 | "go-zero-admin/pkg/result/xerr" 11 | 12 | "github.com/jinzhu/copier" 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type GetMenuLogic struct { 17 | logx.Logger 18 | ctx context.Context 19 | svcCtx *svc.ServiceContext 20 | } 21 | 22 | func NewGetMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenuLogic { 23 | return &GetMenuLogic{ 24 | Logger: logx.WithContext(ctx), 25 | ctx: ctx, 26 | svcCtx: svcCtx, 27 | } 28 | } 29 | 30 | func (l *GetMenuLogic) GetMenu(req *types.GetMenuRequest) (resp *types.GetMenuResponse, err error) { 31 | authorityId := ctxJwt.GetJwtDataAuthorityId(l.ctx) 32 | menus, err := l.svcCtx.AppletMenuRPC.GetMenuTree(l.ctx, &pb.GetMenuTreeRequest{AuthorityId: int64(authorityId)}) 33 | if err != nil { 34 | logx.Errorf("MenuTreeGet err: %v", err) 35 | return nil, xerr.NewErrCodeMsg(900002, "内部错误:MenuTreeGet") 36 | } 37 | 38 | var getMenuResponse types.GetMenuResponse 39 | _ = copier.Copy(&getMenuResponse.Menus, menus.SysMenu) 40 | return &getMenuResponse, err 41 | } 42 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/menu/add_menu_base_logic.go: -------------------------------------------------------------------------------- 1 | package menulogic 2 | 3 | import ( 4 | "context" 5 | "github.com/pkg/errors" 6 | "gorm.io/gorm" 7 | 8 | "go-zero-admin/application/applet/rpc/internal/model" 9 | "go-zero-admin/application/applet/rpc/internal/svc" 10 | "go-zero-admin/application/applet/rpc/pb" 11 | 12 | "github.com/jinzhu/copier" 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type AddMenuBaseLogic struct { 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | logx.Logger 20 | } 21 | 22 | func NewAddMenuBaseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddMenuBaseLogic { 23 | return &AddMenuBaseLogic{ 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | Logger: logx.WithContext(ctx), 27 | } 28 | } 29 | 30 | // 添加系统基础菜单列表 31 | func (l *AddMenuBaseLogic) AddMenuBase(in *pb.AddMenuBaseRequest) (*pb.NoDataResponse, error) { 32 | if !errors.Is(l.svcCtx.DB.Where("name = ?", in.SysBaseMenu.Name).First(&model.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) { 33 | //return nil, errors.Wrapf(xerr.NewErrCode(xerr.REPEAT_NAME_ERROR), "添加菜单名字重复错误") 34 | return nil, errors.New("存在重复name,请修改name") 35 | } 36 | var baseMenu model.SysBaseMenu 37 | _ = copier.Copy(&baseMenu, in.SysBaseMenu) 38 | return &pb.NoDataResponse{}, l.svcCtx.DB.Omit("deleted_at").Create(&baseMenu).Error 39 | } 40 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/api/delete_api_logic.go: -------------------------------------------------------------------------------- 1 | package apilogic 2 | 3 | import ( 4 | "context" 5 | "gorm.io/gorm" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/pkg/errors" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type DeleteApiLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewDeleteApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteApiLogic { 22 | return &DeleteApiLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 删除API列表 30 | func (l *DeleteApiLogic) DeleteApi(in *pb.DeleteApiRequest) (*pb.NoDataResponse, error) { 31 | var sysApi model.SysApi 32 | if errors.Is(l.svcCtx.DB.Where("id = ?", in.SysApi.ID).First(&sysApi).Error, gorm.ErrRecordNotFound) { 33 | return nil, errors.New("api记录不存在") 34 | } 35 | 36 | //err := l.svcCtx.DB.Delete(&sysApi).Error // 软删除 37 | err := l.svcCtx.DB.Unscoped().Delete(&sysApi).Error //永久删除 硬删除 38 | if err != nil { 39 | logx.Errorf("l.svcCtx.DB.Delete(&sysApi) err: %v", err) 40 | return nil, err 41 | } 42 | 43 | // 这里要做删除功能 casbin 删除对应的策略 RemoveNamedPolicy() 44 | 45 | return &pb.NoDataResponse{}, nil 46 | } 47 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/update_sys_dictionary_info_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type UpdateSysDictionaryInfoLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewUpdateSysDictionaryInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateSysDictionaryInfoLogic { 21 | return &UpdateSysDictionaryInfoLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *UpdateSysDictionaryInfoLogic) UpdateSysDictionaryInfo(req *types.UpdateSysDictionaryInfoRequest) (resp *types.MessageResponse, err error) { 29 | var pbSysDictionaryInfo pb.SysDictionaryInfo 30 | _ = copier.Copy(&pbSysDictionaryInfo, req) 31 | _, err = l.svcCtx.AppletDictionaryRPC.UpdateSysDictionaryInfo(l.ctx, &pb.UpdateSysDictionaryInfoRequest{SysDictionaryInfo: &pbSysDictionaryInfo}) 32 | if err != nil { 33 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.UpdateSysDictionaryInfo err:%v", err) 34 | return nil, err 35 | } 36 | return &types.MessageResponse{Message: "修改成功!"}, nil 37 | } 38 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/create_sys_dictionary_info_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type CreateSysDictionaryInfoLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewCreateSysDictionaryInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateSysDictionaryInfoLogic { 21 | return &CreateSysDictionaryInfoLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *CreateSysDictionaryInfoLogic) CreateSysDictionaryInfo(req *types.CreateSysDictionaryInfoRequest) (resp *types.MessageResponse, err error) { 29 | var pbSysDictionaryInfo pb.SysDictionaryInfo 30 | _ = copier.Copy(&pbSysDictionaryInfo, req) 31 | _, err = l.svcCtx.AppletDictionaryRPC.CreateSysDictionaryInfo(l.ctx, &pb.CreateSysDictionaryInfoRequest{SysDictionaryInfo: &pbSysDictionaryInfo}) 32 | if err != nil { 33 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.CreateSysDictionaryInfo err:%v", err) 34 | return nil, err 35 | } 36 | 37 | return &types.MessageResponse{Message: "创建成功!"}, nil 38 | } 39 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/create_sys_dictionary_info_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/rpc/internal/model" 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/pkg/errors" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type CreateSysDictionaryInfoLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewCreateSysDictionaryInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateSysDictionaryInfoLogic { 22 | return &CreateSysDictionaryInfoLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 创建SysDictionaryInfo 30 | func (l *CreateSysDictionaryInfoLogic) CreateSysDictionaryInfo(in *pb.CreateSysDictionaryInfoRequest) (*pb.NoDataResponse, error) { 31 | if in.SysDictionaryInfo.SysDictionaryID == 0 { 32 | return &pb.NoDataResponse{}, errors.New("错误: 父级id不能为空") 33 | } 34 | 35 | var modelSysDictionaryInfo model.SysDictionaryInfo 36 | _ = copier.Copy(&modelSysDictionaryInfo, in.SysDictionaryInfo) 37 | modelSysDictionaryInfo.DeletedAt.Valid = false 38 | err := l.svcCtx.DB.Create(&modelSysDictionaryInfo).Error 39 | 40 | return &pb.NoDataResponse{}, err 41 | } 42 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/get_sys_dictionary_list_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetSysDictionaryListLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewGetSysDictionaryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSysDictionaryListLogic { 21 | return &GetSysDictionaryListLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *GetSysDictionaryListLogic) GetSysDictionaryList(req *types.GetSysDictionaryListRequest) (resp *types.GetSysDictionaryListResponse, err error) { 29 | sysDictionaryList, err := l.svcCtx.AppletDictionaryRPC.GetSysDictionaryList(l.ctx, &pb.NoDataResponse{}) 30 | if err != nil { 31 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.GetSysDictionaryList err:%v", err) 32 | return nil, err 33 | } 34 | var typesSysDictionaryList []types.SysDictionary 35 | _ = copier.Copy(&typesSysDictionaryList, sysDictionaryList.SysDictionaryList) 36 | 37 | return &types.GetSysDictionaryListResponse{ 38 | List: typesSysDictionaryList, 39 | }, err 40 | } 41 | -------------------------------------------------------------------------------- /test/testMain/openai/chatGPT/chatGPT.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "fmt" 7 | openai "github.com/sashabaranov/go-openai" 8 | "io" 9 | "net/http" 10 | "net/url" 11 | ) 12 | 13 | func main() { 14 | config := openai.DefaultConfig("") 15 | proxyUrl, err := url.Parse("") 16 | if err != nil { 17 | panic(err) 18 | } 19 | transport := &http.Transport{ 20 | Proxy: http.ProxyURL(proxyUrl), 21 | } 22 | config.HTTPClient = &http.Client{ 23 | Transport: transport, 24 | } 25 | 26 | c := openai.NewClientWithConfig(config) 27 | ctx := context.Background() 28 | 29 | req := openai.ChatCompletionRequest{ 30 | Model: openai.GPT3Dot5Turbo, 31 | MaxTokens: 20, 32 | Messages: []openai.ChatCompletionMessage{ 33 | { 34 | Role: openai.ChatMessageRoleUser, 35 | Content: "Lorem ipsum", 36 | }, 37 | }, 38 | Stream: true, 39 | } 40 | stream, err := c.CreateChatCompletionStream(ctx, req) 41 | if err != nil { 42 | fmt.Printf("ChatCompletionStream error: %v\n", err) 43 | return 44 | } 45 | defer stream.Close() 46 | 47 | fmt.Printf("Stream response: ") 48 | for { 49 | response, err := stream.Recv() 50 | if errors.Is(err, io.EOF) { 51 | fmt.Println("\nStream finished") 52 | return 53 | } 54 | 55 | if err != nil { 56 | fmt.Printf("\nStream error: %v\n", err) 57 | return 58 | } 59 | 60 | fmt.Printf(response.Choices[0].Delta.Content) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/casbin/get_path_by_authority_id_logic.go: -------------------------------------------------------------------------------- 1 | package casbinlogic 2 | 3 | import ( 4 | "context" 5 | "strconv" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type GetPathByAuthorityIdLogic struct { 14 | ctx context.Context 15 | svcCtx *svc.ServiceContext 16 | logx.Logger 17 | } 18 | 19 | func NewGetPathByAuthorityIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPathByAuthorityIdLogic { 20 | return &GetPathByAuthorityIdLogic{ 21 | ctx: ctx, 22 | svcCtx: svcCtx, 23 | Logger: logx.WithContext(ctx), 24 | } 25 | } 26 | 27 | // 根据角色id获取对应的casbin数据 28 | func (l *GetPathByAuthorityIdLogic) GetPathByAuthorityId(in *pb.GetPathByAuthorityIdRequest) (*pb.GetPathByAuthorityIdResponse, error) { 29 | csb := l.svcCtx.Config.CasbinConf.MustNewCasbinWithRedisWatcher(l.svcCtx.Config.DB.DataSource, l.svcCtx.Config.BizRedis) 30 | 31 | authorityId := strconv.Itoa(int(in.AuthorityId)) 32 | list := csb.GetFilteredPolicy(0, authorityId) 33 | //var casbinInfo []pb.CasbinInfo 34 | var pbGetPathByAuthorityId pb.GetPathByAuthorityIdResponse 35 | for _, v := range list { 36 | pbGetPathByAuthorityId.CasbinInfoList = append(pbGetPathByAuthorityId.CasbinInfoList, &pb.CasbinInfo{ 37 | Path: v[1], 38 | Method: v[2], 39 | }) 40 | } 41 | return &pbGetPathByAuthorityId, nil 42 | } 43 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/authority/create_authority_logic.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type CreateAuthorityLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewCreateAuthorityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAuthorityLogic { 21 | return &CreateAuthorityLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *CreateAuthorityLogic) CreateAuthority(req *types.CreateAuthorityRequest) (resp *types.CreateAuthorityResponse, err error) { 29 | var pbSysAuthority pb.SysAuthority 30 | _ = copier.Copy(&pbSysAuthority, req) 31 | 32 | createAuthority, err := l.svcCtx.AppletAuthorityRPC.CreateAuthority(l.ctx, &pb.CreateAuthorityRequest{SysAuthority: &pbSysAuthority}) 33 | if err != nil { 34 | logx.Errorf("l.svcCtx.AppletAuthorityRPC.CreateAuthority 错误 error: %v", err) 35 | return nil, err 36 | } 37 | 38 | var typesSysAuthority types.SysAuthority 39 | _ = copier.Copy(&typesSysAuthority, createAuthority.SysAuthority) 40 | 41 | return &types.CreateAuthorityResponse{SysAuthority: typesSysAuthority}, nil 42 | } 43 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/authority/update_authority_logic.go: -------------------------------------------------------------------------------- 1 | package authoritylogic 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/jinzhu/copier" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type UpdateAuthorityLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewUpdateAuthorityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAuthorityLogic { 22 | return &UpdateAuthorityLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 更新角色 -- 设为首页 30 | func (l *UpdateAuthorityLogic) UpdateAuthority(in *pb.UpdateAuthorityRequest) (*pb.UpdateAuthorityResponse, error) { 31 | var sysAuthority model.SysAuthority 32 | 33 | _ = copier.Copy(&sysAuthority, in.SysAuthority) 34 | err := l.svcCtx.DB.Where("authority_id = ?", sysAuthority.AuthorityId).First(&model.SysAuthority{}).Omit("deleted_at").Updates(&sysAuthority).Error 35 | fmt.Println("---------- sysAuthority", sysAuthority) 36 | 37 | var pbSysAuthority pb.SysAuthority 38 | _ = copier.Copy(&pbSysAuthority, sysAuthority) 39 | fmt.Println("---------- pbSysAuthority", pbSysAuthority) 40 | 41 | return &pb.UpdateAuthorityResponse{SysAuthority: &pbSysAuthority}, err 42 | } 43 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/user/get_user_list_logic.go: -------------------------------------------------------------------------------- 1 | package userlogic 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/rpc/internal/model" 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetUserListLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic { 21 | return &GetUserListLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | // 分页获取用户列表 29 | func (l *GetUserListLogic) GetUserList(in *pb.GetUserListRequest) (*pb.GetUserListResponse, error) { 30 | offset := int(in.PageRequest.PageSize * (in.PageRequest.PageNo - 1)) 31 | db := l.svcCtx.DB.Model(&model.SysUser{}) 32 | var sysUserList []model.SysUser 33 | var total int64 34 | err := db.Count(&total).Error 35 | if err != nil { 36 | return nil, err 37 | } 38 | err = db.Limit(int(in.PageRequest.PageSize)).Offset(offset).Preload("Authorities").Preload("Authority").Find(&sysUserList).Error 39 | //var userInfoList []pb.UserInfo 40 | var pbGetUserList pb.GetUserListResponse 41 | _ = copier.Copy(&pbGetUserList.UserInfoList, sysUserList) 42 | pbGetUserList.Total = total 43 | 44 | return &pbGetUserList, nil 45 | } 46 | -------------------------------------------------------------------------------- /test/testMain/openai/nginx--.conf: -------------------------------------------------------------------------------- 1 | # nginx -c G:\yanghao\code\github_yh\go-zero-admin\test\openai\nginx.conf 2 | 3 | 4 | events { 5 | worker_connections 1024; 6 | } 7 | 8 | http { 9 | server{ 10 | listen 8001; 11 | 12 | location /openAi { 13 | proxy_set_header Host $http_host; 14 | proxy_set_header X-Real-IP $remote_addr; 15 | proxy_set_header REMOTE-HOST $remote_addr; 16 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 17 | proxy_pass https://api.openai.com/v1/engines/davinci-codex/completions; 18 | } 19 | 20 | location /gitee { 21 | proxy_set_header Host $http_host; 22 | proxy_set_header X-Real-IP $remote_addr; 23 | proxy_set_header REMOTE-HOST $remote_addr; 24 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 25 | proxy_pass https://gitee.com/go-study/beyond_go-zero; 26 | } 27 | location /gva { 28 | proxy_set_header Host $http_host; 29 | proxy_set_header X-Real-IP $remote_addr; 30 | proxy_set_header REMOTE-HOST $remote_addr; 31 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 32 | proxy_pass https://gin-vue-admin.com; 33 | } 34 | 35 | 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /application/applet/api/internal/logic/authority/update_authority_logic.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type UpdateAuthorityLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewUpdateAuthorityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAuthorityLogic { 21 | return &UpdateAuthorityLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *UpdateAuthorityLogic) UpdateAuthority(req *types.UpdateAuthorityRequest) (resp *types.UpdateAuthorityResponse, err error) { 29 | var pbSysAuthority pb.SysAuthority 30 | _ = copier.Copy(&pbSysAuthority, req) 31 | updateAuthority, err := l.svcCtx.AppletAuthorityRPC.UpdateAuthority(l.ctx, &pb.UpdateAuthorityRequest{SysAuthority: &pbSysAuthority}) 32 | if err != nil { 33 | logx.Errorf("l.svcCtx.AppletAuthorityRPC.UpdateAuthority 错误 error: %v", err) 34 | return nil, err 35 | } 36 | 37 | var typesSysAuthority types.SysAuthority 38 | _ = copier.Copy(&typesSysAuthority, updateAuthority.SysAuthority) 39 | 40 | return &types.UpdateAuthorityResponse{SysAuthority: typesSysAuthority, Message: "更新成功"}, nil 41 | } 42 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/api/delete_apis_by_ids_logic.go: -------------------------------------------------------------------------------- 1 | package apilogic 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/rpc/internal/model" 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | //var casbin = casbinlogic.UpdateCasbinDataLogic{} 14 | 15 | type DeleteApisByIdsLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewDeleteApisByIdsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteApisByIdsLogic { 22 | return &DeleteApisByIdsLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 删除多条api 30 | func (l *DeleteApisByIdsLogic) DeleteApisByIds(in *pb.DeleteApisByIdsRequest) (*pb.NoDataResponse, error) { 31 | csb := l.svcCtx.Config.CasbinConf.MustNewCasbinWithRedisWatcher(l.svcCtx.Config.DB.DataSource, l.svcCtx.Config.BizRedis) 32 | 33 | var apis []model.SysApi 34 | err := l.svcCtx.DB.Find(&apis, "id in ?", in.Ids).Delete(&apis).Error 35 | if err != nil { 36 | return nil, err 37 | } else { 38 | for _, sysApi := range apis { 39 | _, err = csb.RemoveFilteredPolicy(1, sysApi.Path, sysApi.Method) 40 | //_, err = casbin.ClearCasbin(1, sysApi.Path, sysApi.Method) 41 | if err != nil { 42 | return nil, err 43 | } 44 | } 45 | } 46 | return &pb.NoDataResponse{}, nil 47 | } 48 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/server/casbin/casbin_server.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT. 2 | // Source: applet.proto 3 | 4 | package server 5 | 6 | import ( 7 | "context" 8 | 9 | "go-zero-admin/application/applet/rpc/internal/logic/casbin" 10 | "go-zero-admin/application/applet/rpc/internal/svc" 11 | "go-zero-admin/application/applet/rpc/pb" 12 | ) 13 | 14 | type CasbinServer struct { 15 | svcCtx *svc.ServiceContext 16 | pb.UnimplementedCasbinServer 17 | } 18 | 19 | func NewCasbinServer(svcCtx *svc.ServiceContext) *CasbinServer { 20 | return &CasbinServer{ 21 | svcCtx: svcCtx, 22 | } 23 | } 24 | 25 | // 根据角色id获取对应的casbin数据 26 | func (s *CasbinServer) GetPathByAuthorityId(ctx context.Context, in *pb.GetPathByAuthorityIdRequest) (*pb.GetPathByAuthorityIdResponse, error) { 27 | l := casbinlogic.NewGetPathByAuthorityIdLogic(ctx, s.svcCtx) 28 | return l.GetPathByAuthorityId(in) 29 | } 30 | 31 | // 更新一个角色的对应的casbin数据 32 | func (s *CasbinServer) UpdateCasbinData(ctx context.Context, in *pb.UpdateCasbinDataRequest) (*pb.NoDataResponse, error) { 33 | l := casbinlogic.NewUpdateCasbinDataLogic(ctx, s.svcCtx) 34 | return l.UpdateCasbinData(in) 35 | } 36 | 37 | // 更新一个角色的对应的casbin数据 用api的ids 查数据 38 | func (s *CasbinServer) UpdateCasbinDataByApiIds(ctx context.Context, in *pb.UpdateCasbinDataByApiIdsRequest) (*pb.UpdateCasbinDataByApiIdsResponse, error) { 39 | l := casbinlogic.NewUpdateCasbinDataByApiIdsLogic(ctx, s.svcCtx) 40 | return l.UpdateCasbinDataByApiIds(in) 41 | } 42 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/mongo/model_custom.tpl: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | {{if .Cache}}import ( 4 | "github.com/zeromicro/go-zero/core/stores/cache" 5 | "github.com/zeromicro/go-zero/core/stores/monc" 6 | ){{else}}import "github.com/zeromicro/go-zero/core/stores/mon"{{end}} 7 | 8 | {{if .Easy}} 9 | const {{.Type}}CollectionName = "{{.snakeType}}" 10 | {{end}} 11 | 12 | var _ {{.Type}}Model = (*custom{{.Type}}Model)(nil) 13 | 14 | type ( 15 | // {{.Type}}Model is an interface to be customized, add more methods here, 16 | // and implement the added methods in custom{{.Type}}Model. 17 | {{.Type}}Model interface { 18 | {{.lowerType}}Model 19 | } 20 | 21 | custom{{.Type}}Model struct { 22 | *default{{.Type}}Model 23 | } 24 | ) 25 | 26 | 27 | // New{{.Type}}Model returns a model for the mongo. 28 | {{if .Easy}}func New{{.Type}}Model(url, db string{{if .Cache}}, c cache.CacheConf{{end}}) {{.Type}}Model { 29 | conn := {{if .Cache}}monc{{else}}mon{{end}}.MustNewModel(url, db, {{.Type}}CollectionName{{if .Cache}}, c{{end}}) 30 | return &custom{{.Type}}Model{ 31 | default{{.Type}}Model: newDefault{{.Type}}Model(conn), 32 | } 33 | }{{else}}func New{{.Type}}Model(url, db, collection string{{if .Cache}}, c cache.CacheConf{{end}}) {{.Type}}Model { 34 | conn := {{if .Cache}}monc{{else}}mon{{end}}.MustNewModel(url, db, collection{{if .Cache}}, c{{end}}) 35 | return &custom{{.Type}}Model{ 36 | default{{.Type}}Model: newDefault{{.Type}}Model(conn), 37 | } 38 | }{{end}} 39 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/get_sys_dictionary_details_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | "gorm.io/gorm" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/jinzhu/copier" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type GetSysDictionaryDetailsLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewGetSysDictionaryDetailsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSysDictionaryDetailsLogic { 22 | return &GetSysDictionaryDetailsLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 根据ID或者type获取SysDictionary 30 | func (l *GetSysDictionaryDetailsLogic) GetSysDictionaryDetails(in *pb.GetSysDictionaryDetailsRequest) (*pb.GetSysDictionaryDetailsResponse, error) { 31 | var modelSysDictionary model.SysDictionary 32 | err := l.svcCtx.DB.Where("(type = ? OR id = ?) and status = ?", in.Type, in.ID, in.Status).Preload("SysDictionaryInfoList", func(db *gorm.DB) *gorm.DB { 33 | return db.Where("status = ?", 1).Order("sort") 34 | }).First(&modelSysDictionary).Error 35 | if err != nil { 36 | return nil, err 37 | } 38 | var pbSysDictionary pb.SysDictionary 39 | _ = copier.Copy(&pbSysDictionary, modelSysDictionary) 40 | 41 | return &pb.GetSysDictionaryDetailsResponse{SysDictionary: &pbSysDictionary}, err 42 | } 43 | -------------------------------------------------------------------------------- /test/goctl/1.6.0/model/model.tpl: -------------------------------------------------------------------------------- 1 | package {{.pkg}} 2 | {{if .withCache}} 3 | import ( 4 | "github.com/zeromicro/go-zero/core/stores/cache" 5 | "github.com/zeromicro/go-zero/core/stores/sqlx" 6 | ) 7 | {{else}} 8 | 9 | import "github.com/zeromicro/go-zero/core/stores/sqlx" 10 | {{end}} 11 | var _ {{.upperStartCamelObject}}Model = (*custom{{.upperStartCamelObject}}Model)(nil) 12 | 13 | type ( 14 | // {{.upperStartCamelObject}}Model is an interface to be customized, add more methods here, 15 | // and implement the added methods in custom{{.upperStartCamelObject}}Model. 16 | {{.upperStartCamelObject}}Model interface { 17 | {{.lowerStartCamelObject}}Model 18 | {{if not .withCache}}withSession(session sqlx.Session) {{.upperStartCamelObject}}Model{{end}} 19 | } 20 | 21 | custom{{.upperStartCamelObject}}Model struct { 22 | *default{{.upperStartCamelObject}}Model 23 | } 24 | ) 25 | 26 | // New{{.upperStartCamelObject}}Model returns a model for the database table. 27 | func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf, opts ...cache.Option{{end}}) {{.upperStartCamelObject}}Model { 28 | return &custom{{.upperStartCamelObject}}Model{ 29 | default{{.upperStartCamelObject}}Model: new{{.upperStartCamelObject}}Model(conn{{if .withCache}}, c, opts...{{end}}), 30 | } 31 | } 32 | 33 | {{if not .withCache}} 34 | func (m *custom{{.upperStartCamelObject}}Model) withSession(session sqlx.Session) {{.upperStartCamelObject}}Model { 35 | return New{{.upperStartCamelObject}}Model(sqlx.NewSqlConnFromSession(session)) 36 | } 37 | {{end}} 38 | 39 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/create_sys_dictionary_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "gorm.io/gorm" 7 | 8 | "go-zero-admin/application/applet/rpc/internal/model" 9 | "go-zero-admin/application/applet/rpc/internal/svc" 10 | "go-zero-admin/application/applet/rpc/pb" 11 | 12 | "github.com/jinzhu/copier" 13 | "github.com/pkg/errors" 14 | "github.com/zeromicro/go-zero/core/logx" 15 | ) 16 | 17 | type CreateSysDictionaryLogic struct { 18 | ctx context.Context 19 | svcCtx *svc.ServiceContext 20 | logx.Logger 21 | } 22 | 23 | func NewCreateSysDictionaryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateSysDictionaryLogic { 24 | return &CreateSysDictionaryLogic{ 25 | ctx: ctx, 26 | svcCtx: svcCtx, 27 | Logger: logx.WithContext(ctx), 28 | } 29 | } 30 | 31 | // 新建SysDictionary 32 | func (l *CreateSysDictionaryLogic) CreateSysDictionary(in *pb.CreateSysDictionaryRequest) (*pb.NoDataResponse, error) { 33 | fmt.Println("============ in", in.SysDictionary.Status) 34 | if in.SysDictionary.Status == 0 { 35 | in.SysDictionary.Status = 1 36 | } 37 | err := l.svcCtx.DB.First(&model.SysDictionary{}, "type = ?", in.SysDictionary.Type).Error 38 | if !errors.Is(err, gorm.ErrRecordNotFound) { 39 | return nil, errors.New("错误: 存在相同的type") 40 | } 41 | var modelSysDictionary model.SysDictionary 42 | _ = copier.Copy(&modelSysDictionary, in.SysDictionary) 43 | modelSysDictionary.DeletedAt.Valid = false 44 | err = l.svcCtx.DB.Create(&modelSysDictionary).Error 45 | 46 | return &pb.NoDataResponse{}, err 47 | } 48 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/delete_sys_dictionary_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | "gorm.io/gorm" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/pkg/errors" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type DeleteSysDictionaryLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewDeleteSysDictionaryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteSysDictionaryLogic { 22 | return &DeleteSysDictionaryLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 更新SysDictionary 30 | func (l *DeleteSysDictionaryLogic) DeleteSysDictionary(in *pb.DeleteSysDictionaryRequest) (*pb.NoDataResponse, error) { 31 | var modelSysDictionary model.SysDictionary 32 | err := l.svcCtx.DB.Where("id = ?", in.ID).Preload("SysDictionaryInfoList").First(&modelSysDictionary).Error 33 | if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { 34 | return nil, errors.New("无效id") 35 | } 36 | if err != nil { 37 | return nil, err 38 | } 39 | err = l.svcCtx.DB.Delete(&modelSysDictionary).Error 40 | if err != nil { 41 | return nil, err 42 | } 43 | if modelSysDictionary.SysDictionaryInfoList != nil { 44 | err = l.svcCtx.DB.Where("sys_dictionary_id=?", modelSysDictionary.ID).Delete(modelSysDictionary.SysDictionaryInfoList).Error 45 | } 46 | 47 | return &pb.NoDataResponse{}, err 48 | } 49 | -------------------------------------------------------------------------------- /test/casbin/casbin_rule.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : 本地 5 | Source Server Type : MySQL 6 | Source Server Version : 80034 7 | Source Host : localhost:3306 8 | Source Schema : gva03 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 80034 12 | File Encoding : 65001 13 | 14 | Date: 18/12/2023 10:14:55 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for casbin_rule 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `casbin_rule`; 24 | CREATE TABLE `casbin_rule` ( 25 | `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, 26 | `ptype` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 27 | `v0` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 28 | `v1` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 29 | `v2` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 30 | `v3` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 31 | `v4` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 32 | `v5` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 33 | PRIMARY KEY (`id`) USING BTREE, 34 | UNIQUE INDEX `idx_casbin_rule`(`ptype`, `v0`, `v1`, `v2`, `v3`, `v4`, `v5`) USING BTREE 35 | ) ENGINE = InnoDB AUTO_INCREMENT = 381 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; 36 | 37 | SET FOREIGN_KEY_CHECKS = 1; 38 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/update_sys_dictionary_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | "gorm.io/gorm" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/pkg/errors" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type UpdateSysDictionaryLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewUpdateSysDictionaryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateSysDictionaryLogic { 22 | return &UpdateSysDictionaryLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 更新SysDictionary 30 | func (l *UpdateSysDictionaryLogic) UpdateSysDictionary(in *pb.UpdateSysDictionaryRequest) (*pb.NoDataResponse, error) { 31 | var modelSysDictionary model.SysDictionary 32 | sysDictionaryMap := map[string]interface{}{ 33 | "Name": in.SysDictionary.Name, 34 | "Type": in.SysDictionary.Type, 35 | "Status": in.SysDictionary.Status, 36 | "Desc": in.SysDictionary.Desc, 37 | } 38 | db := l.svcCtx.DB.Where("id = ?", in.SysDictionary.ID).First(&modelSysDictionary) 39 | if modelSysDictionary.Type != in.SysDictionary.Type { 40 | if !errors.Is(l.svcCtx.DB.First(&model.SysDictionary{}, "type = ?", in.SysDictionary.Type).Error, gorm.ErrRecordNotFound) { 41 | return nil, errors.New("错误: 存在相同的type,不允许修改") 42 | } 43 | } 44 | err := db.Updates(sysDictionaryMap).Error 45 | 46 | return &pb.NoDataResponse{}, err 47 | } 48 | -------------------------------------------------------------------------------- /test/casbin_RBAC-with-domainste_nants/casbin_rule.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : 本地 5 | Source Server Type : MySQL 6 | Source Server Version : 80034 7 | Source Host : localhost:3306 8 | Source Schema : gva03 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 80034 12 | File Encoding : 65001 13 | 14 | Date: 18/12/2023 10:14:55 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for casbin_rule 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `casbin_rule`; 24 | CREATE TABLE `casbin_rule` ( 25 | `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT, 26 | `ptype` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 27 | `v0` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 28 | `v1` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 29 | `v2` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 30 | `v3` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 31 | `v4` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 32 | `v5` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, 33 | PRIMARY KEY (`id`) USING BTREE, 34 | UNIQUE INDEX `idx_casbin_rule`(`ptype`, `v0`, `v1`, `v2`, `v3`, `v4`, `v5`) USING BTREE 35 | ) ENGINE = InnoDB AUTO_INCREMENT = 381 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; 36 | 37 | SET FOREIGN_KEY_CHECKS = 1; 38 | -------------------------------------------------------------------------------- /pkg/result/httpResult.go: -------------------------------------------------------------------------------- 1 | package result 2 | 3 | import ( 4 | "fmt" 5 | "google.golang.org/grpc/status" 6 | "net/http" 7 | 8 | "go-zero-admin/pkg/result/xerr" 9 | 10 | "github.com/pkg/errors" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | "github.com/zeromicro/go-zero/rest/httpx" 13 | ) 14 | 15 | // 设置http 返回数据的结构 16 | func HttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) { 17 | fmt.Println("========== HttpResult", err) 18 | if err == nil { 19 | // 成功返回 20 | r := Success(resp) 21 | httpx.WriteJson(w, http.StatusOK, r) 22 | } else { 23 | //错误返回 24 | errcode := xerr.SERVER_COMMON_ERROR 25 | errmsg := "服务器开小差啦,稍后再来试一试" 26 | 27 | causeErr := errors.Cause(err) // err类型 28 | fmt.Println("causeErr:", causeErr) //causeErr: rpc error: code = Unknown desc = 存在重复name,请修改name 29 | fmt.Printf("causeErr: %T:", causeErr) //causeErr: *status.Error: 30 | 31 | if e, ok := causeErr.(*xerr.CodeError); ok { //自定义错误类型 32 | //自定义CodeError 33 | errcode = e.GetErrCode() 34 | errmsg = e.GetErrMsg() 35 | } else { 36 | if gstatus, ok := status.FromError(causeErr); ok { // grpc err错误 37 | grpcCode := uint32(gstatus.Code()) 38 | if xerr.IsCodeErr(grpcCode) { //区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端 39 | errcode = grpcCode 40 | errmsg = gstatus.Message() 41 | } else { // 临时加上 42 | errcode = grpcCode 43 | errmsg = "grpc err:" + gstatus.Message() 44 | } 45 | } 46 | } 47 | 48 | logx.WithContext(r.Context()).Errorf("【API-ERR】 : %+v ", err) 49 | //httpx.WriteJson(w, http.StatusBadRequest, Error(errcode, errmsg)) 50 | httpx.WriteJson(w, http.StatusOK, Error(errcode, errmsg)) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/get_sys_dictionary_info_list_details_by_id_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | "go-zero-admin/application/applet/api/internal/svc" 6 | "go-zero-admin/application/applet/api/internal/types" 7 | "go-zero-admin/application/applet/rpc/pb" 8 | 9 | "github.com/jinzhu/copier" 10 | "github.com/zeromicro/go-zero/core/logx" 11 | ) 12 | 13 | type GetSysDictionaryInfoListDetailsByIdLogic struct { 14 | logx.Logger 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | } 18 | 19 | func NewGetSysDictionaryInfoListDetailsByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSysDictionaryInfoListDetailsByIdLogic { 20 | return &GetSysDictionaryInfoListDetailsByIdLogic{ 21 | Logger: logx.WithContext(ctx), 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | } 25 | } 26 | 27 | func (l *GetSysDictionaryInfoListDetailsByIdLogic) GetSysDictionaryInfoListDetailsById(req *types.GetSysDictionaryInfoListDetailsByIdRequest) (resp *types.GetSysDictionaryInfoListDetailsByIdResponse, err error) { 28 | sysDictionaryInfoListDetailsById, err := l.svcCtx.AppletDictionaryRPC.GetSysDictionaryInfoListDetailsById(l.ctx, &pb.GetSysDictionaryInfoListDetailsByIdRequest{ 29 | ID: req.ID, 30 | }) 31 | if err != nil { 32 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.GetSysDictionaryInfoListDetailsById err:%v", err) 33 | return nil, err 34 | } 35 | var typesSysDictionaryInfo types.SysDictionaryInfo 36 | _ = copier.Copy(&typesSysDictionaryInfo, sysDictionaryInfoListDetailsById.SysDictionaryInfo) 37 | return &types.GetSysDictionaryInfoListDetailsByIdResponse{ 38 | SysDictionaryInfo: typesSysDictionaryInfo, 39 | }, nil 40 | } 41 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/menu/get_menu_authority_logic.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | "go-zero-admin/pkg/ctxJwt" 11 | 12 | "github.com/jinzhu/copier" 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type GetMenuAuthorityLogic struct { 17 | logx.Logger 18 | ctx context.Context 19 | svcCtx *svc.ServiceContext 20 | } 21 | 22 | func NewGetMenuAuthorityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenuAuthorityLogic { 23 | return &GetMenuAuthorityLogic{ 24 | Logger: logx.WithContext(ctx), 25 | ctx: ctx, 26 | svcCtx: svcCtx, 27 | } 28 | } 29 | 30 | func (l *GetMenuAuthorityLogic) GetMenuAuthority(req *types.GetMenuAuthorityRequest) (resp *types.GetMenuAuthorityResponse, err error) { 31 | authorityId := ctxJwt.GetJwtDataAuthorityId(l.ctx) 32 | authority, err := l.svcCtx.AppletMenuRPC.GetMenuAuthority(l.ctx, &pb.GetMenuAuthorityRequest{AuthorityId: int64(authorityId)}) 33 | fmt.Println("------------ authority", authority) 34 | if err != nil { 35 | return nil, err 36 | } 37 | //var sysMenus []types.SysMenu 38 | //for _, v := range authority.SysMenuList { 39 | // fmt.Println("---- v", v.) 40 | // 41 | //} 42 | 43 | var typesGetMenuAuthorityResponse types.GetMenuAuthorityResponse 44 | var sysMenus []types.SysMenu 45 | _ = copier.Copy(&sysMenus, authority.SysMenuList) 46 | fmt.Println("------------ sysMenus", sysMenus) 47 | typesGetMenuAuthorityResponse.SysMenuList = sysMenus 48 | 49 | return &typesGetMenuAuthorityResponse, err 50 | } 51 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/user/get_user_list_logic.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetUserListLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic { 21 | return &GetUserListLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *GetUserListLogic) GetUserList(req *types.GetUserListRequest) (resp *types.GetUserListResponse, err error) { 29 | var page pb.PageRequest 30 | if req.PageNo == 0 { 31 | req.PageNo = l.svcCtx.Config.Page.PageNo 32 | } 33 | if req.PageSize == 0 { 34 | req.PageSize = l.svcCtx.Config.Page.PageSize 35 | } 36 | page.PageNo = req.PageNo 37 | page.PageSize = req.PageSize 38 | 39 | getUserList, err := l.svcCtx.AppletUserRPC.GetUserList(l.ctx, &pb.GetUserListRequest{PageRequest: &page}) 40 | if err != nil { 41 | logx.Errorf("l.svcCtx.AppletUserRPC.GetUserList err: %v", err) 42 | return nil, err 43 | } 44 | var typesUserInfoList []types.UserInfo 45 | _ = copier.Copy(&typesUserInfoList, getUserList.UserInfoList) 46 | 47 | return &types.GetUserListResponse{ 48 | List: typesUserInfoList, 49 | PageResponse: types.PageResponse{ 50 | Total: getUserList.Total, 51 | PageNo: page.PageNo, 52 | PageSize: page.PageSize, 53 | }, 54 | }, nil 55 | } 56 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/get_sys_dictionary_details_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | "go-zero-admin/application/applet/api/internal/svc" 6 | "go-zero-admin/application/applet/api/internal/types" 7 | "go-zero-admin/application/applet/rpc/pb" 8 | "go-zero-admin/pkg/result/xerr" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetSysDictionaryDetailsLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewGetSysDictionaryDetailsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSysDictionaryDetailsLogic { 21 | return &GetSysDictionaryDetailsLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *GetSysDictionaryDetailsLogic) GetSysDictionaryDetails(req *types.GetSysDictionaryDetailsRequest) (resp *types.GetSysDictionaryDetailsResponse, err error) { 29 | if req.Status == 0 { 30 | req.Status = 1 31 | } 32 | if req.Type == "" && req.ID == 0 { 33 | return nil, xerr.NewErrCodeMsg(900000, "参数错误,id或者type 要有一个!") 34 | } 35 | sysDictionaryDetails, err := l.svcCtx.AppletDictionaryRPC.GetSysDictionaryDetails(l.ctx, &pb.GetSysDictionaryDetailsRequest{ 36 | ID: req.ID, 37 | Type: req.Type, 38 | Status: req.Status, 39 | }) 40 | if err != nil { 41 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.GetSysDictionaryDetails err:%v", err) 42 | return nil, err 43 | } 44 | var typesSysDictionary types.SysDictionary 45 | _ = copier.Copy(&typesSysDictionary, sysDictionaryDetails.SysDictionary) 46 | 47 | return &types.GetSysDictionaryDetailsResponse{SysDictionary: typesSysDictionary}, nil 48 | } 49 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/menu/get_menu_list_logic.go: -------------------------------------------------------------------------------- 1 | package menu 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetMenuListLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewGetMenuListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenuListLogic { 21 | return &GetMenuListLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *GetMenuListLogic) GetMenuList(req *types.GetMenuListRequest) (resp *types.GetMenuListResponse, err error) { 29 | //csb := l.svcCtx.Config.CasbinConf.MustNewCasbinWithRedisWatcher("mysql", l.svcCtx.Config.DB.DataSource, l.svcCtx.Config.BizRedis) 30 | //csb := l.svcCtx.Config.CasbinConf.MustNewCasbinWithRedisWatcher(l.svcCtx.Config.DB.DataSource, l.svcCtx.Config.BizRedis) 31 | //_, err = csb.UpdatePolicy([]string{"100001", "/v1/sys/menu/getMenuList", "GET"}, []string{"100001", "/v1/sys/menu/getMenuList1111", "GET"}) 32 | menuInfoListGet, err := l.svcCtx.AppletMenuRPC.GetMenuBaseInfoList(l.ctx, &pb.NoDataResponse{}) 33 | if err != nil { 34 | logx.Errorf("l.svcCtx.AppletRPC.MenuInfoListGet err:%v", err) 35 | return nil, err 36 | } 37 | var MenuListRes types.GetMenuListResponse 38 | _ = copier.Copy(&MenuListRes.List, menuInfoListGet.SysBaseMenu) 39 | MenuListRes.Total = menuInfoListGet.Total 40 | MenuListRes.PageNo = req.PageNo 41 | MenuListRes.PageSize = req.PageSize 42 | 43 | return &MenuListRes, err 44 | } 45 | -------------------------------------------------------------------------------- /test/casbin_RBAC-with-domainste_nants/casbin.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/casbin/casbin/v2" 6 | gormadapter "github.com/casbin/gorm-adapter/v3" 7 | _ "github.com/go-sql-driver/mysql" 8 | ) 9 | 10 | func main() { 11 | fmt.Println("1111111111") 12 | a, _ := gormadapter.NewAdapter("mysql", "root:123456@tcp(127.0.0.1:3306)/casbinRBAC", true) // Your driver and data source. 13 | 14 | e, err := casbin.NewEnforcer("test/casbin_RBAC-with-domainste_nants/model.conf", a) 15 | if err != nil { 16 | fmt.Println("NewEnforcer err:", err) 17 | panic(err) 18 | } 19 | 20 | //added, err := e.AddPolicy("yanghao", "nanli", "data2", "read") // 增加 21 | //fmt.Println("added:", added, " err:", err) 22 | 23 | //removed, _ := e.RemovePolicy("zhangsan", "data2", "read") // 删除 24 | //updated, err := e.UpdatePolicy([]string{"zhangsan", "data3", "read"}, []string{"zhangsan", "data3", "write"}) // 修改 25 | //fmt.Println("removed:", removed) 26 | //fmt.Println("updated:", updated) 27 | 28 | //added, err = e.AddGroupingPolicy("yanghao", "yh", "nanli") // 向当前策略添加角色继承规则。 29 | //fmt.Println("added:", added, " err:", err) 30 | 31 | //filteredPolicy := e.GetFilteredPolicy(0, "zhangsan") 32 | //filteredPolicy1 := e.GetFilteredPolicy(1, "data3") 33 | //fmt.Println("filteredPolicy:", filteredPolicy) 34 | //fmt.Println("filteredPolicy1:", filteredPolicy1) 35 | 36 | //ok, err := e.Enforce("yh", "nanli", "data2", "read") // 决定“主体”是否能够用“动作”操作访问“对象” 37 | ok, err := e.Enforce("yanghao", "nanli", "data2", "read") // 决定“主体”是否能够用“动作”操作访问“对象” 38 | 39 | if err != nil { 40 | // 处理err 41 | fmt.Printf(" 错误 err:%v", err) 42 | } 43 | if ok == true { 44 | // 允许alice读取data1 45 | fmt.Println("通过") 46 | } else { 47 | // 拒绝请求,抛出异常 48 | fmt.Println("不通过") 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/authority/get_authority_list_logic.go: -------------------------------------------------------------------------------- 1 | package authority 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetAuthorityListLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewGetAuthorityListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAuthorityListLogic { 21 | return &GetAuthorityListLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *GetAuthorityListLogic) GetAuthorityList(req *types.GetAuthorityListRequest) (resp *types.GetAuthorityListResponse, err error) { 29 | var page pb.PageRequest 30 | if req.PageNo == 0 { 31 | req.PageNo = l.svcCtx.Config.Page.PageNo 32 | } 33 | if req.PageSize == 0 { 34 | req.PageSize = l.svcCtx.Config.Page.PageSize 35 | } 36 | page.PageNo = int64(req.PageNo) 37 | page.PageSize = int64(req.PageSize) 38 | 39 | authorityListGet, err := l.svcCtx.AppletAuthorityRPC.GetAuthorityList(l.ctx, &pb.GetAuthorityListRequest{Page: &page}) 40 | if err != nil { 41 | logx.Errorf("AuthorityListGet 错误 error: %v", err) 42 | return nil, err 43 | } 44 | 45 | var typesGetAuthorityListRes types.GetAuthorityListResponse 46 | 47 | typesGetAuthorityListRes.Total = authorityListGet.Total 48 | typesGetAuthorityListRes.PageNo = req.PageNo 49 | typesGetAuthorityListRes.PageSize = req.PageSize 50 | _ = copier.Copy(&typesGetAuthorityListRes.List, authorityListGet.SysAuthority) 51 | 52 | return &typesGetAuthorityListRes, err 53 | } 54 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/menu/delete_base_menu_logic.go: -------------------------------------------------------------------------------- 1 | package menulogic 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/rpc/internal/model" 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/pkg/errors" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type DeleteBaseMenuLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewDeleteBaseMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteBaseMenuLogic { 21 | return &DeleteBaseMenuLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | // 删除系统菜单 29 | func (l *DeleteBaseMenuLogic) DeleteBaseMenu(in *pb.DeleteBaseMenuRequest) (*pb.NoDataResponse, error) { 30 | err := l.svcCtx.DB.Preload("MenuBtn").Preload("Parameters").Where("parent_id = ?", in.ID).First(&model.SysBaseMenu{}).Error 31 | if err != nil { 32 | var menu model.SysBaseMenu 33 | db := l.svcCtx.DB.Preload("SysAuthoritys").Where("id = ?", in.ID).First(&menu).Delete(&menu) 34 | err = l.svcCtx.DB.Delete(&model.SysBaseMenuParameter{}, "sys_base_menu_id = ?", in.ID).Error 35 | err = l.svcCtx.DB.Delete(&model.SysBaseMenuBtn{}, "sys_base_menu_id = ?", in.ID).Error 36 | err = l.svcCtx.DB.Delete(&model.SysAuthorityBtn{}, "sys_menu_id = ?", in.ID).Error 37 | if err != nil { 38 | return nil, err 39 | } 40 | if len(menu.SysAuthoritys) > 0 { 41 | err = l.svcCtx.DB.Model(&menu).Association("SysAuthoritys").Delete(&menu.SysAuthoritys) 42 | } else { 43 | err = db.Error 44 | if err != nil { 45 | return nil, err 46 | } 47 | } 48 | } else { 49 | return nil, errors.New("此菜单存在子菜单不可删除") 50 | } 51 | return &pb.NoDataResponse{}, err 52 | } 53 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/user/get_user_toke_logic.go: -------------------------------------------------------------------------------- 1 | package userlogic 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "time" 7 | 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | "go-zero-admin/pkg/ctxJwt" 11 | 12 | "github.com/golang-jwt/jwt/v4" 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type GetUserTokeLogic struct { 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | logx.Logger 20 | } 21 | 22 | func NewGetUserTokeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserTokeLogic { 23 | return &GetUserTokeLogic{ 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | Logger: logx.WithContext(ctx), 27 | } 28 | } 29 | 30 | // 获取Token 31 | func (l *GetUserTokeLogic) GetUserToke(in *pb.GetUserTokeRequest) (*pb.GetUserTokeResponse, error) { 32 | fmt.Println("GenerateToken:----", in) 33 | now := time.Now().Unix() 34 | accessExpire := l.svcCtx.Config.JwtAuth.AccessExpire 35 | accessToken, err := l.getJwtToken(l.svcCtx.Config.JwtAuth.AccessSecret, now, accessExpire, ctxJwt.JWTData{ 36 | UUID: in.UUID, 37 | ID: in.ID, 38 | NickName: in.NickName, 39 | Username: in.Username, 40 | AuthorityId: in.AuthorityId, 41 | }) 42 | 43 | return &pb.GetUserTokeResponse{ 44 | Token: accessToken, 45 | ExpiresAt: now + accessExpire, 46 | }, err 47 | } 48 | 49 | func (l *GetUserTokeLogic) getJwtToken(secretKey string, iat, seconds int64, jwtData ctxJwt.JWTData) (string, error) { 50 | fmt.Println("======== getJwtToken jwtData", jwtData) 51 | claims := make(jwt.MapClaims) 52 | claims["exp"] = iat + seconds 53 | claims["iat"] = iat 54 | claims[ctxJwt.CtxKeyJwtData] = jwtData 55 | token := jwt.New(jwt.SigningMethodHS256) 56 | token.Claims = claims 57 | return token.SignedString([]byte(secretKey)) 58 | } 59 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/user/update_user_info_logic.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | 7 | "go-zero-admin/application/applet/api/internal/svc" 8 | "go-zero-admin/application/applet/api/internal/types" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | "go-zero-admin/pkg/result/xerr" 11 | 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type UpdateUserInfoLogic struct { 16 | logx.Logger 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | } 20 | 21 | func NewUpdateUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserInfoLogic { 22 | return &UpdateUserInfoLogic{ 23 | Logger: logx.WithContext(ctx), 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | } 27 | } 28 | 29 | func (l *UpdateUserInfoLogic) UpdateUserInfo(req *types.UpdateUserInfoRequest) (resp *types.MessageResponse, err error) { 30 | fmt.Println("------- req.SideMode", req.SideMode) 31 | 32 | if len(req.AuthorityIds) != 0 { 33 | _, err = l.svcCtx.AppletUserRPC.UpdateUserAuthorities(l.ctx, &pb.UpdateUserAuthoritiesRequest{ID: req.ID, AuthorityIds: req.AuthorityIds}) 34 | if err != nil { 35 | logx.Errorf("l.svcCtx.AppletUserRPC.UpdateUserAuthorities err: %v", err) 36 | return nil, xerr.NewErrCodeMsg(900000, "l.svcCtx.AppletUserRPC.UpdateUserAuthorities") 37 | } 38 | } 39 | userInfo := &pb.UserInfo{ 40 | NickName: req.NickName, 41 | SideMode: req.SideMode, 42 | HeaderImg: req.HeaderImg, 43 | Phone: req.Phone, 44 | Email: req.Email, 45 | Enable: req.Enable, 46 | ID: req.ID, 47 | } 48 | _, err = l.svcCtx.AppletUserRPC.UpdateUserInfo(l.ctx, &pb.UpdateUserInfoRequest{UserInfo: userInfo}) 49 | if err != nil { 50 | logx.Errorf("l.svcCtx.AppletUserRPC.UpdateUserInfo err: %v", err) 51 | return nil, err 52 | } 53 | 54 | return &types.MessageResponse{Message: "更新数据成功!"}, nil 55 | } 56 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/get_sys_dictionary_info_list_details_by_id_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/jinzhu/copier" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type GetSysDictionaryInfoListDetailsByIdLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewGetSysDictionaryInfoListDetailsByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSysDictionaryInfoListDetailsByIdLogic { 22 | return &GetSysDictionaryInfoListDetailsByIdLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 根据id获取SysDictionaryInfo详情 30 | func (l *GetSysDictionaryInfoListDetailsByIdLogic) GetSysDictionaryInfoListDetailsById(in *pb.GetSysDictionaryInfoListDetailsByIdRequest) (*pb.GetSysDictionaryInfoListDetailsByIdResponse, error) { 31 | var modelSysDictionaryInfo model.SysDictionaryInfo 32 | err := l.svcCtx.DB.Where("id = ?", in.ID).First(&modelSysDictionaryInfo).Error 33 | var pbSysDictionaryInfo pb.SysDictionaryInfo 34 | _ = copier.Copy(&pbSysDictionaryInfo, modelSysDictionaryInfo) 35 | pbSysDictionaryInfo.CreatedAt = modelSysDictionaryInfo.CreatedAt.Format(time.RFC3339) 36 | pbSysDictionaryInfo.UpdatedAt = modelSysDictionaryInfo.UpdatedAt.Format(time.RFC3339) 37 | if modelSysDictionaryInfo.DeletedAt.Valid { 38 | pbSysDictionaryInfo.DeletedAt = modelSysDictionaryInfo.DeletedAt.Time.Format(time.RFC3339) 39 | } 40 | _ = copier.Copy(&pbSysDictionaryInfo, modelSysDictionaryInfo) 41 | 42 | return &pb.GetSysDictionaryInfoListDetailsByIdResponse{ 43 | SysDictionaryInfo: &pbSysDictionaryInfo, 44 | }, err 45 | } 46 | -------------------------------------------------------------------------------- /application/applet/api/applet.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "flag" 6 | "fmt" 7 | "net/http" 8 | "os" 9 | 10 | "go-zero-admin/application/applet/api/internal/config" 11 | "go-zero-admin/application/applet/api/internal/handler" 12 | "go-zero-admin/application/applet/api/internal/svc" 13 | "go-zero-admin/pkg/result" 14 | "go-zero-admin/pkg/result/xerr" 15 | 16 | "github.com/zeromicro/go-zero/core/conf" 17 | "github.com/zeromicro/go-zero/core/logx" 18 | "github.com/zeromicro/go-zero/rest" 19 | ) 20 | 21 | var configFile = flag.String("f", "etc/applet-api.yaml", "the config file") 22 | 23 | // var OssEnv = os.Getenv("AliYunOss") 24 | var OssEnv = os.Getenv("AliYunOss_go-zero-admin") 25 | 26 | type AliYunOss struct { 27 | Endpoint string 28 | AccessKeyId string 29 | AccessKeySecret string 30 | BucketName string 31 | } 32 | 33 | func getEnv(c *config.Config, o *AliYunOss) { 34 | c.Oss.AccessKeyId = o.AccessKeyId 35 | c.Oss.AccessKeySecret = o.AccessKeySecret 36 | c.Oss.BucketName = o.BucketName 37 | c.Oss.Endpoint = o.Endpoint 38 | } 39 | 40 | func main() { 41 | flag.Parse() 42 | 43 | var c config.Config 44 | conf.MustLoad(*configFile, &c) 45 | 46 | if OssEnv != "" { 47 | // 把阿里云的值存到本地环境中 48 | var aliYunOssEnv AliYunOss 49 | err := json.Unmarshal([]byte(OssEnv), &aliYunOssEnv) 50 | if err != nil { 51 | logx.Errorf("Obtain local environment Err: %v", err) 52 | panic(err) 53 | } 54 | getEnv(&c, &aliYunOssEnv) 55 | } 56 | 57 | server := rest.MustNewServer(c.RestConf, rest.WithUnauthorizedCallback(func(w http.ResponseWriter, r *http.Request, err error) { 58 | result.HttpResult(r, w, "", xerr.NewErrCode(xerr.TOKEN_EXPIRE_ERROR)) 59 | })) 60 | defer server.Stop() 61 | 62 | ctx := svc.NewServiceContext(c) 63 | handler.RegisterHandlers(server, ctx) 64 | 65 | fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) 66 | server.Start() 67 | } 68 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/server/api/api_server.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT. 2 | // Source: applet.proto 3 | 4 | package server 5 | 6 | import ( 7 | "context" 8 | 9 | "go-zero-admin/application/applet/rpc/internal/logic/api" 10 | "go-zero-admin/application/applet/rpc/internal/svc" 11 | "go-zero-admin/application/applet/rpc/pb" 12 | ) 13 | 14 | type ApiServer struct { 15 | svcCtx *svc.ServiceContext 16 | pb.UnimplementedApiServer 17 | } 18 | 19 | func NewApiServer(svcCtx *svc.ServiceContext) *ApiServer { 20 | return &ApiServer{ 21 | svcCtx: svcCtx, 22 | } 23 | } 24 | 25 | // 获取API列表 26 | func (s *ApiServer) GetApiList(ctx context.Context, in *pb.GetApiListRequest) (*pb.GetApiListResponse, error) { 27 | l := apilogic.NewGetApiListLogic(ctx, s.svcCtx) 28 | return l.GetApiList(in) 29 | } 30 | 31 | // 创建/添加 API列表 32 | func (s *ApiServer) CreateApi(ctx context.Context, in *pb.CreateApiRequest) (*pb.NoDataResponse, error) { 33 | l := apilogic.NewCreateApiLogic(ctx, s.svcCtx) 34 | return l.CreateApi(in) 35 | } 36 | 37 | // 删除API列表 38 | func (s *ApiServer) DeleteApi(ctx context.Context, in *pb.DeleteApiRequest) (*pb.NoDataResponse, error) { 39 | l := apilogic.NewDeleteApiLogic(ctx, s.svcCtx) 40 | return l.DeleteApi(in) 41 | } 42 | 43 | // 获取全部API列表 44 | func (s *ApiServer) GetAllApiList(ctx context.Context, in *pb.NoDataResponse) (*pb.GetAllApiListResponse, error) { 45 | l := apilogic.NewGetAllApiListLogic(ctx, s.svcCtx) 46 | return l.GetAllApiList(in) 47 | } 48 | 49 | // 删除多条api 50 | func (s *ApiServer) DeleteApisByIds(ctx context.Context, in *pb.DeleteApisByIdsRequest) (*pb.NoDataResponse, error) { 51 | l := apilogic.NewDeleteApisByIdsLogic(ctx, s.svcCtx) 52 | return l.DeleteApisByIds(in) 53 | } 54 | 55 | // 更新api 56 | func (s *ApiServer) UpdateApi(ctx context.Context, in *pb.UpdateApiRequest) (*pb.NoDataResponse, error) { 57 | l := apilogic.NewUpdateApiLogic(ctx, s.svcCtx) 58 | return l.UpdateApi(in) 59 | } 60 | -------------------------------------------------------------------------------- /application/applet/rpc/applet.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/config" 8 | apiServer "go-zero-admin/application/applet/rpc/internal/server/api" 9 | authorityServer "go-zero-admin/application/applet/rpc/internal/server/authority" 10 | casbinServer "go-zero-admin/application/applet/rpc/internal/server/casbin" 11 | dictionaryServer "go-zero-admin/application/applet/rpc/internal/server/dictionary" 12 | menuServer "go-zero-admin/application/applet/rpc/internal/server/menu" 13 | userServer "go-zero-admin/application/applet/rpc/internal/server/user" 14 | "go-zero-admin/application/applet/rpc/internal/svc" 15 | "go-zero-admin/application/applet/rpc/pb" 16 | 17 | "github.com/zeromicro/go-zero/core/conf" 18 | "github.com/zeromicro/go-zero/core/service" 19 | "github.com/zeromicro/go-zero/zrpc" 20 | "google.golang.org/grpc" 21 | "google.golang.org/grpc/reflection" 22 | ) 23 | 24 | var configFile = flag.String("f", "etc/applet.yaml", "the config file") 25 | 26 | func main() { 27 | flag.Parse() 28 | 29 | var c config.Config 30 | conf.MustLoad(*configFile, &c) 31 | ctx := svc.NewServiceContext(c) 32 | 33 | s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { 34 | pb.RegisterUserServer(grpcServer, userServer.NewUserServer(ctx)) 35 | pb.RegisterMenuServer(grpcServer, menuServer.NewMenuServer(ctx)) 36 | pb.RegisterAuthorityServer(grpcServer, authorityServer.NewAuthorityServer(ctx)) 37 | pb.RegisterApiServer(grpcServer, apiServer.NewApiServer(ctx)) 38 | pb.RegisterCasbinServer(grpcServer, casbinServer.NewCasbinServer(ctx)) 39 | pb.RegisterDictionaryServer(grpcServer, dictionaryServer.NewDictionaryServer(ctx)) 40 | 41 | if c.Mode == service.DevMode || c.Mode == service.TestMode { 42 | reflection.Register(grpcServer) 43 | } 44 | }) 45 | defer s.Stop() 46 | 47 | fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) 48 | s.Start() 49 | } 50 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/server/authority/authority_server.go: -------------------------------------------------------------------------------- 1 | // Code generated by goctl. DO NOT EDIT. 2 | // Source: applet.proto 3 | 4 | package server 5 | 6 | import ( 7 | "context" 8 | 9 | "go-zero-admin/application/applet/rpc/internal/logic/authority" 10 | "go-zero-admin/application/applet/rpc/internal/svc" 11 | "go-zero-admin/application/applet/rpc/pb" 12 | ) 13 | 14 | type AuthorityServer struct { 15 | svcCtx *svc.ServiceContext 16 | pb.UnimplementedAuthorityServer 17 | } 18 | 19 | func NewAuthorityServer(svcCtx *svc.ServiceContext) *AuthorityServer { 20 | return &AuthorityServer{ 21 | svcCtx: svcCtx, 22 | } 23 | } 24 | 25 | // 获取角色列表 26 | func (s *AuthorityServer) GetAuthorityList(ctx context.Context, in *pb.GetAuthorityListRequest) (*pb.GetAuthorityListResponse, error) { 27 | l := authoritylogic.NewGetAuthorityListLogic(ctx, s.svcCtx) 28 | return l.GetAuthorityList(in) 29 | } 30 | 31 | // 增加base_menu和角色关联关系 -- 用于角色管理的设置权限 32 | func (s *AuthorityServer) AddAuthorityMenu(ctx context.Context, in *pb.AddAuthorityMenuRequest) (*pb.NoDataResponse, error) { 33 | l := authoritylogic.NewAddAuthorityMenuLogic(ctx, s.svcCtx) 34 | return l.AddAuthorityMenu(in) 35 | } 36 | 37 | // 更新角色 -- 设为首页 38 | func (s *AuthorityServer) UpdateAuthority(ctx context.Context, in *pb.UpdateAuthorityRequest) (*pb.UpdateAuthorityResponse, error) { 39 | l := authoritylogic.NewUpdateAuthorityLogic(ctx, s.svcCtx) 40 | return l.UpdateAuthority(in) 41 | } 42 | 43 | // 创建角色 44 | func (s *AuthorityServer) CreateAuthority(ctx context.Context, in *pb.CreateAuthorityRequest) (*pb.CreateAuthorityResponse, error) { 45 | l := authoritylogic.NewCreateAuthorityLogic(ctx, s.svcCtx) 46 | return l.CreateAuthority(in) 47 | } 48 | 49 | // 删除角色 50 | func (s *AuthorityServer) DeleteAuthority(ctx context.Context, in *pb.DeleteAuthorityRequest) (*pb.NoDataResponse, error) { 51 | l := authoritylogic.NewDeleteAuthorityLogic(ctx, s.svcCtx) 52 | return l.DeleteAuthority(in) 53 | } 54 | -------------------------------------------------------------------------------- /test/casbin/casbin.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/casbin/casbin/v2" 6 | gormadapter "github.com/casbin/gorm-adapter/v3" 7 | _ "github.com/go-sql-driver/mysql" 8 | ) 9 | 10 | func main() { 11 | a, _ := gormadapter.NewAdapter("mysql", "root:123456@tcp(127.0.0.1:3306)/casbin", true) // Your driver and data source. 12 | e, _ := casbin.NewEnforcer("./model.conf", a) 13 | 14 | e.AddFunction("my_func", KeyMatchFunc) 15 | 16 | //added, err := e.AddPolicy("zhangsan", "data2", "read") // 增加 17 | //removed, _ := e.RemovePolicy("zhangsan", "data2", "read") // 删除 18 | //updated, err := e.UpdatePolicy([]string{"zhangsan", "data3", "read"}, []string{"zhangsan", "data3", "write"}) // 修改 19 | //fmt.Println("added:", added, " err:", err) 20 | //fmt.Println("removed:", removed) 21 | //fmt.Println("updated:", updated) 22 | 23 | //added, err := e.AddGroupingPolicy("group1", "data2_admin") // 向当前策略添加角色继承规则。 24 | //fmt.Println("added:", added, " err:", err) 25 | 26 | //filteredPolicy := e.GetFilteredPolicy(0, "zhangsan") 27 | //filteredPolicy1 := e.GetFilteredPolicy(1, "data3") 28 | //fmt.Println("filteredPolicy:", filteredPolicy) 29 | //fmt.Println("filteredPolicy1:", filteredPolicy1) 30 | 31 | ok, err := e.Enforce("data2_admin", "data3", "read") // 决定“主体”是否能够用“动作”操作访问“对象” 32 | 33 | if err != nil { 34 | // 处理err 35 | fmt.Printf(" 错误 err:%v", err) 36 | } 37 | if ok == true { 38 | // 允许alice读取data1 39 | fmt.Println("通过") 40 | } else { 41 | // 拒绝请求,抛出异常 42 | fmt.Println("不通过") 43 | } 44 | } 45 | 46 | func KeyMatch(key1 string, key2 string) bool { 47 | //fmt.Println("key1 :", key1) 48 | //fmt.Println("key2 :", key2) 49 | //return key1 != key2 50 | return key1 == key2 51 | 52 | //return key1 != "data1" && key1 == key2 53 | } 54 | 55 | func KeyMatchFunc(args ...interface{}) (interface{}, error) { 56 | name1 := args[0].(string) 57 | name2 := args[1].(string) 58 | 59 | return (bool)(KeyMatch(name1, name2)), nil 60 | } 61 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/casbin/update_casbin_data_logic.go: -------------------------------------------------------------------------------- 1 | package casbinlogic 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "strconv" 7 | 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/pkg/errors" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | ) 14 | 15 | type UpdateCasbinDataLogic struct { 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | logx.Logger 19 | } 20 | 21 | func NewUpdateCasbinDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCasbinDataLogic { 22 | return &UpdateCasbinDataLogic{ 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | Logger: logx.WithContext(ctx), 26 | } 27 | } 28 | 29 | // 更新一个角色的对应的casbin数据 30 | func (l *UpdateCasbinDataLogic) UpdateCasbinData(in *pb.UpdateCasbinDataRequest) (*pb.NoDataResponse, error) { 31 | authorityId := strconv.FormatInt(in.AuthorityId, 10) 32 | fmt.Println("authorityId", authorityId) 33 | _, err := l.ClearCasbin(0, authorityId) 34 | if err != nil { 35 | return nil, err 36 | } 37 | //做权限去重处理 38 | rules := [][]string{} 39 | deduplicateMap := make(map[string]bool) 40 | for _, v := range in.CasbinInfoList { 41 | key := authorityId + v.Path + v.Method 42 | if _, ok := deduplicateMap[key]; !ok { 43 | deduplicateMap[key] = true 44 | rules = append(rules, []string{authorityId, v.Path, v.Method}) 45 | } 46 | } 47 | csb := l.svcCtx.Config.CasbinConf.MustNewCasbinWithRedisWatcher(l.svcCtx.Config.DB.DataSource, l.svcCtx.Config.BizRedis) 48 | success, err := csb.AddPolicies(rules) 49 | if !success { 50 | return nil, errors.New("存在相同api,添加失败,请联系管理员") 51 | } 52 | 53 | return &pb.NoDataResponse{}, err 54 | } 55 | 56 | func (l *UpdateCasbinDataLogic) ClearCasbin(v int, p ...string) (bool, error) { 57 | csb := l.svcCtx.Config.CasbinConf.MustNewCasbinWithRedisWatcher(l.svcCtx.Config.DB.DataSource, l.svcCtx.Config.BizRedis) 58 | success, err := csb.RemoveFilteredPolicy(v, p...) 59 | return success, err 60 | } 61 | -------------------------------------------------------------------------------- /test/testMain/openai/gpt3.5/gpt3.5.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "io/ioutil" 8 | "net/http" 9 | "time" 10 | ) 11 | 12 | const openaiAPIURL = "" 13 | const apiKey = "sk-" 14 | 15 | func main() { 16 | message := "Tell me a joke." 17 | 18 | // 发送聊天请求到 OpenAI 的API 19 | response, err := sendGPTRequest(apiKey, message) 20 | if err != nil { 21 | fmt.Println("Error sending request to GPT-3.5 API:", err) 22 | return 23 | } 24 | 25 | // 解析并打印生成的文本 26 | outputText, err := parseGPTResponse(response) 27 | if err != nil { 28 | fmt.Println("Error parsing GPT-3.5 response:", err) 29 | return 30 | } 31 | 32 | fmt.Println("Generated Text:", outputText) 33 | } 34 | 35 | func sendGPTRequest(apiKey string, inputText string) ([]byte, error) { 36 | client := &http.Client{ 37 | Timeout: time.Second * 10, // 设置为 10 秒的超时 38 | } 39 | 40 | requestData := map[string]interface{}{ 41 | "prompt": inputText, 42 | "max_tokens": 100, 43 | } 44 | 45 | requestBody, err := json.Marshal(requestData) 46 | if err != nil { 47 | return nil, err 48 | } 49 | 50 | request, err := http.NewRequest("POST", openaiAPIURL, bytes.NewBuffer(requestBody)) 51 | if err != nil { 52 | return nil, err 53 | } 54 | 55 | request.Header.Set("Content-Type", "application/json") 56 | request.Header.Set("Authorization", "Bearer "+apiKey) 57 | 58 | response, err := client.Do(request) 59 | if err != nil { 60 | return nil, err 61 | } 62 | defer response.Body.Close() 63 | 64 | return ioutil.ReadAll(response.Body) 65 | } 66 | 67 | func parseGPTResponse(responseData []byte) (string, error) { 68 | var jsonResponse map[string]interface{} 69 | 70 | err := json.Unmarshal(responseData, &jsonResponse) 71 | if err != nil { 72 | return "", err 73 | } 74 | 75 | if output, ok := jsonResponse["choices"].([]interface{}); ok && len(output) > 0 { 76 | if text, ok := output[0].(map[string]interface{})["text"].(string); ok { 77 | return text, nil 78 | } 79 | } 80 | 81 | return "", fmt.Errorf("Unable to parse GPT-3.5 response") 82 | } 83 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/base/upload_file_img_logic.go: -------------------------------------------------------------------------------- 1 | package base 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "net/http" 7 | "time" 8 | 9 | "go-zero-admin/application/applet/api/internal/svc" 10 | "go-zero-admin/application/applet/api/internal/types" 11 | "go-zero-admin/pkg/result/xerr" 12 | 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | const maxFileSize = 10 << 20 // 10MB 17 | 18 | type UploadFileImgLogic struct { 19 | logx.Logger 20 | ctx context.Context 21 | svcCtx *svc.ServiceContext 22 | } 23 | 24 | func NewUploadFileImgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadFileImgLogic { 25 | return &UploadFileImgLogic{ 26 | Logger: logx.WithContext(ctx), 27 | ctx: ctx, 28 | svcCtx: svcCtx, 29 | } 30 | } 31 | func (l *UploadFileImgLogic) UploadFileImg(req *types.UploadFileImgRequest, r *http.Request) (resp *types.UploadFileImgResponse, err error) { 32 | _ = r.ParseMultipartForm(maxFileSize) 33 | file, handler, err := r.FormFile("file_img") 34 | if err != nil { 35 | return nil, err 36 | } 37 | defer file.Close() 38 | 39 | bucket, err := l.svcCtx.OssClient.Bucket(l.svcCtx.Config.Oss.BucketName) 40 | if err != nil { 41 | logx.Errorf("get bucket failed, err: %v", err) 42 | return nil, xerr.NewErrCode(300001) 43 | } 44 | 45 | objectKey := genFilename(handler.Filename, "go-zero-admin") // 指定目录为 go-zero-admin 46 | err = bucket.PutObject(objectKey, file) 47 | if err != nil { 48 | logx.Errorf("put object failed, err: %v", err) 49 | return nil, xerr.NewErrCode(300002) 50 | } 51 | 52 | return &types.UploadFileImgResponse{ 53 | FileImgUrl: genFileURL(l.svcCtx.Config.Oss.BucketName, l.svcCtx.Config.Oss.Endpoint, objectKey), 54 | }, nil 55 | } 56 | 57 | func genFilename(filename, directory string) string { 58 | // 将文件名拼接到指定目录中 59 | return fmt.Sprintf("%s/%d_%s", directory, time.Now().UnixMilli(), filename) 60 | } 61 | 62 | func genFileURL(bucketName, Endpoint, objectKey string) string { 63 | // 返回文件的 URL,注意 OSS 对象键中的目录名称 64 | return fmt.Sprintf("https://%s.%s/%s", bucketName, Endpoint, objectKey) 65 | } 66 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/dictionary/get_sys_dictionary_info_list_logic.go: -------------------------------------------------------------------------------- 1 | package dictionary 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/api/internal/svc" 7 | "go-zero-admin/application/applet/api/internal/types" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetSysDictionaryInfoListLogic struct { 15 | logx.Logger 16 | ctx context.Context 17 | svcCtx *svc.ServiceContext 18 | } 19 | 20 | func NewGetSysDictionaryInfoListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSysDictionaryInfoListLogic { 21 | return &GetSysDictionaryInfoListLogic{ 22 | Logger: logx.WithContext(ctx), 23 | ctx: ctx, 24 | svcCtx: svcCtx, 25 | } 26 | } 27 | 28 | func (l *GetSysDictionaryInfoListLogic) GetSysDictionaryInfoList(req *types.GetSysDictionaryInfoListRequest) (resp *types.GetSysDictionaryInfoListResponse, err error) { 29 | if req.PageNo == 0 { 30 | req.PageNo = l.svcCtx.Config.Page.PageNo 31 | } 32 | if req.PageSize == 0 { 33 | req.PageSize = l.svcCtx.Config.Page.PageSize 34 | } 35 | var pbSysDictionaryInfo pb.SysDictionaryInfo 36 | var pbPageRequest pb.PageRequest 37 | _ = copier.Copy(&pbSysDictionaryInfo, req.SysDictionaryInfo) 38 | _ = copier.Copy(&pbPageRequest, req.PageRequest) 39 | 40 | sysDictionaryInfoList, err := l.svcCtx.AppletDictionaryRPC.GetSysDictionaryInfoList(l.ctx, &pb.GetSysDictionaryInfoListRequest{ 41 | SysDictionaryInfo: &pbSysDictionaryInfo, 42 | PageRequest: &pbPageRequest, 43 | }) 44 | if err != nil { 45 | logx.Errorf("l.svcCtx.AppletDictionaryRPC.GetSysDictionaryInfoList err:%v", err) 46 | return nil, err 47 | } 48 | var typesSysDictionaryInfoList []types.SysDictionaryInfo 49 | _ = copier.Copy(&typesSysDictionaryInfoList, sysDictionaryInfoList.SysDictionaryInfoList) 50 | 51 | return &types.GetSysDictionaryInfoListResponse{ 52 | List: typesSysDictionaryInfoList, 53 | PageResponse: types.PageResponse{ 54 | Total: sysDictionaryInfoList.Total, 55 | PageNo: req.PageNo, 56 | PageSize: req.PageSize, 57 | }, 58 | }, nil 59 | } 60 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/update_sys_dictionary_info_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | "reflect" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type UpdateSysDictionaryInfoLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewUpdateSysDictionaryInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateSysDictionaryInfoLogic { 21 | return &UpdateSysDictionaryInfoLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | // 更新SysDictionaryInfo 29 | func (l *UpdateSysDictionaryInfoLogic) UpdateSysDictionaryInfo(in *pb.UpdateSysDictionaryInfoRequest) (*pb.NoDataResponse, error) { 30 | // 构造要更新的字段和值的 map 31 | sysDictionaryInfo := map[string]interface{}{} 32 | 33 | // 判断字段是否为空,不为空则添加到 map 中 34 | addIfNotEmpty("Label", in.SysDictionaryInfo.Label, sysDictionaryInfo) 35 | addIfNotEmpty("Value", in.SysDictionaryInfo.Value, sysDictionaryInfo) 36 | addIfNotEmpty("Extend", in.SysDictionaryInfo.Extend, sysDictionaryInfo) 37 | addIfNotEmpty("Status", in.SysDictionaryInfo.Status, sysDictionaryInfo) 38 | addIfNotEmpty("Sort", in.SysDictionaryInfo.Sort, sysDictionaryInfo) 39 | 40 | // 检查是否有任何需要更新的字段 41 | if len(sysDictionaryInfo) == 0 { 42 | // 没有需要更新的字段,直接返回 43 | logx.Error("No fields to update.") 44 | return &pb.NoDataResponse{}, nil 45 | } 46 | 47 | // 使用 Update 方法更新指定 id 的记录 48 | err := l.svcCtx.DB.Model(model.SysDictionaryInfo{}). 49 | Where("id = ?", in.SysDictionaryInfo.ID). 50 | Updates(sysDictionaryInfo).Error 51 | 52 | // 检查是否有错误,并返回相应的错误消息 53 | if err != nil { 54 | return nil, err 55 | } 56 | 57 | return &pb.NoDataResponse{}, nil 58 | } 59 | 60 | func addIfNotEmpty(fieldName string, fieldValue interface{}, updateMap map[string]interface{}) { 61 | if fieldValue != nil && !reflect.ValueOf(fieldValue).IsZero() { 62 | updateMap[fieldName] = fieldValue 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/menu/get_menu_authority_logic.go: -------------------------------------------------------------------------------- 1 | package menulogic 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "strconv" 7 | 8 | "go-zero-admin/application/applet/rpc/internal/model" 9 | "go-zero-admin/application/applet/rpc/internal/svc" 10 | "go-zero-admin/application/applet/rpc/pb" 11 | 12 | "github.com/jinzhu/copier" 13 | "github.com/zeromicro/go-zero/core/logx" 14 | ) 15 | 16 | type GetMenuAuthorityLogic struct { 17 | ctx context.Context 18 | svcCtx *svc.ServiceContext 19 | logx.Logger 20 | } 21 | 22 | func NewGetMenuAuthorityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenuAuthorityLogic { 23 | return &GetMenuAuthorityLogic{ 24 | ctx: ctx, 25 | svcCtx: svcCtx, 26 | Logger: logx.WithContext(ctx), 27 | } 28 | } 29 | 30 | // 获取指定角色menu -- 用于角色管理的设置权限 31 | func (l *GetMenuAuthorityLogic) GetMenuAuthority(in *pb.GetMenuAuthorityRequest) (*pb.GetMenuAuthorityResponse, error) { 32 | var baseMenuList []model.SysBaseMenu 33 | var SysAuthorityMenus []model.SysAuthorityMenu 34 | err := l.svcCtx.DB.Where("sys_authority_authority_id = ?", in.AuthorityId).Find(&SysAuthorityMenus).Error 35 | if err != nil { 36 | return nil, err 37 | } 38 | 39 | var MenuIds []string 40 | for i := range SysAuthorityMenus { 41 | MenuIds = append(MenuIds, SysAuthorityMenus[i].MenuId) 42 | } 43 | 44 | err = l.svcCtx.DB.Where("id in (?) ", MenuIds).Order("sort").Find(&baseMenuList).Error 45 | 46 | var menus []model.SysMenu 47 | for i := range baseMenuList { 48 | menus = append(menus, model.SysMenu{ 49 | SysBaseMenu: baseMenuList[i], 50 | AuthorityId: in.AuthorityId, 51 | MenuId: strconv.FormatInt(baseMenuList[i].ID, 10), 52 | Parameters: baseMenuList[i].Parameters, 53 | }) 54 | } 55 | 56 | var pbGetMenuAuthorityRes pb.GetMenuAuthorityResponse 57 | var pbSysMenuList []pb.SysMenu 58 | _ = copier.Copy(&pbSysMenuList, menus) 59 | _ = copier.Copy(&pbGetMenuAuthorityRes.SysMenuList, pbSysMenuList) 60 | 61 | fmt.Println("----------- menus", menus) 62 | fmt.Println("----------- pbSysMenuList", pbSysMenuList) 63 | fmt.Println("----------- pbGetMenuAuthorityRes", pbGetMenuAuthorityRes) 64 | 65 | return &pbGetMenuAuthorityRes, nil 66 | } 67 | -------------------------------------------------------------------------------- /test/gormTest/gormTest.go: -------------------------------------------------------------------------------- 1 | package gormTest 2 | 3 | // 4 | //import ( 5 | // "context" 6 | // "fmt" 7 | // "strconv" 8 | // "time" 9 | // 10 | // "gorm.io/gorm" 11 | //) 12 | // 13 | //type tenantModel interface { 14 | // Insert(ctx context.Context, data *Tenant) error 15 | // FindOne(ctx context.Context, id int64) (*Tenant, error) 16 | // FindOneByUserId(ctx context.Context, userId int64) (*Tenant, error) 17 | // Update(ctx context.Context, data *Tenant) error 18 | // Delete(ctx context.Context, id int64) error 19 | //} 20 | // 21 | //type defaultTenantModel struct { 22 | // db *gorm.DB 23 | // cache cache.Cache 24 | // table string 25 | // cachePrefix string 26 | //} 27 | // 28 | //func NewTenantModel(db *gorm.DB, c cache.Cache) *defaultTenantModel { 29 | // return &defaultTenantModel{ 30 | // db: db, 31 | // cache: c, 32 | // table: "tenant", 33 | // cachePrefix: "tenant:", 34 | // } 35 | //} 36 | // 37 | //func (m *defaultTenantModel) Insert(ctx context.Context, data *Tenant) error { 38 | // // 插入到数据库 39 | // err := m.db.WithContext(ctx).Create(data).Error 40 | // if err != nil { 41 | // return err 42 | // } 43 | // 44 | // // 将数据存入缓存 45 | // cacheKey := m.cachePrefix + strconv.FormatInt(data.Id, 10) 46 | // if err := m.cache.Set(ctx, cacheKey, data, 24*time.Hour); err != nil { 47 | // return err 48 | // } 49 | // 50 | // return nil 51 | //} 52 | // 53 | //func (m *defaultTenantModel) FindOne(ctx context.Context, id int64) (*Tenant, error) { 54 | // // 尝试从缓存获取 55 | // cacheKey := m.cachePrefix + strconv.FormatInt(id, 10) 56 | // var tenant Tenant 57 | // if err := m.cache.Get(ctx, cacheKey, &tenant); err == nil { 58 | // return &tenant, nil 59 | // } 60 | // 61 | // // 从数据库获取 62 | // err := m.db.WithContext(ctx).Table(m.table).Where("id = ?", id).First(&tenant).Error 63 | // if err != nil { 64 | // return nil, err 65 | // } 66 | // 67 | // // 将数据存入缓存 68 | // if err := m.cache.Set(ctx, cacheKey, &tenant, 24*time.Hour); err != nil { 69 | // // 如果缓存设置失败,这可能不是关键错误,可以记录日志并继续 70 | // fmt.Println("Failed to set cache for tenant with id:", id, "Error:", err) 71 | // } 72 | // 73 | // return &tenant, nil 74 | //} 75 | // 76 | //// 其他方法的实现与FindOne方法类似,即先从缓存尝试获取,如果未命中则从数据库获取,并在获取后存入缓存。 77 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/dictionary/get_sys_dictionary_info_list_logic.go: -------------------------------------------------------------------------------- 1 | package dictionarylogic 2 | 3 | import ( 4 | "context" 5 | 6 | "go-zero-admin/application/applet/rpc/internal/model" 7 | "go-zero-admin/application/applet/rpc/internal/svc" 8 | "go-zero-admin/application/applet/rpc/pb" 9 | 10 | "github.com/jinzhu/copier" 11 | "github.com/zeromicro/go-zero/core/logx" 12 | ) 13 | 14 | type GetSysDictionaryInfoListLogic struct { 15 | ctx context.Context 16 | svcCtx *svc.ServiceContext 17 | logx.Logger 18 | } 19 | 20 | func NewGetSysDictionaryInfoListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSysDictionaryInfoListLogic { 21 | return &GetSysDictionaryInfoListLogic{ 22 | ctx: ctx, 23 | svcCtx: svcCtx, 24 | Logger: logx.WithContext(ctx), 25 | } 26 | } 27 | 28 | // 获取SysDictionaryInfo列表 29 | func (l *GetSysDictionaryInfoListLogic) GetSysDictionaryInfoList(in *pb.GetSysDictionaryInfoListRequest) (*pb.GetSysDictionaryInfoListResponse, error) { 30 | offset := int(in.PageRequest.PageSize * (in.PageRequest.PageNo - 1)) 31 | var total int64 32 | 33 | // 创建db 34 | db := l.svcCtx.DB.Model(&model.SysDictionaryInfo{}) 35 | var modelSysDictionaryInfoList []model.SysDictionaryInfo 36 | // 如果有条件搜索 下方会自动创建搜索语句 37 | if in.SysDictionaryInfo.Label != "" { 38 | db = db.Where("label LIKE ?", "%"+in.SysDictionaryInfo.Label+"%") 39 | } 40 | if in.SysDictionaryInfo.Value != 0 { 41 | db = db.Where("value = ?", in.SysDictionaryInfo.Value) 42 | } 43 | if in.SysDictionaryInfo.Status != 0 { 44 | db = db.Where("status = ?", in.SysDictionaryInfo.Status) 45 | } 46 | if in.SysDictionaryInfo.SysDictionaryID != 0 { 47 | db = db.Where("sys_dictionary_id = ?", in.SysDictionaryInfo.SysDictionaryID) 48 | } 49 | err := db.Count(&total).Error 50 | if err != nil { 51 | return nil, err 52 | } 53 | err = db.Limit(int(in.PageRequest.PageSize)).Offset(offset).Order("sort").Find(&modelSysDictionaryInfoList).Error 54 | 55 | var pbSysDictionaryInfoList []*pb.SysDictionaryInfo 56 | _ = copier.Copy(&pbSysDictionaryInfoList, modelSysDictionaryInfoList) 57 | 58 | return &pb.GetSysDictionaryInfoListResponse{ 59 | SysDictionaryInfoList: pbSysDictionaryInfoList, 60 | Total: total, 61 | }, nil 62 | } 63 | -------------------------------------------------------------------------------- /application/applet/api/internal/logic/usernocasbin/random_image_logic.go: -------------------------------------------------------------------------------- 1 | package usernocasbin 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "strings" 7 | 8 | "go-zero-admin/application/applet/api/internal/svc" 9 | "go-zero-admin/application/applet/api/internal/types" 10 | 11 | "github.com/mojocn/base64Captcha" 12 | "github.com/zeromicro/go-zero/core/logx" 13 | "github.com/zeromicro/go-zero/core/stores/redis" 14 | ) 15 | 16 | var store = base64Captcha.DefaultMemStore 17 | 18 | const ( 19 | prefixCaptcha = "biz#captcha#ip:%s" 20 | expireCaptcha = 60 * 3000 // 2分钟 21 | captchaImgWidth = 105 22 | captchaImgHeight = 36 23 | captchaImgLength = 6 24 | ) 25 | 26 | type RandomImageLogic struct { 27 | logx.Logger 28 | ctx context.Context 29 | svcCtx *svc.ServiceContext 30 | } 31 | 32 | func NewRandomImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RandomImageLogic { 33 | return &RandomImageLogic{ 34 | Logger: logx.WithContext(ctx), 35 | ctx: ctx, 36 | svcCtx: svcCtx, 37 | } 38 | } 39 | 40 | func (l *RandomImageLogic) RandomImage(req *types.RandomImageRequest) (resp *types.RandomImageResponse, err error) { 41 | remoteAddrIp := l.ctx.Value("RemoteAddr") // 获取id 存redis 42 | driver := base64Captcha.NewDriverDigit(captchaImgHeight, captchaImgWidth, captchaImgLength, 0.1, 10) 43 | 44 | cp := base64Captcha.NewCaptcha(driver, store) 45 | 46 | //id, b64s, answer, err := cp.Generate() 47 | _, b64s, answer, err := cp.Generate() 48 | 49 | if err != nil { 50 | logx.Errorf("验证码获取失败! error: %v", err) 51 | return 52 | } 53 | 54 | err = saveActivationCache(GetIP(remoteAddrIp), answer, l.svcCtx.BizRedis) 55 | if err != nil { 56 | logx.Errorf("验证码存redis错误 error: %v", err) 57 | return nil, err 58 | } 59 | return &types.RandomImageResponse{CaptchaImg: b64s}, nil 60 | } 61 | 62 | func saveActivationCache(keyStr, code string, rds *redis.Redis) error { 63 | key := fmt.Sprintf(prefixCaptcha, keyStr) 64 | return rds.Setex(key, code, expireCaptcha) 65 | } 66 | 67 | func GetActivationCache(keyStr string, rds *redis.Redis) (string, error) { 68 | ket := fmt.Sprintf(prefixCaptcha, keyStr) 69 | return rds.Get(ket) 70 | } 71 | 72 | func GetIP(ipStr any) string { 73 | index := strings.Index(ipStr.(string), ":") 74 | return ipStr.(string)[:index] 75 | } 76 | -------------------------------------------------------------------------------- /application/applet/rpc/internal/logic/api/update_api_logic.go: -------------------------------------------------------------------------------- 1 | package apilogic 2 | 3 | import ( 4 | "context" 5 | "gorm.io/gorm" 6 | 7 | "go-zero-admin/application/applet/rpc/internal/model" 8 | "go-zero-admin/application/applet/rpc/internal/svc" 9 | "go-zero-admin/application/applet/rpc/pb" 10 | 11 | gormadapter "github.com/casbin/gorm-adapter/v3" 12 | "github.com/jinzhu/copier" 13 | "github.com/pkg/errors" 14 | "github.com/zeromicro/go-zero/core/logx" 15 | ) 16 | 17 | type UpdateApiLogic struct { 18 | ctx context.Context 19 | svcCtx *svc.ServiceContext 20 | logx.Logger 21 | } 22 | 23 | func NewUpdateApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateApiLogic { 24 | return &UpdateApiLogic{ 25 | ctx: ctx, 26 | svcCtx: svcCtx, 27 | Logger: logx.WithContext(ctx), 28 | } 29 | } 30 | 31 | // 更新api 32 | func (l *UpdateApiLogic) UpdateApi(in *pb.UpdateApiRequest) (*pb.NoDataResponse, error) { 33 | var modelSysApi model.SysApi 34 | var sysApi model.SysApi 35 | _ = copier.Copy(&sysApi, in.SysApi) 36 | err := l.svcCtx.DB.Where("id = ?", sysApi.ID).First(&modelSysApi).Error 37 | if modelSysApi.Path != sysApi.Path || modelSysApi.Method != sysApi.Method { 38 | if !errors.Is(l.svcCtx.DB.Where("path = ? AND method = ?", sysApi.Path, sysApi.Method).First(&model.SysApi{}).Error, gorm.ErrRecordNotFound) { 39 | return nil, errors.New("存在相同api路径或者请求method一样") 40 | } 41 | } 42 | if err != nil { 43 | return nil, err 44 | } else { 45 | err = l.UpdateCasbinApi(modelSysApi.Path, sysApi.Path, modelSysApi.Method, sysApi.Method) 46 | if err != nil { 47 | return nil, err 48 | } 49 | sysApi.DeletedAt.Valid = false 50 | err = l.svcCtx.DB.Save(&sysApi).Error 51 | } 52 | 53 | return &pb.NoDataResponse{}, nil 54 | } 55 | 56 | func (l *UpdateApiLogic) UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error { 57 | err := l.svcCtx.DB.Model(&gormadapter.CasbinRule{}).Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Updates(map[string]interface{}{ 58 | "v1": newPath, 59 | "v2": newMethod, 60 | }).Error 61 | csb := l.svcCtx.Config.CasbinConf.MustNewCasbinWithRedisWatcher(l.svcCtx.Config.DB.DataSource, l.svcCtx.Config.BizRedis) 62 | err = csb.LoadPolicy() 63 | if err != nil { 64 | return err 65 | } 66 | return err 67 | } 68 | --------------------------------------------------------------------------------