Add func comment

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

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

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

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

@ -3,13 +3,13 @@ package jwt
import (
"fmt"
. "git.simts.cc/common/lib/types"
"git.simts.cc/common/lib/types"
"github.com/dgrijalva/jwt-go"
)
// Encode jwt編碼
func Encode(values Data, key string) (string, error) {
func Encode(values types.Data, key string) (string, error) {
claims := jwt.MapClaims{}
for key, value := range values {
claims[key] = value
@ -26,7 +26,7 @@ func Encode(values Data, key string) (string, error) {
}
// 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) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
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 Data(claims), nil
return types.Data(claims), nil
}

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

@ -7,6 +7,7 @@ import (
"strings"
)
// Send 寄送信件
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"})
if err != nil {

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

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

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

Loading…
Cancel
Save