feat: 前端升级至vue3,后端代码结构重构,新增权限管理相关功能

This commit is contained in:
meilin.huang
2021-06-07 17:22:07 +08:00
parent af0d51293e
commit e9b58b4eab
370 changed files with 22339 additions and 18399 deletions

25
base/cache/cache.go vendored Normal file
View File

@@ -0,0 +1,25 @@
package cache
type Cache interface {
// 添加缓存,如果缓存则返回错误
Add(k string, v interface{}) error
// 如果不存在则添加缓存值,否则直接返回
AddIfAbsent(k string, v interface{})
// 如果存在则直接返回否则调用getValue回调函数获取值并添加该缓存值
// @return 缓存值
ComputeIfAbsent(k string, getValueFunc func(string) (interface{}, error)) (interface{}, error)
// 获取缓存值参数1为值参数2->是否存在该缓存
Get(k string) (interface{}, bool)
// 缓存数量
Count() int
// 删除缓存
Delete(k string)
// 清空所有缓存
Clear()
}

426
base/cache/timed_cache.go vendored Normal file
View File

@@ -0,0 +1,426 @@
package cache
import (
"encoding/gob"
"fmt"
"io"
"os"
"sync"
"time"
)
type Item struct {
Value interface{} // 对象
Expiration int64 // 缓存有效时间
UseCount int64 // 使用次数
AccessTime int64 // 访问时间
}
// 是否过期
func (item Item) Expired() bool {
if item.Expiration == 0 {
return false
}
return time.Now().UnixNano() > item.AccessTime+item.Expiration
}
// 是否过期
// @return 值 and 是否过期
func (item *Item) GetValue(updateAccessTime bool) (interface{}, bool) {
isExpired := item.Expired()
// 更新最后访问时间,用于增加值的有效期
if !isExpired && updateAccessTime {
item.AccessTime = time.Now().UnixNano()
}
item.UseCount = item.UseCount + 1
return item.Value, isExpired
}
const (
// 无过期时间
NoExpiration time.Duration = -1
// 默认过期时间
DefaultExpiration time.Duration = 0
// 默认清理缓存时间差
DefaultCleanupInterval = 10
)
type TimedCache struct {
*timedcache
}
type timedcache struct {
defaultExpiration time.Duration
updateAccessTime bool // 是否更新最后访问时间
items map[string]*Item
mu sync.RWMutex
onEvicted func(string, interface{}) // 移除时回调函数
janitor *janitor
}
// Add an item to the cache only if an item doesn't already exist for the given
// key, or if the existing item has expired. Returns an error otherwise.
func (c *timedcache) Add(k string, x interface{}, d time.Duration) error {
c.mu.Lock()
defer c.mu.Unlock()
_, found := c.get(k)
if found {
return fmt.Errorf("Item %s already exists", k)
}
c.set(k, x, d)
return nil
}
func (c *timedcache) AddIfAbsent(k string, x interface{}) {
c.mu.Lock()
defer c.mu.Unlock()
_, found := c.get(k)
if found {
return
}
c.set(k, x, c.defaultExpiration)
}
func (c *timedcache) ComputeIfAbsent(k string, getValueFunc func(string) (interface{}, error)) (interface{}, error) {
c.mu.Lock()
defer c.mu.Unlock()
value, found := c.get(k)
if found {
return value, nil
}
value, err := getValueFunc(k)
if err != nil {
return nil, err
}
c.set(k, value, c.defaultExpiration)
return value, nil
}
func (c *timedcache) set(k string, x interface{}, d time.Duration) {
var e int64
if d == DefaultExpiration {
d = c.defaultExpiration
}
if d > 0 {
e = d.Nanoseconds()
}
c.items[k] = &Item{
Value: x,
Expiration: e,
AccessTime: time.Now().UnixNano(),
}
}
// Get an item from the cache. Returns the item or nil, and a bool indicating
// whether the key was found.
func (c *timedcache) Get(k string) (interface{}, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
return c.get(k)
}
func (c *timedcache) get(k string) (interface{}, bool) {
item, found := c.items[k]
if !found {
return nil, false
}
value, expired := item.GetValue(c.updateAccessTime)
if expired {
// c.Delete(k)
return nil, false
}
return value, true
}
// Increment an item of type int, int8, int16, int32, int64, uintptr, uint,
// uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the
// item's value is not an integer, if it was not found, or if it is not
// possible to increment it by n. To retrieve the incremented value, use one
// of the specialized methods, e.g. IncrementInt64.
func (c *timedcache) Increment(k string, n int64) error {
c.mu.Lock()
v, found := c.items[k]
if !found || v.Expired() {
c.mu.Unlock()
return fmt.Errorf("Item %s not found", k)
}
switch v.Value.(type) {
case int:
v.Value = v.Value.(int) + int(n)
case int8:
v.Value = v.Value.(int8) + int8(n)
case int16:
v.Value = v.Value.(int16) + int16(n)
case int32:
v.Value = v.Value.(int32) + int32(n)
case int64:
v.Value = v.Value.(int64) + n
case uint:
v.Value = v.Value.(uint) + uint(n)
case uintptr:
v.Value = v.Value.(uintptr) + uintptr(n)
case uint8:
v.Value = v.Value.(uint8) + uint8(n)
case uint16:
v.Value = v.Value.(uint16) + uint16(n)
case uint32:
v.Value = v.Value.(uint32) + uint32(n)
case uint64:
v.Value = v.Value.(uint64) + uint64(n)
case float32:
v.Value = v.Value.(float32) + float32(n)
case float64:
v.Value = v.Value.(float64) + float64(n)
default:
c.mu.Unlock()
return fmt.Errorf("The value for %s is not an integer", k)
}
c.items[k] = v
c.mu.Unlock()
return nil
}
// Returns the number of items in the cache. This may include items that have
// expired, but have not yet been cleaned up.
func (c *timedcache) Count() int {
c.mu.RLock()
n := len(c.items)
c.mu.RUnlock()
return n
}
// Copies all unexpired items in the cache into a new map and returns it.
func (c *timedcache) Items() map[string]*Item {
c.mu.RLock()
defer c.mu.RUnlock()
m := make(map[string]*Item, len(c.items))
now := time.Now().UnixNano()
for k, v := range c.items {
// "Inlining" of Expired
if v.Expiration > 0 {
if now > (v.Expiration + v.AccessTime) {
continue
}
}
m[k] = v
}
return m
}
// 删除指定key的数据
func (c *timedcache) Delete(k string) {
c.mu.Lock()
v, evicted := c.delete(k)
c.mu.Unlock()
if evicted {
c.onEvicted(k, v)
}
}
func (c *timedcache) delete(k string) (interface{}, bool) {
// 如果有移除回调函数,则返回值及是否有删除回调函数用于进行回调处理
if c.onEvicted != nil {
if v, found := c.items[k]; found {
delete(c.items, k)
return v.Value, true
}
}
delete(c.items, k)
return nil, false
}
type keyAndValue struct {
key string
value interface{}
}
// Delete all expired items from the cache.
func (c *timedcache) DeleteExpired() {
var evictedItems []keyAndValue
now := time.Now().UnixNano()
c.mu.Lock()
for k, v := range c.items {
// "Inlining" of expired
if v.Expiration > 0 && now > (v.Expiration+v.AccessTime) {
ov, evicted := c.delete(k)
if evicted {
evictedItems = append(evictedItems, keyAndValue{k, ov})
}
}
}
c.mu.Unlock()
for _, v := range evictedItems {
c.onEvicted(v.key, v.value)
}
}
// 清空所有缓存
func (c *timedcache) Clear() {
c.mu.Lock()
c.items = map[string]*Item{}
c.mu.Unlock()
}
// Write the cache's items (using Gob) to an io.Writer.
//
// NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the
// documentation for NewFrom().)
func (c *timedcache) Save(w io.Writer) (err error) {
enc := gob.NewEncoder(w)
defer func() {
if x := recover(); x != nil {
err = fmt.Errorf("Error registering item types with Gob library")
}
}()
c.mu.RLock()
defer c.mu.RUnlock()
for _, v := range c.items {
gob.Register(v.Value)
}
err = enc.Encode(&c.items)
return
}
// Save the cache's items to the given filename, creating the file if it
// doesn't exist, and overwriting it if it does.
//
// NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the
// documentation for NewFrom().)
func (c *timedcache) SaveFile(fname string) error {
fp, err := os.Create(fname)
if err != nil {
return err
}
err = c.Save(fp)
if err != nil {
fp.Close()
return err
}
return fp.Close()
}
// Add (Gob-serialized) cache items from an io.Reader, excluding any items with
// keys that already exist (and haven't expired) in the current cache.
//
// NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the
// documentation for NewFrom().)
func (c *timedcache) Load(r io.Reader) error {
dec := gob.NewDecoder(r)
items := map[string]*Item{}
err := dec.Decode(&items)
if err == nil {
c.mu.Lock()
defer c.mu.Unlock()
for k, v := range items {
ov, found := c.items[k]
if !found || ov.Expired() {
c.items[k] = v
}
}
}
return err
}
// Load and add cache items from the given filename, excluding any items with
// keys that already exist in the current cache.
//
// NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the
// documentation for NewFrom().)
func (c *timedcache) LoadFile(fname string) error {
fp, err := os.Open(fname)
if err != nil {
return err
}
err = c.Load(fp)
if err != nil {
fp.Close()
return err
}
return fp.Close()
}
type janitor struct {
Interval time.Duration
stop chan bool
}
func (j *janitor) Run(c *timedcache) {
ticker := time.NewTicker(j.Interval)
for {
select {
case <-ticker.C:
c.DeleteExpired()
case <-j.stop:
ticker.Stop()
return
}
}
}
func stopJanitor(c *TimedCache) {
c.janitor.stop <- true
}
func runJanitor(c *timedcache, ci time.Duration) {
j := &janitor{
Interval: ci,
stop: make(chan bool),
}
c.janitor = j
go j.Run(c)
}
func newCache(de time.Duration, m map[string]*Item) *timedcache {
if de == 0 {
de = -1
}
c := &timedcache{
defaultExpiration: de,
items: m,
}
return c
}
func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item) *TimedCache {
c := newCache(de, m)
// This trick ensures that the janitor goroutine (which--granted it
// was enabled--is running DeleteExpired on c forever) does not keep
// the returned C object from being garbage collected. When it is
// garbage collected, the finalizer stops the janitor goroutine, after
// which c can be collected.
C := &TimedCache{c}
if ci > 0 {
runJanitor(c, ci)
// runtime.SetFinalizer(C, stopJanitor)
}
return C
}
// Return a new cache with a given default expiration duration and cleanup
// interval. If the expiration duration is less than one (or NoExpiration),
// the items in the cache never expire (by default), and must be deleted
// manually. If the cleanup interval is less than one, expired items are not
// deleted from the cache before calling c.DeleteExpired().
func NewTimedCache(defaultExpiration, cleanupInterval time.Duration) *TimedCache {
items := make(map[string]*Item)
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
}
// 调用删除函数时,会回调该剔除函数
func (c *timedcache) OnEvicted(f func(string, interface{})) *timedcache {
c.mu.Lock()
c.onEvicted = f
c.mu.Unlock()
return c
}
// 是否更新最后访问时间,是则会更新最后访问时间
// 即只要在指定缓存时间内都没有访问该缓存,则会失效,反之失效开始时间点为最后访问时间
func (c *timedcache) WithUpdateAccessTime(update bool) *timedcache {
c.mu.Lock()
c.updateAccessTime = update
c.mu.Unlock()
return c
}

