common.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package common
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "net"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. func StringToInt(id string) int {
  12. if id != "" {
  13. id, err := strconv.Atoi(id)
  14. if err == nil {
  15. return id
  16. }
  17. }
  18. return -1
  19. }
  20. func RandomString(n int) string {
  21. var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  22. rand.Seed(time.Now().Unix())
  23. b := make([]rune, n)
  24. for i := range b {
  25. b[i] = letters[rand.Intn(len(letters))]
  26. }
  27. return string(b)
  28. }
  29. func RandomString2(n int) string {
  30. var letters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  31. rand.Seed(time.Now().Unix())
  32. b := make([]rune, n)
  33. for i := range b {
  34. b[i] = letters[rand.Intn(len(letters))]
  35. }
  36. return string(b)
  37. }
  38. func StringToInt64Array(str string) []int64 {
  39. tmp := strings.Split(str, ",")
  40. var result []int64
  41. for _, t := range tmp {
  42. i, _ := strconv.ParseInt(t, 10, 64)
  43. result = append(result, i)
  44. }
  45. return result
  46. }
  47. func IntInArray(value int, arr []int) bool {
  48. for _, i := range arr {
  49. if value == i {
  50. return true
  51. }
  52. }
  53. return false
  54. }
  55. func StringToIntArray(value string) []int {
  56. var result []int
  57. arr := strings.Split(value, ",")
  58. for _, a := range arr {
  59. i, _ := strconv.Atoi(a)
  60. result = append(result, i)
  61. }
  62. return result
  63. }
  64. func CheckTimesHasOverlap(condition1Start, condition1End, condition2Start, condition2End float64) bool {
  65. //时间不能都为0
  66. if condition1Start == 0 && condition1End == 0 && condition2Start == 0 && condition2End == 0 {
  67. return true
  68. }
  69. //不能2个都跨天
  70. if condition1Start > condition1End && condition2Start > condition2End {
  71. return true
  72. }
  73. //开始结束时间不能一致
  74. if condition1Start == condition2End || condition2Start == condition1End || condition1Start == condition1End || condition2Start == condition2End {
  75. return true
  76. }
  77. //时控1跨天时不能重叠
  78. if condition1Start > condition1End {
  79. return condition2Start < condition1End || condition2End > condition1Start
  80. }
  81. //时控2跨天时不能重叠
  82. if condition2Start > condition2End {
  83. return condition1Start < condition2End || condition1End > condition2Start
  84. }
  85. //都不跨天时不能重叠
  86. return !(condition2End < condition1Start || condition2Start > condition1End)
  87. }
  88. func CheckHourInvalid(hour float64) bool {
  89. if hour < 0 || hour > 24 {
  90. return true
  91. }
  92. return false
  93. }
  94. func MlParseTime(strTime string) (time.Time, error) {
  95. if strings.Contains(strTime, ".") {
  96. return time.ParseInLocation("2006-01-02 15:04:05.000", strTime, time.Local)
  97. }
  98. return time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  99. }
  100. // ControlRelayId 返回回路名
  101. func ControlRelayId(deviceSn string, relayId int) string {
  102. if relayId == -1 {
  103. return "全部回路"
  104. }
  105. return fmt.Sprintf("回路%v", relayId)
  106. }
  107. // ControlRelaysStatus 返回回路操作状态
  108. func ControlRelaysStatus(deviceSn string, status []uint16) string {
  109. statusInt := status[0:1]
  110. if statusInt[0] == 1 {
  111. return "开"
  112. }
  113. return "关"
  114. }
  115. func GetClientIp(ip string) string {
  116. if ip != "" {
  117. return ip
  118. }
  119. addrs, err := net.InterfaceAddrs()
  120. if err != nil {
  121. return "none"
  122. }
  123. for _, address := range addrs {
  124. // 检查ip地址判断是否回环地址
  125. if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  126. if ipnet.IP.To4() != nil {
  127. return ipnet.IP.String()
  128. }
  129. }
  130. }
  131. return "none"
  132. }
  133. func ClientIP(r *http.Request) string {
  134. xForwardedFor := r.Header.Get("X-Forwarded-For")
  135. ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
  136. if ip != "" {
  137. return ip
  138. }
  139. ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
  140. if ip != "" {
  141. return ip
  142. }
  143. if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
  144. return ip
  145. }
  146. return ""
  147. }