refactor: 后端包结构重构、去除无用的文件

This commit is contained in:
meilin.huang
2022-06-02 17:41:11 +08:00
parent 51d06ab206
commit b2dc9dff0b
234 changed files with 749 additions and 816 deletions

View File

@@ -0,0 +1,231 @@
package api
import (
"fmt"
"mayfly-go/internal/sys/api/form"
"mayfly-go/internal/sys/api/vo"
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/captcha"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/utils"
"strconv"
"strings"
"time"
)
type Account struct {
AccountApp application.Account
ResourceApp application.Resource
RoleApp application.Role
MsgApp application.Msg
}
/** 登录者个人相关操作 **/
// @router /accounts/login [post]
func (a *Account) Login(rc *ctx.ReqCtx) {
loginForm := &form.LoginForm{}
ginx.BindJsonAndValid(rc.GinCtx, loginForm)
rc.ReqParam = loginForm.Username
// 校验验证码
biz.IsTrue(captcha.Verify(loginForm.Cid, loginForm.Captcha), "验证码错误")
account := &entity.Account{Username: loginForm.Username, Password: utils.Md5(loginForm.Password)}
biz.ErrIsNil(a.AccountApp.GetAccount(account, "Id", "Username", "Status", "LastLoginTime", "LastLoginIp"), "用户名或密码错误")
biz.IsTrue(account.IsEnable(), "该账号不可用")
var resources vo.AccountResourceVOList
// 获取账号菜单资源
a.ResourceApp.GetAccountResources(account.Id, &resources)
// 菜单树与权限code数组
var menus vo.AccountResourceVOList
var permissions []string
for _, v := range resources {
if v.Type == entity.ResourceTypeMenu {
menus = append(menus, v)
} else {
permissions = append(permissions, *v.Code)
}
}
// 保存该账号的权限codes
ctx.SavePermissionCodes(account.Id, permissions)
// 保存登录消息
go a.saveLogin(account, rc.GinCtx.ClientIP())
rc.ResData = map[string]interface{}{
"token": ctx.CreateToken(account.Id, account.Username),
"username": account.Username,
"lastLoginTime": account.LastLoginTime,
"lastLoginIp": account.LastLoginIp,
"menus": menus.ToTrees(0),
"permissions": permissions,
}
}
// 保存更新账号登录信息
func (a *Account) saveLogin(account *entity.Account, ip string) {
// 更新账号最后登录时间
now := time.Now()
updateAccount := &entity.Account{LastLoginTime: &now}
updateAccount.Id = account.Id
updateAccount.LastLoginIp = ip
a.AccountApp.Update(updateAccount)
// 创建登录消息
loginMsg := &entity.Msg{
RecipientId: int64(account.Id),
Msg: fmt.Sprintf("于%s登录", now.Format("2006-01-02 15:04:05")),
Type: 1,
}
loginMsg.CreateTime = &now
loginMsg.Creator = account.Username
loginMsg.CreatorId = account.Id
a.MsgApp.Create(loginMsg)
// bodyMap, err := httpclient.NewRequest(fmt.Sprintf("http://ip-api.com/json/%s?lang=zh-CN", ip)).Get().BodyToMap()
// if err != nil {
// global.Log.Errorf("获取客户端ip地址信息失败%s", err.Error())
// return
// }
// if bodyMap["status"].(string) == "fail" {
// return
// }
// msg := fmt.Sprintf("%s于%s-%s登录", account.Username, bodyMap["regionName"], bodyMap["city"])
// global.Log.Info(msg)
}
// 获取个人账号信息
func (a Account) AccountInfo(rc *ctx.ReqCtx) {
ap := new(vo.AccountPersonVO)
// 角色信息
roles := new([]vo.AccountRoleVO)
a.RoleApp.GetAccountRoles(rc.LoginAccount.Id, roles)
ap.Roles = *roles
rc.ResData = ap
}
// 更新个人账号信息
func (a Account) UpdateAccount(rc *ctx.ReqCtx) {
updateForm := &form.AccountUpdateForm{}
ginx.BindJsonAndValid(rc.GinCtx, updateForm)
updateAccount := new(entity.Account)
utils.Copy(updateAccount, updateForm)
// 账号id为登录者账号
updateAccount.Id = rc.LoginAccount.Id
if updateAccount.Password != "" {
updateAccount.Password = utils.Md5(updateAccount.Password)
}
a.AccountApp.Update(updateAccount)
}
// 获取账号接收的消息列表
func (a Account) GetMsgs(rc *ctx.ReqCtx) {
condition := &entity.Msg{
RecipientId: int64(rc.LoginAccount.Id),
}
rc.ResData = a.MsgApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]entity.Msg))
}
/** 后台账号操作 **/
// @router /accounts [get]
func (a *Account) Accounts(rc *ctx.ReqCtx) {
condition := &entity.Account{}
condition.Username = rc.GinCtx.Query("username")
rc.ResData = a.AccountApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.AccountManageVO))
}
// @router /accounts [get]
func (a *Account) CreateAccount(rc *ctx.ReqCtx) {
form := &form.AccountCreateForm{}
ginx.BindJsonAndValid(rc.GinCtx, form)
rc.ReqParam = form
account := &entity.Account{}
utils.Copy(account, form)
account.SetBaseInfo(rc.LoginAccount)
a.AccountApp.Create(account)
}
func (a *Account) ChangeStatus(rc *ctx.ReqCtx) {
g := rc.GinCtx
account := &entity.Account{}
account.Id = uint64(ginx.PathParamInt(g, "id"))
account.Status = int8(ginx.PathParamInt(g, "status"))
rc.ReqParam = fmt.Sprintf("accountId: %d, status: %d", account.Id, account.Status)
a.AccountApp.Update(account)
}
func (a *Account) DeleteAccount(rc *ctx.ReqCtx) {
id := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
rc.ReqParam = id
a.AccountApp.Delete(id)
}
// 获取账号角色id列表用户回显角色分配
func (a *Account) AccountRoleIds(rc *ctx.ReqCtx) {
rc.ResData = a.RoleApp.GetAccountRoleIds(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
}
// 获取账号角色id列表用户回显角色分配
func (a *Account) AccountRoles(rc *ctx.ReqCtx) {
vos := new([]vo.AccountRoleVO)
a.RoleApp.GetAccountRoles(uint64(ginx.PathParamInt(rc.GinCtx, "id")), vos)
rc.ResData = vos
}
func (a *Account) AccountResources(rc *ctx.ReqCtx) {
var resources vo.ResourceManageVOList
// 获取账号菜单资源
a.ResourceApp.GetAccountResources(uint64(ginx.PathParamInt(rc.GinCtx, "id")), &resources)
rc.ResData = resources.ToTrees(0)
}
// 保存账号角色信息
func (a *Account) SaveRoles(rc *ctx.ReqCtx) {
g := rc.GinCtx
var form form.AccountRoleForm
ginx.BindJsonAndValid(g, &form)
aid := uint64(form.Id)
rc.ReqParam = form
// 将,拼接的字符串进行切割
idsStr := strings.Split(form.RoleIds, ",")
var newIds []interface{}
for _, v := range idsStr {
id, _ := strconv.Atoi(v)
newIds = append(newIds, uint64(id))
}
// 将[]uint64转为[]interface{}
oIds := a.RoleApp.GetAccountRoleIds(uint64(form.Id))
var oldIds []interface{}
for _, v := range oIds {
oldIds = append(oldIds, v)
}
addIds, delIds, _ := utils.ArrayCompare(newIds, oldIds, func(i1, i2 interface{}) bool {
return i1.(uint64) == i2.(uint64)
})
createTime := time.Now()
creator := rc.LoginAccount.Username
creatorId := rc.LoginAccount.Id
for _, v := range addIds {
rr := &entity.AccountRole{AccountId: aid, RoleId: v.(uint64), CreateTime: &createTime, CreatorId: creatorId, Creator: creator}
a.RoleApp.SaveAccountRole(rr)
}
for _, v := range delIds {
a.RoleApp.DeleteAccountRole(aid, v.(uint64))
}
}

View File

@@ -0,0 +1,11 @@
package api
import (
"mayfly-go/pkg/captcha"
"mayfly-go/pkg/ctx"
)
func GenerateCaptcha(rc *ctx.ReqCtx) {
id, image := captcha.Generate()
rc.ResData = map[string]interface{}{"base64Captcha": image, "cid": id}
}

View File

@@ -0,0 +1,9 @@
package form
type AccountCreateForm struct {
Username *string `json:"username" binding:"required,min=4,max=16"`
}
type AccountUpdateForm struct {
Password *string `json:"password" binding:"min=6,max=16"`
}

View File

@@ -0,0 +1,9 @@
package form
// 登录表单
type LoginForm struct {
Username string `json:"username" binding:"required"`
Password string `binding:"required"`
Captcha string `binding:"required"`
Cid string `binding:"required"`
}

View File

@@ -0,0 +1,23 @@
package form
type ResourceForm struct {
Pid int
Id int
Code string `binding:"required"`
Name string `binding:"required"`
Type int `binding:"required,oneof=1 2"`
Weight int
Meta map[string]interface{}
}
type MenuResourceMeta struct {
RouteName string `binding:"required"`
Component string `binding:"required"`
Redirect string
Path string `binding:"required"`
IsKeepAlive bool //
IsHide bool // 是否在菜单栏显示,默认显示
IsAffix bool // tag标签是否不可删除
IsIframe bool
Link string
}

View File

@@ -0,0 +1,22 @@
package form
// 分配角色资源表单信息
type RoleResourceForm struct {
Id int
ResourceIds string
}
// 保存角色信息表单
type RoleForm struct {
Id int
Status int `json:"status"` // 1可用-1不可用
Name string `binding:"required"`
Code string `binding:"required"`
Remark string `json:"remark"`
}
// 账号分配角色表单
type AccountRoleForm struct {
Id int `binding:"required"`
RoleIds string
}

View File

@@ -0,0 +1,53 @@
package api
import (
"encoding/json"
"mayfly-go/internal/sys/api/form"
"mayfly-go/internal/sys/api/vo"
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/utils"
)
type Resource struct {
ResourceApp application.Resource
}
func (r *Resource) GetAllResourceTree(rc *ctx.ReqCtx) {
var resources vo.ResourceManageVOList
r.ResourceApp.GetResourceList(new(entity.Resource), &resources, "weight asc")
rc.ResData = resources.ToTrees(0)
}
func (r *Resource) GetById(rc *ctx.ReqCtx) {
rc.ResData = r.ResourceApp.GetById(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
}
func (r *Resource) SaveResource(rc *ctx.ReqCtx) {
g := rc.GinCtx
form := new(form.ResourceForm)
ginx.BindJsonAndValid(g, form)
rc.ReqParam = form
entity := new(entity.Resource)
utils.Copy(entity, form)
// 将meta转为json字符串存储
bytes, _ := json.Marshal(form.Meta)
entity.Meta = string(bytes)
entity.SetBaseInfo(rc.LoginAccount)
r.ResourceApp.Save(entity)
}
func (r *Resource) DelResource(rc *ctx.ReqCtx) {
r.ResourceApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
}
func (r *Resource) ChangeStatus(rc *ctx.ReqCtx) {
re := &entity.Resource{}
re.Id = uint64(ginx.PathParamInt(rc.GinCtx, "id"))
re.Status = int8(ginx.PathParamInt(rc.GinCtx, "status"))
r.ResourceApp.Save(re)
}

View File

@@ -0,0 +1,98 @@
package api
import (
"mayfly-go/internal/sys/api/form"
"mayfly-go/internal/sys/api/vo"
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/utils"
"strconv"
"strings"
"time"
)
type Role struct {
RoleApp application.Role
ResourceApp application.Resource
}
func (r *Role) Roles(rc *ctx.ReqCtx) {
g := rc.GinCtx
condition := &entity.Role{}
rc.ResData = r.RoleApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Role))
}
// 保存角色信息
func (r *Role) SaveRole(rc *ctx.ReqCtx) {
g := rc.GinCtx
form := &form.RoleForm{}
ginx.BindJsonAndValid(g, form)
role := new(entity.Role)
utils.Copy(role, form)
role.SetBaseInfo(rc.LoginAccount)
r.RoleApp.SaveRole(role)
}
// 删除角色及其资源关联关系
func (r *Role) DelRole(rc *ctx.ReqCtx) {
r.RoleApp.DeleteRole(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
}
// 获取角色关联的资源id数组用于分配资源时回显已拥有的资源
func (r *Role) RoleResourceIds(rc *ctx.ReqCtx) {
rc.ResData = r.RoleApp.GetRoleResourceIds(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
}
// 查看角色关联的资源树信息
func (r *Role) RoleResource(rc *ctx.ReqCtx) {
g := rc.GinCtx
var resources vo.ResourceManageVOList
r.RoleApp.GetRoleResources(uint64(ginx.PathParamInt(g, "id")), &resources)
rc.ResData = resources.ToTrees(0)
}
// 保存角色资源
func (r *Role) SaveResource(rc *ctx.ReqCtx) {
g := rc.GinCtx
var form form.RoleResourceForm
ginx.BindJsonAndValid(g, &form)
rid := uint64(form.Id)
rc.ReqParam = form
// 将,拼接的字符串进行切割
idsStr := strings.Split(form.ResourceIds, ",")
var newIds []interface{}
for _, v := range idsStr {
id, _ := strconv.Atoi(v)
newIds = append(newIds, uint64(id))
}
// 将[]uint64转为[]interface{}
oIds := r.RoleApp.GetRoleResourceIds(uint64(form.Id))
var oldIds []interface{}
for _, v := range oIds {
oldIds = append(oldIds, v)
}
addIds, delIds, _ := utils.ArrayCompare(newIds, oldIds, func(i1, i2 interface{}) bool {
return i1.(uint64) == i2.(uint64)
})
createTime := time.Now()
creator := rc.LoginAccount.Username
creatorId := rc.LoginAccount.Id
for _, v := range addIds {
rr := &entity.RoleResource{RoleId: rid, ResourceId: v.(uint64), CreateTime: &createTime, CreatorId: creatorId, Creator: creator}
r.RoleApp.SaveRoleResource(rr)
}
for _, v := range delIds {
r.RoleApp.DeleteRoleResource(rid, v.(uint64))
}
}

View File

@@ -0,0 +1,37 @@
package api
import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ws"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
type System struct {
}
// 连接websocket
func (s *System) ConnectWs(g *gin.Context) {
wsConn, err := ws.Upgrader.Upgrade(g.Writer, g.Request, nil)
defer func() {
if err := recover(); err != nil {
wsConn.WriteMessage(websocket.TextMessage, []byte(err.(error).Error()))
wsConn.Close()
}
}()
if err != nil {
panic(biz.NewBizErr("升级websocket失败"))
}
// 权限校验
rc := ctx.NewReqCtxWithGin(g)
if err = ctx.PermissionHandler(rc); err != nil {
panic(biz.NewBizErr("没有权限"))
}
// 登录账号信息
la := rc.LoginAccount
ws.Put(la.Id, wsConn)
}

View File

@@ -0,0 +1,26 @@
package vo
import (
"mayfly-go/pkg/model"
"time"
)
type AccountManageVO struct {
model.Model
Username *string `json:"username"`
Status int `json:"status"`
LastLoginTime *time.Time `json:"lastLoginTime"`
}
// 账号角色信息
type AccountRoleVO struct {
Name *string `json:"name"`
Status int `json:"status"`
CreateTime *time.Time `json:"createTime"`
Creator string `json:"creator"`
}
// 账号个人信息
type AccountPersonVO struct {
Roles []AccountRoleVO `json:"roles"` // 角色信息
}

View File

@@ -0,0 +1,92 @@
package vo
import "time"
type AccountResourceVO struct {
Id int `json:"id"`
Pid int `json:"pid"`
Name *string `json:"name"`
Code *string `json:"code"`
Type int8 `json:"type"`
Meta string `json:"meta"`
}
// 账号拥有的资源vo
type AccountResourceVOList []AccountResourceVO
type resourceItem struct {
AccountResourceVO
Children []resourceItem `json:"children"`
}
func (m *AccountResourceVOList) ToTrees(pid int) []resourceItem {
var resourceTree []resourceItem
list := m.findChildren(pid)
if len(list) == 0 {
return resourceTree
}
for _, v := range list {
Children := m.ToTrees(int(v.Id))
resourceTree = append(resourceTree, resourceItem{v, Children})
}
return resourceTree
}
func (m *AccountResourceVOList) findChildren(pid int) []AccountResourceVO {
child := []AccountResourceVO{}
for _, v := range *m {
if v.Pid == pid {
child = append(child, v)
}
}
return child
}
// 系统管理/资源管理
type ResourceManageVO struct {
Id int `json:"id"`
Pid int `json:"pid"`
Name string `json:"name"`
Type int `json:"type"`
Status int `json:"status"`
Creator string `json:"creator"`
CreateTime *time.Time `json:"createTime"`
}
type ResourceManageVOList []ResourceManageVO
type resourceManageItem struct {
ResourceManageVO
Children []resourceManageItem `json:"children"`
}
func (m *ResourceManageVOList) ToTrees(pid int) []resourceManageItem {
var resourceTree []resourceManageItem
list := m.findChildren(pid)
if len(list) == 0 {
return resourceTree
}
for _, v := range list {
Children := m.ToTrees(int(v.Id))
resourceTree = append(resourceTree, resourceManageItem{v, Children})
}
return resourceTree
}
func (m *ResourceManageVOList) findChildren(pid int) []ResourceManageVO {
child := []ResourceManageVO{}
for _, v := range *m {
if v.Pid == pid {
child = append(child, v)
}
}
return child
}