View File

@@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"mayfly-go/base/biz"
"mayfly-go/base/mlog"
"mayfly-go/base/logger"
"mayfly-go/base/utils"
"reflect"
"runtime/debug"
@@ -13,7 +13,7 @@ import (
)
func init() {
log.SetFormatter(new(mlog.LogFormatter))
log.SetFormatter(new(logger.LogFormatter))
log.SetReportCaller(true)
}

View File

@@ -1,8 +1,9 @@
package ginx
import (
"fmt"
"mayfly-go/base/biz"
"mayfly-go/base/mlog"
"mayfly-go/base/global"
"mayfly-go/base/model"
"net/http"
"strconv"
@@ -40,6 +41,13 @@ func QueryInt(g *gin.Context, qm string, defaultInt int) int {
return qvi
}
// 获取路径参数
func PathParamInt(g *gin.Context, pm string) int {
value, _ := strconv.Atoi(g.Param(pm))
biz.IsTrue(value != 0, fmt.Sprintf("%s不存在", pm))
return value
}
// 文件下载
func Download(g *gin.Context, data []byte, filename string) {
g.Header("Content-Type", "application/octet-stream")
@@ -60,15 +68,15 @@ func ErrorRes(g *gin.Context, err interface{}) {
break
case error:
g.JSON(http.StatusOK, model.ServerError())
mlog.Log.Error(t)
global.Log.Error(t)
// panic(err)
break
case string:
g.JSON(http.StatusOK, model.ServerError())
mlog.Log.Error(t)
global.Log.Error(t)
// panic(err)
break
default:
mlog.Log.Error(t)
global.Log.Error(t)
}
}

View File

@@ -2,13 +2,13 @@ package global
import (
"mayfly-go/base/config"
"mayfly-go/base/mlog"
"mayfly-go/base/logger"
"gorm.io/gorm"
)
// 日志
var Log = mlog.Log
var Log = logger.Log
// config.yml配置文件映射对象
var Config = config.Conf

View File

@@ -1,8 +1,7 @@
package mlog
package logger
import (
"fmt"
"path/filepath"
"strings"
"time"
@@ -17,22 +16,25 @@ func init() {
// customFormatter.FullTimestamp = true
Log.SetFormatter(new(LogFormatter))
Log.SetReportCaller(true)
Log.SetLevel(logrus.DebugLevel)
}
type LogFormatter struct{}
func (l *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
timestamp := time.Now().Local().Format("2006-01-02 15:04:05.000")
level := entry.Level
logMsg := fmt.Sprintf("%s [%s]", timestamp, strings.ToUpper(level.String()))
// 如果存在调用信息且为error级别以上记录文件及行号
if caller := entry.Caller; caller != nil {
var fp string
if level <= logrus.ErrorLevel {
fp = caller.File
// 全路径切割,只获取项目相关路径,
// 即/Users/hml/Desktop/project/go/mayfly-go/server/test.go只获取/server/test.go
ps := strings.Split(caller.File, "mayfly-go/")
if len(ps) >= 2 {
fp = ps[1]
} else {
fp = filepath.Base(caller.File)
fp = ps[0]
}
logMsg = logMsg + fmt.Sprintf(" [%s:%d]", fp, caller.Line)
}

View File

@@ -1,11 +1,14 @@
package model
import (
"fmt"
"mayfly-go/base/global"
"strconv"
"strings"
"time"
"gorm.io/gorm"
)
type Model struct {
@@ -40,6 +43,25 @@ func (m *Model) SetBaseInfo(account *LoginAccount) {
m.ModifierId = id
}
func Tx(funcs ...func(db *gorm.DB) error) (err error) {
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
err = fmt.Errorf("%v", err)
}
}()
for _, f := range funcs {
err = f(tx)
if err != nil {
tx.Rollback()
return
}
}
err = tx.Commit().Error
return
}
// 根据id获取实体对象。model需为指针类型需要将查询出来的值赋值给model
//
// 若error不为nil则为不存在该记录
@@ -47,6 +69,33 @@ func GetById(model interface{}, id uint64, cols ...string) error {
return global.Db.Select(cols).Where("id = ?", id).First(model).Error
}
// 根据id列表查询
func GetByIdIn(model interface{}, list interface{}, ids []uint64, orderBy ...string) {
var idsStr string
for i, v := range ids {
idStr := strconv.Itoa(int(v))
if i == 0 {
idsStr += idStr
} else {
idsStr += ("," + idStr)
}
}
var orderByStr string
if orderBy == nil {
orderByStr = "id desc"
} else {
orderByStr = strings.Join(orderBy, ",")
}
global.Db.Model(model).Where("id in (?)", idsStr).Order(orderByStr).Find(list)
}
// 根据id列表查询
func CountBy(model interface{}) int64 {
var count int64
global.Db.Model(model).Where(model).Count(&count)
return count
}
// 根据id更新model更新字段为model中不为空的值即int类型不为0ptr类型不为nil这类字段值
func UpdateById(model interface{}) error {
return global.Db.Model(model).Updates(model).Error
@@ -57,6 +106,11 @@ func DeleteById(model interface{}, id uint64) error {
return global.Db.Delete(model, "id = ?", id).Error
}
// 根据条件删除
func DeleteByCondition(model interface{}) error {
return global.Db.Where(model).Delete(model).Error
}
// 插入model
func Insert(model interface{}) error {
return global.Db.Create(model).Error
@@ -64,11 +118,24 @@ func Insert(model interface{}) error {
// 获取满足model中不为空的字段值条件的所有数据.
//
// @param list为数组类型 如 var users []*User可指定为非model结构体即只包含需要返回的字段结构体
// @param list为数组类型 如 var users *[]User可指定为非model结构体即只包含需要返回的字段结构体
func ListBy(model interface{}, list interface{}, cols ...string) {
global.Db.Model(model).Select(cols).Where(model).Find(list)
}
// 获取满足model中不为空的字段值条件的所有数据.
//
// @param list为数组类型 如 var users *[]User可指定为非model结构体
func ListByOrder(model interface{}, list interface{}, order ...string) {
var orderByStr string
if order == nil {
orderByStr = "id desc"
} else {
orderByStr = strings.Join(order, ",")
}
global.Db.Model(model).Where(model).Order(orderByStr).Find(list)
}
// 获取满足model中不为空的字段值条件的单个对象。model需为指针类型需要将查询出来的值赋值给model
//
// 若 error不为nil则为不存在该记录
@@ -126,3 +193,7 @@ func GetListBySql(sql string, params ...interface{}) []map[string]interface{} {
global.Db.Raw(sql, params).Scan(&maps)
return maps
}
func GetListBySql2Model(sql string, toEntity interface{}, params ...interface{}) {
global.Db.Raw(sql, params).Find(toEntity)
}

33
base/utils/array_utils.go Normal file
View File

@@ -0,0 +1,33 @@
package utils
// 数组比较
// 依次返回,新增值,删除值,以及不变值
func ArrayCompare(newArr []interface{}, oldArr []interface{}, compareFun func(interface{}, interface{}) bool) ([]interface{}, []interface{}, []interface{}) {
var unmodifierValue []interface{}
ni, oi := 0, 0
for {
if ni >= len(newArr) {
break
}
nv := newArr[ni]
for {
if oi >= len(oldArr) {
oi = 0
break
}
ov := oldArr[oi]
if compareFun(nv, ov) {
unmodifierValue = append(unmodifierValue, nv)
// 新数组移除该位置值
newArr = append(newArr[:ni], newArr[ni+1:]...)
oldArr = append(oldArr[:oi], oldArr[oi+1:]...)
ni = ni - 1
oi = oi - 1
}
oi = oi + 1
}
ni = ni + 1
}
return newArr, oldArr, unmodifierValue
}

View File

@@ -0,0 +1,17 @@
package utils
import (
"fmt"
"testing"
)
func TestArrayCompare(t *testing.T) {
newArr := []interface{}{1, 2, 3, 5}
oldArr := []interface{}{3, 6}
add, del, unmodifier := ArrayCompare(newArr, oldArr, func(i1, i2 interface{}) bool {
return i1.(int) == i2.(int)
})
fmt.Println(add...)
fmt.Println(del...)
fmt.Println(unmodifier...)
}

14
base/utils/json_utils.go Normal file
View File

@@ -0,0 +1,14 @@
package utils
import (
"encoding/json"
)
func Json2Map(jsonStr string) map[string]interface{} {
var res map[string]interface{}
if jsonStr == "" {
return res
}
_ = json.Unmarshal([]byte(jsonStr), &res)
return res
}

74
base/utils/tree_utils.go Normal file
View File

@@ -0,0 +1,74 @@
package utils
// ConvertToINodeArray 其他的结构体想要生成菜单树,直接实现这个接口
type INode interface {
// GetId获取id
GetId() int
// GetPid 获取父id
GetPid() int
// IsRoot 判断当前节点是否是顶层根节点
IsRoot() bool
SetChildren(childern interface{})
}
type INodes []INode
func (nodes INodes) Len() int {
return len(nodes)
}
func (nodes INodes) Swap(i, j int) {
nodes[i], nodes[j] = nodes[j], nodes[i]
}
func (nodes INodes) Less(i, j int) bool {
return nodes[i].GetId() < nodes[j].GetId()
}
// GenerateTree 自定义的结构体实现 INode 接口后调用此方法生成树结构
// nodes 需要生成树的节点
// selectedNode 生成树后选中的节点
// menuTrees 生成成功后的树结构对象
func GenerateTree(nodes []INode) (trees []INode) {
trees = []INode{}
// 定义顶层根和子节点
var roots, childs []INode
for _, v := range nodes {
if v.IsRoot() {
// 判断顶层根节点
roots = append(roots, v)
}
childs = append(childs, v)
}
for _, v := range roots {
// 递归
setChildren(v, childs)
trees = append(trees, v)
}
return
}
// recursiveTree 递归生成树结构
// tree 递归的树对象
// nodes 递归的节点
// selectedNodes 选中的节点
func setChildren(parent INode, nodes []INode) {
children := []INode{}
for _, v := range nodes {
if v.IsRoot() {
// 如果当前节点是顶层根节点就跳过
continue
}
if parent.GetId() == v.GetPid() {
children = append(children, v)
}
}
if len(children) == 0 {
return
}
parent.SetChildren(children)
for _, c := range children {
setChildren(c, nodes)
}
}