gin-vue-admin 2.0代码重构

This commit is contained in:
QM303176530
2020-04-04 21:39:07 +08:00
parent 5ed41ddd8c
commit 1b9adea284
202 changed files with 1240 additions and 1299 deletions
+10
View File
@@ -0,0 +1,10 @@
package utils
import (
"fmt"
"strings"
)
func ArrayToString(array []interface{}) string {
return strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1)
}
+74
View File
@@ -0,0 +1,74 @@
package utils
import (
"io/ioutil"
"os"
"strconv"
)
// 前端传来文件片与当前片为什么文件的第几片
// 后端拿到以后比较次分片是否上传 或者是否为不完全片
// 前端发送每片多大
// 前端告知是否为最后一片且是否完成
const breakpointDir = "./breakpointDir/"
const finishDir = "./fileDir/"
func BreakPointContinue(content []byte, fileName string, contentNumber int, contentTotal int, fileMd5 string) (error, string) {
path := breakpointDir + fileMd5 + "/"
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
return err, path
}
err, pathc := makeFileContent(content, fileName, path, contentNumber)
return err, pathc
}
func CheckMd5(content []byte, chunkMd5 string) (CanUpload bool) {
fileMd5 := MD5V(content)
if fileMd5 == chunkMd5 {
return true // "可以继续上传"
} else {
return false // "切片不完整,废弃"
}
}
func makeFileContent(content []byte, fileName string, FileDir string, contentNumber int) (error, string) {
path := FileDir + fileName + "_" + strconv.Itoa(contentNumber)
f, err := os.Create(path)
defer f.Close()
if err != nil {
return err, path
} else {
_, err = f.Write(content)
if err != nil {
return err, path
}
}
return nil, path
}
func MakeFile(fileName string, FileMd5 string) (error, string) {
rd, err := ioutil.ReadDir(breakpointDir + FileMd5)
if err != nil {
return err, finishDir + fileName
}
_ = os.MkdirAll(finishDir, os.ModePerm)
fd, _ := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
for k, _ := range rd {
content, _ := ioutil.ReadFile(breakpointDir + FileMd5 + "/" + fileName + "_" + strconv.Itoa(k))
_, err = fd.Write(content)
if err != nil {
_ = os.Remove(finishDir + fileName)
return err, finishDir + fileName
}
}
defer fd.Close()
return nil, finishDir + fileName
}
func RemoveChunk(FileMd5 string) error {
err := os.RemoveAll(breakpointDir + FileMd5)
return err
}
+41
View File
@@ -0,0 +1,41 @@
package tools
import (
"bytes"
"crypto/cipher"
"crypto/des"
)
func padding(src []byte, blocksize int) []byte {
n := len(src)
padnum := blocksize - n%blocksize
pad := bytes.Repeat([]byte{byte(padnum)}, padnum)
dst := append(src, pad...)
return dst
}
func unpadding(src []byte) []byte {
n := len(src)
unpadnum := int(src[n-1])
dst := src[:n-unpadnum]
return dst
}
func EncryptDES(src []byte) []byte {
key := []byte("qimiao66")
block, _ := des.NewCipher(key)
src = padding(src, block.BlockSize())
blockmode := cipher.NewCBCEncrypter(block, key)
blockmode.CryptBlocks(src, src)
return src
}
func DecryptDES(src []byte) []byte {
key := []byte("qimiao66")
block, _ := des.NewCipher(key)
blockmode := cipher.NewCBCDecrypter(block, key)
blockmode.CryptBlocks(src, src)
src = unpadding(src)
return src
}
+14
View File
@@ -0,0 +1,14 @@
package tools
import "os"
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
+38
View File
@@ -0,0 +1,38 @@
// 空值校验工具 仅用于检验空字符串 其余类型请勿使用
package tools
import (
"errors"
"fmt"
"reflect"
)
func HasGap(input interface{}) error {
getType := reflect.TypeOf(input)
getValue := reflect.ValueOf(input)
// 获取方法字段
for i := 0; i < getType.NumField(); i++ {
field := getType.Field(i)
value := getValue.Field(i).Interface()
switch value.(type) {
case string:
if value == "" {
fmt.Printf("%s为空", field.Name)
return errors.New(fmt.Sprintf("%s为空", field.Name))
}
default:
if value == nil {
fmt.Printf("%s为空", field.Name)
return errors.New(fmt.Sprintf("%s为空", field.Name))
}
}
}
// 获取方法
// 1. 先获取interface的reflect.Type,然后通过.NumMethod进行遍历
//for i := 0; i < getType.NumMethod(); i++ {
// m := getType.Method(i)
// fmt.Printf("%s: %v\n", m.Name, m.Type)
//}
return nil
}
+12
View File
@@ -0,0 +1,12 @@
package tools
import (
"crypto/md5"
"encoding/hex"
)
func MD5V(str []byte) string {
h := md5.New()
h.Write(str)
return hex.EncodeToString(h.Sum(nil))
}
+70
View File
@@ -0,0 +1,70 @@
package servers
import (
"context"
"fmt"
"gin-vue-admin/config"
"github.com/qiniu/api.v7/auth/qbox"
"github.com/qiniu/api.v7/storage"
"mime/multipart"
"time"
)
var accessKey string = config.GinVueAdminconfig.Qiniu.AccessKey // 你在七牛云的accessKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西
var secretKey string = config.GinVueAdminconfig.Qiniu.SecretKey // 你在七牛云的secretKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西
// 接收两个参数 一个文件流 一个 bucket 你的七牛云标准空间的名字
func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err error, path string, key string) {
putPolicy := storage.PutPolicy{
Scope: bucket,
}
mac := qbox.NewMac(accessKey, secretKey)
upToken := putPolicy.UploadToken(mac)
cfg := storage.Config{}
// 空间对应的机房
cfg.Zone = &storage.ZoneHuadong
// 是否使用https域名
cfg.UseHTTPS = false
// 上传是否使用CDN上传加速
cfg.UseCdnDomains = false
formUploader := storage.NewFormUploader(&cfg)
ret := storage.PutRet{}
putExtra := storage.PutExtra{
Params: map[string]string{
"x:name": "github logo",
},
}
f, e := file.Open()
if e != nil {
fmt.Println(e)
return e, "", ""
}
dataLen := file.Size
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性
err = formUploader.Put(context.Background(), &ret, upToken, fileKey, f, dataLen, &putExtra)
if err != nil {
fmt.Println(err)
//qmlog.QMLog.Info(err)
return err, "", ""
}
return err, urlPath + "/" + ret.Key, ret.Key
}
func DeleteFile(bucket string, key string) error {
mac := qbox.NewMac(accessKey, secretKey)
cfg := storage.Config{
// 是否使用https域名进行资源管理
UseHTTPS: false,
}
// 指定空间所在的区域,如果不指定将自动探测
// 如果没有特殊需求,默认不需要指定
//cfg.Zone=&storage.ZoneHuabei
bucketManager := storage.NewBucketManager(mac, &cfg)
err := bucketManager.Delete(bucket, key)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
+57
View File
@@ -0,0 +1,57 @@
package tools
import (
"archive/zip"
"io"
"os"
"strings"
)
func ZipFiles(filename string, files []string, oldform, newform string) error {
newZipFile, err := os.Create(filename)
if err != nil {
return err
}
defer newZipFile.Close()
zipWriter := zip.NewWriter(newZipFile)
defer zipWriter.Close()
// 把files添加到zip中
for _, file := range files {
zipfile, err := os.Open(file)
if err != nil {
return err
}
defer zipfile.Close()
// 获取file的基础信息
info, err := zipfile.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
//使用上面的FileInforHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面
header.Name = strings.Replace(file, oldform, newform, -1)
// 优化压缩
// 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants
header.Method = zip.Deflate
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
if _, err = io.Copy(writer, zipfile); err != nil {
return err
}
}
return nil
}