package utils import ( "database/sql" "github.com/boombuler/barcode" "github.com/boombuler/barcode/qr" "hash/crc32" "image/png" "os" "reflect" "strconv" "time" uuid "github.com/satori/go.uuid" ) func GetUUID() string { return uuid.NewV4().String() } //the result likes 1423361979 func GetTimestamp() int64 { return time.Now().Unix() } func Int2String(val int64) string { return strconv.Itoa(int(val)) } func ValidTime(t time.Time) bool { t1970, _ := time.Parse("2006-01-02", "1970-01-01") return t.After(t1970) } func String2Int64(str string) int64 { number, _ := strconv.ParseInt(str, 10, 64) return number } func HashCode(s string) int { v := int(crc32.ChecksumIEEE([]byte(s))) if v >= 0 { return v } if -v >= 0 { return -v } return 0 } func GetTimeYmd() time.Time { timeNow := time.Now() return time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local) } //剔除sqlx.NullTime等model数据结构 func UnmarshalMysqlRows(row interface{}) []map[string]interface{} { tyR := reflect.TypeOf(row) if tyR.Kind() != reflect.Slice { panic("row must slice") } vR := reflect.ValueOf(row) var rows []map[string]interface{} for i := 0; i < vR.Len(); i++ { value := vR.Index(i).Interface() r := UnmarshalMysqlRow(value) rows = append(rows, r) } return rows } func UnmarshalMysqlRow(row interface{}) map[string]interface{} { tyR := reflect.TypeOf(row) if tyR.Kind() != reflect.Struct { panic("row must struct") } tyV := reflect.ValueOf(row) var Row = make(map[string]interface{}) for i := 0; i < tyR.NumField(); i++ { tag := tyR.Field(i).Tag.Get("db") if tag == "" { tag = tyR.Field(i).Name } tyF := reflect.TypeOf(tyV.Field(i).Interface()) switch tyF.Kind() { case reflect.Struct: switch tyV.Field(i).Type().String() { case "sql.NullTime": reStruct := tyV.Field(i).Interface().(sql.NullTime) if !reStruct.Time.Equal(time.Time{}) && reStruct.Time.Unix() != 0 { Row[tag] = reStruct.Time } else { Row[tag] = "" } case "sql.NullBool": reStruct := tyV.Field(i).Interface().(sql.NullBool) Row[tag] = reStruct.Bool case "sql.NullFloat64": reStruct := tyV.Field(i).Interface().(sql.NullFloat64) Row[tag] = reStruct.Float64 case "sql.NullInt32": reStruct := tyV.Field(i).Interface().(sql.NullInt32) Row[tag] = reStruct.Int32 case "sql.NullInt64": reStruct := tyV.Field(i).Interface().(sql.NullInt64) Row[tag] = reStruct.Int64 case "sql.NullString": reStruct := tyV.Field(i).Interface().(sql.NullString) Row[tag] = reStruct.String case "time.Time": reStruct := tyV.Field(i).Interface().(time.Time) if !reStruct.Equal(time.Time{}) && reStruct.Unix() != 0 { Row[tag] = tyV.Field(i).Interface().(time.Time) } else { Row[tag] = "" } } case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Complex128, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.String, reflect.Float64, reflect.Complex64: Row[tag] = tyV.Field(i).Interface() } } return Row } //生成二维码 func BuildBarcodeQr(url, dir string, width, height int) error { code, err := qr.Encode(url, qr.M, qr.Unicode) if err != nil { return err } code, err = barcode.Scale(code, width, height) if err != nil { return err } file, err := os.Create(dir) if err != nil { return err } defer file.Close() err = png.Encode(file, code) if err != nil { return err } return nil }