增加备份和导出功能

This commit is contained in:
dushixiang
2021-11-15 14:21:46 +08:00
parent 52309870d8
commit 1d232f7269
14 changed files with 472 additions and 134 deletions

56
server/utils/jsontime.go Normal file
View File

@ -0,0 +1,56 @@
package utils
import (
"database/sql/driver"
"fmt"
"strings"
"time"
)
type JsonTime struct {
time.Time
}
func NewJsonTime(t time.Time) JsonTime {
return JsonTime{
Time: t,
}
}
func NowJsonTime() JsonTime {
return JsonTime{
Time: time.Now(),
}
}
func (j *JsonTime) MarshalJSON() ([]byte, error) {
var stamp = fmt.Sprintf("\"%s\"", j.Format("2006-01-02 15:04:05"))
return []byte(stamp), nil
}
func (j *JsonTime) UnmarshalJSON(b []byte) error {
s := strings.ReplaceAll(string(b), "\"", "")
t, err := time.Parse("2006-01-02 15:04:05", s)
if err != nil {
return err
}
*j = NewJsonTime(t)
return nil
}
func (j JsonTime) Value() (driver.Value, error) {
var zeroTime time.Time
if j.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return j.Time, nil
}
func (j *JsonTime) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*j = JsonTime{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}

44
server/utils/password.go Normal file
View File

@ -0,0 +1,44 @@
package utils
import (
"math/rand"
"time"
"golang.org/x/crypto/bcrypt"
)
type Bcrypt struct {
cost int
}
func (b *Bcrypt) Encode(password []byte) ([]byte, error) {
return bcrypt.GenerateFromPassword(password, b.cost)
}
func (b *Bcrypt) Match(hashedPassword, password []byte) error {
return bcrypt.CompareHashAndPassword(hashedPassword, password)
}
var Encoder = Bcrypt{
cost: bcrypt.DefaultCost,
}
func GenPassword() string {
rand.Seed(time.Now().UnixNano())
digits := "0123456789"
specials := "~=+%^*/()[]{}/!@#$?|"
all := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
digits + specials
length := 8
buf := make([]byte, length)
buf[0] = digits[rand.Intn(len(digits))]
buf[1] = specials[rand.Intn(len(specials))]
for i := 2; i < length; i++ {
buf[i] = all[rand.Intn(len(all))]
}
rand.Shuffle(len(buf), func(i, j int) {
buf[i], buf[j] = buf[j], buf[i]
})
return string(buf)
}

View File

@ -11,7 +11,6 @@ import (
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"database/sql/driver"
"encoding/base64"
"encoding/pem"
"errors"
@ -36,64 +35,9 @@ import (
"github.com/gofrs/uuid"
errors2 "github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/pbkdf2"
)
type JsonTime struct {
time.Time
}
func NewJsonTime(t time.Time) JsonTime {
return JsonTime{
Time: t,
}
}
func NowJsonTime() JsonTime {
return JsonTime{
Time: time.Now(),
}
}
func (t JsonTime) MarshalJSON() ([]byte, error) {
var stamp = fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05"))
return []byte(stamp), nil
}
func (t JsonTime) Value() (driver.Value, error) {
var zeroTime time.Time
if t.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return t.Time, nil
}
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)
}
type Bcrypt struct {
cost int
}
func (b *Bcrypt) Encode(password []byte) ([]byte, error) {
return bcrypt.GenerateFromPassword(password, b.cost)
}
func (b *Bcrypt) Match(hashedPassword, password []byte) error {
return bcrypt.CompareHashAndPassword(hashedPassword, password)
}
var Encoder = Bcrypt{
cost: bcrypt.DefaultCost,
}
func UUID() string {
v4, _ := uuid.NewV4()
return v4.String()