From 4637a5fb4bc7fcccd3e4fbc5c4e638a2f792a144 Mon Sep 17 00:00:00 2001 From: henry Date: Thu, 13 Jan 2022 16:50:38 +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=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/admin/api/sys.go | 68 ++++++++ app/api/admin/controller/sys/agreement.go | 151 ++++++++++++++++++ app/api/admin/controller/sys/banner.go | 19 +-- app/api/admin/model/sys_agreement.go | 45 ++++++ .../enterprise/controller/user/withdrawal.go | 4 +- app/basic/api/struct.go | 12 +- app/common/migrate/instance.go | 3 +- app/common/model/common.go | 13 +- app/common/model/sys_agreement.go | 30 ++++ router/address.go | 18 ++- 10 files changed, 326 insertions(+), 37 deletions(-) create mode 100644 app/api/admin/controller/sys/agreement.go create mode 100644 app/api/admin/model/sys_agreement.go create mode 100644 app/common/model/sys_agreement.go diff --git a/app/api/admin/api/sys.go b/app/api/admin/api/sys.go index ffe175b..c135093 100644 --- a/app/api/admin/api/sys.go +++ b/app/api/admin/api/sys.go @@ -25,6 +25,18 @@ func (*Sys) Banner(c *gin.Context) { api.APIResponse(err, data)(c) } +func (*Sys) BannerLocal(c *gin.Context) { + form := &struct { + api.TenantIDStringForm + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := sys.NewBanner()(api.GetSession()(c).(*session.Admin)).Local(form.Convert()) + api.APIResponse(err, data)(c) +} + func (*Sys) BannerForm(c *gin.Context) { form := &struct { api.IDStringForm @@ -136,3 +148,59 @@ func (*Sys) NavigationDelete(c *gin.Context) { err := sys.NewNavigation()(api.GetSession()(c).(*session.Admin)).Delete(form.Convert()) api.APIResponse(err)(c) } + +func (*Sys) Agreement(c *gin.Context) { + form := &struct { + api.TenantIDStringForm + Title string `json:"title" form:"title"` + api.PageForm + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := sys.NewAgreement()(api.GetSession()(c).(*session.Admin)).Instance(form.Convert(), form.Title, + form.Page, form.PageSize) + api.APIResponse(err, data)(c) +} + +func (*Sys) AgreementDetail(c *gin.Context) { + form := new(api.IDStringForm) + + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := sys.NewAgreement()(api.GetSession()(c).(*session.Admin)).Detail(form.Convert()) + api.APIResponse(err, data)(c) +} + +func (*Sys) AgreementForm(c *gin.Context) { + form := &struct { + api.IDStringForm + api.TenantIDStringForm + Title string `json:"title" form:"title" binding:"required"` + Content string `json:"content" form:"content" binding:"required"` + Status int `json:"status" form:"status" binding:"required"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + err := sys.NewAgreement()(api.GetSession()(c).(*session.Admin)).Form(&sys.AgreementParams{ + ID: form.IDStringForm.Convert(), TenantID: form.TenantIDStringForm.Convert(), + Title: form.Title, Content: form.Content, Status: form.Status, + }) + api.APIResponse(err)(c) +} + +func (*Sys) AgreementDelete(c *gin.Context) { + form := new(api.IDStringForm) + + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + err := sys.NewAgreement()(api.GetSession()(c).(*session.Admin)).Delete(form.Convert()) + api.APIResponse(err)(c) +} diff --git a/app/api/admin/controller/sys/agreement.go b/app/api/admin/controller/sys/agreement.go new file mode 100644 index 0000000..ae9f673 --- /dev/null +++ b/app/api/admin/controller/sys/agreement.go @@ -0,0 +1,151 @@ +package sys + +import ( + "SciencesServer/app/api/admin/model" + "SciencesServer/app/basic/controller" + model2 "SciencesServer/app/common/model" + "SciencesServer/app/session" + "errors" +) + +type Agreement struct { + *session.Admin +} + +type AgreementHandle func(session *session.Admin) *Agreement + +type ( + // AgreementInfo 协议信息 + AgreementInfo struct { + ID string `json:"id"` + TenantID string `json:"tenant_id"` + Area string `json:"area"` + *model2.SysAgreement + } + // AgreementDetailInfo 协议详细信息 + AgreementDetailInfo struct { + ID string `json:"id"` + TenantID string `json:"tenant_id"` + *model2.SysAgreement + } + // AgreementParams 协议参数信息 + AgreementParams struct { + ID, TenantID uint64 + Title, Content string + Status int + } +) + +// Instance 首页信息 +func (c *Agreement) Instance(tenantID uint64, title string, page, pageSize int) (*controller.ReturnPages, error) { + mSysAgreement := model.NewSysAgreement() + + where := make([]*model2.ModelWhere, 0) + + if c.TenantID > 0 { + where = append(where, model2.NewWhere("s.tenant_id", c.TenantID)) + } + if tenantID > 0 { + where = append(where, model2.NewWhere("s.tenant_id", tenantID)) + } + if title != "" { + where = append(where, model2.NewWhereLike("s.title", title)) + } + var count int64 + + out, err := mSysAgreement.Agreement(page, pageSize, &count, where...) + + if err != nil { + return nil, err + } + + list := make([]*AgreementInfo, 0) + + for _, v := range out { + list = append(list, &AgreementInfo{ + ID: v.GetEncodeID(), + TenantID: v.GetEncodeTenantID(), + Area: v.FormatBasic(), + SysAgreement: v.SysAgreement, + }) + } + return &controller.ReturnPages{Data: list, Count: count}, nil +} + +// Detail 详细信息 +func (c *Agreement) Detail(id uint64) (*AgreementDetailInfo, error) { + mSysAgreement := model.NewSysAgreement() + mSysAgreement.ID = id + + isExist, err := model2.FirstField(mSysAgreement.SysAgreement, []string{"id", "tenant_id"}) + + if err != nil { + return nil, err + } else if !isExist { + return nil, errors.New("操作错误,协议信息不存在或已被删除") + } + return &AgreementDetailInfo{ + ID: mSysAgreement.GetEncodeID(), + TenantID: mSysAgreement.GetEncodeTenantID(), + SysAgreement: mSysAgreement.SysAgreement, + }, nil +} + +func (c *Agreement) Form(params *AgreementParams) error { + mSysAgreement := model.NewSysAgreement() + + if params.ID > 0 { + mSysAgreement.ID = params.ID + + isExist, err := model2.FirstField(mSysAgreement.SysAgreement, []string{"id", "tenant_id", "created_at"}) + + if err != nil { + return err + } else if !isExist { + return errors.New("操作错误,协议信息不存在或已被删除") + } + if c.TenantID > 0 && c.TenantID != mSysAgreement.TenantID { + return errors.New("操作错误,无权限操作") + } + } + mSysAgreement.Title = params.Title + mSysAgreement.Content = params.Content + mSysAgreement.Status = model2.SysAgreementStatus(params.Status) + + if mSysAgreement.ID > 0 { + if c.TenantID <= 0 { + mSysAgreement.TenantID = params.TenantID + } + return model2.Updates(mSysAgreement.SysAgreement, mSysAgreement.SysAgreement) + } + mSysAgreement.TenantID = params.TenantID + + if c.TenantID > 0 { + mSysAgreement.TenantID = c.TenantID + } + return model2.Create(mSysAgreement.SysAgreement) +} + +// Delete 删除操作 +func (c *Agreement) Delete(id uint64) error { + mSysAgreement := model.NewSysAgreement() + mSysAgreement.ID = id + + isExist, err := model2.FirstField(mSysAgreement.SysAgreement, []string{"id", "tenant_id"}) + + if err != nil { + return err + } else if !isExist { + return errors.New("操作错误,协议信息不存在或已被删除") + } + if c.TenantID > 0 && mSysAgreement.TenantID != c.TenantID { + return errors.New("操作错误,无权限操作") + } + return model2.Delete(mSysAgreement.SysAgreement) +} + +func NewAgreement() AgreementHandle { + return func(session *session.Admin) *Agreement { + return &Agreement{session} + } +} diff --git a/app/api/admin/controller/sys/banner.go b/app/api/admin/controller/sys/banner.go index b6a5af1..f7339da 100644 --- a/app/api/admin/controller/sys/banner.go +++ b/app/api/admin/controller/sys/banner.go @@ -22,8 +22,8 @@ type ( BannerInfo struct { ID string `json:"id"` *model2.SysBanner - Area string `json:"area"` - Images []string `json:"images"` + Area string `json:"area"` + Images string `json:"images"` } // BannerLocalInfo 轮播图位置信息 BannerLocalInfo struct { @@ -37,10 +37,9 @@ type ( } // BannerParams 轮播图参数信息 BannerParams struct { - ID, TenantID uint64 - Title, Local, Size, Remark string - Images []string - IsMultiple int + ID, TenantID uint64 + Title, Local, Size, Images, Remark string + IsMultiple int } ) @@ -156,7 +155,7 @@ func (c *Banner) Form(params *BannerParams) error { } mSysBanner.Title = params.Title mSysBanner.Local = params.Local - mSysBanner.Images.SetImagesAttribute(params.Images) + mSysBanner.Images.Images = params.Images mSysBanner.Size = params.Size mSysBanner.IsMultiple = params.IsMultiple mSysBanner.Remark = params.Remark @@ -185,16 +184,12 @@ func (c *Banner) Local(tenantID uint64) ([]*BannerLocalInfo, error) { if tenantID > 0 { where = append(where, model2.NewWhere("tenant_id", tenantID)) - } else { - where = append(where, model2.NewWhere("tenant_id", c.TenantID)) } if err := model2.Pluck(mSysBanner.SysBanner, "local", &out, where...); err != nil { return nil, err } - c.filter(out, ">") - - return nil, nil + return c.tree(c.filter(out, ">"), defaultFirstLevelKey), nil } // Delete 删除操作 diff --git a/app/api/admin/model/sys_agreement.go b/app/api/admin/model/sys_agreement.go new file mode 100644 index 0000000..1eec4ec --- /dev/null +++ b/app/api/admin/model/sys_agreement.go @@ -0,0 +1,45 @@ +package model + +import ( + "SciencesServer/app/common/model" + "SciencesServer/serve/orm" + "fmt" +) + +type SysAgreement struct { + *model.SysAgreement +} + +// SysAgreementInfo 轮播图信息 +type SysAgreementInfo struct { + *model.SysAgreement + model.Area +} + +// Agreement 协议信息 +func (m *SysAgreement) Agreement(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*SysAgreementInfo, error) { + db := orm.GetDB().Table(m.TableName()+" AS s"). + Select("s.id", "s.tenant_id", "s.title", "s.status", "s.created_at", "s.updated_at", + "t.province", "t.city"). + Joins(fmt.Sprintf("LEFT JOIN %s AS t ON s.tenant_id = t.id", model.NewSysTenant().TableName())). + Where("s.is_deleted = ?", model.DeleteStatusForNot) + + if len(where) > 0 { + for _, wo := range where { + db = db.Where(wo.Condition, wo.Value) + } + } + out := make([]*SysAgreementInfo, 0) + + if err := db.Count(count).Error; err != nil { + return nil, err + } + if err := db.Order("s.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil { + return nil, err + } + return out, nil +} + +func NewSysAgreement() *SysAgreement { + return &SysAgreement{model.NewSysAgreement()} +} diff --git a/app/api/enterprise/controller/user/withdrawal.go b/app/api/enterprise/controller/user/withdrawal.go index e6f1dc4..973e90b 100644 --- a/app/api/enterprise/controller/user/withdrawal.go +++ b/app/api/enterprise/controller/user/withdrawal.go @@ -30,8 +30,8 @@ type ( } // WithdrawalTransferInfo 提现转账信息 WithdrawalTransferInfo struct { - Images []string `json:"images"` - Remark string `json:"remark"` + Images string `json:"images"` + Remark string `json:"remark"` } ) diff --git a/app/basic/api/struct.go b/app/basic/api/struct.go index 9c29082..0c2cdf3 100644 --- a/app/basic/api/struct.go +++ b/app/basic/api/struct.go @@ -49,14 +49,16 @@ func (this *ImageForm) FilterImageURL() string { } type ImagesForm struct { - Images []string `json:"images" form:"images"` + Images string `json:"images" form:"images"` } -func (this *ImagesForm) FilterImageURL() []string { - for _, v := range this.Images { - v = strings.Replace(v, config.SettingInfo.Domain, "", -1) +func (this *ImagesForm) FilterImageURL() string { + out := make([]string, 0) + + for _, v := range strings.Split(this.Images, ",") { + out = append(out, strings.Replace(v, config.SettingInfo.Domain, "", -1)) } - return this.Images + return strings.Join(out, ",") } type FileForm struct { diff --git a/app/common/migrate/instance.go b/app/common/migrate/instance.go index 9b3d4dd..8b02ccb 100644 --- a/app/common/migrate/instance.go +++ b/app/common/migrate/instance.go @@ -121,8 +121,7 @@ func (this *Instance) Handle() { return out }}, &synchronized{iModel: model.NewSysNavigation()}, - &synchronized{iModel: model.NewSysAbout()}, - &synchronized{iModel: model.NewSysBanner()}, + &synchronized{iModel: model.NewSysBanner()}, &synchronized{iModel: model.NewSysAgreement()}, // 日志管理 &synchronized{iModel: model.NewSysLog()}, &synchronized{iModel: model.NewSysUserLoginLog()}, // 用户管理 diff --git a/app/common/model/common.go b/app/common/model/common.go index 6310d5b..87ca493 100644 --- a/app/common/model/common.go +++ b/app/common/model/common.go @@ -4,7 +4,6 @@ import ( config2 "SciencesServer/app/basic/config" "SciencesServer/config" "SciencesServer/utils" - "encoding/json" "strings" "time" ) @@ -46,19 +45,13 @@ type Images struct { } // AnalysisSlice Slice解析 -func (m *Images) AnalysisSlice(domain string) []string { - images := make([]string, 0) - utils.FromJSON(m.Images, &images) +func (m *Images) AnalysisSlice(domain string) string { + images := strings.Split(m.Images, ",") for k, v := range images { images[k] = domain + v } - return images -} - -func (m *Images) SetImagesAttribute(value []string) { - _bytes, _ := json.Marshal(value) - m.Images = string(_bytes) + return strings.Join(images, ",") } type Local struct { diff --git a/app/common/model/sys_agreement.go b/app/common/model/sys_agreement.go new file mode 100644 index 0000000..d65f3ca --- /dev/null +++ b/app/common/model/sys_agreement.go @@ -0,0 +1,30 @@ +package model + +// SysAgreement 协议数据模型 +type SysAgreement struct { + Model + ModelTenant + Title string `gorm:"column:title;type:varchar(50);default:'';comment:协议名称" json:"title"` + Content string `gorm:"column:content;type:varchar(255);default:'';comment:协议内容" json:"content"` + Status SysAgreementStatus `gorm:"column:status;type:tinyint(1);default:0;comment:状态" json:"status"` + ModelDeleted + ModelAt +} + +// SysAgreementStatus 协议数据状态 +type SysAgreementStatus int + +const ( + // SysAgreementStatusForShow 显示 + SysAgreementStatusForShow SysAgreementStatus = iota + 1 + // SysAgreementStatusForHidden 隐藏 + SysAgreementStatusForHidden +) + +func (m *SysAgreement) TableName() string { + return "sys_agreement" +} + +func NewSysAgreement() *SysAgreement { + return &SysAgreement{} +} diff --git a/router/address.go b/router/address.go index c8e6b9b..ce2af6b 100644 --- a/router/address.go +++ b/router/address.go @@ -177,17 +177,23 @@ func registerAdminAPI(app *gin.Engine) { _api := new(api1.Sys) // 导航信息 sys.POST("/banner", _api.Banner) - sys.POST("/banner/add", _api.NavigationForm) - sys.POST("/banner/edit", _api.NavigationForm) - sys.POST("/banner/delete", _api.NavigationDelete) + sys.POST("/banner/local", _api.BannerLocal) + sys.POST("/banner/add", _api.BannerForm) + sys.POST("/banner/edit", _api.BannerForm) + sys.POST("/banner/delete", _api.BannerDelete) sys.GET("/industry", _api.Industry) - sys.POST("/industry/add", _api.NavigationForm) - sys.POST("/industry/edit", _api.NavigationForm) - sys.POST("/industry/delete", _api.NavigationDelete) + sys.POST("/industry/add", _api.IndustryForm) + sys.POST("/industry/edit", _api.IndustryForm) + sys.POST("/industry/delete", _api.IndustryDelete) sys.POST("/navigation", _api.Navigation) sys.POST("/navigation/add", _api.NavigationForm) sys.POST("/navigation/edit", _api.NavigationForm) sys.POST("/navigation/delete", _api.NavigationDelete) + sys.POST("/agreement", _api.Agreement) + sys.POST("/agreement/detail", _api.AgreementDetail) + sys.POST("/agreement/add", _api.AgreementForm) + sys.POST("/agreement/edit", _api.AgreementForm) + sys.POST("/agreement/delete", _api.AgreementDelete) } // User 用户管理 user := v1.Group("/user")