Migrate all methods in the model package to the service package

This commit is contained in:
Granty1
2020-04-08 11:07:27 +08:00
parent 2c35adaffc
commit 5d222585dd
49 changed files with 1348 additions and 1261 deletions
+79
View File
@@ -0,0 +1,79 @@
package service
import (
"gin-vue-admin/global"
"gin-vue-admin/model"
)
// @title FindOrCreateFile
// @description Check your file if it does not exist, or return current slice of the file
// 上传文件时检测当前文件属性,如果没有文件则创建,有则返回文件的当前切片
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param ChunkTotal int
// @return err error
// @return file ExaFile
func FindOrCreateFile(FileMd5 string, FileName string, ChunkTotal int) (err error, file model.ExaFile) {
var cfile model.ExaFile
cfile.FileMd5 = FileMd5
cfile.FileName = FileName
cfile.ChunkTotal = ChunkTotal
notHaveSameMd5Finish := global.GVA_DB.Where("file_md5 = ? AND is_finish = ?", FileMd5, true).First(&file).RecordNotFound()
if notHaveSameMd5Finish {
err = global.GVA_DB.Where("file_md5 = ? AND file_name = ?", FileMd5, FileName).Preload("ExaFileChunk").FirstOrCreate(&file, cfile).Error
return err, file
} else {
cfile.IsFinish = true
cfile.FilePath = file.FilePath
err = global.GVA_DB.Create(&cfile).Error
return err, cfile
}
}
// @title CreateFileChunk
// @description create a chunk of the file, 创建文件切片记录
// @auth 2020/04/05 20:22
// @param FileChunkPath string
// @param FileChunkNumber int
// @return error
func CreateFileChunk(id uint, FileChunkPath string, FileChunkNumber int) error {
var chunk model.ExaFileChunk
chunk.FileChunkPath = FileChunkPath
chunk.ExaFileId = id
chunk.FileChunkNumber = FileChunkNumber
err := global.GVA_DB.Create(&chunk).Error
return err
}
// @title FileCreateComplete
// @description file creation, 文件合成完成
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
func FileCreateComplete(FileMd5 string, FileName string, FilePath string) error {
var file model.ExaFile
upDateFile := make(map[string]interface{})
upDateFile["FilePath"] = FilePath
upDateFile["IsFinish"] = true
err := global.GVA_DB.Where("file_md5 = ? AND file_name = ?", FileMd5, FileName).First(&file).Updates(upDateFile).Error
return err
}
// @title DeleteFileChunk
// @description delete a chuck of the file, 删除文件切片记录
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
func DeleteFileChunk(fileMd5 string, fileName string, filePath string) error {
var chunks []model.ExaFileChunk
var file model.ExaFile
err := global.GVA_DB.Where("file_md5 = ? AND file_name = ?", fileMd5, fileName).First(&file).Update("IsFinish", true).Update("file_path", filePath).Error
err = global.GVA_DB.Where("exa_file_id = ?", file.ID).Delete(&chunks).Unscoped().Error
return err
}
+74
View File
@@ -0,0 +1,74 @@
package service
import (
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
)
// @title CreateExaCustomer
// @description create a customer, 创建用户
// @auth 2020/04/05 20:22
// @return err error
func CreateExaCustomer(e model.ExaCustomer) (err error) {
err = global.GVA_DB.Create(e).Error
return err
}
// @title DeleteFileChunk
// @description delete a customer, 删除用户
// @auth 2020/04/05 20:22
// @return error
func DeleteExaCustomer(e model.ExaCustomer) (err error) {
err = global.GVA_DB.Delete(e).Error
return err
}
// @title UpdateExaCustomer
// @description update a customer, 更新用户
// @auth 2020/04/05 20:22
// @return error
func UpdateExaCustomer(e *model.ExaCustomer) (err error) {
err = global.GVA_DB.Save(e).Error
return err
}
// @title GetExaCustomer
// @description get the info of a costumer , 获取用户信息
// @auth 2020/04/05 20:22
// @return error
// @return customer ExaCustomer
func GetExaCustomer(id uint) (err error, customer model.ExaCustomer) {
err = global.GVA_DB.Where("id = ?",id).First(&customer).Error
return
}
// @title GetCustomerInfoList
// @description get customer list by pagination, 分页获取用户列表
// @auth 2020/04/05 20:22
// @param info PageInfo
// @return error
func GetCustomerInfoList(sysUserAuthorityID string, info request.PageInfo) (err error, list interface{}, total int) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB
if err != nil {
return
} else {
var a model.SysAuthority
a.AuthorityId = sysUserAuthorityID
err, auth :=GetAuthorityInfo(a)
var dataId []string
for _, v := range auth.DataAuthorityId {
dataId = append(dataId, v.AuthorityId)
}
var CustomerList []model.ExaCustomer
err = db.Where("sys_user_authority_id in (?)", dataId).Find(&CustomerList).Count(&total).Error
if err != nil {
return err, CustomerList, total
} else {
err = db.Limit(limit).Offset(offset).Preload("SysUser").Where("sys_user_authority_id in (?)", dataId).Find(&CustomerList).Error
}
return err, CustomerList, total
}
}
@@ -0,0 +1,55 @@
package service
import (
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
)
// @title Upload
// @description 创建文件上传记录
// @auth 2020/04/05 20:22
// @return error
func Upload(f model.ExaFileUploadAndDownload) error {
err := global.GVA_DB.Create(f).Error
return err
}
// @title FindFile
// @description 删除文件切片记录
// @auth 2020/04/05 20:22
// @return error
func FindFile(id uint) (error, model.ExaFileUploadAndDownload) {
var file model.ExaFileUploadAndDownload
err := global.GVA_DB.Where("id = ?", id).First(&file).Error
return err, file
}
// @title DeleteFile
// @description 删除文件记录
// @auth 2020/04/05 20:22
// @return error
func DeleteFile(f model.ExaFileUploadAndDownload) error {
err := global.GVA_DB.Where("id = ?", f.ID).Unscoped().Delete(f).Error
return err
}
// @title GetFileRecordInfoList
// @description 分页获取数据
// @auth 2020/04/05 20:22
// @param info PageInfo
// @return err error
// @return list error
// @return total error
func GetFileRecordInfoList(info request.PageInfo) (err error, list interface{}, total int) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB
if err != nil {
return
} else {
var fileLists []model.ExaFileUploadAndDownload
err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&fileLists).Error
return err, fileLists, total
}
}
+46
View File
@@ -0,0 +1,46 @@
package service
import (
"gin-vue-admin/global"
"gin-vue-admin/model"
)
// @title JsonInBlacklist
// @description create jwt blacklist
// @auth 2020/04/05 20:22
// @return err error
func JsonInBlacklist(j model.JwtBlacklist) (err error) {
err = global.GVA_DB.Create(j).Error
return
}
// @title IsBlacklist
// @description check if the Jwt is in the blacklist or not, 判断JWT是否在黑名单内部
// @auth 2020/04/05 20:22
// @param newPassword string
// @return err error
func IsBlacklist(Jwt string, j model.JwtBlacklist) bool {
isNotFound := global.GVA_DB.Where("jwt = ?", Jwt).First(j).RecordNotFound()
return !isNotFound
}
// @title GetRedisJWT
// @description Get user info in redis
// @auth 2020/04/05 20:22
// @param newPassword string
// @return err error
func GetRedisJWT(userName string) (err error, RedisJWT string) {
RedisJWT, err = global.GVA_REDIS.Get(userName).Result()
return err, RedisJWT
}
// @title SetRedisJWT
// @description set jwt into the Redis
// @auth 2020/04/05 20:22
// @param userName string
// @return err error
func SetRedisJWT(j model.JwtBlacklist, userName string) (err error) {
err = global.GVA_REDIS.Set(userName, j.Jwt, 1000*1000*1000*60*60*24*7).Err()
return err
}
+130
View File
@@ -0,0 +1,130 @@
package service
import (
"errors"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
)
// @title CreateApi
// @description create base apis, 新增基础api
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
func CreateApi(a model.SysApi) (err error) {
findOne := global.GVA_DB.Where("path = ?", a.Path).Find(&model.SysApi{}).Error
if findOne == nil {
return errors.New("存在相同api")
} else {
err = global.GVA_DB.Create(a).Error
}
return err
}
// @title DeleteApi
// @description delete base apis, 删除基础api
// @auth 2020/04/05 20:22
// @return error
func DeleteApi(a model.SysApi) (err error) {
err = global.GVA_DB.Delete(a).Error
ClearCasbin(1, a.Path)
return err
}
// @title GetInfoList
// @description get apis by pagination, 分页获取数据
// @auth 2020/04/05 20:22
// @param info PageInfo
// @return err error
// @return list interface{}
// @return total int
func GetAPIInfoList(a model.SysApi, info request.PageInfo, Order string, Desc bool) (err error, list interface{}, total int) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB
if err != nil {
return
} else {
var apiList []model.SysApi
if a.Path != "" {
db = db.Where("path LIKE ?", "%"+a.Path+"%")
}
if a.Description != "" {
db = db.Where("description LIKE ?", "%"+a.Description+"%")
}
if a.Method != "" {
db = db.Where("method = ?", a.Method)
}
err = db.Find(&apiList).Count(&total).Error
if err != nil {
return err, apiList, total
} else {
db = db.Limit(limit).Offset(offset)
if Order != "" {
var OrderStr string
if Desc {
OrderStr = Order + " desc"
} else {
OrderStr = Order
}
err = db.Order(OrderStr, true).Find(&apiList).Error
} else {
err = db.Order("api_group", true).Find(&apiList).Error
}
}
return err, apiList, total
}
}
// @title GetAllApis
// @description get all apis, 获取所有的api
// @auth 2020/04/05 20:22
// @return err error
// @return apis []SysApi
func GetAllApis() (err error, apis []model.SysApi) {
err = global.GVA_DB.Find(&apis).Error
return
}
// @title GetApiById
// @description 根据id获取api
// @auth 2020/04/05 20:22
// @param id float64
// @return error
func GetApiById(id float64) (err error, api model.SysApi) {
err = global.GVA_DB.Where("id = ?", id).First(&api).Error
return
}
// @title UpdateApi
// @description update a base api, update api
// @auth 2020/04/05 20:22
// @return error
func UpdateApi(a model.SysApi) (err error) {
var oldA model.SysApi
flag := global.GVA_DB.Where("path = ?", a.Path).Find(&model.SysApi{}).RecordNotFound()
if !flag {
return errors.New("存在相同api路径")
}
err = global.GVA_DB.Where("id = ?", a.ID).First(&oldA).Error
if err != nil {
return err
} else {
err = UpdateCasbinApi(oldA.Path, a.Path)
if err != nil {
return err
} else {
err = global.GVA_DB.Save(a).Error
}
}
return err
}
+133
View File
@@ -0,0 +1,133 @@
package service
import (
"errors"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
)
// @title CreateAuthority
// @description 创建一个角色
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
func CreateAuthority(a *model.SysAuthority) (err error, authority *model.SysAuthority) {
err = global.GVA_DB.Create(a).Error
return err, a
}
// @title DeleteAuthority
// @description 删除角色
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
// 删除角色
func DeleteAuthority(a model.SysAuthority) (err error) {
err = global.GVA_DB.Where("authority_id = ?", a.AuthorityId).Find(&model.SysUser{}).Error
if err == nil {
err = errors.New("此角色有用户正在使用禁止删除")
return
}
err = global.GVA_DB.Where("parent_id = ?", a.AuthorityId).Find(&model.SysAuthority{}).Error
if err == nil {
err = errors.New("此角色存在子角色不允许删除")
return
}
db := global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", a.AuthorityId).First(a).Unscoped().Delete(a)
if len(a.SysBaseMenus) > 0 {
err = db.Association("SysBaseMenus").Delete(a.SysBaseMenus).Error
} else {
err = db.Error
}
ClearCasbin(0, a.AuthorityId)
return err
}
// @title GetInfoList
// @description 删除文件切片记录
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
// 分页获取数据
func GetAuthorityInfoList(info request.PageInfo) (err error, list interface{}, total int) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB
if err != nil {
return
} else {
var authority []model.SysAuthority
err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error
if len(authority) > 0 {
for k, _ := range authority {
err = findChildrenAuthority(&authority[k])
}
}
return err, authority, total
}
}
// @title GetAuthorityInfo
// @description 获取所有角色信息
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
func GetAuthorityInfo(a model.SysAuthority) (err error, sa model.SysAuthority) {
err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", a.AuthorityId).First(&sa).Error
return err, sa
}
// @title SetDataAuthority
// @description 设置角色资源权限
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
func SetDataAuthority(a model.SysAuthority) error {
var s model.SysAuthority
global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", a.AuthorityId)
err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&a.DataAuthorityId).Error
return err
}
// @title SetMenuAuthority
// @description 菜单与角色绑定
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
func SetMenuAuthority(a *model.SysAuthority) error {
var s model.SysAuthority
global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", a.AuthorityId)
err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&a.SysBaseMenus).Error
return err
}
// @title findChildrenAuthority
// @description 查询子角色
// @auth 2020/04/05 20:22
// @param FileMd5 string
// @param FileName string
// @param FilePath string
// @return error
func findChildrenAuthority(authority *model.SysAuthority) (err error) {
err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
if len(authority.Children) > 0 {
for k, _ := range authority.Children {
err = findChildrenAuthority(&authority.Children[k])
}
}
return err
}
+116
View File
@@ -0,0 +1,116 @@
package service
import (
"gin-vue-admin/model"
"gin-vue-admin/utils"
"html/template"
"os"
)
// @title CreateTemp
// @description 函数的详细描述
// @auth 2020/04/05 20:22
// @return err error
func CreateTemp(a model.AutoCodeStruct) (err error) {
basePath := "./resource/template"
modelTmpl, err := template.ParseFiles(basePath + "/te/model.go.tpl")
if err != nil {
return err
}
apiTmpl, err := template.ParseFiles(basePath + "/te/api.go.tpl")
if err != nil {
return err
}
routerTmpl, err := template.ParseFiles(basePath + "/te/router.go.tpl")
if err != nil {
return err
}
feapiTmpl, err := template.ParseFiles(basePath + "/fe/api.js.tpl")
if err != nil {
return err
}
readmeTmpl, err := template.ParseFiles(basePath + "/readme.txt.tpl")
if err != nil {
return err
}
//自动化总目录
_autoCode := "./autoCode/"
//自动化后台代码目录
_te := "./autoCode/te/"
_dir := _te + a.PackageName
_modeldir := _te + a.PackageName + "/model"
_apidir := _te + a.PackageName + "/api"
_routerdir := _te + a.PackageName + "/router"
//自动化前台代码目录
_fe := "./autoCode/fe/"
_fe_dir := _fe + a.PackageName
_fe_apidir := _fe + a.PackageName + "/api"
err = utils.CreateDir(_autoCode, _te, _dir, _modeldir, _apidir, _routerdir, _fe, _fe_dir, _fe_apidir)
if err != nil {
return err
}
model, err := os.OpenFile(_te+a.PackageName+"/model/model.go", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
api, err := os.OpenFile(_te+a.PackageName+"/api/api.go", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
router, err := os.OpenFile(_te+a.PackageName+"/router/router.go", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
feapi, err := os.OpenFile(_fe+a.PackageName+"/api/api.js", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
readme, err := os.OpenFile(_autoCode+"readme.txt", os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
// 生成代码
{
err = modelTmpl.Execute(model, a)
if err != nil {
return err
}
err = apiTmpl.Execute(api, a)
if err != nil {
return err
}
err = routerTmpl.Execute(router, a)
if err != nil {
return err
}
err = feapiTmpl.Execute(feapi, a)
if err != nil {
return err
}
err = readmeTmpl.Execute(readme, a)
if err != nil {
return err
}
}
_ = model.Close()
_ = api.Close()
_ = router.Close()
_ = feapi.Close()
_ = readme.Close()
fileList := []string{
_te + a.PackageName + "/model/model.go",
_te + a.PackageName + "/api/api.go",
_te + a.PackageName + "/router/router.go",
_fe + a.PackageName + "/api/api.js",
_autoCode + "readme.txt",
}
err = utils.ZipFiles("./ginvueadmin.zip", fileList, ".", ".")
if err != nil {
return err
}
err = os.RemoveAll(_autoCode)
if err != nil {
return err
}
return nil
}
+58
View File
@@ -0,0 +1,58 @@
package service
import (
"errors"
"gin-vue-admin/global"
"gin-vue-admin/model"
)
// @title DeleteBaseMenu
// @description 删除基础路由
// @auth 2020/04/05 20:22
// @param newPassword string
// @return err error
func DeleteBaseMenu(id float64) (err error) {
err = global.GVA_DB.Where("parent_id = ?", id).First(&model.SysBaseMenu{}).Error
if err != nil {
var menu model.SysBaseMenu
db := global.GVA_DB.Preload("SysAuthoritys").Where("id = ?", id).First(&menu).Delete(&menu)
if len(menu.SysAuthoritys) > 0 {
err = db.Association("SysAuthoritys").Delete(menu.SysAuthoritys).Error
} else {
err = db.Error
}
} else {
return errors.New("此菜单存在子菜单不可删除")
}
return err
}
// @title UpdateBaseMenu
// @description 更新路由
// @auth 2020/04/05 20:22
// @param newPassword string
// @return err error
func UpdateBaseMenu(menu model.SysBaseMenu) (err error) {
upDateMap := make(map[string]interface{})
upDateMap["parent_id"] = menu.ParentId
upDateMap["path"] = menu.Path
upDateMap["name"] = menu.Name
upDateMap["hidden"] = menu.Hidden
upDateMap["component"] = menu.Component
upDateMap["title"] = menu.Title
upDateMap["icon"] = menu.Icon
upDateMap["sort"] = menu.Sort
err = global.GVA_DB.Where("id = ?", menu.ID).Find(&model.SysBaseMenu{}).Updates(upDateMap).Error
global.GVA_LOG.Debug("菜单修改时候,关联菜单err:%v", err)
return err
}
// @title GetBaseMenuById
// @description get current menus, 返回当前选中menu
// @auth 2020/04/05 20:22
// @param newPassword string
// @return err error
func GetBaseMenuById(id float64) (err error, menu model.SysBaseMenu) {
err = global.GVA_DB.Where("id = ?", id).First(&menu).Error
return
}
+121
View File
@@ -0,0 +1,121 @@
package service
import (
"errors"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
"github.com/casbin/casbin"
"github.com/casbin/casbin/util"
gormadapter "github.com/casbin/gorm-adapter"
"strings"
)
// @title UpdateCasbin
// @description update casbin authority, 更新casbin权限
// @auth 2020/04/05 20:22
// @param authorityId string
// @param casbinInfos []CasbinInfo
// @return error
func UpdateCasbin(authorityId string, casbinInfos []request.CasbinInfo) error {
ClearCasbin(0, authorityId)
for _, v := range casbinInfos {
cm := model.CasbinModel{
ID: 0,
Ptype: "p",
AuthorityId: authorityId,
Path: v.Path,
Method: v.Method,
}
addflag := AddCasbin(cm)
if addflag == false {
return errors.New("存在相同api,添加失败,请联系管理员")
}
}
return nil
}
// @title AddCasbin
// @description add casbin authority, 添加权限
// @auth 2020/04/05 20:22
// @param cm CasbinModel
// @return bool
func AddCasbin(cm model.CasbinModel) bool {
e := Casbin()
return e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method)
}
// @title UpdateCasbinApi
// @description update casbin apis, API更新随动
// @auth 2020/04/05 20:22
// @param oldPath string
// @param newPath string
// @return error
func UpdateCasbinApi(oldPath string, newPath string) error {
var cs []model.CasbinModel
err := global.GVA_DB.Table("casbin_rule").Where("v1 = ?", oldPath).Find(&cs).Update("v1", newPath).Error
return err
}
// @title GetPolicyPathByAuthorityId
// @description get policy path by authorityId, 获取权限列表
// @auth 2020/04/05 20:22
// @param authorityId string
// @return []string
func GetPolicyPathByAuthorityId(authorityId string) []string {
e := Casbin()
var pathList []string
list := e.GetFilteredPolicy(0, authorityId)
for _, v := range list {
pathList = append(pathList, v[1])
}
return pathList
}
// @title ClearCasbin
// @description 清除匹配的权限
// @auth 2020/04/05 20:22
// @param v int
// @param p string
// @return bool
func ClearCasbin(v int, p string) bool {
e := Casbin()
return e.RemoveFilteredPolicy(v, p)
}
// @title Casbin
// @description store to DB, 持久化到数据库 引入自定义规则
// @auth 2020/04/05 20:22
func Casbin() *casbin.Enforcer {
a := gormadapter.NewAdapterByDB(global.GVA_DB)
e := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
e.AddFunction("ParamsMatch", ParamsMatchFunc)
_ = e.LoadPolicy()
return e
}
// @title ParamsMatch
// @description customized rule, 自定义规则函数
// @auth 2020/04/05 20:22
// @param fullNameKey1 string
// @param key2 string
// @return bool
func ParamsMatch(fullNameKey1 string, key2 string) bool {
key1 := strings.Split(fullNameKey1, "?")[0]
//剥离路径后再使用casbin的keyMatch2
return util.KeyMatch2(key1, key2)
}
// @title ParamsMatchFunc
// @description customized function, 自定义规则函数
// @auth 2020/04/05 20:22
// @param args ...interface{}
// @return interface{}
// @return error
func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
name1 := args[0].(string)
name2 := args[1].(string)
return (bool)(ParamsMatch(name1, name2)), nil
}
+129
View File
@@ -0,0 +1,129 @@
package service
import (
"errors"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
)
// @title GetMenuTree
// @description 获取动态菜单树
// @auth 2020/04/05 20:22
// @param authorityId string
// @return err error
// @return menus []SysMenu
func GetMenuTree(authorityId string) (err error, menus []model.SysMenu) {
sql := "SELECT authority_menu.created_at,authority_menu.updated_at,authority_menu.deleted_at,authority_menu.menu_level,authority_menu.parent_id,authority_menu.path,authority_menu.`name`,authority_menu.hidden,authority_menu.component,authority_menu.title,authority_menu.icon,authority_menu.sort,authority_menu.menu_id,authority_menu.authority_id FROM authority_menu WHERE authority_menu.authority_id = ? AND authority_menu.parent_id = ?"
err = global.GVA_DB.Raw(sql, authorityId, 0).Scan(&menus).Error
for i := 0; i < len(menus); i++ {
err = getChildrenList(&menus[i], sql)
}
return err, menus
}
// @title getChildrenList
// @description 获取子菜单
// @auth 2020/04/05 20:22
// @param menu *SysMenu
// @param SQLstatement string
// @return err error
func getChildrenList(menu *model.SysMenu, sql string) (err error) {
err = global.GVA_DB.Raw(sql, menu.AuthorityId, menu.MenuId).Scan(&menu.Children).Error
for i := 0; i < len(menu.Children); i++ {
err = getChildrenList(&menu.Children[i], sql)
}
return err
}
// @title GetInfoList
// @description 获取路由分页
// @auth 2020/04/05 20:22
// @param newPassword string
// @return err error
func GetInfoList(info request.PageInfo) (err error, list interface{}, total int) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB
if err != nil {
return
} else {
var menuList []model.SysBaseMenu
err = db.Limit(limit).Offset(offset).Where("parent_id = 0").Order("sort", true).Find(&menuList).Error
for i := 0; i < len(menuList); i++ {
err = getBaseChildrenList(&menuList[i])
}
return err, menuList, total
}
}
// @title getBaseChildrenList
// @description get children of menu, 获取菜单的子菜单
// @auth 2020/04/05 20:22
// @param menu *SysBaseMenu
// @return err error
func getBaseChildrenList(menu *model.SysBaseMenu) (err error) {
err = global.GVA_DB.Where("parent_id = ?", menu.ID).Order("sort", true).Find(&menu.Children).Error
for i := 0; i < len(menu.Children); i++ {
err = getBaseChildrenList(&menu.Children[i])
}
return err
}
// @title AddBaseMenu
// @description 函数的详细描述
// @auth 2020/04/05 20:22
// @param newPassword string
// @return err error
//增加基础路由
func AddBaseMenu(menu model.SysBaseMenu) (err error) {
findOne := global.GVA_DB.Where("name = ?", menu.Name).Find(&model.SysBaseMenu{}).Error
if findOne != nil {
err = global.GVA_DB.Create(menu).Error
} else {
err = errors.New("存在重复name,请修改name")
}
return err
}
// @title GetBaseMenuTree
// @description 获取基础路由树
// @auth 2020/04/05 20:22
// @return err error
// @return menus []SysBaseMenu
func GetBaseMenuTree() (err error, menus []model.SysBaseMenu) {
err = global.GVA_DB.Where(" parent_id = ?", 0).Order("sort", true).Find(&menus).Error
for i := 0; i < len(menus); i++ {
err = getBaseChildrenList(&menus[i])
}
return err, menus
}
// @title AddMenuAuthority
// @description 为角色增加menu树
// @auth 2020/04/05 20:22
// @param menus []SysBaseMenu
// @param authorityId string
// @return error
func AddMenuAuthority(menus []model.SysBaseMenu, authorityId string) (err error) {
var auth model.SysAuthority
auth.AuthorityId = authorityId
auth.SysBaseMenus = menus
err = SetMenuAuthority(&auth)
return err
}
// @title GetMenuAuthority
// @description 查看当前角色树
// @auth 2020/04/05 20:22
// @param authorityId string
// @return err error
// @return menus []SysBaseMenu
func GetMenuAuthority(authorityId string) (err error, menus []model.SysMenu) {
sql := "SELECT authority_menu.created_at,authority_menu.updated_at,authority_menu.deleted_at,authority_menu.menu_level,authority_menu.parent_id,authority_menu.path,authority_menu.`name`,authority_menu.hidden,authority_menu.component,authority_menu.title,authority_menu.icon,authority_menu.sort,authority_menu.menu_id,authority_menu.authority_id FROM authority_menu WHERE authority_menu.authority_id = ?"
err = global.GVA_DB.Raw(sql, authorityId).Scan(&menus).Error
return err, menus
}
+31
View File
@@ -0,0 +1,31 @@
package service
import (
"gin-vue-admin/config"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/utils"
)
// @title GetSystemConfig
// @description 读取配置文件
// @auth 2020/04/05 20:22
// @return err error
// @return conf Server
func GetSystemConfig() (err error, conf config.Server) {
return nil, global.GVA_CONFIG
}
// @title SetSystemConfig
// @description set system config, 设置配置文件
// @auth 2020/04/05 20:22
// @return err error
func SetSystemConfig(s model.System) (err error) {
cs := utils.StructToMap(s.Config)
for k, v := range cs {
global.GVA_VP.Set(k, v)
}
err = global.GVA_VP.WriteConfig()
return err
}
+106
View File
@@ -0,0 +1,106 @@
package service
import (
"errors"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
"gin-vue-admin/utils"
uuid "github.com/satori/go.uuid"
)
// @title Register
// @description register, 用户注册
// @auth 2020/04/05 20:22
// @return err error
// @return userInter *SysUser
func Register(u *model.SysUser) (err error, userInter *model.SysUser) {
var user model.SysUser
//判断用户名是否注册
notRegister := global.GVA_DB.Where("username = ?", u.Username).First(&user).RecordNotFound()
//notRegister为false表明读取到了 不能注册
if !notRegister {
return errors.New("用户名已注册"), nil
} else {
// 否则 附加uuid 密码md5简单加密 注册
u.Password = utils.MD5V([]byte(u.Password))
u.UUID = uuid.NewV4()
err = global.GVA_DB.Create(u).Error
}
return err, u
}
// @title Login
// @description login, 用户登录
// @auth 2020/04/05 20:22
// @return err error
// @return userInter *SysUser
func Login(u *model.SysUser) (err error, userInter *model.SysUser) {
var user model.SysUser
u.Password = utils.MD5V([]byte(u.Password))
err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Error
if err != nil {
return err, &user
}
err = global.GVA_DB.Where("authority_id = ?", user.AuthorityId).First(&user.Authority).Error
return err, &user
}
// @title ChangePassword
// @description change the password of a certain user, 修改用户密码
// @auth 2020/04/05 20:22
// @param newPassword string
// @return err error
// @return userInter *SysUser
func ChangePassword(u *model.SysUser, newPassword string) (err error, userInter *model.SysUser) {
var user model.SysUser
//后期修改jwt+password模式
u.Password = utils.MD5V([]byte(u.Password))
err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
return err, u
}
// @title GetInfoList
// @description get user list by pagination, 分页获取数据
// @auth 2020/04/05 20:22
// @param PageInfo int
// @return err error
// @return list interface{}
// @return total int
func GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB
if err != nil {
return
} else {
var userList []model.SysUser
err = db.Limit(limit).Offset(offset).Preload("Authority").Find(&userList).Error
return err, userList, total
}
}
// @title SetUserAuthority
// @description set the authority of a certain user, 设置一个用户的权限
// @auth 2020/04/05 20:22
// @param uuid UUID
// @param authorityId string
// @return err error
func SetUserAuthority(uuid uuid.UUID, authorityId string) (err error) {
err = global.GVA_DB.Where("uuid = ?", uuid).First(&model.SysUser{}).Update("authority_id", authorityId).Error
return err
}
// @title UploadHeaderImg
// @description upload avatar, 用户头像上传更新地址
// @auth 2020/04/05 20:22
// @param uuid UUID
// @param filePath string
// @return err error
// @return userInter *SysUser
func UploadHeaderImg(uuid uuid.UUID, filePath string) (err error, userInter *model.SysUser) {
var user model.SysUser
err = global.GVA_DB.Where("uuid = ?", uuid).First(&user).Update("header_img", filePath).First(&user).Error
return err, &user
}
+15
View File
@@ -0,0 +1,15 @@
package service
import (
"gin-vue-admin/global"
"gin-vue-admin/model"
)
// @title Create
// @description create a workflow, 创建工作流
// @auth 2020/04/05 20:22
// @return error
func Create(wk model.SysWorkflow) error {
err := global.GVA_DB.Create(&wk).Error
return err
}