Compare commits

...

5 Commits
v1.0.0 ... main

Author SHA1 Message Date
4551df34b6 合并pkg 2024-12-18 23:38:28 +08:00
5f5193fd70 Update go.mod 2024-12-18 03:41:00 +08:00
bfe2dd913c 分离pkg 2024-12-18 03:39:12 +08:00
6f28ddd69b Delete QueryManager.go 2024-12-18 03:14:51 +08:00
2378d75406 跟进 2024-12-18 03:08:49 +08:00
8 changed files with 106 additions and 160 deletions

25
go.mod
View File

@ -4,7 +4,7 @@ go 1.23.1
require ( require (
github.com/Masterminds/squirrel v1.5.4 github.com/Masterminds/squirrel v1.5.4
github.com/aliyun/alibaba-cloud-sdk-go v1.63.68 github.com/aliyun/alibaba-cloud-sdk-go v1.63.71
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
github.com/gin-gonic/gin v1.10.0 github.com/gin-gonic/gin v1.10.0
github.com/go-pay/gopay v1.5.106 github.com/go-pay/gopay v1.5.106
@ -21,14 +21,14 @@ require (
require ( require (
filippo.io/edwards25519 v1.1.0 // indirect filippo.io/edwards25519 v1.1.0 // indirect
github.com/bytedance/sonic v1.12.2 // indirect github.com/bytedance/sonic v1.12.6 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect github.com/cloudwego/iasm v0.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect github.com/gabriel-vasile/mimetype v1.4.7 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-pay/crypto v0.0.1 // indirect github.com/go-pay/crypto v0.0.1 // indirect
github.com/go-pay/errgroup v0.0.2 // indirect github.com/go-pay/errgroup v0.0.2 // indirect
@ -37,20 +37,21 @@ require (
github.com/go-pay/xtime v0.0.2 // indirect github.com/go-pay/xtime v0.0.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect github.com/goccy/go-json v0.10.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect github.com/magiconair/properties v1.8.9 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
@ -61,16 +62,16 @@ require (
github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.0 // indirect github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/atomic v1.11.0 // indirect go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.9.0 // indirect golang.org/x/arch v0.12.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 // indirect
golang.org/x/net v0.28.0 // indirect golang.org/x/net v0.32.0 // indirect
golang.org/x/sys v0.28.0 // indirect golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.8.0 // indirect golang.org/x/time v0.8.0 // indirect

View File

@ -1,29 +0,0 @@
package lightCore
import "code.zhecent.com/gopkg/light-core/pkg/myViper"
type Start struct {
config *Config
}
func NewByConfig(config *Config) *Start {
return &Start{config: config}
}
func NewByYAML(fileName string, path string) *Start {
c := &Config{}
myViper.NewSimpleViper(c, "yaml", fileName, path).Apply()
return &Start{config: c}
}
func (t *Start) Start() {
//校验配置
if t.config.App.JwtSecret == "" {
panic("App JwtSecret Empty")
}
if t.config.Server.RunMode == "" || t.config.Server.HttpPort == 0 {
panic("Server Config Error")
}
}

View File

@ -12,39 +12,21 @@ func NewFacade(tableName string, dbFunc func() *gorm.DB) *Facade {
} }
func (t *Facade) NewQuery() *Query { func (t *Facade) NewQuery() *Query {
if t.dbFunc != nil { return NewQuery(t.tableName, t.dbFunc())
return NewQuery(t.tableName).SetDB(t.dbFunc())
}
return NewQuery(t.tableName)
} }
func (t *Facade) NewUpdate() *Update { func (t *Facade) NewUpdate() *Update {
if t.dbFunc != nil { return NewUpdate(t.tableName, t.dbFunc())
return NewUpdate(t.tableName).SetDB(t.dbFunc())
}
return NewUpdate(t.tableName)
} }
func (t *Facade) NewInsert() *Insert { func (t *Facade) NewInsert() *Insert {
if t.dbFunc != nil { return NewInsert(t.tableName, t.dbFunc())
return NewInsert(t.tableName).SetDB(t.dbFunc())
}
return NewInsert(t.tableName)
} }
func (t *Facade) NewDelete() *Delete { func (t *Facade) NewDelete() *Delete {
if t.dbFunc != nil { return NewDelete(t.tableName, t.dbFunc())
return NewDelete(t.tableName).SetDB(t.dbFunc())
}
return NewDelete(t.tableName)
} }
func (t *Facade) Create(val interface{}) error { func (t *Facade) Create(val interface{}) error {
if t.dbFunc != nil { return Create(val, t.dbFunc())
db := t.dbFunc()
if db != nil {
return Create(val, db)
}
}
return Create(val)
} }

View File

@ -1,91 +0,0 @@
package mySql
import (
"errors"
"gorm.io/gorm"
"reflect"
)
// 因为有大量的是简单的通过where查询一条数据的例如查询某个用户的资料所以封装这个工具类简化操作用法与query保持一致
type QueryManager struct {
tableName string
wheres []*QueryManagerWhere
errorText string
error error
}
type QueryManagerWhere struct {
colName string
value interface{}
emptyCheck bool
}
func NewQueryManager(tableName string) *QueryManager {
return &QueryManager{
tableName: tableName,
wheres: make([]*QueryManagerWhere, 0),
}
}
func (t *QueryManager) SetErrorText(text string) *QueryManager {
t.errorText = text
return t
}
func (t *QueryManager) Where(colName string, value interface{}) *QueryManager {
t.wheres = append(t.wheres, &QueryManagerWhere{
colName: colName,
value: value,
emptyCheck: true,
})
return t
}
func (t *QueryManager) WhereRow(colName string, value interface{}, emptyCheck bool) *QueryManager {
t.wheres = append(t.wheres, &QueryManagerWhere{
colName: colName,
value: value,
emptyCheck: emptyCheck,
})
return t
}
func (t *QueryManager) Take(data interface{}) error {
if len(t.wheres) == 0 {
return errors.New("not where")
}
for colName := range t.wheres {
if isBlank(reflect.ValueOf(t.wheres[colName])) {
//如果是空值
return t.getEmptyError()
}
}
query := NewQuery(t.tableName)
for _, where := range t.wheres {
if where.emptyCheck {
//检查空值
if isBlank(reflect.ValueOf(where.value)) {
return t.getEmptyError()
}
}
query.Where(where.colName, where.value)
}
err := query.Take(data)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return t.getEmptyError()
}
}
return err
}
func (t *QueryManager) getEmptyError() error {
if t.errorText != "" {
return errors.New(t.errorText)
} else {
return errors.New("不存在")
}
}

37
util/amount.go Normal file
View File

@ -0,0 +1,37 @@
package util
import (
"github.com/shopspring/decimal"
"strings"
)
func Fen2Yuan(price int64) string {
d := decimal.New(1, 2) //分除以100得到元
result := decimal.NewFromInt(int64(price)).DivRound(d, 2).String()
//fmt.Printf("输入值为:%d, 分转元后,精度为二的结果为:%s\n", price, result)
return result
}
func FenToYuan00(price int64) string {
d := decimal.New(1, 2) //分除以100得到元
result := decimal.NewFromInt(int64(price)).DivRound(d, 2).String()
if find := strings.Contains(result, "."); find {
return result
} else {
return result + ".00"
}
}
// 元转分,乘以100后保留整数部分
func Yuan2Fen(price float64) int64 {
d := decimal.New(1, 2) //分转元乘以100
d1 := decimal.New(1, 0) //乘完之后保留2为小数需要这么一个中间参数
//df := decimal.NewFromFloat(price).Mul(d).DivRound(d1,2).String()
//df := decimal.NewFromFloat(price).Mul(d).IntPart()
//如下是满足当乘以100后仍然有小数位取四舍五入法后再取整数部分
dff := decimal.NewFromFloat(price).Mul(d).DivRound(d1, 0).IntPart()
//fmt.Printf("输入值为:%f, 简单的元转分后,取整数部分:%d\n", price, df)
//fmt.Printf("输入值为:%f, 元转分后,若还有小数,需做四舍五入后,再取整数:%d\n", price, dff)
return dff
}

View File

@ -1,4 +1,4 @@
package password package util
import ( import (
"crypto/md5" "crypto/md5"
@ -6,7 +6,7 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
func Encode(userPassword string) (string, error) { func PasswordEncode(userPassword string) (string, error) {
md5Password := md5.Sum([]byte(userPassword)) md5Password := md5.Sum([]byte(userPassword))
encode, err := generatePassword(string(md5Password[:])) encode, err := generatePassword(string(md5Password[:]))
if err != nil { if err != nil {
@ -15,18 +15,18 @@ func Encode(userPassword string) (string, error) {
return string(encode), nil return string(encode), nil
} }
} }
func ValidateCode(userPassword string, hashed string) (isOK bool, err error) { func PasswordValidateCode(userPassword string, hashed string) (isOK bool, err error) {
md5Password := md5.Sum([]byte(userPassword)) md5Password := md5.Sum([]byte(userPassword))
isOk, err := validatePassword(string(md5Password[:]), hashed) isOk, err := validatePassword(string(md5Password[:]), hashed)
return isOk, err return isOk, err
} }
//generatePassword 给密码就行加密操作 // generatePassword 给密码就行加密操作
func generatePassword(userPassword string) ([]byte, error) { func generatePassword(userPassword string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(userPassword), bcrypt.DefaultCost) return bcrypt.GenerateFromPassword([]byte(userPassword), bcrypt.DefaultCost)
} }
//validatePassword 密码比对 // validatePassword 密码比对
func validatePassword(userPassword string, hashed string) (isOK bool, err error) { func validatePassword(userPassword string, hashed string) (isOK bool, err error) {
if err = bcrypt.CompareHashAndPassword([]byte(hashed), []byte(userPassword)); err != nil { if err = bcrypt.CompareHashAndPassword([]byte(hashed), []byte(userPassword)); err != nil {
return false, errors.New("密码错误!") return false, errors.New("密码错误!")

21
util/size.go Normal file
View File

@ -0,0 +1,21 @@
package util
import "fmt"
// 字节的单位转换 保留两位小数
func FormatFileSize(fileSize int64) (size string) {
if fileSize < 1024 {
//return strconv.FormatInt(fileSize, 10) + "B"
return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
} else if fileSize < (1024 * 1024) {
return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
} else if fileSize < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}

25
util/time.go Normal file
View File

@ -0,0 +1,25 @@
package util
import (
"time"
)
func IsToday(t time.Time) bool {
toDay := time.Now()
if toDay.Year() == t.Year() && toDay.Month() == t.Month() && toDay.Day() == t.Day() {
return true
} else {
return false
}
}
func GetOneDayBetween(t time.Time) (time.Time, time.Time) {
startTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
endTime := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location())
return startTime, endTime
}
func ParseTime(layout, str string) time.Time {
t, _ := time.Parse(layout, str)
return t
}