新增系统设置接口

This commit is contained in:
wxb
2020-08-07 10:22:52 +08:00
commit 389d2970c0
150 changed files with 21555 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
package utils
import (
"io/ioutil"
"mime/multipart"
"os"
"path"
)
// 获取文件大小
func GetSize(f multipart.File) (int, error) {
content, err := ioutil.ReadAll(f)
return len(content), err
}
// 获取文件后缀
func GetExt(fileName string) string {
return path.Ext(fileName)
}
//检查文件是否存在
func CheckExist(src string) bool {
_, err := os.Stat(src)
return os.IsNotExist(err)
}
// 检查文件权限
func CheckPermission(src string) bool {
_, err := os.Stat(src)
return os.IsPermission(err)
}
//如果不存在则新建文件夹
func IsNotExistMkDir(src string) error {
if exist := CheckExist(src); exist == false {
if err := MkDir(src); err != nil {
return err
}
}
return nil
}
//新建文件夹
func MkDir(src string) error {
err := os.MkdirAll(src, os.ModePerm)
if err != nil {
return err
}
return nil
}
// 打开文件
func Open(name string, flag int, perm os.FileMode) (*os.File, error) {
f, err := os.OpenFile(name, flag, perm)
if err != nil {
return nil, err
}
return f, nil
}
+42
View File
@@ -0,0 +1,42 @@
package utils
import (
"database/sql/driver"
"fmt"
"time"
)
// JSONTime format json time field by myself
type JSONTime struct {
time.Time
}
// MarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S
func (t JSONTime) MarshalJSON() ([]byte, error) {
if (t == JSONTime{}) {
formatted := fmt.Sprintf("\"%s\"", "")
return []byte(formatted), nil
} else {
formatted := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05"))
return []byte(formatted), nil
}
}
// Value insert timestamp into mysql need this function.
func (t JSONTime) Value() (driver.Value, error) {
var zeroTime time.Time
if t.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return t.Time, nil
}
// Scan valueof time.Time
func (t *JSONTime) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = JSONTime{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
+55
View File
@@ -0,0 +1,55 @@
package utils
import (
"net/http"
"time"
)
// api结构体
type APIException struct {
Code int `json:"code"`
Success bool `json:"success"`
Msg string `json:"msg"`
Timestamp int64 `json:"timestamp"`
Result interface{} `json:"result"`
}
// 实现接口
func (e *APIException) Error() string {
return e.Msg
}
func newAPIException(code int,msg string,data interface{},success bool) *APIException {
return &APIException{
Code: code,
Success: success,
Msg: msg,
Timestamp: time.Now().Unix(),
Result: data,
}
}
// 500 错误处理
func ServerError() *APIException {
return newAPIException(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError),nil,false)
}
// 404 错误
func NotFound() *APIException {
return newAPIException(http.StatusNotFound, http.StatusText(http.StatusNotFound),nil,false)
}
// 未知错误
func UnknownError(message string) *APIException {
return newAPIException(http.StatusForbidden, message,nil,false)
}
// 参数错误
func ParameterError(message string) *APIException {
return newAPIException(http.StatusBadRequest, message,nil,false)
}
// 授权错误
func AuthError(message string) *APIException {
return newAPIException(http.StatusBadRequest, message,nil,false)
}
// 200
func ResponseJson(message string,data interface{},success bool) *APIException {
return newAPIException(http.StatusOK,message,data,success)
}
+94
View File
@@ -0,0 +1,94 @@
package utils
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
uuid "github.com/satori/go.uuid"
"io/ioutil"
"os"
"strings"
"time"
)
func Hmac(data string) string {
h := md5.New()
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}
func IsStringEmpty(str string) bool {
return strings.Trim(str, " ") == ""
}
func GetUUID() string {
u := uuid.NewV4()
return strings.ReplaceAll(u.String(),"-","")
}
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func Base64ToImage(imageBase64 string) ([]byte, error) {
image, err := base64.StdEncoding.DecodeString(imageBase64)
if err != nil {
return nil, err
}
return image, nil
}
func GetDirFiles(dir string) ([]string, error) {
dirList, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
filesRet := make([]string, 0)
for _, file := range dirList {
if file.IsDir() {
files, err := GetDirFiles(dir + string(os.PathSeparator) + file.Name())
if err != nil {
return nil, err
}
filesRet = append(filesRet, files...)
} else {
filesRet = append(filesRet, dir+string(os.PathSeparator)+file.Name())
}
}
return filesRet, nil
}
func GetCurrentTimeStamp() int64 {
return time.Now().UnixNano() / 1e6
}
//slice去重
func RemoveRepByMap(slc []string) []string {
result := []string{}
tempMap := map[string]byte{}
for _, e := range slc {
l := len(tempMap)
tempMap[e] = 0
if len(tempMap) != l {
result = append(result, e)
}
}
return result
}