frist commit
This commit is contained in:
commit
2c78861ee6
1548 files changed
+507028
No files matched your search
@@ -0,0 +1,74 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/shopspring/decimal"
|
||||
orm "goadmin/database"
|
||||
_ "log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AccountRecords struct {
|
||||
Ids []int `json:"ids"`
|
||||
}
|
||||
|
||||
type Accountrecord struct {
|
||||
Id int64 `gorm:"column:id" json:"id"`
|
||||
//AccountType int `gorm:"column:account_type" json:"account_type"`
|
||||
//Type int `gorm:"column:type" json:"type"`
|
||||
//OmId int64 `gorm:"column:om_id" json:"om_id"`
|
||||
//StId int64 `gorm:"column:st_id" json:"st_id"`
|
||||
//ContractId int64 `gorm:"column:contract_id" json:"contract_id"`
|
||||
//FeeDepartmentId int `gorm:"column:fee_department_id" json:"fee_department_id"`
|
||||
//FeeDepartment string `gorm:"column:fee_department" json:"fee_department"`
|
||||
//Payer string `gorm:"column:payer" json:"payer"`
|
||||
//PayerPhone string `gorm:"column:payer_phone" json:"payer_phone"`
|
||||
//FeeType int `gorm:"column:fee_type" json:"fee_type"`
|
||||
//OgFeeAmt decimal.Decimal `gorm:"column:og_fee_amt" json:"og_fee_amt"`
|
||||
//FeeAmt decimal.Decimal `gorm:"column:fee_amt" json:"fee_amt"`
|
||||
LeftAmt decimal.Decimal `gorm:"column:left_amt" json:"left_amt"`
|
||||
Status string `gorm:"column:status" json:"status"`
|
||||
Description string `gorm:"column:description" json:"description"`
|
||||
Certificate string `gorm:"column:certificate" json:"certificate"`
|
||||
FeeBeginTime string `gorm:"column:fee_begin_time" json:"fee_begin_time"`
|
||||
FeeEndTime string `gorm:"column:fee_end_time" json:"fee_end_time"`
|
||||
PayTime string `gorm:"column:pay_time" json:"pay_time"`
|
||||
ReceiptTime string `gorm:"column:receipt_time" json:"receipt_time"`
|
||||
PayType int `gorm:"column:pay_type" json:"pay_type"`
|
||||
BankOrderNo string `gorm:"column:bank_order_no" json:"bank_order_no"`
|
||||
BillNo string `gorm:"column:bill_no" json:"bill_no"`
|
||||
Appendix string `gorm:"column:appendix" json:"appendix"`
|
||||
TransId int64 `gorm:"column:trans_id" json:"trans_id"`
|
||||
Channel int `gorm:"column:channel" json:"channel"`
|
||||
DelFlag string `gorm:"column:del_flag" json:"del_flag"`
|
||||
OverdueDays int `gorm:"column:overdue_days" json:"overdue_days"`
|
||||
ParentId int64 `gorm:"column:parent_id" json:"parent_id"`
|
||||
CreateTime string `gorm:"column:create_time" json:"create_time"`
|
||||
Creator string `gorm:"column:creator" json:"creator"`
|
||||
CreatorId int `gorm:"column:creator_id" json:"creator_id"`
|
||||
UpdateTime string `gorm:"column:update_time" json:"update_time"`
|
||||
Updater string `gorm:"column:updater" json:"updater"`
|
||||
UpdaterId int `gorm:"column:updater_id" json:"updater_id"`
|
||||
Remark string `gorm:"column:remark" json:"remark"`
|
||||
}
|
||||
|
||||
func (data *Accountrecord) Get() (dataList Accountrecord, err error) {
|
||||
if err = orm.Eloquent.Table("tb_account_record").First(&dataList, data.Id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (data *Accountrecord) GetList(ids []int) (dataList []Accountrecord, err error) {
|
||||
if err = orm.Eloquent.Table("tb_account_record").Where("id IN (?)", ids).Find(&dataList).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (data *Accountrecord) BatchUpdate(ids []int, maps map[string]interface{}) (err error) {
|
||||
data.UpdateTime = time.Now().String()
|
||||
if err = orm.Eloquent.Table("tb_account_record").Where("id IN (?)", ids).Updates(maps).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
//casbin_rule
|
||||
type CasbinRule struct {
|
||||
PType string `json:"p_type" gorm:"column:p_type"`
|
||||
V0 string `json:"v0" gorm:"column:v0"`
|
||||
V1 string `json:"v1" gorm:"column:v1"`
|
||||
V2 string `json:"v2" gorm:"column:v2"`
|
||||
V3 string `json:"v3" gorm:"column:v3"`
|
||||
V4 string `json:"v4" gorm:"column:v4"`
|
||||
V5 string `json:"v5" gorm:"column:v5"`
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
||||
//编码
|
||||
ConfigId int64 `json:"configId" gorm:"column:configId;primary_key"`
|
||||
//参数名称
|
||||
ConfigName string `json:"configName" gorm:"column:configName"`
|
||||
//参数键名
|
||||
ConfigKey string `json:"configKey" gorm:"column:configKey"`
|
||||
//参数键值
|
||||
ConfigValue string `json:"configValue" gorm:"column:configValue"`
|
||||
//是否系统内置
|
||||
ConfigType string `json:"configType" gorm:"column:configType"`
|
||||
//备注
|
||||
Remark string `json:"remark" gorm:"column:remark"`
|
||||
|
||||
CreateBy string `json:"createBy" gorm:"column:create_by"`
|
||||
CreateTime string `json:"createTime" gorm:"column:create_time"`
|
||||
UpdateBy string `json:"updateBy" gorm:"column:update_by"`
|
||||
UpdateTime string `json:"updateTime" gorm:"column:update_time"`
|
||||
DataScope string `json:"dataScope" gorm:"_"`
|
||||
Params string `json:"params" gorm:"column:params"`
|
||||
IsDel string `json:"isDel" gorm:"column:is_del"`
|
||||
}
|
||||
|
||||
// Config 创建
|
||||
func (e *Config) Create() (Config, error) {
|
||||
var doc Config
|
||||
doc.IsDel = "0"
|
||||
e.CreateTime = time.Now().String()
|
||||
result := orm.Eloquent.Table("sys_config").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
// 获取 Config
|
||||
func (e *Config) Get() (Config, error) {
|
||||
var doc Config
|
||||
|
||||
table := orm.Eloquent.Table("sys_config")
|
||||
if e.ConfigId != 0 {
|
||||
table = table.Where("configId = ?", e.ConfigId)
|
||||
}
|
||||
|
||||
if e.ConfigKey != "" {
|
||||
table = table.Where("configKey = ?", e.ConfigKey)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *Config) GetPage(pageSize int, pageIndex int) ([]Config, int32, error) {
|
||||
var doc []Config
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("sys_config")
|
||||
|
||||
if e.ConfigName != "" {
|
||||
table = table.Where("configName = ?", e.ConfigName)
|
||||
}
|
||||
if e.ConfigKey != "" {
|
||||
table = table.Where("configKey = ?", e.ConfigKey)
|
||||
}
|
||||
if e.ConfigType != "" {
|
||||
table = table.Where("configType = ?", e.ConfigType)
|
||||
}
|
||||
|
||||
// 数据权限控制
|
||||
dataPermission := new(DataPermission)
|
||||
dataPermission.UserId, _ = utils.StringToInt64(e.DataScope)
|
||||
table = dataPermission.GetDataScope("sys_config", table)
|
||||
|
||||
var count int32
|
||||
|
||||
if err := table.Where("is_del = 0").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
table.Where("is_del = 0").Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
func (e *Config) Update(id int64) (update Config, err error) {
|
||||
e.UpdateTime = utils.GetCurrntTime()
|
||||
if err = orm.Eloquent.Table("sys_config").Where("configId = ?", id).First(&update).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_config").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Config) Delete(id int64) (success bool, err error) {
|
||||
var mp = map[string]string{}
|
||||
mp["is_del"] = "1"
|
||||
mp["update_time"] = utils.GetCurrntTime()
|
||||
mp["update_by"] = e.UpdateBy
|
||||
if err = orm.Eloquent.Table("sys_config").Where("configId = ?", id).Update(mp).Error; err != nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
success = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
"goadmin/utils"
|
||||
)
|
||||
|
||||
type DataPermission struct {
|
||||
DataScope string
|
||||
UserId int64
|
||||
DeptId int64
|
||||
RoleId int64
|
||||
}
|
||||
|
||||
func (e *DataPermission) GetDataScope(tbname string, table *gorm.DB) *gorm.DB {
|
||||
SysUser := new(SysUser)
|
||||
SysRole := new(SysRole)
|
||||
SysUser.Id = e.UserId
|
||||
user, _ := SysUser.Get()
|
||||
SysRole.Id = user.RoleId
|
||||
role, _ := SysRole.Get()
|
||||
|
||||
if role.DataScope == "2" {
|
||||
table = table.Where(tbname+".create_by in (select sys_user.user_id from sys_role_dept left join sys_user on sys_user.dept_id=sys_role_dept.dept_id where sys_role_dept.role_id = ?)", user.RoleId)
|
||||
}
|
||||
if role.DataScope == "3" {
|
||||
table = table.Where(tbname+".create_by in (SELECT user_id from sys_user where dept_id = ? )", user.DeptId)
|
||||
}
|
||||
if role.DataScope == "4" {
|
||||
table = table.Where(tbname+".create_by in (SELECT user_id from sys_user where dept_id in(select deptId from sys_dept where dept_path like ? ))", "%"+utils.Int64ToString(user.DeptId)+"%")
|
||||
}
|
||||
if role.DataScope == "5" || role.DataScope == "" {
|
||||
table = table.Where(tbname+".create_by = ?", e.UserId)
|
||||
}
|
||||
|
||||
return table
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
)
|
||||
|
||||
type Dept struct {
|
||||
//部门编码
|
||||
Deptid int64 `gorm:"column:deptId;primary_key" json:"deptId" example:"1" extensions:"x-description=标示"`
|
||||
|
||||
//上级部门
|
||||
ParentId int64 `gorm:"column:parent_id" json:"parent_id"`
|
||||
|
||||
DeptPath string `gorm:"column:dept_path" json:"deptath"`
|
||||
|
||||
//部门名称
|
||||
Deptname string `gorm:"column:dept_name" json:"deptname"`
|
||||
|
||||
//排序
|
||||
Sort int64 `gorm:"column:sort" json:"sort"`
|
||||
|
||||
//负责人
|
||||
Leader string `gorm:"column:leader" json:"leader"`
|
||||
|
||||
//手机
|
||||
Phone string `gorm:"column:phone" json:"phone"`
|
||||
|
||||
//邮箱
|
||||
Email string `gorm:"column:email" json:"email"`
|
||||
|
||||
//状态
|
||||
Status string `gorm:"column:status" json:"status"`
|
||||
|
||||
CreateBy string `json:"createBy" gorm:"column:create_by"`
|
||||
CreateTime string `json:"createTime" gorm:"column:create_time"`
|
||||
UpdateBy string `json:"updateBy" gorm:"column:update_by"`
|
||||
UpdateTime string `json:"updateTime" gorm:"column:update_time"`
|
||||
DataScope string `json:"dataScope" gorm:"_"`
|
||||
Params string `json:"params" gorm:"column:params"`
|
||||
IsDel string `json:"isDel" gorm:"column:is_del"`
|
||||
|
||||
Children []Dept `json:"children"`
|
||||
}
|
||||
|
||||
type DeptLable struct {
|
||||
Id int64 `json:"id" gorm:"-"`
|
||||
Label string `json:"label" gorm:"-"`
|
||||
Children []DeptLable `json:"children" gorm:"-"`
|
||||
}
|
||||
|
||||
func (e *Dept) Create() (Dept, error) {
|
||||
var doc Dept
|
||||
e.CreateTime = utils.GetCurrntTime()
|
||||
e.IsDel = "0"
|
||||
result := orm.Eloquent.Table("sys_dept").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
deptPath := "/" + utils.Int64ToString(e.Deptid)
|
||||
if int(e.ParentId) != 0 {
|
||||
var deptP Dept
|
||||
orm.Eloquent.Table("sys_dept").Where("deptId = ?", e.ParentId).First(&deptP)
|
||||
deptPath = deptP.DeptPath + deptPath
|
||||
} else {
|
||||
deptPath = "/0" + deptPath
|
||||
}
|
||||
var mp = map[string]string{}
|
||||
mp["deptPath"] = deptPath
|
||||
if err := orm.Eloquent.Table("sys_dept").Where("deptId = ?", e.Deptid).Update(mp).Error; err != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
doc.DeptPath = deptPath
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *Dept) Get() (Dept, error) {
|
||||
var doc Dept
|
||||
|
||||
table := orm.Eloquent.Table("sys_dept")
|
||||
if e.Deptid != 0 {
|
||||
table = table.Where("deptId = ?", e.Deptid)
|
||||
}
|
||||
if e.Deptname != "" {
|
||||
table = table.Where("dept_name = ?", e.Deptname)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *Dept) GetList() ([]Dept, error) {
|
||||
var doc []Dept
|
||||
|
||||
table := orm.Eloquent.Table("sys_dept")
|
||||
if e.Deptid != 0 {
|
||||
table = table.Where("deptId = ?", e.Deptid)
|
||||
}
|
||||
if e.Deptname != "" {
|
||||
table = table.Where("dept_name = ?", e.Deptname)
|
||||
}
|
||||
if e.Status != "" {
|
||||
table = table.Where("status = ?", e.Status)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").Find(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *Dept) GetPage(bl bool) ([]Dept, error) {
|
||||
var doc []Dept
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("sys_dept")
|
||||
if e.Deptid != 0 {
|
||||
table = table.Where("deptId = ?", e.Deptid)
|
||||
}
|
||||
if e.Deptname != "" {
|
||||
table = table.Where("dept_name = ?", e.Deptname)
|
||||
}
|
||||
if e.Status != "" {
|
||||
table = table.Where("status = ?", e.Status)
|
||||
}
|
||||
if e.DeptPath != "" {
|
||||
table = table.Where("deptPath like %?%", e.DeptPath)
|
||||
}
|
||||
if bl {
|
||||
// 数据权限控制
|
||||
dataPermission := new(DataPermission)
|
||||
dataPermission.UserId, _ = utils.StringToInt64(e.DataScope)
|
||||
table = dataPermission.GetDataScope("sys_dept", table)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").Find(&doc).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *Dept) SetDept(bl bool) ([]Dept, error) {
|
||||
list, err := e.GetPage(bl)
|
||||
|
||||
m := make([]Dept, 0)
|
||||
for i := 0; i < len(list); i++ {
|
||||
if list[i].ParentId != 0 {
|
||||
continue
|
||||
}
|
||||
info := Digui(&list, list[i])
|
||||
|
||||
m = append(m, info)
|
||||
}
|
||||
return m, err
|
||||
}
|
||||
|
||||
func Digui(deptlist *[]Dept, menu Dept) Dept {
|
||||
list := *deptlist
|
||||
|
||||
min := make([]Dept, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
|
||||
if menu.Deptid != list[j].ParentId {
|
||||
continue
|
||||
}
|
||||
mi := Dept{}
|
||||
mi.Deptid = list[j].Deptid
|
||||
mi.ParentId = list[j].ParentId
|
||||
mi.DeptPath = list[j].DeptPath
|
||||
mi.Deptname = list[j].Deptname
|
||||
mi.Sort = list[j].Sort
|
||||
mi.Leader = list[j].Leader
|
||||
mi.Phone = list[j].Phone
|
||||
mi.Email = list[j].Email
|
||||
mi.Status = list[j].Status
|
||||
mi.CreateTime = list[j].CreateTime
|
||||
mi.UpdateTime = list[j].UpdateTime
|
||||
mi.IsDel = list[j].IsDel
|
||||
mi.Children = []Dept{}
|
||||
ms := Digui(deptlist, mi)
|
||||
min = append(min, ms)
|
||||
|
||||
}
|
||||
menu.Children = min
|
||||
return menu
|
||||
}
|
||||
|
||||
func (e *Dept) Update(id int64) (update Dept, err error) {
|
||||
e.UpdateTime = utils.GetCurrntTime()
|
||||
if err = orm.Eloquent.Table("sys_dept").Where("deptId = ?", id).First(&update).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
deptPath := "/" + utils.Int64ToString(e.Deptid)
|
||||
if int(e.ParentId) != 0 {
|
||||
var deptP Dept
|
||||
orm.Eloquent.Table("sys_dept").Where("deptId = ?", e.ParentId).First(&deptP)
|
||||
deptPath = deptP.DeptPath + deptPath
|
||||
} else {
|
||||
deptPath = "/0" + deptPath
|
||||
}
|
||||
e.DeptPath = deptPath
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_dept").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Dept) Delete(id int64) (success bool, err error) {
|
||||
var mp = map[string]string{}
|
||||
mp["is_del"] = "1"
|
||||
mp["update_time"] = utils.GetCurrntTime()
|
||||
mp["update_by"] = e.UpdateBy
|
||||
if err = orm.Eloquent.Table("sys_dept").Where("deptId = ?", id).Update(mp).Error; err != nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
success = true
|
||||
return
|
||||
}
|
||||
|
||||
func (dept *Dept) SetDeptLable() (m []DeptLable, err error) {
|
||||
deptlist, err := dept.GetList()
|
||||
|
||||
m = make([]DeptLable, 0)
|
||||
for i := 0; i < len(deptlist); i++ {
|
||||
if deptlist[i].ParentId != 0 {
|
||||
continue
|
||||
}
|
||||
e := DeptLable{}
|
||||
e.Id = deptlist[i].Deptid
|
||||
e.Label = deptlist[i].Deptname
|
||||
deptsInfo := DiguiDeptLable(&deptlist, e)
|
||||
|
||||
m = append(m, deptsInfo)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DiguiDeptLable(deptlist *[]Dept, dept DeptLable) DeptLable {
|
||||
list := *deptlist
|
||||
|
||||
min := make([]DeptLable, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
|
||||
if dept.Id != list[j].ParentId {
|
||||
continue
|
||||
}
|
||||
mi := DeptLable{list[j].Deptid, list[j].Deptname, []DeptLable{}}
|
||||
ms := DiguiDeptLable(deptlist, mi)
|
||||
min = append(min, ms)
|
||||
|
||||
}
|
||||
dept.Children = min
|
||||
return dept
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DictData struct {
|
||||
//字典编码
|
||||
DictCode int64 `gorm:"column:dictCode;primary_key" json:"dictCode" example:"1"`
|
||||
|
||||
//显示顺序
|
||||
DictSort int64 `gorm:"column:dictSort" json:"dictSort" example:"1"`
|
||||
|
||||
//数据标签
|
||||
DictLabel string `gorm:"column:dictLabel" json:"dictLabel"`
|
||||
|
||||
//数据键值
|
||||
DictValue string `gorm:"column:dictValue" json:"dictValue"`
|
||||
|
||||
//字典类型
|
||||
DictType string `gorm:"column:dictType" json:"dictType"`
|
||||
CssClass string `gorm:"column:cssClass" json:"cssClass"`
|
||||
ListClass string `gorm:"column:listClass" json:"listClass"`
|
||||
IsDefault string `gorm:"column:isDefault" json:"isDefault"`
|
||||
|
||||
//状态
|
||||
Status string `gorm:"column:status" json:"status"`
|
||||
Default string `gorm:"column:default" json:"default"`
|
||||
CreateBy string `gorm:"column:create_by" json:"createBy"`
|
||||
CreateTime string `gorm:"column:create_time" json:"createTime"`
|
||||
UpdateBy string `gorm:"column:update_by" json:"updateBy"`
|
||||
UpdateTime string `gorm:"column:update_time" json:"updateTime"`
|
||||
|
||||
//备注
|
||||
Remark string `gorm:"column:remark" json:"remark"`
|
||||
Params string `gorm:"column:params" json:"params"`
|
||||
DataScope string `gorm:"column:dataScope" json:"dataScope"`
|
||||
IsDel string `gorm:"column:is_del" json:"isDel"`
|
||||
}
|
||||
|
||||
func (e *DictData) Create() (DictData, error) {
|
||||
var doc DictData
|
||||
e.CreateTime = time.Now().String()
|
||||
result := orm.Eloquent.Table("sys_dict_data").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *DictData) GetByCode() (DictData, error) {
|
||||
var doc DictData
|
||||
|
||||
table := orm.Eloquent.Table("sys_dict_data")
|
||||
if e.DictCode != 0 {
|
||||
table = table.Where("dictCode = ?", e.DictCode)
|
||||
}
|
||||
if e.DictLabel != "" {
|
||||
table = table.Where("dictLabel = ?", e.DictLabel)
|
||||
}
|
||||
if e.DictType != "" {
|
||||
table = table.Where("dictType = ?", e.DictType)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *DictData) Get() ([]DictData, error) {
|
||||
var doc []DictData
|
||||
|
||||
table := orm.Eloquent.Table("sys_dict_data")
|
||||
if e.DictCode != 0 {
|
||||
table = table.Where("dictCode = ?", e.DictCode)
|
||||
}
|
||||
if e.DictLabel != "" {
|
||||
table = table.Where("dictLabel = ?", e.DictLabel)
|
||||
}
|
||||
if e.DictType != "" {
|
||||
table = table.Where("dictType = ?", e.DictType)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").Find(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *DictData) GetPage(pageSize int, pageIndex int) ([]DictData, int32, error) {
|
||||
var doc []DictData
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("sys_dict_data")
|
||||
if e.DictCode != 0 {
|
||||
table = table.Where("dictCode = ?", e.DictCode)
|
||||
}
|
||||
if e.DictType != "" {
|
||||
table = table.Where("dictType = ?", e.DictType)
|
||||
}
|
||||
if e.DictLabel != "" {
|
||||
table = table.Where("dictLabel = ?", e.DictLabel)
|
||||
}
|
||||
if e.Status != "" {
|
||||
table = table.Where("status = ?", e.Status)
|
||||
}
|
||||
|
||||
// 数据权限控制
|
||||
dataPermission := new(DataPermission)
|
||||
dataPermission.UserId, _ = utils.StringToInt64(e.DataScope)
|
||||
table = dataPermission.GetDataScope("sys_dict_data", table)
|
||||
|
||||
var count int32
|
||||
|
||||
if err := table.Where("is_del = 0").Order("dictSort").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
table.Where("is_del = 0").Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
func (e *DictData) Update(id int64) (update DictData, err error) {
|
||||
e.UpdateTime = time.Now().String()
|
||||
if err = orm.Eloquent.Table("sys_dict_data").Where("dictCode = ?", id).First(&update).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_dict_data").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *DictData) Delete(id int64) (success bool, err error) {
|
||||
var mp = map[string]string{}
|
||||
mp["is_del"] = "1"
|
||||
mp["update_time"] = utils.GetCurrntTime()
|
||||
mp["update_by"] = e.UpdateBy
|
||||
if err = orm.Eloquent.Table("sys_dict_data").Where("dictCode = ?", id).Update(mp).Error; err != nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
success = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DictType struct {
|
||||
DictId int64 `gorm:"column:dictId;primary_key" json:"dictId" example:"1"`
|
||||
//字典名称
|
||||
DictName string `gorm:"column:dictName" json:"dictName"`
|
||||
//字典类型
|
||||
DictType string `gorm:"column:dictType" json:"dictType"`
|
||||
//状态
|
||||
Status string `gorm:"column:status" json:"status"`
|
||||
|
||||
DataScope string `gorm:"_" json:"dataScope"`
|
||||
|
||||
Params string `gorm:"column:params" json:"params"`
|
||||
//创建者
|
||||
CreateBy string `gorm:"column:create_by" json:"createBy"`
|
||||
//创建时间
|
||||
CreateTime string `gorm:"column:create_time" json:"createTime"`
|
||||
//更新者
|
||||
UpdateBy string `gorm:"column:update_by" json:"updateBy"`
|
||||
//更新时间
|
||||
UpdateTime string `gorm:"column:update_time" json:"updateTime"`
|
||||
//备注
|
||||
Remark string `gorm:"column:remark" json:"remark"`
|
||||
IsDel string `gorm:"column:is_del" json:"isDel"`
|
||||
}
|
||||
|
||||
func (e *DictType) Create() (DictType, error) {
|
||||
var doc DictType
|
||||
e.CreateTime = time.Now().String()
|
||||
result := orm.Eloquent.Table("sys_dict_type").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *DictType) Get() (DictType, error) {
|
||||
var doc DictType
|
||||
|
||||
table := orm.Eloquent.Table("sys_dict_type")
|
||||
if e.DictId != 0 {
|
||||
table = table.Where("dictId = ?", e.DictId)
|
||||
}
|
||||
if e.DictName != "" {
|
||||
table = table.Where("dictName = ?", e.DictName)
|
||||
}
|
||||
if e.DictType != "" {
|
||||
table = table.Where("dictType = ?", e.DictType)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *DictType) GetPage(pageSize int, pageIndex int) ([]DictType, int32, error) {
|
||||
var doc []DictType
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("sys_dict_type")
|
||||
if e.DictId != 0 {
|
||||
table = table.Where("dictId = ?", e.DictId)
|
||||
}
|
||||
if e.DictName != "" {
|
||||
table = table.Where("dictName = ?", e.DictName)
|
||||
}
|
||||
|
||||
// 数据权限控制
|
||||
dataPermission := new(DataPermission)
|
||||
dataPermission.UserId, _ = utils.StringToInt64(e.DataScope)
|
||||
table = dataPermission.GetDataScope("sys_dict_type", table)
|
||||
|
||||
var count int32
|
||||
|
||||
if err := table.Where("is_del = 0").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
table.Where("is_del = 0").Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
func (e *DictType) Update(id int64) (update DictType, err error) {
|
||||
e.UpdateTime = time.Now().String()
|
||||
if err = orm.Eloquent.Table("sys_dict_type").First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_dict_type").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *DictType) Delete(id int64) (success bool, err error) {
|
||||
var mp = map[string]string{}
|
||||
mp["is_del"] = "1"
|
||||
mp["update_time"] = utils.GetCurrntTime()
|
||||
mp["update_by"] = e.UpdateBy
|
||||
if err = orm.Eloquent.Table("sys_dict_type").Where("dictId = ?", id).Update(mp).Error; err != nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
success = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
orm "goadmin/database"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func InitDb() error {
|
||||
sql, err := Ioutil("config/db.sql")
|
||||
if err != nil {
|
||||
fmt.Println("数据库初始化脚本读取失败!原因:", err.Error())
|
||||
return err
|
||||
}
|
||||
sqlList := strings.Split(sql, ";")
|
||||
for i := 0; i < len(sqlList); i++ {
|
||||
if strings.Contains(sqlList[i], "--") {
|
||||
fmt.Println(sqlList[i])
|
||||
continue
|
||||
}
|
||||
sql := strings.Replace(sqlList[i]+";", "\n", "", 0)
|
||||
if err = orm.Eloquent.Exec(sql).Error; err != nil {
|
||||
if !strings.Contains(err.Error(), "Query was empty") {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Ioutil(name string) (string, error) {
|
||||
if contents, err := ioutil.ReadFile(name); err == nil {
|
||||
//因为contents是[]byte类型,直接转换成string类型后会多一行空格,需要使用strings.Replace替换换行符
|
||||
result := strings.Replace(string(contents), "\n", "", 1)
|
||||
fmt.Println("Use ioutil.ReadFile to read a file:", result)
|
||||
return result, nil
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
orm "goadmin/database"
|
||||
"goadmin/pkg"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//var IdentityKey = "id"
|
||||
|
||||
type Login struct {
|
||||
Username string `form:"UserName" json:"username" binding:"required"`
|
||||
Password string `form:"Password" json:"password" binding:"required"`
|
||||
Code string `form:"Code" json:"code" binding:"required"`
|
||||
UUID string `form:"UUID" json:"uuid" binding:"required"`
|
||||
}
|
||||
|
||||
func (u *Login) GetUser() (user SysUser, role SysRole, e error) {
|
||||
|
||||
e = orm.Eloquent.Table("sys_user").Where("UserName = ? ", u.Username).Find(&user).Error
|
||||
if e != nil {
|
||||
if strings.Contains(e.Error(), "record not found") {
|
||||
pkg.AssertErr(e, "账号或密码错误(代码204)", 500)
|
||||
}
|
||||
log.Print(e)
|
||||
return
|
||||
}
|
||||
ss, _ := pkg.Encrypt("ceshi")
|
||||
print(string(ss))
|
||||
_, e = pkg.CompareHashAndPassword(user.Password, u.Password)
|
||||
if e != nil {
|
||||
if strings.Contains(e.Error(), "hashedPassword is not the hash of the given password") {
|
||||
pkg.AssertErr(e, "账号或密码错误(代码201)", 500)
|
||||
}
|
||||
log.Print(e)
|
||||
return
|
||||
}
|
||||
e = orm.Eloquent.Table("sys_role").Where("id = ? ", user.RoleId).First(&role).Error
|
||||
if e != nil {
|
||||
log.Print(e.Error())
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LoginLog struct {
|
||||
//主键
|
||||
InfoId int64 `json:"infoId" gorm:"column:infoId;primary_key"`
|
||||
//用户名
|
||||
UserName string `json:"userName" gorm:"column:userName;"`
|
||||
//状态
|
||||
Status string `json:"status" gorm:"column:status;"`
|
||||
//ip地址
|
||||
Ipaddr string `json:"ipaddr" gorm:"column:ipaddr;"`
|
||||
//归属地
|
||||
LoginLocation string `json:"loginLocation" gorm:"column:loginLocation;"`
|
||||
//浏览器
|
||||
Browser string `json:"browser" gorm:"column:browser;"`
|
||||
//系统
|
||||
Os string `json:"os" gorm:"column:os;"`
|
||||
// 固件
|
||||
Platform string `json:"platform" gorm:"column:platform;"`
|
||||
//登录时间
|
||||
LoginTime string `json:"loginTime" gorm:"column:loginTime;"`
|
||||
//创建人
|
||||
CreateBy string `json:"createBy" gorm:"column:create_by;"`
|
||||
//创建时间
|
||||
CreateTime string `json:"createTime" gorm:"column:create_time;"`
|
||||
//更新者
|
||||
UpdateBy string `json:"updateBy" gorm:"column:update_by;"`
|
||||
//更新时间
|
||||
UpdateTime string `json:"updateTime" gorm:"column:update_time;"`
|
||||
//数据
|
||||
DataScope string `json:"dataScope" gorm:"column:dataScope;"`
|
||||
//参数
|
||||
Params string `json:"params" gorm:"column:params;"`
|
||||
//备注
|
||||
Remark string `json:"remark" gorm:"column:remark;"`
|
||||
//是否删除
|
||||
IsDel string `json:"isDel" gorm:"column:is_del;"`
|
||||
Msg string `json:"msg" gorm:"column:msg;"`
|
||||
}
|
||||
|
||||
func (e *LoginLog) Get() (LoginLog, error) {
|
||||
var doc LoginLog
|
||||
|
||||
table := orm.Eloquent.Table("sys_loginlog")
|
||||
if e.Ipaddr != "" {
|
||||
table = table.Where("ipaddr = ?", e.Ipaddr)
|
||||
}
|
||||
if e.InfoId != 0 {
|
||||
table = table.Where("infoId = ?", e.InfoId)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *LoginLog) GetPage(pageSize int, pageIndex int) ([]LoginLog, int32, error) {
|
||||
var doc []LoginLog
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("sys_loginlog")
|
||||
if e.Ipaddr != "" {
|
||||
table = table.Where("ipaddr = ?", e.Ipaddr)
|
||||
}
|
||||
if e.Status != "" {
|
||||
table = table.Where("status = ?", e.Status)
|
||||
}
|
||||
if e.UserName != "" {
|
||||
table = table.Where("userName = ?", e.UserName)
|
||||
}
|
||||
|
||||
var count int32
|
||||
|
||||
if err := table.Where("is_del = 0").Order("infoId desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
table.Where("is_del = 0").Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
func (e *LoginLog) Create() (LoginLog, error) {
|
||||
var doc LoginLog
|
||||
e.CreateTime = utils.GetCurrntTime()
|
||||
e.CreateBy = "0"
|
||||
e.UpdateBy = "0"
|
||||
result := orm.Eloquent.Table("sys_loginlog").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *LoginLog) Update(id int64) (update LoginLog, err error) {
|
||||
e.UpdateTime = time.Now().String()
|
||||
|
||||
if err = orm.Eloquent.Table("sys_loginlog").First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_loginlog").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *LoginLog) BatchDelete(id []int64) (Result bool, err error) {
|
||||
if err = orm.Eloquent.Table("sys_loginlog").Where("is_del=0 and infoId in (?)", id).Update(map[string]interface{}{"is_del": "1", "update_time": time.Now(), "update_by": e.UpdateBy}).Error; err != nil {
|
||||
return
|
||||
}
|
||||
Result = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Menu struct {
|
||||
MenuId int64 `json:"menuId" gorm:"column:menuId;primary_key"`
|
||||
MenuName string `json:"menuName" gorm:"column:menuName"`
|
||||
Title string `json:"title" gorm:"column:title"`
|
||||
Icon string `json:"icon" gorm:"column:icon"`
|
||||
Path string `json:"path" gorm:"column:path"`
|
||||
Paths string `json:"paths" gorm:"column:paths"`
|
||||
MenuType string `json:"menuType" gorm:"column:menuType"`
|
||||
Action string `json:"action" gorm:"column:action"`
|
||||
Permission string `json:"permission" gorm:"column:permission"`
|
||||
ParentId int64 `json:"parentId" gorm:"column:parentId"`
|
||||
NoCache bool `json:"noCache" gorm:"column:noCache"`
|
||||
Breadcrumb string `json:"breadcrumb" gorm:"column:breadcrumb"`
|
||||
Component string `json:"component" gorm:"column:component"`
|
||||
Sort int `json:"sort" gorm:"column:sort"`
|
||||
|
||||
Visible string `json:"visible" gorm:"column:visible"`
|
||||
Children []Menu `json:"children"`
|
||||
IsSelect bool `json:"is_select" gorm:"-"`
|
||||
RoleId int64 `gorm:"-"`
|
||||
|
||||
CreateBy string `json:"createBy" gorm:"column:create_by"`
|
||||
CreateTime string `json:"createTime" gorm:"column:create_time"`
|
||||
UpdateBy string `json:"updateBy" gorm:"column:update_by"`
|
||||
UpdateTime string `json:"updateTime" gorm:"column:update_time"`
|
||||
DataScope string `json:"dataScope" gorm:"-"`
|
||||
Params string `json:"params" gorm:"-"`
|
||||
IsDel string `json:"isDel" gorm:"column:is_del"`
|
||||
}
|
||||
type MenuLable struct {
|
||||
Id int64 `json:"id" gorm:"-"`
|
||||
Label string `json:"label" gorm:"-"`
|
||||
Children []MenuLable `json:"children" gorm:"-"`
|
||||
}
|
||||
|
||||
type Menus struct {
|
||||
MenuId int64 `json:"menuId" gorm:"column:menuId;primary_key"`
|
||||
MenuName string `json:"menuName" gorm:"column:menuName"`
|
||||
Title string `json:"title" gorm:"column:title"`
|
||||
Icon string `json:"icon" gorm:"column:icon"`
|
||||
Path string `json:"path" gorm:"column:path"`
|
||||
MenuType string `json:"menuType" gorm:"column:menuType"`
|
||||
Action string `json:"action" gorm:"column:action"`
|
||||
Permission string `json:"permission" gorm:"column:permission"`
|
||||
ParentId int64 `json:"parentId" gorm:"column:parentId"`
|
||||
NoCache bool `json:"noCache" gorm:"column:noCache"`
|
||||
Breadcrumb string `json:"breadcrumb" gorm:"column:breadcrumb"`
|
||||
Component string `json:"component" gorm:"column:component"`
|
||||
Sort int `json:"sort" gorm:"column:sort"`
|
||||
|
||||
Visible string `json:"visible" gorm:"column:visible"`
|
||||
Children []Menu `json:"children"`
|
||||
|
||||
CreateBy string `json:"createBy" gorm:"column:create_by"`
|
||||
CreateTime string `json:"createTime" gorm:"column:create_time"`
|
||||
UpdateBy string `json:"updateBy" gorm:"column:update_by"`
|
||||
UpdateTime string `json:"updateTime" gorm:"column:update_time"`
|
||||
DataScope string `json:"dataScope" gorm:"-"`
|
||||
Params string `json:"params" gorm:"-"`
|
||||
IsDel string `json:"isDel" gorm:"column:is_del"`
|
||||
}
|
||||
|
||||
type MenuRole struct {
|
||||
Menus
|
||||
IsSelect bool `json:"is_select" gorm:"-"`
|
||||
}
|
||||
|
||||
type MS []Menu
|
||||
|
||||
func (e *Menu) GetByMenuId() (Menu Menu, err error) {
|
||||
|
||||
table := orm.Eloquent.Table("sys_menu")
|
||||
table = table.Where("is_del = ?", 0)
|
||||
table = table.Where("menuId = ?", e.MenuId)
|
||||
if err = table.Find(&Menu).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Menu) SetMenu() (m []Menu, err error) {
|
||||
menulist, err := e.GetPage()
|
||||
|
||||
m = make([]Menu, 0)
|
||||
for i := 0; i < len(menulist); i++ {
|
||||
if menulist[i].ParentId != 0 {
|
||||
continue
|
||||
}
|
||||
menusInfo := DiguiMenu(&menulist, menulist[i])
|
||||
|
||||
m = append(m, menusInfo)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DiguiMenu(menulist *[]Menu, menu Menu) Menu {
|
||||
list := *menulist
|
||||
|
||||
min := make([]Menu, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
|
||||
if menu.MenuId != list[j].ParentId {
|
||||
continue
|
||||
}
|
||||
mi := Menu{}
|
||||
mi.MenuId = list[j].MenuId
|
||||
mi.MenuName = list[j].MenuName
|
||||
mi.Title = list[j].Title
|
||||
mi.Icon = list[j].Icon
|
||||
mi.Path = list[j].Path
|
||||
mi.MenuType = list[j].MenuType
|
||||
mi.Action = list[j].Action
|
||||
mi.Permission = list[j].Permission
|
||||
mi.ParentId = list[j].ParentId
|
||||
mi.NoCache = list[j].NoCache
|
||||
mi.Breadcrumb = list[j].Breadcrumb
|
||||
mi.Component = list[j].Component
|
||||
mi.Sort = list[j].Sort
|
||||
mi.CreateTime = list[j].CreateTime
|
||||
mi.UpdateTime = list[j].UpdateTime
|
||||
mi.IsDel = list[j].IsDel
|
||||
mi.Visible = list[j].Visible
|
||||
mi.Children = []Menu{}
|
||||
|
||||
if mi.MenuType != "F" {
|
||||
ms := DiguiMenu(menulist, mi)
|
||||
min = append(min, ms)
|
||||
|
||||
} else {
|
||||
min = append(min, mi)
|
||||
}
|
||||
|
||||
}
|
||||
menu.Children = min
|
||||
return menu
|
||||
}
|
||||
|
||||
func (e *Menu) SetMenuLable() (m []MenuLable, err error) {
|
||||
menulist, err := e.Get()
|
||||
|
||||
m = make([]MenuLable, 0)
|
||||
for i := 0; i < len(menulist); i++ {
|
||||
if menulist[i].ParentId != 0 {
|
||||
continue
|
||||
}
|
||||
e := MenuLable{}
|
||||
e.Id = menulist[i].MenuId
|
||||
e.Label = menulist[i].Title
|
||||
menusInfo := DiguiMenuLable(&menulist, e)
|
||||
|
||||
m = append(m, menusInfo)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func DiguiMenuLable(menulist *[]Menu, menu MenuLable) MenuLable {
|
||||
list := *menulist
|
||||
|
||||
min := make([]MenuLable, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
|
||||
if menu.Id != list[j].ParentId {
|
||||
continue
|
||||
}
|
||||
mi := MenuLable{}
|
||||
mi.Id = list[j].MenuId
|
||||
mi.Label = list[j].Title
|
||||
mi.Children = []MenuLable{}
|
||||
if list[j].MenuType != "F" {
|
||||
ms := DiguiMenuLable(menulist, mi)
|
||||
min = append(min, ms)
|
||||
} else {
|
||||
min = append(min, mi)
|
||||
}
|
||||
|
||||
}
|
||||
menu.Children = min
|
||||
return menu
|
||||
}
|
||||
|
||||
func (e *Menu) SetMenuRole(rolename string) (m []Menu, err error) {
|
||||
|
||||
menulist, err := e.GetByRoleName(rolename)
|
||||
|
||||
m = make([]Menu, 0)
|
||||
for i := 0; i < len(menulist); i++ {
|
||||
if menulist[i].ParentId != 0 {
|
||||
continue
|
||||
}
|
||||
menusInfo := DiguiMenu(&menulist, menulist[i])
|
||||
|
||||
m = append(m, menusInfo)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (menu *MenuRole) Get() (Menus []MenuRole, err error) {
|
||||
table := orm.Eloquent.Table("sys_menu")
|
||||
table = table.Where("is_del = ?", 0)
|
||||
if menu.MenuName != "" {
|
||||
table = table.Where("menuName = ?", menu.MenuName)
|
||||
}
|
||||
if err = table.Find(&Menus).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Menu) GetByRoleName(rolename string) (Menus []Menu, err error) {
|
||||
table := orm.Eloquent.Table("sys_menu").Select("sys_menu.*").Joins("left join sys_role_menu on sys_role_menu.menu_id=sys_menu.menuId")
|
||||
table = table.Where("is_del = ? and sys_role_menu.role_name=? and menuType in ('M','C')", 0, rolename)
|
||||
if err = table.Find(&Menus).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Menu) Get() (Menus []Menu, err error) {
|
||||
table := orm.Eloquent.Table("sys_menu")
|
||||
table = table.Where("is_del = ?", 0)
|
||||
if e.MenuName != "" {
|
||||
table = table.Where("menuName = ?", e.MenuName)
|
||||
}
|
||||
if e.MenuType != "" {
|
||||
table = table.Where("menuType = ?", e.MenuType)
|
||||
}
|
||||
|
||||
if err = table.Find(&Menus).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Menu) GetPage() (Menus []Menu, err error) {
|
||||
table := orm.Eloquent.Table("sys_menu")
|
||||
table = table.Where("is_del = ?", 0)
|
||||
if e.MenuName != "" {
|
||||
table = table.Where("menuName = ?", e.MenuName)
|
||||
}
|
||||
if e.MenuType != "" {
|
||||
table = table.Where("menuType = ?", e.MenuType)
|
||||
}
|
||||
if e.Visible != "" {
|
||||
table = table.Where("visible = ?", e.Visible)
|
||||
}
|
||||
|
||||
// 数据权限控制
|
||||
dataPermission := new(DataPermission)
|
||||
dataPermission.UserId, _ = utils.StringToInt64(e.DataScope)
|
||||
table = dataPermission.GetDataScope("sys_menu", table)
|
||||
|
||||
if err = table.Find(&Menus).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Menu) Create() (id int64, err error) {
|
||||
e.CreateTime = utils.GetCurrntTime()
|
||||
e.IsDel = "0"
|
||||
result := orm.Eloquent.Table("sys_menu").Create(&e)
|
||||
if result.Error != nil {
|
||||
err = result.Error
|
||||
return
|
||||
}
|
||||
err = InitPaths(e)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id = e.MenuId
|
||||
return
|
||||
}
|
||||
|
||||
func InitPaths(menu *Menu) (err error) {
|
||||
parentMenu := new(Menu)
|
||||
if int(menu.ParentId) != 0 {
|
||||
orm.Eloquent.Table("sys_menu").Where("menuId = ?", menu.ParentId).First(parentMenu)
|
||||
if parentMenu.Paths == "" {
|
||||
err = errors.New("父级paths异常,请尝试对当前节点父级菜单进行更新操作!")
|
||||
return
|
||||
}
|
||||
menu.Paths = parentMenu.Paths + "/" + utils.Int64ToString(menu.MenuId)
|
||||
} else {
|
||||
menu.Paths = "/0/" + utils.Int64ToString(menu.MenuId)
|
||||
}
|
||||
orm.Eloquent.Table("sys_menu").Where("menuId = ?", menu.MenuId).Update("paths", menu.Paths)
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Menu) Update(id int64) (update Menu, err error) {
|
||||
e.UpdateTime = time.Now().String()
|
||||
if err = orm.Eloquent.Table("sys_menu").First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_menu").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
err = InitPaths(e)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Menu) Delete(id int64) (success bool, err error) {
|
||||
var mp = map[string]string{}
|
||||
mp["is_del"] = "1"
|
||||
mp["update_time"] = utils.GetCurrntTime()
|
||||
mp["update_by"] = e.UpdateBy
|
||||
if err = orm.Eloquent.Table("sys_menu").Where("menuId = ?", id).Update(mp).Error; err != nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
success = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
)
|
||||
|
||||
//sys_operlog
|
||||
type SysOperLog struct {
|
||||
|
||||
//日志编码
|
||||
OperId int64 `json:"operId" gorm:"column:operId;primary_key"`
|
||||
//操作模块
|
||||
Title string `json:"title" gorm:"column:title;"`
|
||||
//操作类型
|
||||
BusinessType string `json:"businessType" gorm:"column:businessType;"`
|
||||
BusinessTypes string `json:"businessTypes" gorm:"column:businessTypes;"`
|
||||
//函数
|
||||
Method string `json:"method" gorm:"column:method;"`
|
||||
//请求方式
|
||||
RequestMethod string `json:"requestMethod" gorm:"column:requestMethod;"`
|
||||
//操作类型
|
||||
OperatorType string `json:"operatorType" gorm:"column:operatorType;"`
|
||||
//操作者
|
||||
OperName string `json:"operName" gorm:"column:operName;"`
|
||||
//部门名称
|
||||
DeptName string `json:"deptName" gorm:"column:deptName;"`
|
||||
//访问地址
|
||||
OperUrl string `json:"operUrl" gorm:"column:operUrl;"`
|
||||
//客户端ip
|
||||
OperIp string `json:"operIp" gorm:"column:operIp;"`
|
||||
//访问位置
|
||||
OperLocation string `json:"operLocation" gorm:"column:operLocation;"`
|
||||
//请求参数
|
||||
OperParam string `json:"operParam" gorm:"column:operParam;"`
|
||||
//操作状态
|
||||
Status string `json:"status" gorm:"column:status;"`
|
||||
//操作时间
|
||||
OperTime string `json:"operTime" gorm:"column:operTime;"`
|
||||
//返回数据
|
||||
JsonResult string `json:"jsonResult" gorm:"column:jsonResult;"`
|
||||
//创建人
|
||||
CreateBy string `json:"createBy" gorm:"column:create_by;"`
|
||||
//创建时间
|
||||
CreateTime string `json:"createTime" gorm:"column:create_time;"`
|
||||
//更新者
|
||||
UpdateBy string `json:"updateBy" gorm:"column:update_by;"`
|
||||
//更新时间
|
||||
UpdateTime string `json:"updateTime" gorm:"column:update_time;"`
|
||||
//数据
|
||||
DataScope string `json:"dataScope" gorm:"column:data_scope;"`
|
||||
//参数
|
||||
Params string `json:"params" gorm:"column:params;"`
|
||||
//备注
|
||||
Remark string `json:"remark" gorm:"column:remark;"`
|
||||
//是否删除
|
||||
IsDel string `json:"isDel" gorm:"column:is_del;"`
|
||||
//耗时
|
||||
LatencyTime string `json:"latencyime" gorm:"column:latency_time;"`
|
||||
//us
|
||||
UserAgent string `json:"userAgent" gorm:"column:user_agent;"`
|
||||
}
|
||||
|
||||
func (e *SysOperLog) Get() (SysOperLog, error) {
|
||||
var doc SysOperLog
|
||||
|
||||
table := orm.Eloquent.Table("sys_operlog")
|
||||
if e.OperIp != "" {
|
||||
table = table.Where("operIp = ?", e.OperIp)
|
||||
}
|
||||
if e.OperId != 0 {
|
||||
table = table.Where("operId = ?", e.OperId)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysOperLog) GetPage(pageSize int, pageIndex int) ([]SysOperLog, int32, error) {
|
||||
var doc []SysOperLog
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("sys_operlog")
|
||||
if e.OperIp != "" {
|
||||
table = table.Where("operIp = ?", e.OperIp)
|
||||
}
|
||||
if e.Status != "" {
|
||||
table = table.Where("status = ?", e.Status)
|
||||
}
|
||||
if e.OperName != "" {
|
||||
table = table.Where("operName = ?", e.OperName)
|
||||
}
|
||||
if e.BusinessType != "" {
|
||||
table = table.Where("businessType = ?", e.BusinessType)
|
||||
}
|
||||
|
||||
var count int32
|
||||
|
||||
if err := table.Where("is_del = 0").Order("operId desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
table.Where("is_del = 0").Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
func (e *SysOperLog) Create() (SysOperLog, error) {
|
||||
var doc SysOperLog
|
||||
e.CreateTime = utils.GetCurrntTime()
|
||||
e.CreateBy = "0"
|
||||
e.UpdateBy = "0"
|
||||
result := orm.Eloquent.Table("sys_operlog").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysOperLog) Update(id int64) (update SysOperLog, err error) {
|
||||
e.UpdateTime = utils.GetCurrntTime()
|
||||
|
||||
if err = orm.Eloquent.Table("sys_operlog").First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_operlog").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *SysOperLog) BatchDelete(id []int64) (Result bool, err error) {
|
||||
if err = orm.Eloquent.Table("sys_operlog").Where("is_del=0 and operId in (?)", id).Update(map[string]interface{}{"is_del": "1", "update_time": utils.GetCurrntTime(), "update_by": e.UpdateBy}).Error; err != nil {
|
||||
return
|
||||
}
|
||||
Result = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
)
|
||||
|
||||
type Post struct {
|
||||
//岗位编号
|
||||
PostId int64 `gorm:"column:postId;primary_key" json:"postId" example:"1" extensions:"x-description=标示"`
|
||||
|
||||
//岗位名称
|
||||
PostName string `gorm:"column:postName" json:"postName"`
|
||||
|
||||
//岗位代码
|
||||
PostCode string `gorm:"column:postCode" json:"postCode"`
|
||||
|
||||
//岗位排序
|
||||
Sort int `gorm:"column:sort" json:"sort"`
|
||||
|
||||
//状态
|
||||
Status string `gorm:"column:status" json:"status"`
|
||||
|
||||
//描述
|
||||
Remark string `gorm:"column:remark" json:"remark"`
|
||||
|
||||
//创建时间
|
||||
CreateTime string `gorm:"column:create_time" json:"createTime"`
|
||||
|
||||
//最后修改时间
|
||||
UpdateTime string `gorm:"column:update_time" json:"updateTime"`
|
||||
|
||||
//是否删除
|
||||
IsDel int64 `gorm:"column:is_del" json:"isDel"`
|
||||
|
||||
CreateBy string `gorm:"column:create_by" json:"createBy"`
|
||||
|
||||
UpdateBy string `gorm:"column:update_by" json:"updateBy"`
|
||||
|
||||
DataScope string `gorm:"-" json:"dataScope"`
|
||||
|
||||
Params string `gorm:"column:params" json:"params"`
|
||||
}
|
||||
|
||||
func (e *Post) Create() (Post, error) {
|
||||
var doc Post
|
||||
e.CreateTime = utils.GetCurrntTime()
|
||||
result := orm.Eloquent.Table("sys_post").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *Post) Get() (Post, error) {
|
||||
var doc Post
|
||||
|
||||
table := orm.Eloquent.Table("sys_post")
|
||||
if e.PostId != 0 {
|
||||
table = table.Where("postId = ?", e.PostId)
|
||||
}
|
||||
if e.PostName != "" {
|
||||
table = table.Where("postName = ?", e.PostName)
|
||||
}
|
||||
if e.PostCode != "" {
|
||||
table = table.Where("postCode = ?", e.PostCode)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *Post) GetList() ([]Post, error) {
|
||||
var doc []Post
|
||||
|
||||
table := orm.Eloquent.Table("sys_post")
|
||||
if e.PostId != 0 {
|
||||
table = table.Where("postId = ?", e.PostId)
|
||||
}
|
||||
if e.PostName != "" {
|
||||
table = table.Where("postName = ?", e.PostName)
|
||||
}
|
||||
if e.PostCode != "" {
|
||||
table = table.Where("postCode = ?", e.PostCode)
|
||||
}
|
||||
|
||||
if err := table.Where("is_del = 0").Find(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *Post) GetPage(pageSize int, pageIndex int) ([]Post, int32, error) {
|
||||
var doc []Post
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("sys_post")
|
||||
if e.PostId != 0 {
|
||||
table = table.Where("postId = ?", e.PostId)
|
||||
}
|
||||
if e.PostName != "" {
|
||||
table = table.Where("postName = ?", e.PostName)
|
||||
}
|
||||
|
||||
// 数据权限控制
|
||||
dataPermission := new(DataPermission)
|
||||
dataPermission.UserId, _ = utils.StringToInt64(e.DataScope)
|
||||
table = dataPermission.GetDataScope("sys_post", table)
|
||||
|
||||
var count int32
|
||||
|
||||
if err := table.Where("is_del = 0").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
table.Where("is_del = 0").Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
func (e *Post) Update(id int64) (update Post, err error) {
|
||||
e.UpdateTime = utils.GetCurrntTime()
|
||||
if err = orm.Eloquent.Table("sys_post").First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_post").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Post) Delete(id int64) (success bool, err error) {
|
||||
var mp = map[string]string{}
|
||||
mp["is_del"] = "1"
|
||||
mp["update_time"] = utils.GetCurrntTime()
|
||||
mp["update_by"] = e.UpdateBy
|
||||
if err = orm.Eloquent.Table("sys_post").Where("postId = ?", id).Update(mp).Error; err != nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
success = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package models
|
||||
|
||||
type Response struct {
|
||||
// 代码
|
||||
Code int `json:"code" example:"200,500,400,401"`
|
||||
// 数据集
|
||||
Data interface{} `json:"data"`
|
||||
// 错误消息
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func (res *Response) ReturnOK() *Response {
|
||||
res.Code = 200
|
||||
return res
|
||||
}
|
||||
|
||||
func (res *Response) ReturnError(code int) *Response {
|
||||
res.Code = code
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SysRole struct {
|
||||
// 角色编码
|
||||
Id int64 `json:"roleId" gorm:"column:id;primary_key"`
|
||||
// 角色名称
|
||||
Name string `json:"roleName" gorm:"column:name"`
|
||||
|
||||
Status string `json:"status" gorm:"column:status"`
|
||||
|
||||
//角色代码
|
||||
RoleKey string `json:"roleKey" gorm:"column:role_key"`
|
||||
//角色排序
|
||||
RoleSort int64 `json:"roleSort" gorm:"column:role_sort"`
|
||||
|
||||
Flag string `json:"flag" gorm:"column:flag"`
|
||||
|
||||
CreateBy string `gorm:"column:create_by" json:"createBy"`
|
||||
CreateTime string `gorm:"column:create_time" json:"createTime"`
|
||||
UpdateBy string `gorm:"column:update_by" json:"updateBy"`
|
||||
UpdateTime string `gorm:"column:update_time" json:"updateTime"`
|
||||
|
||||
//备注
|
||||
Remark string `gorm:"column:remark" json:"remark"`
|
||||
Params string `gorm:"column:params" json:"params"`
|
||||
DataScope string `gorm:"_" json:"dataScope"`
|
||||
IsDel string `gorm:"column:is_del" json:"isDel"`
|
||||
|
||||
Admin bool `gorm:"column:admin" json:"admin"`
|
||||
|
||||
MenuIds []int64 `gorm:"-" json:"menuIds"`
|
||||
DeptIds []int64 `gorm:"-" json:"deptIds"`
|
||||
}
|
||||
|
||||
type MenuIdList struct {
|
||||
MenuId int64 `json:"menuId"`
|
||||
}
|
||||
|
||||
func (e *SysRole) GetPage(pageSize int, pageIndex int) ([]SysRole, int32, error) {
|
||||
var doc []SysRole
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("sys_role")
|
||||
if e.Id != 0 {
|
||||
table = table.Where("id = ?", e.Id)
|
||||
}
|
||||
if e.Name != "" {
|
||||
table = table.Where("name = ?", e.Name)
|
||||
}
|
||||
if e.Status != "" {
|
||||
table = table.Where("status = ?", e.Status)
|
||||
}
|
||||
if e.RoleKey != "" {
|
||||
table = table.Where("roleKey = ?", e.RoleKey)
|
||||
}
|
||||
|
||||
// 数据权限控制
|
||||
dataPermission := new(DataPermission)
|
||||
dataPermission.UserId, _ = utils.StringToInt64(e.DataScope)
|
||||
table = dataPermission.GetDataScope("sys_role", table)
|
||||
|
||||
var count int32
|
||||
|
||||
if err := table.Where("is_del = 0").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
table.Where("is_del = 0").Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
func (role *SysRole) Get() (SysRole SysRole, err error) {
|
||||
table := orm.Eloquent.Table("sys_role")
|
||||
if role.Id != 0 {
|
||||
table = table.Where("id = ?", role.Id)
|
||||
}
|
||||
if role.Name != "" {
|
||||
table = table.Where("name = ?", role.Name)
|
||||
}
|
||||
if err = table.First(&SysRole).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (role *SysRole) GetList() (SysRole []SysRole, err error) {
|
||||
table := orm.Eloquent.Table("sys_role").Where("is_del = 0")
|
||||
if role.Id != 0 {
|
||||
table = table.Where("id = ?", role.Id)
|
||||
}
|
||||
if role.Name != "" {
|
||||
table = table.Where("name = ?", role.Name)
|
||||
}
|
||||
if err = table.Find(&SysRole).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (role *SysRole) GetRoleMeunId() ([]int64, error) {
|
||||
menuIds := make([]int64, 0)
|
||||
menuList := make([]MenuIdList, 0)
|
||||
if err := orm.Eloquent.Table("sys_role_menu").Select("menu_id").Joins("LEFT JOIN sys_menu on sys_menu.menuId=sys_role_menu.menu_id").Where("role_id = ? ", role.Id).Where(" menu_id not in(select sys_menu.parentId from sys_role_menu LEFT JOIN sys_menu on sys_menu.menuId=sys_role_menu.menu_id where role_id =? )", role.Id).Find(&menuList).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := 0; i < len(menuList); i++ {
|
||||
menuIds = append(menuIds, menuList[i].MenuId)
|
||||
}
|
||||
return menuIds, nil
|
||||
}
|
||||
|
||||
func (role *SysRole) Insert() (id int64, err error) {
|
||||
role.CreateTime = time.Now().Format("2006/01/02 15:04:05")
|
||||
role.UpdateBy = ""
|
||||
role.UpdateTime = time.Now().Format("2006/01/02 15:04:05")
|
||||
role.IsDel = "0"
|
||||
result := orm.Eloquent.Table("sys_role").Create(&role)
|
||||
if result.Error != nil {
|
||||
err = result.Error
|
||||
return
|
||||
}
|
||||
id = role.Id
|
||||
return
|
||||
}
|
||||
|
||||
type DeptIdList struct {
|
||||
DeptId int64 `json:"DeptId"`
|
||||
}
|
||||
|
||||
func (role *SysRole) GetRoleDeptId() ([]int64, error) {
|
||||
deptIds := make([]int64, 0)
|
||||
deptList := make([]DeptIdList, 0)
|
||||
if err := orm.Eloquent.Table("sys_role_dept").Select("dept_id").Joins("LEFT JOIN sys_dept on sys_dept.deptId=sys_role_dept.dept_id").Where("role_id = ? ", role.Id).Where(" deptId not in(select sys_dept.parent_id from sys_role_dept LEFT JOIN sys_dept on sys_dept.deptId=sys_role_dept.dept_id where role_id =? )", role.Id).Find(&deptList).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := 0; i < len(deptList); i++ {
|
||||
deptIds = append(deptIds, deptList[i].DeptId)
|
||||
}
|
||||
|
||||
return deptIds, nil
|
||||
}
|
||||
|
||||
//修改
|
||||
func (role *SysRole) Update(id int64) (update SysRole, err error) {
|
||||
role.UpdateTime = time.Now().Format("2006/01/02 15:04:05.9999")
|
||||
if err = orm.Eloquent.Table("sys_role").First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_role").Model(&update).Updates(&role).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//批量删除
|
||||
func (e *SysRole) BatchDelete(id []int64) (Result bool, err error) {
|
||||
var mp = map[string]string{}
|
||||
mp["is_del"] = "1"
|
||||
mp["update_time"] = utils.GetCurrntTime()
|
||||
mp["update_by"] = e.UpdateBy
|
||||
if err = orm.Eloquent.Table("sys_role").Where("is_del=0 and id in (?)", id).Update(mp).Error; err != nil {
|
||||
return
|
||||
}
|
||||
Result = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
orm "goadmin/database"
|
||||
)
|
||||
|
||||
//sys_role_dept
|
||||
type SysRoleDept struct {
|
||||
RoleId int64
|
||||
DeptId int64
|
||||
}
|
||||
|
||||
func (rm *SysRoleDept) Insert(roleId int64, deptIds []int64) (bool, error) {
|
||||
//ORM不支持批量插入所以需要拼接 sql 串
|
||||
sql := "INSERT INTO `sys_role_dept` (`role_id`,`dept_id`) VALUES "
|
||||
|
||||
for i := 0; i < len(deptIds); i++ {
|
||||
if len(deptIds)-1 == i {
|
||||
//最后一条数据 以分号结尾
|
||||
sql += fmt.Sprintf("(%d,%d);", roleId, deptIds[i])
|
||||
} else {
|
||||
sql += fmt.Sprintf("(%d,%d),", roleId, deptIds[i])
|
||||
}
|
||||
}
|
||||
orm.Eloquent.Exec(sql)
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (rm *SysRoleDept) DeleteRoleDept(roleId int64) (bool, error) {
|
||||
if err := orm.Eloquent.Table("sys_role_dept").Where("role_id = ?", roleId).Delete(&rm).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
var role SysRole
|
||||
if err := orm.Eloquent.Table("sys_role").Where("id = ?", roleId).First(&role).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
orm "goadmin/database"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type RoleMenu struct {
|
||||
RoleId int64
|
||||
MenuId int64
|
||||
RoleName string
|
||||
CreateBy string
|
||||
UpdateBy string
|
||||
}
|
||||
|
||||
type MenuPath struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
func (rm *RoleMenu) Get() ([]RoleMenu, error) {
|
||||
var r []RoleMenu
|
||||
table := orm.Eloquent.Table("sys_role_menu")
|
||||
if rm.RoleId != 0 {
|
||||
table = table.Where("role_id = ?", rm.RoleId)
|
||||
|
||||
}
|
||||
if err := table.Find(&r).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (rm *RoleMenu) GetPermis() ([]string, error) {
|
||||
var r []Menu
|
||||
table := orm.Eloquent.Select("sys_menu.permission").Table("sys_role_menu").Joins("left join sys_menu on sys_menu.menuId = sys_role_menu.menu_id")
|
||||
|
||||
table = table.Where("role_id = ?", rm.RoleId)
|
||||
|
||||
table = table.Where("sys_menu.menuType in('F','C')")
|
||||
if err := table.Find(&r).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var list []string
|
||||
for i := 0; i < len(r); i++ {
|
||||
list = append(list, r[i].Permission)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (rm *RoleMenu) GetIDS() ([]MenuPath, error) {
|
||||
var r []MenuPath
|
||||
table := orm.Eloquent.Select("sys_menu.path").Table("sys_role_menu")
|
||||
table = table.Joins("left join sys_role on sys_role.id=sys_role_menu.role_id")
|
||||
table = table.Joins("left join sys_menu on sys_menu.id=sys_role_menu.menu_id")
|
||||
table = table.Where("sys_role.name = ? and sys_menu.type=1", rm.RoleName)
|
||||
if err := table.Find(&r).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (rm *RoleMenu) DeleteRoleMenu(roleId int64) (bool, error) {
|
||||
if err := orm.Eloquent.Table("sys_role_dept").Where("role_id = ?", roleId).Delete(&rm).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := orm.Eloquent.Table("sys_role_menu").Where("role_id = ?", roleId).Delete(&rm).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
var role SysRole
|
||||
if err := orm.Eloquent.Table("sys_role").Where("id = ?", roleId).First(&role).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
sql3 := "delete from casbin_rule where v0= '" + role.RoleKey + "';"
|
||||
orm.Eloquent.Exec(sql3)
|
||||
|
||||
return true, nil
|
||||
|
||||
}
|
||||
|
||||
func (rm *RoleMenu) BatchDeleteRoleMenu(roleIds []int64) (bool, error) {
|
||||
if err := orm.Eloquent.Table("sys_role_menu").Where("role_id in (?)", roleIds).Delete(&rm).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
var role []SysRole
|
||||
if err := orm.Eloquent.Table("sys_role").Where("id in (?)", roleIds).Find(&role).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
sql := ""
|
||||
for i := 0; i < len(role); i++ {
|
||||
sql += "delete from casbin_rule where v0= '" + role[i].Name + "';"
|
||||
}
|
||||
orm.Eloquent.Exec(sql)
|
||||
return true, nil
|
||||
|
||||
}
|
||||
|
||||
func (rm *RoleMenu) Insert(roleId int64, menuId []int64) (bool, error) {
|
||||
var role SysRole
|
||||
if err := orm.Eloquent.Table("sys_role").Where("id = ?", roleId).First(&role).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
var menu []Menu
|
||||
if err := orm.Eloquent.Table("sys_menu").Where("menuId in (?)", menuId).Find(&menu).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
//ORM不支持批量插入所以需要拼接 sql 串
|
||||
sql := "INSERT INTO `sys_role_menu` (`role_id`,`menu_id`,`role_name`) VALUES "
|
||||
|
||||
sql2 := "INSERT INTO casbin_rule (`p_type`,`v0`,`v1`,`v2`) VALUES "
|
||||
for i := 0; i < len(menu); i++ {
|
||||
if len(menu)-1 == i {
|
||||
//最后一条数据 以分号结尾
|
||||
sql += fmt.Sprintf("(%d,%d,'%s');", role.Id, menu[i].MenuId, role.RoleKey)
|
||||
if menu[i].MenuType == "A" {
|
||||
sql2 += fmt.Sprintf("('p','%s','%s','%s');", role.RoleKey, menu[i].Path, menu[i].Action)
|
||||
}
|
||||
} else {
|
||||
sql += fmt.Sprintf("(%d,%d,'%s'),", role.Id, menu[i].MenuId, role.RoleKey)
|
||||
if menu[i].MenuType == "A" {
|
||||
sql2 += fmt.Sprintf("('p','%s','%s','%s'),", role.RoleKey, menu[i].Path, menu[i].Action)
|
||||
}
|
||||
}
|
||||
}
|
||||
orm.Eloquent.Exec(sql)
|
||||
sql2 = sql2[0:len(sql2)-1] + ";"
|
||||
orm.Eloquent.Exec(sql2)
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (rm *RoleMenu) Delete(RoleId string, MenuID string) (bool, error) {
|
||||
rm.RoleId, _ = strconv.ParseInt(RoleId, 10, 64)
|
||||
table := orm.Eloquent.Table("sys_role_menu").Where("role_id = ?", RoleId)
|
||||
if MenuID != "" {
|
||||
table = table.Where("menu_id = ?", MenuID)
|
||||
}
|
||||
if err := table.Delete(&rm).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
orm "goadmin/database"
|
||||
"goadmin/utils"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// User
|
||||
type User struct {
|
||||
// key
|
||||
IdentityKey string
|
||||
// 用户名
|
||||
UserName string
|
||||
FirstName string
|
||||
LastName string
|
||||
// 角色
|
||||
Role string
|
||||
}
|
||||
|
||||
type UserName struct {
|
||||
Username string `gorm:"column:username" json:"username"`
|
||||
}
|
||||
|
||||
type PassWord struct {
|
||||
// 密码
|
||||
Password string `gorm:"column:password" json:"password"`
|
||||
}
|
||||
|
||||
type LoginM struct {
|
||||
UserName
|
||||
PassWord
|
||||
}
|
||||
|
||||
type SysUserId struct {
|
||||
// 编码
|
||||
Id int64 `gorm:"column:user_id;primary_key" json:"userId"`
|
||||
}
|
||||
|
||||
type SysUserB struct {
|
||||
// 昵称
|
||||
NickName string `gorm:"column:nick_name" json:"nickName"`
|
||||
// 手机号
|
||||
Phone string `gorm:"column:phone" json:"phone"`
|
||||
// 角色编码
|
||||
RoleId int64 `gorm:"column:role_id" json:"roleId"`
|
||||
//盐
|
||||
Salt string `gorm:"column:salt" json:"salt"`
|
||||
//头像
|
||||
Avatar string `gorm:"column:avatar" json:"avatar"`
|
||||
//性别
|
||||
Sex string `gorm:"column:sex" json:"sex"`
|
||||
//邮箱
|
||||
Email string `gorm:"column:email" json:"email"`
|
||||
// 创建时间
|
||||
CreateTime string `gorm:"column:create_time" json:"createTime"`
|
||||
// 修改时间
|
||||
UpdateTime string `gorm:"column:update_time" json:"updateTime"`
|
||||
|
||||
//部门编码
|
||||
DeptId int64 `gorm:"column:dept_id" json:"deptId"`
|
||||
|
||||
//职位编码
|
||||
PostId int64 `gorm:"column:post_id" json:"postId"`
|
||||
|
||||
CreateBy string `gorm:"column:create_by" json:"createBy"`
|
||||
UpdateBy string `gorm:"column:update_by" json:"updateBy"`
|
||||
|
||||
//备注
|
||||
Remark string `gorm:"column:remark" json:"remark"`
|
||||
Params string `gorm:"column:params" json:"params"`
|
||||
Status string `gorm:"column:status" json:"status"`
|
||||
DataScope string `gorm:"_" json:"dataScope"`
|
||||
IsDel string `gorm:"column:is_del" json:"isDel"`
|
||||
}
|
||||
|
||||
type SysUser struct {
|
||||
SysUserId
|
||||
SysUserB
|
||||
LoginM
|
||||
}
|
||||
|
||||
type SysUserPage struct {
|
||||
SysUserId
|
||||
SysUserB
|
||||
LoginM
|
||||
DeptName string `gorm:"-" json:"deptName"`
|
||||
}
|
||||
|
||||
type SysUserView struct {
|
||||
SysUserId
|
||||
SysUserB
|
||||
LoginM
|
||||
RoleName string `gorm:"column:role_name" json:"role_name"`
|
||||
}
|
||||
|
||||
// 获取用户数据
|
||||
func (e *SysUser) Get() (SysUserView SysUserView, err error) {
|
||||
|
||||
table := orm.Eloquent.Table("sys_user").Select([]string{"sys_user.*", "sys_role.name"})
|
||||
table = table.Joins("left join sys_role on sys_user.role_id=sys_role.id")
|
||||
if e.Id != 0 {
|
||||
table = table.Where("user_id = ?", e.Id)
|
||||
}
|
||||
|
||||
if e.Username != "" {
|
||||
table = table.Where("username = ?", e.Username)
|
||||
}
|
||||
|
||||
if e.Password != "" {
|
||||
table = table.Where("password = ?", e.Password)
|
||||
}
|
||||
|
||||
if e.RoleId != 0 {
|
||||
table = table.Where("role_id = ?", e.RoleId)
|
||||
}
|
||||
|
||||
if e.DeptId != 0 {
|
||||
table = table.Where("dept_id = ?", e.DeptId)
|
||||
}
|
||||
|
||||
if e.PostId != 0 {
|
||||
table = table.Where("post_id = ?", e.PostId)
|
||||
}
|
||||
|
||||
if err = table.First(&SysUserView).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *SysUser) GetPage(pageSize int, pageIndex int) ([]SysUserPage, int32, error) {
|
||||
var doc []SysUserPage
|
||||
|
||||
table := orm.Eloquent.Select("sys_user.*,sys_dept.dept_name").Table("sys_user")
|
||||
table = table.Joins("left join sys_dept on sys_dept.deptId = sys_user.dept_id")
|
||||
|
||||
if e.Username != "" {
|
||||
table = table.Where("username = ?", e.Username)
|
||||
}
|
||||
|
||||
if e.DeptId != 0 {
|
||||
table = table.Where("dept_id in (select deptId from sys_dept where dept_path like ? )", "%"+utils.Int64ToString(e.DeptId)+"%")
|
||||
}
|
||||
|
||||
// 数据权限控制
|
||||
dataPermission := new(DataPermission)
|
||||
dataPermission.UserId, _ = utils.StringToInt64(e.DataScope)
|
||||
table = dataPermission.GetDataScope("sys_user", table)
|
||||
|
||||
var count int32
|
||||
|
||||
if err := table.Where("sys_user.is_del = 0").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
table.Where("sys_user.is_del = 0").Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
//加密
|
||||
func (e *SysUser) Encrypt() (err error) {
|
||||
if e.Password == "" {
|
||||
return
|
||||
}
|
||||
|
||||
var hash []byte
|
||||
if hash, err = bcrypt.GenerateFromPassword([]byte(e.Password), bcrypt.DefaultCost); err != nil {
|
||||
return
|
||||
} else {
|
||||
e.Password = string(hash)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//添加
|
||||
func (e SysUser) Insert() (id int64, err error) {
|
||||
e.CreateTime = utils.GetCurrntTime()
|
||||
if err = e.Encrypt(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// check 用户名
|
||||
var count int
|
||||
orm.Eloquent.Table("sys_user").Where("username = ?", e.Username).Count(&count)
|
||||
if count > 0 {
|
||||
err = errors.New("账户已存在!")
|
||||
return
|
||||
}
|
||||
|
||||
//添加数据
|
||||
if err = orm.Eloquent.Table("sys_user").Create(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
id = e.Id
|
||||
return
|
||||
}
|
||||
|
||||
//修改
|
||||
func (e *SysUser) Update(id int64) (update SysUser, err error) {
|
||||
e.UpdateTime = utils.GetCurrntTime()
|
||||
if err = e.Encrypt(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = orm.Eloquent.Table("sys_user").First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
if e.RoleId == 0 {
|
||||
e.RoleId = update.RoleId
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table("sys_user").Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *SysUser) BatchDelete(id []int64) (Result bool, err error) {
|
||||
var mp = map[string]string{}
|
||||
mp["is_del"] = "1"
|
||||
mp["update_time"] = utils.GetCurrntTime()
|
||||
mp["update_by"] = e.UpdateBy
|
||||
if err = orm.Eloquent.Table("sys_user").Where("is_del=0 and user_id in (?)", id).Update(mp).Error; err != nil {
|
||||
return
|
||||
}
|
||||
Result = true
|
||||
return
|
||||
}
|
||||
Reference in new issue
Block a user