From c5fb6023f364e56234a1257fe572b0fcaec7dabe Mon Sep 17 00:00:00 2001 From: henry Date: Sat, 15 Jan 2022 11:54:05 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E5=96=84=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/admin/controller/manage/examine.go | 18 ++--- app/api/website/api/activity.go | 3 +- app/api/website/api/config.go | 5 +- app/api/website/api/service.go | 11 ++- app/api/website/api/sys.go | 15 +++- app/api/website/controller/activity.go | 12 +-- app/api/website/controller/config.go | 22 ++++-- app/api/website/controller/index.go | 38 +--------- .../controller/service/solution_case.go | 25 ++++--- app/api/website/controller/sys/banner.go | 40 ++++++++++ .../website/controller/{ => sys}/platform.go | 2 +- app/api/website/model/activity_instance.go | 3 +- .../website/model/service_solution_case.go | 7 +- app/api/website/model/sys_banner.go | 15 +++- app/session/admin.go | 4 + router/address.go | 22 ++++-- router/auth.go | 75 +++++++++++-------- router/middleware.go | 3 +- 18 files changed, 191 insertions(+), 129 deletions(-) create mode 100644 app/api/website/controller/sys/banner.go rename app/api/website/controller/{ => sys}/platform.go (98%) diff --git a/app/api/admin/controller/manage/examine.go b/app/api/admin/controller/manage/examine.go index 8926c29..1d42621 100644 --- a/app/api/admin/controller/manage/examine.go +++ b/app/api/admin/controller/manage/examine.go @@ -184,24 +184,18 @@ func (c *Examine) Launch(id uint64, identity, status int) error { users := make([]*model2.UserInstance, 0) if err = model2.ScanFields(mUserInstance.UserInstance, &users, []string{"id", "uuid", "identity"}, - &model2.ModelWhereOrder{ - Where: model2.NewWhereIn("uuid", data.UIDs), - }); err != nil { + &model2.ModelWhereOrder{Where: model2.NewWhereIn("uuid", data.UIDs)}); err != nil { return err } - mUserIdentity := model.NewUserIdentity() - - identitys := make([]*model2.UserIdentity, 0) + userIdentitys := make([]*model2.UserIdentity, 0) now := time.Now() for _, v := range users { - identitys = append(identitys, &model2.UserIdentity{ + userIdentitys = append(userIdentitys, &model2.UserIdentity{ UID: v.UUID, Identity: identity, - ModelAt: model2.ModelAt{ - CreatedAt: now, UpdatedAt: now, - }, + ModelAt: model2.ModelAt{CreatedAt: now, UpdatedAt: now}, }) if err = model2.UpdatesWhere(mUserInstance.UserInstance, map[string]interface{}{ "identity": mUserInstance.Identity | identity, "updated_at": now, @@ -211,8 +205,8 @@ func (c *Examine) Launch(id uint64, identity, status int) error { return err } } - if len(identitys) > 0 { - if err = model2.Creates(mUserIdentity.UserIdentity, identity, tx); err != nil { + if len(userIdentitys) > 0 { + if err = model2.Creates(model.NewUserIdentity().UserIdentity, userIdentitys, tx); err != nil { return err } } diff --git a/app/api/website/api/activity.go b/app/api/website/api/activity.go index 3f34b72..e397431 100644 --- a/app/api/website/api/activity.go +++ b/app/api/website/api/activity.go @@ -11,14 +11,13 @@ type Activity struct{} func (*Activity) Instance(c *gin.Context) { form := &struct { Title string `json:"title" form:"title"` - Mode int `json:"mode" form:"mode"` api.PageForm }{} if err := api.Bind(form)(c); err != nil { api.APIFailure(err.(error))(c) return } - data, err := controller.NewActivity()(getSession(c), api.GetTenantID()(c).(uint64)).Instance(form.Title, form.Mode, form.Page, form.PageSize) + data, err := controller.NewActivity()(getSession(c), api.GetTenantID()(c).(uint64)).Instance(form.Title, form.Page, form.PageSize) api.APIResponse(err, data)(c) } diff --git a/app/api/website/api/config.go b/app/api/website/api/config.go index 55404d1..12c9f38 100644 --- a/app/api/website/api/config.go +++ b/app/api/website/api/config.go @@ -10,12 +10,13 @@ type Config struct{} func (*Config) Index(c *gin.Context) { form := &struct { - Kind int `json:"kind" form:"kind" binding:"required"` + Kind int `json:"kind" form:"kind"` + Key string `json:"key" form:"key" binding:"required"` }{} if err := api.Bind(form)(c); err != nil { api.APIFailure(err.(error))(c) return } - data, err := controller.NewConfig()(nil).Instance(form.Kind) + data, err := controller.NewConfig()(nil).Instance(form.Kind, form.Key) api.APIResponse(err, data)(c) } diff --git a/app/api/website/api/service.go b/app/api/website/api/service.go index 23103a5..5715a36 100644 --- a/app/api/website/api/service.go +++ b/app/api/website/api/service.go @@ -3,13 +3,22 @@ package api import ( "SciencesServer/app/api/website/controller/service" "SciencesServer/app/basic/api" + "fmt" "github.com/gin-gonic/gin" ) type Service struct{} func (*Service) SolutionCase(c *gin.Context) { - data, err := service.NewSolutionCase()(getSession(c), api.GetTenantID()(c).(uint64)).Instance() + form := &struct { + Mode int `json:"mode" form:"mode"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + fmt.Println(form.Mode) + data, err := service.NewSolutionCase()(getSession(c), api.GetTenantID()(c).(uint64)).Instance(form.Mode) api.APIResponse(err, data)(c) } diff --git a/app/api/website/api/sys.go b/app/api/website/api/sys.go index 6af2a25..5757c97 100644 --- a/app/api/website/api/sys.go +++ b/app/api/website/api/sys.go @@ -1,7 +1,6 @@ package api import ( - "SciencesServer/app/api/website/controller" "SciencesServer/app/api/website/controller/sys" "SciencesServer/app/basic/api" "github.com/gin-gonic/gin" @@ -10,7 +9,7 @@ import ( type Sys struct{} func (*Sys) Platform(c *gin.Context) { - data, err := controller.NewPlatform()().Instance() + data, err := sys.NewPlatform()().Instance() api.APIResponse(err, data)(c) } @@ -19,6 +18,18 @@ func (*Sys) Navigation(c *gin.Context) { api.APIResponse(err, data)(c) } +func (*Sys) Banner(c *gin.Context) { + form := &struct { + Key string `json:"key" form:"key" binding:"required"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := sys.NewBanner()(api.GetTenantID()(c).(uint64)).Instance(form.Key) + api.APIResponse(err, data)(c) +} + func (*Sys) About(c *gin.Context) { form := &struct { NavigationID string `json:"navigation_id" form:"navigation_id"` diff --git a/app/api/website/controller/activity.go b/app/api/website/controller/activity.go index bf17b32..f71c823 100644 --- a/app/api/website/controller/activity.go +++ b/app/api/website/controller/activity.go @@ -33,7 +33,7 @@ type ( ) // Instance 活动信息 -func (c *Activity) Instance(title string, mode, page, pageSize int) (*controller.ReturnPages, error) { +func (c *Activity) Instance(title string, page, pageSize int) (*controller.ReturnPages, error) { mActivityInstance := model.NewActivityInstance() var count int64 @@ -43,10 +43,8 @@ func (c *Activity) Instance(title string, mode, page, pageSize int) (*controller if title != "" { where = append(where, model2.NewWhereLike("a.title", title)) } - if mode > 0 { - where = append(where, model2.NewWhere("a.mode", mode)) - } - out, err := mActivityInstance.Activity(c.UID, c.SelectIdentity, page, pageSize, &count) + + out, err := mActivityInstance.Activity(c.UID, c.SelectIdentity, page, pageSize, &count, where...) if err != nil { return nil, err @@ -71,6 +69,8 @@ func (c *Activity) Detail(id uint64) (*ActivityDetail, error) { if err != nil { return nil, err } + mActivityInstance.Image.Image = mActivityInstance.Image.Analysis(config.SettingInfo.Domain) + return &ActivityDetail{ ID: out.GetEncodeID(), ActivityInstanceDetail: out, @@ -82,7 +82,7 @@ func (c *Activity) Detail(id uint64) (*ActivityDetail, error) { func (c *Activity) Join(id uint64) error { mActivityInstance := model.NewActivityInstance() - isExist, err := model2.FirstField(mActivityInstance.ActivityInstance, []string{"id", "mode", "join_deadline"}, + isExist, err := model2.FirstField(mActivityInstance.ActivityInstance, []string{"id", "amount", "join_deadline"}, model2.NewWhere("id", id), model2.NewWhere("tenant_id", c.tenantID), model2.NewWhere("status", model2.ActivityInstanceStatusForShow)) diff --git a/app/api/website/controller/config.go b/app/api/website/controller/config.go index 52ede56..c48b805 100644 --- a/app/api/website/controller/config.go +++ b/app/api/website/controller/config.go @@ -4,6 +4,7 @@ import ( "SciencesServer/app/api/admin/model" model2 "SciencesServer/app/common/model" "SciencesServer/app/session" + "errors" ) type Config struct{ *session.Enterprise } @@ -11,24 +12,29 @@ type Config struct{ *session.Enterprise } type ConfigHandle func(session *session.Enterprise) *Config type ( + // ConfigInfo 配置信息 ConfigInfo struct { - Kind int `json:"kind"` Name string `json:"name"` - Key string `json:"key"` Value string `json:"value"` } ) -func (c *Config) Instance(kind int) ([]*ConfigInfo, error) { +func (c *Config) Instance(kind int, key string) (*ConfigInfo, error) { mSysConfig := model.NewSysConfig() - out := make([]*ConfigInfo, 0) + where := []*model2.ModelWhere{model2.NewWhere("`key`", key)} - if err := model2.ScanFields(mSysConfig.SysConfig, &out, []string{"kind", "name", "`key`", "`value`"}, - &model2.ModelWhereOrder{Where: model2.NewWhere("kind", kind)}); err != nil { - return nil, err + if kind > 0 { + where = append(where, model2.NewWhere("kind", kind)) } - return out, nil + isExist, err := model2.FirstField(mSysConfig.SysConfig, []string{"id", "name", "`value`"}, where...) + + if err != nil { + return nil, err + } else if !isExist { + return nil, errors.New("操作错误,数据不存在或已被删除") + } + return &ConfigInfo{Name: mSysConfig.Name, Value: mSysConfig.Value}, nil } func NewConfig() ConfigHandle { diff --git a/app/api/website/controller/index.go b/app/api/website/controller/index.go index c5303be..ab6be9c 100644 --- a/app/api/website/controller/index.go +++ b/app/api/website/controller/index.go @@ -309,43 +309,7 @@ func (c *Index) DistributionCompany(province, city string) (map[string]*Instance } _out := c.distribution(out) c.filter(_out) - _out["230000"] = &InstanceDistributionDetailInfo{ - Code: "230000", - Name: "黑龙江", - Count: 6000, - Industry: nil, - Children: map[string]*InstanceDistributionDetailInfo{ - "230100": &InstanceDistributionDetailInfo{ - Code: "230100", - Name: "哈尔滨市", - Count: 3000, - Industry: nil, - Children: nil, - }, - "230200": &InstanceDistributionDetailInfo{ - Code: "230200", - Name: "齐齐哈尔市", - Count: 3000, - Industry: nil, - Children: nil, - }, - }, - } - _out["330000"] = &InstanceDistributionDetailInfo{ - Code: "330000", - Name: "浙江省", - Count: 5000, - Industry: nil, - Children: map[string]*InstanceDistributionDetailInfo{ - "330100": &InstanceDistributionDetailInfo{ - Code: "330100", - Name: "杭州市", - Count: 5000, - Industry: nil, - Children: nil, - }, - }, - } + return _out, nil } diff --git a/app/api/website/controller/service/solution_case.go b/app/api/website/controller/service/solution_case.go index 0dca655..70725ad 100644 --- a/app/api/website/controller/service/solution_case.go +++ b/app/api/website/controller/service/solution_case.go @@ -22,12 +22,13 @@ type SolutionCaseHandle func(session *session.Enterprise, tenantID uint64) *Solu type ( // SolutionCaseInfo 基本信息 SolutionCaseInfo struct { - ID string `json:"id"` - MarkID uint64 `json:"-"` - Mode model2.ServiceSolutionCaseMode `json:"mode"` - Title string `json:"title"` - Image string `json:"image"` - Children []*SolutionCaseBasic `json:"children"` + ID string `json:"id"` + MarkID uint64 `json:"-"` + Mode model2.ServiceSolutionCaseMode `json:"mode"` + Title string `json:"title"` + Image string `json:"image"` + Description string `json:"description"` + Children []*SolutionCaseBasic `json:"children"` } // SolutionCaseBasic 基本信息 SolutionCaseBasic struct { @@ -46,9 +47,12 @@ type ( ) // Instance 服务解决方案案例 -func (c *SolutionCase) Instance() ([]*SolutionCaseInfo, error) { +func (c *SolutionCase) Instance(mode int) ([]*SolutionCaseInfo, error) { mServiceSolutionCase := model.NewServiceSolutionCase() - out, err := mServiceSolutionCase.SolutionCase(6) + + where := []*model2.ModelWhere{model2.NewWhere("k.mode", mode)} //model2.NewWhere("k.tenant_id", c.tenantID), + + out, err := mServiceSolutionCase.SolutionCase(6, where...) if err != nil { return nil, err @@ -73,8 +77,9 @@ func (c *SolutionCase) Instance() ([]*SolutionCaseInfo, error) { if !isExist { ret = append(ret, &SolutionCaseInfo{ ID: v.GetEncodeID(), MarkID: v.ID, Mode: v.Mode, Title: v.Title, - Image: v.Image.Analysis(config.SettingInfo.Domain), - Children: []*SolutionCaseBasic{detail}, + Image: v.Image.Analysis(config.SettingInfo.Domain), + Description: v.Description, + Children: []*SolutionCaseBasic{detail}, }) } } diff --git a/app/api/website/controller/sys/banner.go b/app/api/website/controller/sys/banner.go new file mode 100644 index 0000000..de65123 --- /dev/null +++ b/app/api/website/controller/sys/banner.go @@ -0,0 +1,40 @@ +package sys + +import ( + "SciencesServer/app/api/website/model" + "SciencesServer/config" +) + +type Banner struct { + tenantID uint64 +} + +type BannerHandle func(tenantID uint64) *Banner + +type BannerInfo struct { + ID string `json:"id"` + Title string `json:"title"` + IsMultiple int `json:"is_multiple"` + Images string `json:"images"` +} + +func (c *Banner) Instance(key string) (*BannerInfo, error) { + mSysBanner := model.NewSysBanner() + if err := mSysBanner.Get(c.tenantID, map[string]interface{}{ + "key": key, "local": key, + }); err != nil { + return nil, err + } + return &BannerInfo{ + ID: mSysBanner.GetEncodeID(), + Title: mSysBanner.Title, + IsMultiple: mSysBanner.IsMultiple, + Images: mSysBanner.Images.AnalysisSlice(config.SettingInfo.Domain), + }, nil +} + +func NewBanner() BannerHandle { + return func(tenantID uint64) *Banner { + return &Banner{tenantID: tenantID} + } +} diff --git a/app/api/website/controller/platform.go b/app/api/website/controller/sys/platform.go similarity index 98% rename from app/api/website/controller/platform.go rename to app/api/website/controller/sys/platform.go index 6550515..15186aa 100644 --- a/app/api/website/controller/platform.go +++ b/app/api/website/controller/sys/platform.go @@ -1,4 +1,4 @@ -package controller +package sys import ( "SciencesServer/app/api/website/model" diff --git a/app/api/website/model/activity_instance.go b/app/api/website/model/activity_instance.go index 72d0e77..b50072a 100644 --- a/app/api/website/model/activity_instance.go +++ b/app/api/website/model/activity_instance.go @@ -63,8 +63,7 @@ func (m *ActivityInstance) Detail(id, uid uint64, identity int) (*ActivityInstan mActivityJoin := model.NewActivityJoin() db := orm.GetDB().Table(m.TableName()+" AS a"). - Select("a.id", "a.title", "a.image", "a.begin_at", "a.finish_at", "a.join_deadline", - "IFNULL(u.id, 0) AS join_id", "j.count AS join_count"). + Select("a.*", "IFNULL(u.id, 0) AS join_id", "j.count AS join_count"). Joins(fmt.Sprintf("LEFT JOIN %s u ON a.id = u.activity_id AND u.uid = %d AND u.identity = %d AND u.status = %d AND u.is_deleted = %d", mActivityJoin.TableName(), uid, identity, model.ActivityJoinStatusForSuccess, model.DeleteStatusForNot)). Joins(fmt.Sprintf("LEFT JOIN (SELECT activity_id, COUNT(id) AS count FROM %s WHERE status = %d AND is_deleted = %d GROUP BY activity_id) AS j ON a.id = j.activity_id", diff --git a/app/api/website/model/service_solution_case.go b/app/api/website/model/service_solution_case.go index 14cf58b..3f9d86e 100644 --- a/app/api/website/model/service_solution_case.go +++ b/app/api/website/model/service_solution_case.go @@ -13,8 +13,9 @@ type ServiceSolutionCase struct { // ServiceSolutionCaseInfo 解决方案案例信息 type ServiceSolutionCaseInfo struct { model.Model - Mode model.ServiceSolutionCaseMode `json:"mode"` - Title string `json:"title"` + Mode model.ServiceSolutionCaseMode `json:"mode"` + Title string `json:"title"` + Description string `json:"description"` model.Image DetailID uint64 `json:"detail_id"` DetailTitle string `json:"detail_title"` @@ -37,7 +38,7 @@ func (m *ServiceSolutionCase) SolutionCase(limit int, where ...*model.ModelWhere // Order("s.sort " + model.OrderModeToDesc) db := orm.GetDB().Table(model.NewServiceSolutionCaseKind().TableName()+" AS k"). - Select("k.id AS id", "k.mode", "k.image AS image", + Select("k.id AS id", "k.mode", "k.title", "k.image AS image", "k.description", "d.id AS detail_id", "d.title AS detail_title", "d.image AS detail_image", "d.description AS detail_description"). Joins(fmt.Sprintf(`LEFT JOIN (SELECT id, kind_id, title, image, description, sort FROM %s AS a WHERE (SELECT count( b.id ) FROM %s AS b WHERE a.kind_id = b.kind_id AND a.id < b.id AND b.is_deleted = %d) < %d diff --git a/app/api/website/model/sys_banner.go b/app/api/website/model/sys_banner.go index 24d2560..eb68b8d 100644 --- a/app/api/website/model/sys_banner.go +++ b/app/api/website/model/sys_banner.go @@ -1,11 +1,24 @@ package model -import "SciencesServer/app/common/model" +import ( + "SciencesServer/app/common/model" + "SciencesServer/serve/orm" + "fmt" +) type SysBanner struct { *model.SysBanner } +func (m *SysBanner) Get(tenantID uint64, condition map[string]interface{}) error { + db := orm.GetDB().Table(m.TableName()) + + for k, v := range condition { + db = db.Or(fmt.Sprintf("`%s` = '%v' AND is_deleted = %d AND tenant_id = %d", k, v, model.DeleteStatusForNot, tenantID)) + } + return db.Scan(m.SysBanner).Error +} + func NewSysBanner() *SysBanner { return &SysBanner{model.NewSysBanner()} } diff --git a/app/session/admin.go b/app/session/admin.go index 25d13c3..da25c88 100644 --- a/app/session/admin.go +++ b/app/session/admin.go @@ -27,6 +27,10 @@ func (this *Admin) TenantIDFormat() string { return fmt.Sprintf("%d", this.TenantID) } +func (this *Admin) UIDFormat() string { + return fmt.Sprintf("%d", this.UID) +} + func (this *Admin) MarshalBinary() ([]byte, error) { return json.Marshal(this) } diff --git a/router/address.go b/router/address.go index 4cd2021..3d56bab 100644 --- a/router/address.go +++ b/router/address.go @@ -50,7 +50,12 @@ func registerAPI(app *gin.Engine) { userV1.POST("/collect/launch", _api.Collect) } // Activity 活动信息管理 - activityV1 := v1.Group("/activity") + activityV1 := v1.Group("/activity").Use(NeedLogin(config.RedisKeyForAccountEnterprise, + session.NewEnterprise(), AddSkipperURL([]string{ + apiPrefix + "/v1/activity", + apiPrefix + "/v1/activity/detail", + apiPrefix + "/v1/account", + }...))) { _api := new(api2.Activity) activityV1.POST("", _api.Instance) @@ -62,6 +67,7 @@ func registerAPI(app *gin.Engine) { { _api := new(api2.Sys) sysV1.GET("/platform", _api.Platform) + sysV1.GET("/banner", _api.Banner) sysV1.GET("/navigation", _api.Navigation) sysV1.GET("/agreement", _api.Agreement) sysV1.GET("/agreement/detail", _api.AgreementDetail) @@ -84,7 +90,7 @@ func registerAPI(app *gin.Engine) { serviceV1 := v1.Group("/service") { _api := new(api2.Service) - serviceV1.GET("/solution_case", _api.SolutionCase) + serviceV1.POST("/solution_case", _api.SolutionCase) serviceV1.POST("/solution_case/list", _api.SolutionCaseList) serviceV1.POST("/solution_case/detail", _api.SolutionCaseDetail) serviceV1.POST("/innovate", _api.Innovate) @@ -141,11 +147,11 @@ func registerAdminAPI(app *gin.Engine) { apiPrefix + "/v1/account/logout", }...))) // 权限验证 - //g.Use(NeedPermission(AddSkipperURL([]string{ - // apiPrefix + "/captcha", - // apiPrefix + "/account/login", - // apiPrefix + "/account/logout", - //}...))) + v1.Use(NeedPermission(AddSkipperURL([]string{ + apiPrefix + "/v1/captcha", + apiPrefix + "/v1/account/login", + apiPrefix + "/v1/account/logout", + }...))) // Captcha 验证码 v1.GET("/captcha", new(api1.Captcha).Captcha) // Upload 上传管理 @@ -347,6 +353,8 @@ func registerEnterpriseAPI(app *gin.Engine) { apiPrefix + "/v1/account/authorize", }...))) + v1.Use(NeedAuthIdentity()) + // Upload 上传管理 v1.POST("/upload", new(api.Upload).Upload) // Config 配置管理 diff --git a/router/auth.go b/router/auth.go index 7cc9abf..aa5fa39 100644 --- a/router/auth.go +++ b/router/auth.go @@ -6,6 +6,7 @@ import ( "SciencesServer/app/session" "SciencesServer/config" "SciencesServer/utils" + "fmt" "github.com/gin-gonic/gin" "net/http" ) @@ -27,6 +28,7 @@ func AddSkipperURL(url ...string) SkipperURL { // NeedLogin 需要登录 func NeedLogin(key string, session logic.ISession, skipperURL ...SkipperURL) gin.HandlerFunc { return func(c *gin.Context) { + fmt.Println(c.Request.URL.Path) if len(skipperURL) > 0 && skipperURL[0](c) { c.Next() return @@ -50,41 +52,48 @@ func NeedLogin(key string, session logic.ISession, skipperURL ...SkipperURL) gin } } -func NeedHaveIdentity() gin.HandlerFunc { - return func(c *gin.Context) { - - } -} - // NeedPermission 需要权限验证 -func NeedPermission(skipperURL ...SkipperURL) PermissionHandle { - return func(key string) gin.HandlerFunc { - return func(c *gin.Context) { - if len(skipperURL) > 0 && skipperURL[0](c) { - c.Next() - return - } - value, _ := c.Get(config.TokenForSession) - _session := value.(*session.Admin) - - if !_session.IsAdmin { - if _session.TenantID > 0 { - //if isExist, _ := cache.Cache.SIsMember(config.RedisKeyForTenant, _session.TenantKey); !isExist { - // c.JSON(http.StatusForbidden, gin.H{"message": "租户/公司信息协议已到期或已被禁用,无权限访问!"}) - // c.Abort() - // return - //} - } - //if pass, _ := service.NewPermission(nil, &service.AuthRequest{ - // Url: key, - // Method: c.Request.Method, - //})(_session.TenantKey, fmt.Sprintf("%d", _session.UID)).Enforce(); !pass { - // c.JSON(http.StatusOK, gin.H{"code": http.StatusForbidden, "msg": "无权限访问!"}) - // c.Abort() - // return - //} - } +func NeedPermission(skipperURL ...SkipperURL) gin.HandlerFunc { + return func(c *gin.Context) { + if len(skipperURL) > 0 && skipperURL[0](c) { c.Next() + return } + value, _ := c.Get(config.TokenForSession) + _session := value.(*session.Admin) + + if _session.IsAdmin && _session.TenantID <= 0 { + c.Next() + return + } + pass, err := service.NewPermission( + service.WithAuthTenant(_session.TenantIDFormat()), + service.WithAuthUser(_session.UIDFormat()), + service.WithAuthRequest([]*service.AuthRequest{ + &service.AuthRequest{ + Url: c.Request.URL.Path, + Method: c.Request.Method, + }, + }), + ).Enforce() + + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{ + "message": fmt.Sprintf("权限验证错误【%v】,请联系管理员!", err), + }) + c.Abort() + return + } else if !pass { + c.JSON(http.StatusForbidden, gin.H{"message": "无权限访问!"}) + c.Abort() + return + } + c.Next() + } +} + +func NeedAuthIdentity() gin.HandlerFunc { + return func(c *gin.Context) { + c.Next() } } diff --git a/router/middleware.go b/router/middleware.go index d4f91e3..3fe8bfe 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -4,7 +4,6 @@ import ( "SciencesServer/config" "SciencesServer/serve/logger" "context" - "fmt" "net/http" "time" @@ -86,7 +85,7 @@ func RecoveryHandler() gin.HandlerFunc { return func(c *gin.Context) { defer func() { if err := recover(); err != nil { - fmt.Printf("Recover:request【%s】 error:【%v】\n", c.Request.URL, err) + //fmt.Printf("Recover:request【%s】 error:【%v】\n", c.Request.URL, err) c.JSON(http.StatusInternalServerError, gin.H{ "message": "Internal Server Error!", })