SdkUtils.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package commons
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/google/uuid"
  7. "reflect"
  8. "strconv"
  9. "time"
  10. )
  11. func GetTimeStamp() string {
  12. timestamp := time.Now().UnixNano() / int64(time.Millisecond)
  13. str := strconv.FormatInt(timestamp, 10)
  14. return str
  15. }
  16. func GetUUID() string {
  17. // 生成UUID
  18. uuidObj := uuid.New()
  19. // 将UUID转换成字符串
  20. uuidStr := uuidObj.String()
  21. // 打印UUID字符串
  22. return uuidStr
  23. }
  24. func GetMD5(str string) string {
  25. md5 := md5.New()
  26. md5.Write([]byte(str))
  27. result := fmt.Sprintf("%x", md5.Sum(nil))
  28. return result
  29. }
  30. func IsValidJSON(str string) bool {
  31. var js json.RawMessage
  32. return json.Unmarshal([]byte(str), &js) == nil
  33. }
  34. // isPropertyExists 判断是否包含指定属性
  35. func IsPropertyExists(obj interface{}, propertyName string) bool {
  36. if obj == nil || obj == "" {
  37. return false
  38. }
  39. v := reflect.ValueOf(obj)
  40. if v.Kind() == reflect.Ptr {
  41. v = v.Elem()
  42. }
  43. t := v.Type()
  44. for i := 0; i < t.NumField(); i++ {
  45. field := t.Field(i)
  46. if field.Name == propertyName {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. func IsNil(value interface{}) bool {
  53. if value == nil {
  54. return true
  55. }
  56. switch reflect.TypeOf(value).Kind() {
  57. case reflect.Ptr, reflect.Map, reflect.Slice, reflect.Chan, reflect.Interface:
  58. return reflect.ValueOf(value).IsNil()
  59. default:
  60. return false
  61. }
  62. }