Add func comment

tags/v1.0.8
Stanly 4 years ago
parent 56b2b22f66
commit d003330393

@ -4,11 +4,13 @@ import (
"github.com/mojocn/base64Captcha" "github.com/mojocn/base64Captcha"
) )
// Generate 產生驗證圖示
func Generate() (string, string, error) { func Generate() (string, string, error) {
driver := base64Captcha.NewDriverDigit(80, 160, 4, 0.1, 4) driver := base64Captcha.NewDriverDigit(80, 160, 4, 0.1, 4)
return base64Captcha.NewCaptcha(driver, base64Captcha.DefaultMemStore).Generate() return base64Captcha.NewCaptcha(driver, base64Captcha.DefaultMemStore).Generate()
} }
// Verify 驗證驗證碼
func Verify(id, code string) bool { func Verify(id, code string) bool {
return base64Captcha.DefaultMemStore.Verify(id, code, true) return base64Captcha.DefaultMemStore.Verify(id, code, true)
} }

@ -30,6 +30,7 @@ func SHA256(v string) string {
return fmt.Sprintf("%x", bs) return fmt.Sprintf("%x", bs)
} }
// EncryptPassword 加密密碼
func EncryptPassword(pwd string) (string, error) { func EncryptPassword(pwd string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost) hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
if err != nil { if err != nil {
@ -39,10 +40,12 @@ func EncryptPassword(pwd string) (string, error) {
return string(hash), nil return string(hash), nil
} }
// CheckPassword 檢查密碼
func CheckPassword(pwd, hash string) error { func CheckPassword(pwd, hash string) error {
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(pwd)) return bcrypt.CompareHashAndPassword([]byte(hash), []byte(pwd))
} }
// EncryptAES aes加密
func EncryptAES(value, key string) ([]byte, error) { func EncryptAES(value, key string) ([]byte, error) {
o := openssl.New() o := openssl.New()
@ -54,6 +57,7 @@ func EncryptAES(value, key string) ([]byte, error) {
return enc, nil return enc, nil
} }
// DecryptAES aes解密
func DecryptAES(value, key string) ([]byte, error) { func DecryptAES(value, key string) ([]byte, error) {
o := openssl.New() o := openssl.New()
@ -65,6 +69,7 @@ func DecryptAES(value, key string) ([]byte, error) {
return dec, nil return dec, nil
} }
// EncryptRSA rsa加密
func EncryptRSA(value, publicKey []byte) ([]byte, error) { func EncryptRSA(value, publicKey []byte) ([]byte, error) {
block, _ := pem.Decode(publicKey) block, _ := pem.Decode(publicKey)
if block == nil { if block == nil {
@ -80,10 +85,11 @@ func EncryptRSA(value, publicKey []byte) ([]byte, error) {
return rsa.EncryptPKCS1v15(rand.Reader, pub, value) return rsa.EncryptPKCS1v15(rand.Reader, pub, value)
} }
// DecryptRSA rsa解密
func DecryptRSA(ciphertext, privateKey []byte) ([]byte, error) { func DecryptRSA(ciphertext, privateKey []byte) ([]byte, error) {
block, _ := pem.Decode(privateKey) block, _ := pem.Decode(privateKey)
if block == nil { if block == nil {
return nil, errors.New("private key error!") return nil, errors.New("private key error")
} }
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)

@ -11,6 +11,7 @@ import (
"os" "os"
) )
// CompressImage 壓縮圖片至指定位置
func CompressImage(input interface{}, output string) (err error) { func CompressImage(input interface{}, output string) (err error) {
var file io.Reader var file io.Reader
switch input.(type) { switch input.(type) {

@ -3,13 +3,13 @@ package jwt
import ( import (
"fmt" "fmt"
. "git.simts.cc/common/lib/types" "git.simts.cc/common/lib/types"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
) )
// Encode jwt編碼 // Encode jwt編碼
func Encode(values Data, key string) (string, error) { func Encode(values types.Data, key string) (string, error) {
claims := jwt.MapClaims{} claims := jwt.MapClaims{}
for key, value := range values { for key, value := range values {
claims[key] = value claims[key] = value
@ -26,7 +26,7 @@ func Encode(values Data, key string) (string, error) {
} }
// Decode jwt解碼 // Decode jwt解碼
func Decode(tokenString string, key string) (Data, error) { func Decode(tokenString string, key string) (types.Data, error) {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected error: %v. ", token.Header["alg"]) return nil, fmt.Errorf("Unexpected error: %v. ", token.Header["alg"])
@ -42,5 +42,5 @@ func Decode(tokenString string, key string) (Data, error) {
return nil, fmt.Errorf("Token error. ") return nil, fmt.Errorf("Token error. ")
} }
return Data(claims), nil return types.Data(claims), nil
} }

@ -4,6 +4,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
// NewLoggerProduction 正式版紀錄
func NewLoggerProduction() (*zap.SugaredLogger, error) { func NewLoggerProduction() (*zap.SugaredLogger, error) {
logger, err := zap.NewProduction() logger, err := zap.NewProduction()
if err != nil { if err != nil {
@ -13,6 +14,7 @@ func NewLoggerProduction() (*zap.SugaredLogger, error) {
return logger.Sugar(), nil return logger.Sugar(), nil
} }
// NewLoggerDevelopment 開發版紀錄
func NewLoggerDevelopment() (*zap.SugaredLogger, error) { func NewLoggerDevelopment() (*zap.SugaredLogger, error) {
logger, err := zap.NewDevelopment() logger, err := zap.NewDevelopment()
if err != nil { if err != nil {

@ -7,6 +7,7 @@ import (
"strings" "strings"
) )
// Send 寄送信件
func Send(host string, port int, username, password, mailto, subject, content string) error { func Send(host string, port int, username, password, mailto, subject, content string) error {
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), &tls.Config{InsecureSkipVerify: true, ServerName: "smtp.gmail.com"}) conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), &tls.Config{InsecureSkipVerify: true, ServerName: "smtp.gmail.com"})
if err != nil { if err != nil {

@ -5,13 +5,16 @@ import (
"errors" "errors"
) )
// 錯誤訊息
var ( var (
ErrExistKey = errors.New("not exist.") ErrExistKey = errors.New("not exist")
ErrFormat = errors.New("error format.") ErrFormat = errors.New("error format")
) )
// Data 資料
type Data map[string]interface{} type Data map[string]interface{}
// Get 取得資料
func (data Data) Get(key string) (interface{}, error) { func (data Data) Get(key string) (interface{}, error) {
value, exist := data[key] value, exist := data[key]
if !exist { if !exist {
@ -21,6 +24,7 @@ func (data Data) Get(key string) (interface{}, error) {
return value, nil return value, nil
} }
// GetString 取得字串
func (data Data) GetString(key string) (string, error) { func (data Data) GetString(key string) (string, error) {
value, err := data.Get(key) value, err := data.Get(key)
if err != nil { if err != nil {
@ -35,11 +39,13 @@ func (data Data) GetString(key string) (string, error) {
return "", ErrFormat return "", ErrFormat
} }
// MustGetString 強制取得字串
func (data Data) MustGetString(key string) string { func (data Data) MustGetString(key string) string {
value, _ := data.GetString(key) value, _ := data.GetString(key)
return value return value
} }
// GetSlice 取得切片
func (data Data) GetSlice(key string) (res []interface{}, err error) { func (data Data) GetSlice(key string) (res []interface{}, err error) {
value, err := data.Get(key) value, err := data.Get(key)
if err != nil { if err != nil {
@ -54,11 +60,13 @@ func (data Data) GetSlice(key string) (res []interface{}, err error) {
} }
} }
// MustGetSlice 強制取得切片
func (data Data) MustGetSlice(key string) []interface{} { func (data Data) MustGetSlice(key string) []interface{} {
values, _ := data.GetSlice(key) values, _ := data.GetSlice(key)
return values return values
} }
// GetStringSlice 取得字串切片
func (data Data) GetStringSlice(key string) (res []string, err error) { func (data Data) GetStringSlice(key string) (res []string, err error) {
values, err := data.GetSlice(key) values, err := data.GetSlice(key)
if err != nil { if err != nil {
@ -77,11 +85,13 @@ func (data Data) GetStringSlice(key string) (res []string, err error) {
return res, nil return res, nil
} }
// MustGetStringSlice 強制取得字串切片
func (data Data) MustGetStringSlice(key string) []string { func (data Data) MustGetStringSlice(key string) []string {
values, _ := data.GetStringSlice(key) values, _ := data.GetStringSlice(key)
return values return values
} }
// GetIntSlice 取得整數切片
func (data Data) GetIntSlice(key string) (res []int, err error) { func (data Data) GetIntSlice(key string) (res []int, err error) {
values, err := data.GetSlice(key) values, err := data.GetSlice(key)
if err != nil { if err != nil {
@ -100,11 +110,13 @@ func (data Data) GetIntSlice(key string) (res []int, err error) {
return res, nil return res, nil
} }
// MustGetIntSlice 強制取得整數切片
func (data Data) MustGetIntSlice(key string) []int { func (data Data) MustGetIntSlice(key string) []int {
values, _ := data.GetIntSlice(key) values, _ := data.GetIntSlice(key)
return values return values
} }
// GetInt64 取得64位元整數
func (data Data) GetInt64(key string) (int64, error) { func (data Data) GetInt64(key string) (int64, error) {
value, err := data.Get(key) value, err := data.Get(key)
if err != nil { if err != nil {
@ -125,11 +137,13 @@ func (data Data) GetInt64(key string) (int64, error) {
return 0, ErrFormat return 0, ErrFormat
} }
// MustGetInt64 強制取得64位元整數
func (data Data) MustGetInt64(key string) int64 { func (data Data) MustGetInt64(key string) int64 {
value, _ := data.GetInt64(key) value, _ := data.GetInt64(key)
return value return value
} }
// GetUint64 取得64位元正整數
func (data Data) GetUint64(key string) (uint64, error) { func (data Data) GetUint64(key string) (uint64, error) {
value, err := data.GetInt64(key) value, err := data.GetInt64(key)
if err != nil { if err != nil {
@ -143,11 +157,13 @@ func (data Data) GetUint64(key string) (uint64, error) {
return uint64(value), nil return uint64(value), nil
} }
// MustGetUint64 強制取得64位元正整數
func (data Data) MustGetUint64(key string) uint64 { func (data Data) MustGetUint64(key string) uint64 {
value, _ := data.GetUint64(key) value, _ := data.GetUint64(key)
return value return value
} }
// GetInt 取得整數
func (data Data) GetInt(key string) (int, error) { func (data Data) GetInt(key string) (int, error) {
value, err := data.GetInt64(key) value, err := data.GetInt64(key)
if err != nil { if err != nil {
@ -157,11 +173,13 @@ func (data Data) GetInt(key string) (int, error) {
return int(value), nil return int(value), nil
} }
// MustGetInt 強制取得整數
func (data Data) MustGetInt(key string) int { func (data Data) MustGetInt(key string) int {
value, _ := data.GetInt(key) value, _ := data.GetInt(key)
return value return value
} }
// GetUint 取得正整數
func (data Data) GetUint(key string) (uint, error) { func (data Data) GetUint(key string) (uint, error) {
value, err := data.GetInt64(key) value, err := data.GetInt64(key)
if err != nil { if err != nil {
@ -175,11 +193,13 @@ func (data Data) GetUint(key string) (uint, error) {
return uint(value), nil return uint(value), nil
} }
// MustGetUint 強制取得正整數
func (data Data) MustGetUint(key string) uint { func (data Data) MustGetUint(key string) uint {
value, _ := data.GetUint(key) value, _ := data.GetUint(key)
return value return value
} }
// GetInt16 取得16位元整數
func (data Data) GetInt16(key string) (int16, error) { func (data Data) GetInt16(key string) (int16, error) {
value, err := data.GetInt64(key) value, err := data.GetInt64(key)
if err != nil { if err != nil {
@ -189,6 +209,7 @@ func (data Data) GetInt16(key string) (int16, error) {
return int16(value), nil return int16(value), nil
} }
// GetUint16 取得16位元正整數
func (data Data) GetUint16(key string) (uint16, error) { func (data Data) GetUint16(key string) (uint16, error) {
value, err := data.GetInt64(key) value, err := data.GetInt64(key)
if err != nil { if err != nil {
@ -202,11 +223,13 @@ func (data Data) GetUint16(key string) (uint16, error) {
return uint16(value), nil return uint16(value), nil
} }
// MustGetUint16 強制取得16位元正整數
func (data Data) MustGetUint16(key string) uint16 { func (data Data) MustGetUint16(key string) uint16 {
value, _ := data.GetUint16(key) value, _ := data.GetUint16(key)
return value return value
} }
// GetInt8 取得8位元整數
func (data Data) GetInt8(key string) (int8, error) { func (data Data) GetInt8(key string) (int8, error) {
value, err := data.GetInt64(key) value, err := data.GetInt64(key)
if err != nil { if err != nil {
@ -216,11 +239,13 @@ func (data Data) GetInt8(key string) (int8, error) {
return int8(value), nil return int8(value), nil
} }
// MustGetInt8 強制取得8位元整數
func (data Data) MustGetInt8(key string) int8 { func (data Data) MustGetInt8(key string) int8 {
value, _ := data.GetInt8(key) value, _ := data.GetInt8(key)
return value return value
} }
// GetUint8 取得8位元正整數
func (data Data) GetUint8(key string) (uint8, error) { func (data Data) GetUint8(key string) (uint8, error) {
value, err := data.GetInt64(key) value, err := data.GetInt64(key)
if err != nil { if err != nil {
@ -234,11 +259,13 @@ func (data Data) GetUint8(key string) (uint8, error) {
return uint8(value), nil return uint8(value), nil
} }
// MustGetUint8 強制取得8位元正整數
func (data Data) MustGetUint8(key string) uint8 { func (data Data) MustGetUint8(key string) uint8 {
value, _ := data.GetUint8(key) value, _ := data.GetUint8(key)
return value return value
} }
// GetBool 取得布林值
func (data Data) GetBool(key string) (bool, error) { func (data Data) GetBool(key string) (bool, error) {
value, err := data.Get(key) value, err := data.Get(key)
if err != nil { if err != nil {
@ -253,11 +280,13 @@ func (data Data) GetBool(key string) (bool, error) {
return false, ErrFormat return false, ErrFormat
} }
// MustGetBool 強制取得布林值
func (data Data) MustGetBool(key string) bool { func (data Data) MustGetBool(key string) bool {
value, _ := data.GetBool(key) value, _ := data.GetBool(key)
return value return value
} }
// GetTimestamp 取得時間戳記
func (data Data) GetTimestamp(key string) (Timestamp, error) { func (data Data) GetTimestamp(key string) (Timestamp, error) {
value, err := data.GetInt64(key) value, err := data.GetInt64(key)
if err != nil { if err != nil {
@ -267,11 +296,13 @@ func (data Data) GetTimestamp(key string) (Timestamp, error) {
return Timestamp(value), nil return Timestamp(value), nil
} }
// MustGetTimestamp 強制取得時間戳記
func (data Data) MustGetTimestamp(key string) Timestamp { func (data Data) MustGetTimestamp(key string) Timestamp {
value, _ := data.GetTimestamp(key) value, _ := data.GetTimestamp(key)
return value return value
} }
// JSON 取得json
func (data Data) JSON() ([]byte, error) { func (data Data) JSON() ([]byte, error) {
bs, err := json.Marshal(&data) bs, err := json.Marshal(&data)
if err != nil { if err != nil {
@ -281,6 +312,7 @@ func (data Data) JSON() ([]byte, error) {
return bs, nil return bs, nil
} }
// MustJSON 強制取得json
func (data Data) MustJSON() []byte { func (data Data) MustJSON() []byte {
bs, _ := data.JSON() bs, _ := data.JSON()
return bs return bs

@ -5,10 +5,12 @@ type Page struct {
Current, Size int Current, Size int
} }
// Take 取得數量
func (p Page) Take() int { func (p Page) Take() int {
return p.Size return p.Size
} }
// Skip 忽略數量
func (p Page) Skip() int { func (p Page) Skip() int {
return (p.Current - 1) * p.Size return (p.Current - 1) * p.Size
} }

@ -9,8 +9,10 @@ const (
FormatDatetime = "2006-01-02 15:04:05" FormatDatetime = "2006-01-02 15:04:05"
) )
// Local 本地時區
var Local = time.Local var Local = time.Local
// SetLocation 設定時區
func SetLocation(name string) { func SetLocation(name string) {
Local, _ = time.LoadLocation(name) Local, _ = time.LoadLocation(name)
} }
@ -18,14 +20,17 @@ func SetLocation(name string) {
// Timestamp 時間戳 // Timestamp 時間戳
type Timestamp int64 type Timestamp int64
// GetUnixNow 取得現在時間戳記
func GetUnixNow() Timestamp { func GetUnixNow() Timestamp {
return Timestamp(time.Now().Unix()) return Timestamp(time.Now().Unix())
} }
// Int64 轉為64位元整數
func (t Timestamp) Int64() int64 { func (t Timestamp) Int64() int64 {
return int64(t) return int64(t)
} }
// Time 轉為time類型
func (t Timestamp) Time() time.Time { func (t Timestamp) Time() time.Time {
return time.Unix(t.Int64(), 0) return time.Unix(t.Int64(), 0)
} }
@ -38,12 +43,12 @@ func (t Timestamp) Datetime() string {
Format(FormatDatetime) Format(FormatDatetime)
} }
// 與現在的時間差 // NowSub 與現在的時間差
func (t Timestamp) NowSub() time.Duration { func (t Timestamp) NowSub() time.Duration {
return time.Since(t.Time()) return time.Since(t.Time())
} }
// 時間差 // Sub 時間差
func (t Timestamp) Sub(t2 time.Time) time.Duration { func (t Timestamp) Sub(t2 time.Time) time.Duration {
return t2.Sub(t.Time()) return t2.Sub(t.Time())
} }

Loading…
Cancel
Save