From 05358e9bbe2ac26acc264fb9ef148a0966720015 Mon Sep 17 00:00:00 2001 From: "Mr. Lan" Date: Wed, 19 Aug 2020 01:09:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=B3=BB=E7=BB=9F=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/public/file.go | 65 +++++++++++++++++++++++++++++++++ apis/system/settings.go | 72 +++++++++++++++++++++++++++++++++++++ config/db.sql | 14 ++++++++ models/gorm/gorm.go | 1 + models/system/settings.go | 21 +++++++++++ pkg/utils/file.go | 65 +++++++++++++++++++++++++++++++++ router/router.go | 1 + router/system/sys_router.go | 18 ++++++++++ 8 files changed, 257 insertions(+) create mode 100644 apis/public/file.go create mode 100644 apis/system/settings.go create mode 100644 models/system/settings.go create mode 100644 pkg/utils/file.go diff --git a/apis/public/file.go b/apis/public/file.go new file mode 100644 index 0000000..48ed771 --- /dev/null +++ b/apis/public/file.go @@ -0,0 +1,65 @@ +package public + +import ( + "encoding/base64" + "errors" + "ferry/pkg/utils" + "ferry/tools/app" + "fmt" + "io/ioutil" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +// @Summary 上传图片 +// @Description 获取JSON +// @Tags 公共接口 +// @Accept multipart/form-data +// @Param type query string true "type" (1:单图,2:多图, 3:base64图片) +// @Param file formData file true "file" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": -1, "message": "添加失败"}" +// @Router /api/v1/public/uploadFile [post] + +func UploadFile(c *gin.Context) { + tag, _ := c.GetPostForm("type") + urlPerfix := fmt.Sprintf("http://%s/", c.Request.Host) + if tag == "" { + app.Error(c, 200, errors.New(""), "缺少标识") + return + } else { + switch tag { + case "1": // 单图 + files, err := c.FormFile("file") + if err != nil { + app.Error(c, 200, errors.New(""), "图片不能为空") + return + } + // 上传文件至指定目录 + guid := uuid.New().String() + + singleFile := "static/uploadfile/" + guid + utils.GetExt(files.Filename) + _ = c.SaveUploadedFile(files, singleFile) + app.OK(c, urlPerfix+singleFile, "上传成功") + return + case "2": // 多图 + files := c.Request.MultipartForm.File["file"] + multipartFile := make([]string, len(files)) + for _, f := range files { + guid := uuid.New().String() + multipartFileName := "static/uploadfile/" + guid + utils.GetExt(f.Filename) + _ = c.SaveUploadedFile(f, multipartFileName) + multipartFile = append(multipartFile, urlPerfix+multipartFileName) + } + app.OK(c, multipartFile, "上传成功") + return + case "3": // base64 + files, _ := c.GetPostForm("file") + ddd, _ := base64.StdEncoding.DecodeString(files) + guid := uuid.New().String() + _ = ioutil.WriteFile("static/uploadfile/"+guid+".jpg", ddd, 0666) + app.OK(c, urlPerfix+"static/uploadfile/"+guid+".jpg", "上传成功") + } + } +} diff --git a/apis/system/settings.go b/apis/system/settings.go new file mode 100644 index 0000000..0be42f8 --- /dev/null +++ b/apis/system/settings.go @@ -0,0 +1,72 @@ +package system + +import ( + "ferry/global/orm" + "ferry/models/system" + "ferry/tools/app" + "fmt" + + "github.com/gin-gonic/gin" +) + +/* + @Author : lanyulei +*/ + +// 设置系统信息 +func GetSettingsInfo(c *gin.Context) { + var ( + err error + settingsInfo []*system.Settings + ) + + err = orm.Eloquent.Model(&settingsInfo).Find(&settingsInfo).Error + if err != nil { + app.Error(c, -1, fmt.Errorf("查询数据失败,%v", err.Error()), "") + return + } + + app.OK(c, settingsInfo, "查询配置信息成功") +} + +// 设置系统信息 +func SetSettingsInfo(c *gin.Context) { + var ( + err error + settingsInfo system.Settings + settingsCount int + ) + + err = c.ShouldBind(&settingsInfo) + if err != nil { + app.Error(c, -1, fmt.Errorf("绑定数据失败,%v", err.Error()), "") + return + } + + // 查询数据是否存在 + err = orm.Eloquent.Model(&system.Settings{}). + Where("classify = ?", settingsInfo.Classify). + Count(&settingsCount).Error + if err != nil { + app.Error(c, -1, fmt.Errorf("查询数据失败,%v", err.Error()), "") + return + } + if settingsCount == 0 { + // 创建新的配置信息 + err = orm.Eloquent.Create(&settingsInfo).Error + if err != nil { + app.Error(c, -1, fmt.Errorf("创建配置信息失败,%v", err.Error()), "") + return + } + } else { + err = orm.Eloquent.Model(&settingsInfo). + Where("classify = ?", settingsInfo.Classify). + Updates(&settingsInfo).Error + if err != nil { + app.Error(c, -1, fmt.Errorf("更新配置信息失败,%v", err.Error()), "") + return + } + } + + app.OK(c, "", "配置信息设置成功") +} diff --git a/config/db.sql b/config/db.sql index 0a6bdba..34daee0 100644 --- a/config/db.sql +++ b/config/db.sql @@ -89,6 +89,8 @@ INSERT INTO `casbin_rule`(`p_type`, `v0`, `v1`, `v2`, `v3`, `v4`, `v5`) VALUES ( INSERT INTO `casbin_rule`(`p_type`, `v0`, `v1`, `v2`, `v3`, `v4`, `v5`) VALUES ('p', 'common', '/api/v1/user/avatar', 'POST', NULL, NULL, NULL); INSERT INTO `casbin_rule`(`p_type`, `v0`, `v1`, `v2`, `v3`, `v4`, `v5`) VALUES ('p', 'common', '/api/v1/user/pwd', 'PUT', NULL, NULL, NULL; INSERT INTO `casbin_rule`(`p_type`, `v0`, `v1`, `v2`, `v3`, `v4`, `v5`) VALUES ('p', 'common', '/api/v1/dashboard', 'GET', NULL, NULL, NULL); +INSERT INTO `casbin_rule`(`p_type`, `v0`, `v1`, `v2`, `v3`, `v4`, `v5`) VALUES ('p', 'admin', '/api/v1/settings', 'POST', NULL, NULL, NULL); +INSERT INTO `casbin_rule`(`p_type`, `v0`, `v1`, `v2`, `v3`, `v4`, `v5`) VALUES ('p', 'admin', '/api/v1/settings', 'GET', NULL, NULL, NULL); COMMIT; BEGIN; @@ -266,6 +268,10 @@ INSERT INTO `sys_menu`(`menu_id`, `menu_name`, `title`, `icon`, `path`, `paths`, INSERT INTO `sys_menu`(`menu_id`, `menu_name`, `title`, `icon`, `path`, `paths`, `menu_type`, `action`, `permission`, `parent_id`, `no_cache`, `breadcrumb`, `component`, `sort`, `visible`, `create_by`, `update_by`, `is_frame`, `create_time`, `update_time`, `delete_time`) VALUES (344, '', '首页数据', '', '/api/v1/dashboard', '/0/63/256/344', 'A', 'GET', '', 256, '0', '', '', 0, '1', '11', '', 1, '2020-07-26 21:51:44', '2020-07-26 21:52:10', NULL); INSERT INTO `sys_menu`(`menu_id`, `menu_name`, `title`, `icon`, `path`, `paths`, `menu_type`, `action`, `permission`, `parent_id`, `no_cache`, `breadcrumb`, `component`, `sort`, `visible`, `create_by`, `update_by`, `is_frame`, `create_time`, `update_time`, `delete_time`) VALUES (350, '', '催办工单', '', '', '/0/268/270/350', 'F', '', 'process:list:upcoming:urge', 270, '0', '', '', 0, '0', '11', '', 1, '2020-07-26 21:51:44', '2020-07-26 21:52:10', NULL); INSERT INTO `sys_menu`(`menu_id`, `menu_name`, `title`, `icon`, `path`, `paths`, `menu_type`, `action`, `permission`, `parent_id`, `no_cache`, `breadcrumb`, `component`, `sort`, `visible`, `create_by`, `update_by`, `is_frame`, `create_time`, `update_time`, `delete_time`) VALUES (351, '', '催办工单', 'bug', '/api/v1/work-order/urge', '/0/63/281/333/351', 'A', 'GET', '', 333, '0', '', '', 0, '1', '11', '11', 1, '2020-07-26 21:51:44', '2020-07-26 21:52:10', NULL); +INSERT INTO `sys_menu`(`menu_id`, `menu_name`, `title`, `icon`, `path`, `paths`, `menu_type`, `action`, `permission`, `parent_id`, `no_cache`, `breadcrumb`, `component`, `sort`, `visible`, `create_by`, `update_by`, `is_frame`, `create_time`, `update_time`, `delete_time`) VALUES (352, 'Settings', '系统配置', 'system', 'settings', '/0/2/352', 'C', '', 'system:settings:index', 2, '0', '', '/system/settings/index', 7, '0', '1', '1', 1, '2020-08-18 21:44:58', '2020-08-18 23:07:11', NULL); +INSERT INTO `sys_menu`(`menu_id`, `menu_name`, `title`, `icon`, `path`, `paths`, `menu_type`, `action`, `permission`, `parent_id`, `no_cache`, `breadcrumb`, `component`, `sort`, `visible`, `create_by`, `update_by`, `is_frame`, `create_time`, `update_time`, `delete_time`) VALUES (353, '', '系统配置', 'system', '', '/0/63/280/353', 'M', '', '', 280, '0', '', '', 2, '1', '1', '1', 0, '2020-08-19 00:40:11', '2020-08-19 00:41:32', NULL); +INSERT INTO `sys_menu`(`menu_id`, `menu_name`, `title`, `icon`, `path`, `paths`, `menu_type`, `action`, `permission`, `parent_id`, `no_cache`, `breadcrumb`, `component`, `sort`, `visible`, `create_by`, `update_by`, `is_frame`, `create_time`, `update_time`, `delete_time`) VALUES (354, '', '设置配置', 'bug', '/api/v1/settings', '/0/63/280/353/354', 'A', 'POST', '', 353, '0', '', '', 1, '1', '1', '1', 1, '2020-08-19 00:41:01', '2020-08-19 00:42:25', NULL); +INSERT INTO `sys_menu`(`menu_id`, `menu_name`, `title`, `icon`, `path`, `paths`, `menu_type`, `action`, `permission`, `parent_id`, `no_cache`, `breadcrumb`, `component`, `sort`, `visible`, `create_by`, `update_by`, `is_frame`, `create_time`, `update_time`, `delete_time`) VALUES (355, '', '获取配置', 'bug', '/api/v1/settings', '/0/63/280/353/355', 'A', 'GET', '', 353, '0', '', '', 0, '1', '1', '', 1, '2020-08-19 00:42:47', '2020-08-19 00:42:47', NULL); COMMIT; BEGIN; @@ -441,6 +447,10 @@ INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `upd INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (1, 344, 'admin', NULL, NULL); INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (1, 350, 'admin', NULL, NULL); INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (1, 351, 'admin', NULL, NULL); +INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (1, 352, 'admin', NULL, NULL); +INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (1, 353, 'admin', NULL, NULL); +INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (1, 354, 'admin', NULL, NULL); +INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (1, 355, 'admin', NULL, NULL); INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (2, 63, 'common', NULL, NULL); INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (2, 80, 'common', NULL, NULL); INSERT INTO `sys_role_menu`(`role_id`, `menu_id`, `role_name`, `create_by`, `update_by`) VALUES (2, 92, 'common', NULL, NULL); @@ -466,5 +476,9 @@ INSERT INTO `sys_user`(`user_id`, `nick_name`, `phone`, `role_id`, `salt`, `avat INSERT INTO `sys_user`(`user_id`, `nick_name`, `phone`, `role_id`, `salt`, `avatar`, `sex`, `email`, `dept_id`, `post_id`, `create_by`, `update_by`, `remark`, `status`, `username`, `password`) VALUES (4, '王五', '13535353535', 3, '', '', '2', 'qq@qq.com', 8, 2, '1', '1', '', 0, 'wangwu', '$2a$10$3.RT6rpXANXvvlibX6PzU.FGA2CvfDxd1UmJ2H5zTzF4sYocbvsTO'); COMMIT; +BEGIN; +INSERT INTO `ferry`.`sys_settings`(`id`, `create_time`, `update_time`, `delete_time`, `classify`, `content`) VALUES (3, '2020-08-19 01:00:19', '2020-08-19 01:00:19', NULL, 2, '[{\"ldap_field_name\": \"\", \"local_field_name\": \"username\", \"local_field_nick\": \"用户名\"}, {\"ldap_field_name\": \"\", \"local_field_name\": \"nick_name\", \"local_field_nick\": \"用户昵称\"}, {\"ldap_field_name\": \"\", \"local_field_name\": \"phone\", \"local_field_nick\": \"手机号\"}, {\"ldap_field_name\": 0, \"local_field_name\": \"role_id\", \"local_field_nick\": \"默认角色ID\"}, {\"ldap_field_name\": 0, \"local_field_name\": \"dept_id\", \"local_field_nick\": \"默认部门ID\"}, {\"ldap_field_name\": 0, \"local_field_name\": \"post_id\", \"local_field_nick\": \"默认职位ID\"}, {\"ldap_field_name\": \"\", \"local_field_name\": \"avatar\", \"local_field_nick\": \"头像\"}, {\"ldap_field_name\": \"\", \"local_field_name\": \"sex\", \"local_field_nick\": \"性别\"}, {\"ldap_field_name\": \"\", \"local_field_name\": \"email\", \"local_field_nick\": \"邮箱\"}, {\"ldap_field_name\": 0, \"local_field_name\": \"status\", \"local_field_nick\": \"状态\"}, {\"ldap_field_name\": \"\", \"local_field_name\": \"remark\", \"local_field_nick\": \"备注\"}]'); +COMMIT; + SET FOREIGN_KEY_CHECKS = 1; -- 数据完成 ; \ No newline at end of file diff --git a/models/gorm/gorm.go b/models/gorm/gorm.go index d14349b..4be12db 100644 --- a/models/gorm/gorm.go +++ b/models/gorm/gorm.go @@ -20,6 +20,7 @@ func AutoMigrate(db *gorm.DB) error { new(system.SysUser), new(system.SysRole), new(system.Post), + new(system.Settings), // 流程中心 new(process.Classify), diff --git a/models/system/settings.go b/models/system/settings.go new file mode 100644 index 0000000..da132d3 --- /dev/null +++ b/models/system/settings.go @@ -0,0 +1,21 @@ +package system + +import ( + "encoding/json" + "ferry/models/base" +) + +/* + @Author : lanyulei +*/ + +// 配置信息 +type Settings struct { + base.Model + Classify int `gorm:"column:classify; type:int(11)" json:"classify" form:"classify"` // 设置分类,1 配置信息,2 Ldap配置 + Content json.RawMessage `gorm:"column:content; type:json" json:"content" form:"content"` // 配置内容 +} + +func (Settings) TableName() string { + return "sys_settings" +} diff --git a/pkg/utils/file.go b/pkg/utils/file.go new file mode 100644 index 0000000..47c87ce --- /dev/null +++ b/pkg/utils/file.go @@ -0,0 +1,65 @@ +package utils + +import ( + "io/ioutil" + "mime/multipart" + "os" + "path" +) + +// 获取文件大小 +func GetSize(f multipart.File) (int, error) { + content, err := ioutil.ReadAll(f) + + return len(content), err +} + +// 获取文件后缀 +func GetExt(fileName string) string { + return path.Ext(fileName) +} + +//检查文件是否存在 +func CheckExist(src string) bool { + _, err := os.Stat(src) + + return os.IsNotExist(err) +} + +// 检查文件权限 +func CheckPermission(src string) bool { + _, err := os.Stat(src) + + return os.IsPermission(err) +} + +//如果不存在则新建文件夹 +func IsNotExistMkDir(src string) error { + if exist := CheckExist(src); exist == false { + if err := MkDir(src); err != nil { + return err + } + } + + return nil +} + +//新建文件夹 +func MkDir(src string) error { + err := os.MkdirAll(src, os.ModePerm) + if err != nil { + return err + } + + return nil +} + +// 打开文件 +func Open(name string, flag int, perm os.FileMode) (*os.File, error) { + f, err := os.OpenFile(name, flag, perm) + if err != nil { + return nil, err + } + + return f, nil +} diff --git a/router/router.go b/router/router.go index 1dea422..701f28a 100644 --- a/router/router.go +++ b/router/router.go @@ -61,6 +61,7 @@ func sysCheckRoleRouterInit(r *gin.RouterGroup, authMiddleware *jwtauth.GinJWTMi systemRouter.RegisterPostRouter(v1, authMiddleware) systemRouter.RegisterMenuRouter(v1, authMiddleware) systemRouter.RegisterLoginLogRouter(v1, authMiddleware) + systemRouter.RegisterSysSettingRouter(v1, authMiddleware) // 流程中心 process.RegisterClassifyRouter(v1, authMiddleware) diff --git a/router/system/sys_router.go b/router/system/sys_router.go index 95c5224..1e880bb 100644 --- a/router/system/sys_router.go +++ b/router/system/sys_router.go @@ -3,6 +3,7 @@ package system import ( log2 "ferry/apis/log" "ferry/apis/monitor" + "ferry/apis/public" "ferry/apis/system" _ "ferry/docs" "ferry/handler" @@ -23,6 +24,8 @@ func SysNoCheckRoleRouter(r *gin.RouterGroup) { v1.GET("/monitor/server", monitor.ServerInfo) v1.GET("/getCaptcha", system.GenerateCaptchaHandler) v1.GET("/menuTreeselect", system.GetMenuTreeelect) + + registerPublicRouter(v1) } func RegisterBaseRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { @@ -120,3 +123,18 @@ func RegisterDeptRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddlewar dept.DELETE("/:id", system.DeleteDept) } } + +func RegisterSysSettingRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { + setting := v1.Group("/settings").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) + { + setting.GET("", system.GetSettingsInfo) + setting.POST("", system.SetSettingsInfo) + } +} + +func registerPublicRouter(v1 *gin.RouterGroup) { + p := v1.Group("/public") + { + p.POST("/uploadFile", public.UploadFile) + } +}