Merge branch 'develop' into master
This commit is contained in:
@@ -40,9 +40,9 @@ func BreakPointContinue(content []byte, fileName string, contentNumber int, cont
|
||||
func CheckMd5(content []byte, chunkMd5 string) (CanUpload bool) {
|
||||
fileMd5 := MD5V(content)
|
||||
if fileMd5 == chunkMd5 {
|
||||
return true // "可以继续上传"
|
||||
return true // 可以继续上传
|
||||
} else {
|
||||
return false // "切片不完整,废弃"
|
||||
return false // 切片不完整,废弃
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,6 @@ func MakeFile(fileName string, FileMd5 string) (error, string) {
|
||||
return err, finishDir + fileName
|
||||
}
|
||||
}
|
||||
|
||||
return nil, finishDir + fileName
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gin-vue-admin/global"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewDefaultRedisStore() *RedisStore {
|
||||
return &RedisStore{
|
||||
Expiration: time.Second * 180,
|
||||
PreKey: "CAPTCHA_",
|
||||
}
|
||||
}
|
||||
|
||||
type RedisStore struct {
|
||||
Expiration time.Duration
|
||||
PreKey string
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
func (rs *RedisStore) UseWithCtx(ctx context.Context) base64Captcha.Store {
|
||||
rs.Context = ctx
|
||||
return rs
|
||||
}
|
||||
|
||||
func (rs *RedisStore) Set(id string, value string) {
|
||||
err := global.GVA_REDIS.Set(rs.Context, rs.PreKey+id, value, rs.Expiration).Err()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("RedisStoreSetError!", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
func (rs *RedisStore) Get(key string, clear bool) string {
|
||||
val, err := global.GVA_REDIS.Get(rs.Context, key).Result()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("RedisStoreGetError!", zap.Error(err))
|
||||
return ""
|
||||
}
|
||||
if clear {
|
||||
err := global.GVA_REDIS.Del(rs.Context, key).Err()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("RedisStoreClearError!", zap.Error(err))
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (rs *RedisStore) Verify(id, answer string, clear bool) bool {
|
||||
key := rs.PreKey + id
|
||||
v := rs.Get(key, clear)
|
||||
return v == answer
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
systemReq "gin-vue-admin/model/system/request"
|
||||
"github.com/gin-gonic/gin"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
// 从Gin的Context中获取从jwt解析出来的用户ID
|
||||
func GetUserID(c *gin.Context) uint {
|
||||
if claims, exists := c.Get("claims"); !exists {
|
||||
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户ID失败, 请检查路由是否使用jwt中间件!")
|
||||
return 0
|
||||
} else {
|
||||
waitUse := claims.(*systemReq.CustomClaims)
|
||||
return waitUse.ID
|
||||
}
|
||||
}
|
||||
|
||||
// 从Gin的Context中获取从jwt解析出来的用户UUID
|
||||
func GetUserUuid(c *gin.Context) uuid.UUID {
|
||||
if claims, exists := c.Get("claims"); !exists {
|
||||
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件!")
|
||||
return uuid.UUID{}
|
||||
} else {
|
||||
waitUse := claims.(*systemReq.CustomClaims)
|
||||
return waitUse.UUID
|
||||
}
|
||||
}
|
||||
|
||||
// 从Gin的Context中获取从jwt解析出来的用户角色id
|
||||
func GetUserAuthorityId(c *gin.Context) string {
|
||||
if claims, exists := c.Get("claims"); !exists {
|
||||
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件!")
|
||||
return ""
|
||||
} else {
|
||||
waitUse := claims.(*systemReq.CustomClaims)
|
||||
return waitUse.AuthorityId
|
||||
}
|
||||
}
|
||||
|
||||
// 从Gin的Context中获取从jwt解析出来的用户角色id
|
||||
func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
|
||||
if claims, exists := c.Get("claims"); !exists {
|
||||
global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件!")
|
||||
return nil
|
||||
} else {
|
||||
waitUse := claims.(*systemReq.CustomClaims)
|
||||
return waitUse
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,8 @@ import (
|
||||
//@author: [songzhibin97](https://github.com/songzhibin97)
|
||||
//@function: ClearTable
|
||||
//@description: 清理数据库表数据
|
||||
//@param: target db(数据库对象) *gorm.DB,tableName(表名) string,compareField(比较字段) string , interval string 间隔
|
||||
//@return: err
|
||||
//@param: db(数据库对象) *gorm.DB, tableName(表名) string, compareField(比较字段) string, interval(间隔) string
|
||||
//@return: error
|
||||
|
||||
func ClearTable(db *gorm.DB, tableName string, compareField string, interval string) error {
|
||||
if db == nil {
|
||||
|
||||
@@ -37,9 +37,9 @@ func CreateDir(dirs ...string) (err error) {
|
||||
}
|
||||
if !exist {
|
||||
global.GVA_LOG.Debug("create directory" + v)
|
||||
err = os.MkdirAll(v, os.ModePerm)
|
||||
if err != nil {
|
||||
if err := os.MkdirAll(v, os.ModePerm); err != nil {
|
||||
global.GVA_LOG.Error("create directory"+v, zap.Any(" error:", err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,11 +42,15 @@ Redirect:
|
||||
return os.Rename(src, dst)
|
||||
}
|
||||
|
||||
func DeLFile(filePath string) error {
|
||||
return os.RemoveAll(filePath)
|
||||
}
|
||||
|
||||
//@author: [songzhibin97](https://github.com/songzhibin97)
|
||||
//@function: TrimSpace
|
||||
//@description: 去除结构体空格
|
||||
//@param: target interface (target: 目标结构体,传入必须是指针类型)
|
||||
//@return: err error
|
||||
//@return: null
|
||||
|
||||
func TrimSpace(target interface{}) {
|
||||
t := reflect.TypeOf(target)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
@@ -13,11 +14,20 @@ import (
|
||||
//@function: AutoInjectionCode
|
||||
//@description: 向文件中固定注释位置写入代码
|
||||
//@param: filepath string, funcName string, codeData string
|
||||
//@return: err error
|
||||
//@return: error
|
||||
|
||||
const (
|
||||
startComment = "Code generated by gin-vue-admin Begin; DO NOT EDIT."
|
||||
endComment = "Code generated by gin-vue-admin End; DO NOT EDIT."
|
||||
)
|
||||
|
||||
//@author: [LeonardWang](https://github.com/WangLeonard)
|
||||
//@function: AutoInjectionCode
|
||||
//@description: 向文件中固定注释位置写入代码
|
||||
//@param: filepath string, funcName string, codeData string
|
||||
//@return: error
|
||||
|
||||
func AutoInjectionCode(filepath string, funcName string, codeData string) error {
|
||||
startComment := "Code generated by gin-vue-admin Begin; DO NOT EDIT."
|
||||
endComment := "Code generated by gin-vue-admin End; DO NOT EDIT."
|
||||
srcData, err := ioutil.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -141,3 +151,30 @@ func checkExist(srcData *[]byte, startPos int, endPos int, blockStmt *ast.BlockS
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func AutoClearCode(filepath string, codeData string) error {
|
||||
srcData, err := ioutil.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srcData, err = cleanCode(codeData, string(srcData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(filepath, srcData, 0600)
|
||||
}
|
||||
|
||||
func cleanCode(clearCode string, srcData string) ([]byte, error) {
|
||||
bf := make([]rune, 0, 1024)
|
||||
for i, v := range srcData {
|
||||
if v == '\n' {
|
||||
if strings.TrimSpace(string(bf)) == clearCode {
|
||||
return append([]byte(srcData[:i-len(bf)]), []byte(srcData[i+1:])...), nil
|
||||
}
|
||||
bf = (bf)[:0]
|
||||
continue
|
||||
}
|
||||
bf = append(bf, v)
|
||||
}
|
||||
return []byte(srcData), errors.New("未找到内容")
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
"go.uber.org/zap"
|
||||
"mime/multipart"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -25,9 +24,10 @@ func (*AliyunOSS) UploadFile(file *multipart.FileHeader) (string, string, error)
|
||||
global.GVA_LOG.Error("function file.Open() Failed", zap.Any("err", openError.Error()))
|
||||
return "", "", errors.New("function file.Open() Failed, err:" + openError.Error())
|
||||
}
|
||||
|
||||
//上传阿里云路径 文件名格式 自己可以改 建议保证唯一性
|
||||
yunFileTmpPath := filepath.Join("uploads", time.Now().Format("2006-01-02")) + "/" + file.Filename
|
||||
defer f.Close() // 创建文件 defer 关闭
|
||||
// 上传阿里云路径 文件名格式 自己可以改 建议保证唯一性
|
||||
//yunFileTmpPath := filepath.Join("uploads", time.Now().Format("2006-01-02")) + "/" + file.Filename
|
||||
yunFileTmpPath := global.GVA_CONFIG.AliyunOSS.BasePath + "/" + "uploads" + "/" + time.Now().Format("2006-01-02") + "/" + file.Filename
|
||||
|
||||
// 上传文件流。
|
||||
err = bucket.PutObject(yunFileTmpPath, f)
|
||||
|
||||
@@ -38,6 +38,7 @@ func (*Qiniu) UploadFile(file *multipart.FileHeader) (string, string, error) {
|
||||
|
||||
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
|
||||
}
|
||||
defer f.Close() // 创建文件 defer 关闭
|
||||
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性
|
||||
putErr := formUploader.Put(context.Background(), &ret, upToken, fileKey, f, file.Size, &putExtra)
|
||||
if putErr != nil {
|
||||
@@ -71,8 +72,7 @@ func (*Qiniu) DeleteFile(key string) error {
|
||||
//@object: *Qiniu
|
||||
//@function: qiniuConfig
|
||||
//@description: 根据配置文件进行返回七牛云的配置
|
||||
//@param: key string
|
||||
//@return: error
|
||||
//@return: *storage.Config
|
||||
|
||||
func qiniuConfig() *storage.Config {
|
||||
cfg := storage.Config{
|
||||
|
||||
@@ -24,6 +24,7 @@ func (*TencentCOS) UploadFile(file *multipart.FileHeader) (string, string, error
|
||||
global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error()))
|
||||
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
|
||||
}
|
||||
defer f.Close() // 创建文件 defer 关闭
|
||||
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename)
|
||||
|
||||
_, err := client.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+"/"+fileKey, f, nil)
|
||||
|
||||
@@ -32,8 +32,7 @@ func RegisterRule(key string, rule Rules) (err error) {
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: NotEmpty
|
||||
//@description: 非空 不能为其对应类型的0值
|
||||
//@param: key string, rule Rules
|
||||
//@return: err error
|
||||
//@return: string
|
||||
|
||||
func NotEmpty() string {
|
||||
return "notEmpty"
|
||||
|
||||
@@ -14,5 +14,5 @@ var (
|
||||
AuthorityIdVerify = Rules{"AuthorityId": {NotEmpty()}}
|
||||
OldAuthorityVerify = Rules{"OldAuthorityId": {NotEmpty()}}
|
||||
ChangePasswordVerify = Rules{"Username": {NotEmpty()}, "Password": {NotEmpty()}, "NewPassword": {NotEmpty()}}
|
||||
SetUserAuthorityVerify = Rules{"UUID": {NotEmpty(), RegexpMatch("[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}")}, "AuthorityId": {NotEmpty()}}
|
||||
SetUserAuthorityVerify = Rules{"AuthorityId": {NotEmpty()}}
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
//@function: ZipFiles
|
||||
//@description: 压缩文件
|
||||
//@param: filename string, files []string, oldform, newform string
|
||||
//@param: filename string, files []string, oldForm, newForm string
|
||||
//@return: error
|
||||
|
||||
func ZipFiles(filename string, files []string, oldForm, newForm string) error {
|
||||
|
||||
Reference in New Issue
Block a user