| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package common
- import (
- "fmt"
- "math/rand"
- "net"
- "net/http"
- "strconv"
- "strings"
- "time"
- )
- func StringToInt(id string) int {
- if id != "" {
- id, err := strconv.Atoi(id)
- if err == nil {
- return id
- }
- }
- return -1
- }
- func RandomString(n int) string {
- var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
- rand.Seed(time.Now().Unix())
- b := make([]rune, n)
- for i := range b {
- b[i] = letters[rand.Intn(len(letters))]
- }
- return string(b)
- }
- func RandomString2(n int) string {
- var letters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
- rand.Seed(time.Now().Unix())
- b := make([]rune, n)
- for i := range b {
- b[i] = letters[rand.Intn(len(letters))]
- }
- return string(b)
- }
- func StringToInt64Array(str string) []int64 {
- tmp := strings.Split(str, ",")
- var result []int64
- for _, t := range tmp {
- i, _ := strconv.ParseInt(t, 10, 64)
- result = append(result, i)
- }
- return result
- }
- func IntInArray(value int, arr []int) bool {
- for _, i := range arr {
- if value == i {
- return true
- }
- }
- return false
- }
- func StringToIntArray(value string) []int {
- var result []int
- arr := strings.Split(value, ",")
- for _, a := range arr {
- i, _ := strconv.Atoi(a)
- result = append(result, i)
- }
- return result
- }
- func CheckTimesHasOverlap(condition1Start, condition1End, condition2Start, condition2End float64) bool {
- //时间不能都为0
- if condition1Start == 0 && condition1End == 0 && condition2Start == 0 && condition2End == 0 {
- return true
- }
- //不能2个都跨天
- if condition1Start > condition1End && condition2Start > condition2End {
- return true
- }
- //开始结束时间不能一致
- if condition1Start == condition2End || condition2Start == condition1End || condition1Start == condition1End || condition2Start == condition2End {
- return true
- }
- //时控1跨天时不能重叠
- if condition1Start > condition1End {
- return condition2Start < condition1End || condition2End > condition1Start
- }
- //时控2跨天时不能重叠
- if condition2Start > condition2End {
- return condition1Start < condition2End || condition1End > condition2Start
- }
- //都不跨天时不能重叠
- return !(condition2End < condition1Start || condition2Start > condition1End)
- }
- func CheckHourInvalid(hour float64) bool {
- if hour < 0 || hour > 24 {
- return true
- }
- return false
- }
- func MlParseTime(strTime string) (time.Time, error) {
- if strings.Contains(strTime, ".") {
- return time.ParseInLocation("2006-01-02 15:04:05.000", strTime, time.Local)
- }
- return time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
- }
- // ControlRelayId 返回回路名
- func ControlRelayId(deviceSn string, relayId int) string {
- if relayId == -1 {
- return "全部回路"
- }
- return fmt.Sprintf("回路%v", relayId)
- }
- // ControlRelaysStatus 返回回路操作状态
- func ControlRelaysStatus(deviceSn string, status []uint16) string {
- statusInt := status[0:1]
- if statusInt[0] == 1 {
- return "开"
- }
- return "关"
- }
- func GetClientIp(ip string) string {
- if ip != "" {
- return ip
- }
- addrs, err := net.InterfaceAddrs()
- if err != nil {
- return "none"
- }
- for _, address := range addrs {
- // 检查ip地址判断是否回环地址
- if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
- if ipnet.IP.To4() != nil {
- return ipnet.IP.String()
- }
- }
- }
- return "none"
- }
- func ClientIP(r *http.Request) string {
- xForwardedFor := r.Header.Get("X-Forwarded-For")
- ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
- if ip != "" {
- return ip
- }
- ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
- if ip != "" {
- return ip
- }
- if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
- return ip
- }
- return ""
- }
|