utils.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package utils
  2. import (
  3. "database/sql"
  4. "github.com/boombuler/barcode"
  5. "github.com/boombuler/barcode/qr"
  6. "hash/crc32"
  7. "image/png"
  8. "os"
  9. "reflect"
  10. "strconv"
  11. "time"
  12. uuid "github.com/satori/go.uuid"
  13. )
  14. func GetUUID() string {
  15. return uuid.NewV4().String()
  16. }
  17. //the result likes 1423361979
  18. func GetTimestamp() int64 {
  19. return time.Now().Unix()
  20. }
  21. func Int2String(val int64) string {
  22. return strconv.Itoa(int(val))
  23. }
  24. func ValidTime(t time.Time) bool {
  25. t1970, _ := time.Parse("2006-01-02", "1970-01-01")
  26. return t.After(t1970)
  27. }
  28. func String2Int64(str string) int64 {
  29. number, _ := strconv.ParseInt(str, 10, 64)
  30. return number
  31. }
  32. func HashCode(s string) int {
  33. v := int(crc32.ChecksumIEEE([]byte(s)))
  34. if v >= 0 {
  35. return v
  36. }
  37. if -v >= 0 {
  38. return -v
  39. }
  40. return 0
  41. }
  42. func GetTimeYmd() time.Time {
  43. timeNow := time.Now()
  44. return time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, time.Local)
  45. }
  46. //剔除sqlx.NullTime等model数据结构
  47. func UnmarshalMysqlRows(row interface{}) []map[string]interface{} {
  48. tyR := reflect.TypeOf(row)
  49. if tyR.Kind() != reflect.Slice {
  50. panic("row must slice")
  51. }
  52. vR := reflect.ValueOf(row)
  53. var rows []map[string]interface{}
  54. for i := 0; i < vR.Len(); i++ {
  55. value := vR.Index(i).Interface()
  56. r := UnmarshalMysqlRow(value)
  57. rows = append(rows, r)
  58. }
  59. return rows
  60. }
  61. func UnmarshalMysqlRow(row interface{}) map[string]interface{} {
  62. tyR := reflect.TypeOf(row)
  63. if tyR.Kind() != reflect.Struct {
  64. panic("row must struct")
  65. }
  66. tyV := reflect.ValueOf(row)
  67. var Row = make(map[string]interface{})
  68. for i := 0; i < tyR.NumField(); i++ {
  69. tag := tyR.Field(i).Tag.Get("db")
  70. if tag == "" {
  71. tag = tyR.Field(i).Name
  72. }
  73. tyF := reflect.TypeOf(tyV.Field(i).Interface())
  74. switch tyF.Kind() {
  75. case reflect.Struct:
  76. switch tyV.Field(i).Type().String() {
  77. case "sql.NullTime":
  78. reStruct := tyV.Field(i).Interface().(sql.NullTime)
  79. if !reStruct.Time.Equal(time.Time{}) && reStruct.Time.Unix() != 0 {
  80. Row[tag] = reStruct.Time
  81. } else {
  82. Row[tag] = ""
  83. }
  84. case "sql.NullBool":
  85. reStruct := tyV.Field(i).Interface().(sql.NullBool)
  86. Row[tag] = reStruct.Bool
  87. case "sql.NullFloat64":
  88. reStruct := tyV.Field(i).Interface().(sql.NullFloat64)
  89. Row[tag] = reStruct.Float64
  90. case "sql.NullInt32":
  91. reStruct := tyV.Field(i).Interface().(sql.NullInt32)
  92. Row[tag] = reStruct.Int32
  93. case "sql.NullInt64":
  94. reStruct := tyV.Field(i).Interface().(sql.NullInt64)
  95. Row[tag] = reStruct.Int64
  96. case "sql.NullString":
  97. reStruct := tyV.Field(i).Interface().(sql.NullString)
  98. Row[tag] = reStruct.String
  99. case "time.Time":
  100. reStruct := tyV.Field(i).Interface().(time.Time)
  101. if !reStruct.Equal(time.Time{}) && reStruct.Unix() != 0 {
  102. Row[tag] = tyV.Field(i).Interface().(time.Time)
  103. } else {
  104. Row[tag] = ""
  105. }
  106. }
  107. case reflect.Bool, reflect.Int,
  108. reflect.Int8, reflect.Int16,
  109. reflect.Int32, reflect.Int64,
  110. reflect.Complex128, reflect.Uint,
  111. reflect.Uint8, reflect.Uint16,
  112. reflect.Uint32, reflect.Uint64,
  113. reflect.Uintptr, reflect.Float32,
  114. reflect.String, reflect.Float64, reflect.Complex64:
  115. Row[tag] = tyV.Field(i).Interface()
  116. }
  117. }
  118. return Row
  119. }
  120. //生成二维码
  121. func BuildBarcodeQr(url, dir string, width, height int) error {
  122. code, err := qr.Encode(url, qr.M, qr.Unicode)
  123. if err != nil {
  124. return err
  125. }
  126. code, err = barcode.Scale(code, width, height)
  127. if err != nil {
  128. return err
  129. }
  130. file, err := os.Create(dir)
  131. if err != nil {
  132. return err
  133. }
  134. defer file.Close()
  135. err = png.Encode(file, code)
  136. if err != nil {
  137. return err
  138. }
  139. return nil
  140. }