Merge branch 'dev'

This commit is contained in:
wenjianzhang
2021-06-15 12:25:46 +08:00
14 changed files with 125 additions and 79 deletions
+6 -4
View File
@@ -1,6 +1,7 @@
package apis package apis
import ( import (
"fmt"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"go-admin/app/admin/models" "go-admin/app/admin/models"
"net/http" "net/http"
@@ -96,10 +97,10 @@ func (e SysOperaLog) Get(c *gin.Context) {
// @Router /api/v1/sys-opera-log [delete] // @Router /api/v1/sys-opera-log [delete]
func (e SysOperaLog) Delete(c *gin.Context) { func (e SysOperaLog) Delete(c *gin.Context) {
s := new(service.SysOperaLog) s := new(service.SysOperaLog)
req := new(dto.SysOperaLogById) req := dto.SysOperaLogById{}
err := e.MakeContext(c). err := e.MakeContext(c).
MakeOrm(). MakeOrm().
Bind(req, binding.Form). Bind(&req, binding.JSON).
MakeService(&s.Service). MakeService(&s.Service).
Errors Errors
if err != nil { if err != nil {
@@ -108,10 +109,11 @@ func (e SysOperaLog) Delete(c *gin.Context) {
return return
} }
err = s.Remove(req) err = s.Remove(&req)
if err != nil { if err != nil {
e.Logger.Error(err) e.Logger.Error(err)
e.Error(500,err, fmt.Sprintf("删除失败!错误详情:%s", err.Error()))
return return
} }
e.OK(req.GetId(), "删除成功") e.OK(req.GetId(), "删除成功")
} }
+2 -1
View File
@@ -16,10 +16,11 @@ type SysLoginLogSearch struct {
LoginLocation string `form:"loginLocation" search:"type:exact;column:login_location;table:sys_login_log" comment:"归属地"` LoginLocation string `form:"loginLocation" search:"type:exact;column:login_location;table:sys_login_log" comment:"归属地"`
BeginTime string `form:"beginTime" search:"type:gte;column:ctime;table:sys_login_log" comment:"创建时间"` BeginTime string `form:"beginTime" search:"type:gte;column:ctime;table:sys_login_log" comment:"创建时间"`
EndTime string `form:"endTime" search:"type:lte;column:ctime;table:sys_login_log" comment:"创建时间"` EndTime string `form:"endTime" search:"type:lte;column:ctime;table:sys_login_log" comment:"创建时间"`
SysLoginLogOrder
} }
type SysLoginLogOrder struct { type SysLoginLogOrder struct {
HandleOrder string `search:"type:order;column:created_at;table:sys_login_log" form:"createdAtOrder"` CreatedAtOrder string `search:"type:order;column:created_at;table:sys_login_log" form:"createdAtOrder"`
} }
func (m *SysLoginLogSearch) GetNeedSearch() interface{} { func (m *SysLoginLogSearch) GetNeedSearch() interface{} {
+5
View File
@@ -18,6 +18,11 @@ type SysOperaLogSearch struct {
Status int `form:"status" search:"type:exact;column:status;table:sys_opera_log" comment:"状态"` Status int `form:"status" search:"type:exact;column:status;table:sys_opera_log" comment:"状态"`
BeginTime string `form:"beginTime" search:"type:gte;column:ctime;table:sys_opera_log" comment:"创建时间"` BeginTime string `form:"beginTime" search:"type:gte;column:ctime;table:sys_opera_log" comment:"创建时间"`
EndTime string `form:"endTime" search:"type:lte;column:ctime;table:sys_opera_log" comment:"创建时间"` EndTime string `form:"endTime" search:"type:lte;column:ctime;table:sys_opera_log" comment:"创建时间"`
SysOperaLogOrder
}
type SysOperaLogOrder struct {
CreatedAtOrder string `search:"type:order;column:created_at;table:sys_opera_log" form:"createdAtOrder"`
} }
func (m *SysOperaLogSearch) GetNeedSearch() interface{} { func (m *SysOperaLogSearch) GetNeedSearch() interface{} {
+2 -2
View File
@@ -51,12 +51,12 @@ func (e *SysLoginLog) Get(d *dto.SysLoginLogById, model *models.SysLoginLog) err
return nil return nil
} }
// RemoveSysLoginLog 删除SysLoginLog // Remove 删除SysLoginLog
func (e *SysLoginLog) Remove(c *dto.SysLoginLogById) error { func (e *SysLoginLog) Remove(c *dto.SysLoginLogById) error {
var err error var err error
var data models.SysLoginLog var data models.SysLoginLog
db := e.Orm.Delete(&data, c.Ids) db := e.Orm.Delete(&data, c.GetId())
if db.Error != nil { if db.Error != nil {
err = db.Error err = db.Error
e.Log.Errorf("Delete error: %s", err) e.Log.Errorf("Delete error: %s", err)
+1 -1
View File
@@ -71,7 +71,7 @@ func (e *SysOperaLog) Remove(d *dto.SysOperaLogById) error {
var err error var err error
var data models.SysOperaLog var data models.SysOperaLog
db := e.Orm.Model(&data).Delete(&data, d.Ids) db := e.Orm.Model(&data).Delete(&data, d.GetId())
if err = db.Error; err != nil { if err = db.Error; err != nil {
e.Log.Errorf("Service RemoveSysOperaLog error:%s", err.Error()) e.Log.Errorf("Service RemoveSysOperaLog error:%s", err.Error())
return err return err
+4 -7
View File
@@ -32,18 +32,15 @@ func (e SysChinaAreaData) GetPage(c *gin.Context) {
return return
} }
//数据权限检查
p := actions.GetPermissionFromContext(c)
list := make([]models.SysChinaAreaData, 0) list := make([]models.SysChinaAreaData, 0)
var count int64 err = s.GetPage(&req, &list).Error
err = s.GetPage(&req, p, &list, &count)
if err != nil { if err != nil {
e.Logger.Error(err)
e.Error(http.StatusInternalServerError, err, "查询失败") e.Error(http.StatusInternalServerError, err, "查询失败")
return return
} }
e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") e.OK(list, "查询成功")
} }
func (e SysChinaAreaData) Get(c *gin.Context) { func (e SysChinaAreaData) Get(c *gin.Context) {
@@ -51,7 +48,7 @@ func (e SysChinaAreaData) Get(c *gin.Context) {
req := dto.SysChinaAreaDataById{} req := dto.SysChinaAreaDataById{}
err := e.MakeContext(c). err := e.MakeContext(c).
MakeOrm(). MakeOrm().
Bind(&req, binding.Form). Bind(&req, nil).
MakeService(&s.Service). MakeService(&s.Service).
Errors Errors
if err != nil { if err != nil {
+4 -3
View File
@@ -5,9 +5,10 @@ import (
) )
type SysChinaAreaData struct { type SysChinaAreaData struct {
models.Model Id int `json:"id" gorm:"primaryKey;comment:主键编码"`
PId string `json:"p_id" gorm:"size:11;comment:上级编码"` PId int `json:"pId" gorm:"size:11;comment:上级编码"`
Name string `json:"name" gorm:"size:128;comment:名称"` Name string `json:"name" gorm:"size:128;comment:名称"`
Children []SysChinaAreaData `json:"children,omitempty" gorm:"-"`
models.ControlBy models.ControlBy
models.ModelTime models.ModelTime
} }
+7 -8
View File
@@ -1,14 +1,13 @@
package dto package dto
import ( import (
models2 "go-admin/app/other/models" "go-admin/app/other/models"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk/api" "github.com/go-admin-team/go-admin-core/sdk/api"
"go-admin/common/dto" "go-admin/common/dto"
common "go-admin/common/models"
) )
type SysChinaAreaDataSearch struct { type SysChinaAreaDataSearch struct {
@@ -32,7 +31,7 @@ func (m *SysChinaAreaDataSearch) Bind(ctx *gin.Context) error {
type SysChinaAreaDataControl struct { type SysChinaAreaDataControl struct {
Id int `uri:"id" comment:"编码"` // 编码 Id int `uri:"id" comment:"编码"` // 编码
PId string `json:"pId" comment:"上级编码"` PId int `json:"pId" comment:"上级编码"`
Name string `json:"name" comment:"名称"` Name string `json:"name" comment:"名称"`
CreateTime time.Time `json:"createTime" comment:""` CreateTime time.Time `json:"createTime" comment:""`
UpdateTime time.Time `json:"updateTime" comment:""` UpdateTime time.Time `json:"updateTime" comment:""`
@@ -53,11 +52,11 @@ func (s *SysChinaAreaDataControl) Bind(ctx *gin.Context) error {
return err return err
} }
func (s *SysChinaAreaDataControl) Generate() (*models2.SysChinaAreaData, error) { func (s *SysChinaAreaDataControl) Generate() (*models.SysChinaAreaData, error) {
return &models2.SysChinaAreaData{ return &models.SysChinaAreaData{
Model: common.Model{Id: s.Id}, Id: s.Id,
PId: s.PId, PId: s.PId,
Name: s.Name, Name: s.Name,
}, nil }, nil
} }
+35 -11
View File
@@ -2,15 +2,15 @@ package service
import ( import (
"errors" "errors"
"go-admin/app/other/models"
"go-admin/app/other/service/dto"
common "go-admin/common/models"
"github.com/go-admin-team/go-admin-core/sdk/service" "github.com/go-admin-team/go-admin-core/sdk/service"
"gorm.io/gorm" "gorm.io/gorm"
"go-admin/app/other/models"
"go-admin/app/other/service/dto"
"go-admin/common/actions" "go-admin/common/actions"
cDto "go-admin/common/dto" cDto "go-admin/common/dto"
common "go-admin/common/models"
) )
type SysChinaAreaData struct { type SysChinaAreaData struct {
@@ -18,22 +18,46 @@ type SysChinaAreaData struct {
} }
// GetPage 获取SysChinaAreaData列表 // GetPage 获取SysChinaAreaData列表
func (e *SysChinaAreaData) GetPage(c *dto.SysChinaAreaDataSearch, p *actions.DataPermission, list *[]models.SysChinaAreaData, count *int64) error { func (e *SysChinaAreaData) GetPage(c *dto.SysChinaAreaDataSearch, list *[]models.SysChinaAreaData) *SysChinaAreaData {
var data models.SysChinaAreaData var data models.SysChinaAreaData
var areaDataList []models.SysChinaAreaData
err := e.Orm.Model(&data). err := e.Orm.Model(&data).
Scopes( Scopes(
cDto.MakeCondition(c.GetNeedSearch()), cDto.MakeCondition(c.GetNeedSearch()),
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
actions.Permission(data.TableName(), p),
). ).
Find(list).Limit(-1).Offset(-1). Find(&areaDataList).Error
Count(count).Error
if err != nil { if err != nil {
e.Log.Errorf("Service GetSysChinaAreaDataPage error:%s", err) e.Log.Errorf("Service GetSysChinaAreaDataPage error:%s", err)
return err _ = e.AddError(err)
return e
} }
return nil for i := 0; i < len(areaDataList); i++ {
if areaDataList[i].PId != 86 {
continue
}
menusInfo := areaDataCall(&areaDataList, areaDataList[i])
*list = append(*list, menusInfo)
}
return e
}
// areaDataCall 构建树
func areaDataCall(areaDataList *[]models.SysChinaAreaData, areaData models.SysChinaAreaData) models.SysChinaAreaData {
list := *areaDataList
min := make([]models.SysChinaAreaData, 0)
for j := 0; j < len(list); j++ {
if areaData.Id != list[j].PId {
continue
}
mi := models.SysChinaAreaData{}
mi = list[j]
mi.Children = []models.SysChinaAreaData{}
ms := areaDataCall(areaDataList, mi)
min = append(min, ms)
}
areaData.Children = min
return areaData
} }
// Get 获取SysChinaAreaData对象 // Get 获取SysChinaAreaData对象
@@ -1,8 +1,8 @@
package models package models
type SysChinaAreaData struct { type SysChinaAreaData struct {
Model Id int `json:"id" gorm:"primaryKey;comment:主键编码"`
PId string `json:"p_id" gorm:"size:11;comment:上级编码"` PId int `json:"p_id" gorm:"size:11;comment:上级编码"`
Name string `json:"name" gorm:"size:128;comment:名称"` Name string `json:"name" gorm:"size:128;comment:名称"`
ControlBy ControlBy
ModelTime ModelTime
@@ -16,36 +16,41 @@ func init() {
} }
func _1599190683659Tables(db *gorm.DB, version string) error { func _1599190683659Tables(db *gorm.DB, version string) error {
err := db.Debug().Migrator().AutoMigrate( return db.Transaction(func(tx *gorm.DB) error {
new(models.CasbinRule), err := tx.Debug().Migrator().AutoMigrate(
new(models.SysDept), new(models.CasbinRule),
new(models.SysConfig), new(models.SysDept),
new(models.SysTables), new(models.SysConfig),
new(models.SysColumns), new(models.SysTables),
new(models.SysMenu), new(models.SysColumns),
new(models.SysLoginLog), new(models.SysMenu),
new(models.SysOperaLog), new(models.SysLoginLog),
new(models.SysRoleDept), new(models.SysOperaLog),
new(models.SysUser), new(models.SysRoleDept),
new(models.SysRole), new(models.SysUser),
new(models.SysPost), new(models.SysRole),
new(models.DictData), new(models.SysPost),
new(models.DictType), new(models.DictData),
new(models.SysChinaAreaData), new(models.DictType),
new(models.SysJob), new(models.SysChinaAreaData),
new(models.SysConfig), new(models.SysJob),
new(models.SysSetting), new(models.SysConfig),
new(models.SysFileDir), new(models.SysSetting),
new(models.SysFileInfo), new(models.SysFileDir),
new(models.SysCategory), new(models.SysFileInfo),
new(models.SysContent), new(models.SysCategory),
new(models.SysApi), new(models.SysContent),
new(models.SysChinaAreaData), new(models.SysApi),
) new(models.SysChinaAreaData),
if err != nil { )
return err if err != nil {
} return err
return db.Create(&common.Migration{ }
Version: version, if err := models.InitDb(tx); err != nil {
}).Error
} }
return db.Create(&common.Migration{
Version: version,
}).Error
})
}
+1 -1
View File
@@ -2,7 +2,7 @@ package global
const ( const (
// Version go-admin version info // Version go-admin version info
Version = "2.0.0-beta.3" Version = "2.0.0-beta.4"
) )
var ( var (
+16 -4
View File
@@ -1,17 +1,29 @@
package common package common
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strings" "strings"
) )
func GetClientIP(ctx *gin.Context) string { func GetClientIP(c *gin.Context) string {
ip := ctx.Request.Header.Get("X-Forwarded-For") ClientIP := c.ClientIP()
fmt.Println("ClientIP:", ClientIP)
RemoteIP, _ := c.RemoteIP()
fmt.Println("RemoteIP:", RemoteIP)
ip := c.Request.Header.Get("X-Forwarded-For")
if strings.Contains(ip, "127.0.0.1") || ip == "" { if strings.Contains(ip, "127.0.0.1") || ip == "" {
ip = ctx.Request.Header.Get("X-real-ip") ip = c.Request.Header.Get("X-real-ip")
} }
if ip == "" { if ip == "" {
return "127.0.0.1" ip = "127.0.0.1"
} }
if RemoteIP.String() != "127.0.0.1" {
ip = RemoteIP.String()
}
if ClientIP != "127.0.0.1" {
ip = ClientIP
}
fmt.Println("ip:", ip)
return ip return ip
} }
+2 -2
View File
@@ -32,7 +32,7 @@ func LoggerToFile() gin.HandlerFunc {
// 处理请求 // 处理请求
var body string var body string
switch c.Request.Method { switch c.Request.Method {
case http.MethodPost, http.MethodPut, http.MethodDelete: case http.MethodPost, http.MethodPut, http.MethodGet, http.MethodDelete:
bf := bytes.NewBuffer(nil) bf := bytes.NewBuffer(nil)
wt := bufio.NewWriter(bf) wt := bufio.NewWriter(bf)
_, err := io.Copy(wt, c.Request.Body) _, err := io.Copy(wt, c.Request.Body)
@@ -94,7 +94,7 @@ func LoggerToFile() gin.HandlerFunc {
} }
log.WithFields(logData).Info() log.WithFields(logData).Info()
if c.Request.Method != "OPTIONS" && c.Request.Method != "GET" && config.LoggerConfig.EnabledDB { if c.Request.Method != "OPTIONS" && config.LoggerConfig.EnabledDB && statusCode != 404 {
SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime, body, result, statusBus) SetDBOperLog(c, clientIP, statusCode, reqUri, reqMethod, latencyTime, body, result, statusBus)
} }
} }