functions.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * @description: 常用函数
  3. * @Author: CP
  4. * @Date: 2020-08-21 11:12:56
  5. * @FilePath: \construction_management\comm\functions.go
  6. */
  7. package comm
  8. import (
  9. "crypto/aes"
  10. "crypto/cipher"
  11. "encoding/base64"
  12. "errors"
  13. "math/rand"
  14. "time"
  15. "crypto/md5"
  16. "encoding/binary"
  17. "fmt"
  18. "strconv"
  19. "strings"
  20. "go.mod/conf"
  21. )
  22. // 当前时间的时间戳
  23. func NowUnix() int {
  24. return int(time.Now().In(conf.SysTimeLocation).Unix())
  25. }
  26. // 将unix时间戳格式化为yyyymmdd H:i:s格式字符串
  27. func FormatFromUnixTime(t int64) string {
  28. if t > 0 {
  29. return time.Unix(t, 0).Format(conf.SysTimeform)
  30. } else {
  31. return time.Now().Format(conf.SysTimeform)
  32. }
  33. }
  34. // 将unix时间戳格式化为yyyymmdd格式字符串
  35. func FormatFromUnixTimeShort(t int64) string {
  36. if t > 0 {
  37. return time.Unix(t, 0).Format(conf.SysTimeformShort)
  38. } else {
  39. return time.Now().Format(conf.SysTimeformShort)
  40. }
  41. }
  42. // 将字符串转成时间
  43. func ParseTime(str string) (time.Time, error) {
  44. return time.ParseInLocation(conf.SysTimeform, str, conf.SysTimeLocation)
  45. }
  46. // 得到一个随机数
  47. func Random(max int) int {
  48. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  49. if max < 1 {
  50. return r.Int()
  51. } else {
  52. return r.Intn(max)
  53. }
  54. }
  55. // 对字符串进行签名
  56. func CreateSign(str string) string {
  57. str = string(conf.SignSecret) + str
  58. sign := fmt.Sprintf("%x", md5.Sum([]byte(str)))
  59. return sign
  60. }
  61. // 对一个字符串进行加密
  62. func encrypt(key, text []byte) ([]byte, error) {
  63. block, err := aes.NewCipher(key)
  64. if err != nil {
  65. return nil, err
  66. }
  67. b := base64.StdEncoding.EncodeToString(text)
  68. ciphertext := make([]byte, aes.BlockSize+len(b))
  69. iv := ciphertext[:aes.BlockSize]
  70. //if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  71. // return nil, err
  72. //}
  73. cfb := cipher.NewCFBEncrypter(block, iv)
  74. cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
  75. return ciphertext, nil
  76. }
  77. // 对一个字符串进行解密
  78. func decrypt(key, text []byte) ([]byte, error) {
  79. block, err := aes.NewCipher(key)
  80. if err != nil {
  81. return nil, err
  82. }
  83. if len(text) < aes.BlockSize {
  84. return nil, errors.New("ciphertext too short")
  85. }
  86. iv := text[:aes.BlockSize]
  87. text = text[aes.BlockSize:]
  88. cfb := cipher.NewCFBDecrypter(block, iv)
  89. cfb.XORKeyStream(text, text)
  90. data, err := base64.StdEncoding.DecodeString(string(text))
  91. if err != nil {
  92. return nil, err
  93. }
  94. return data, nil
  95. }
  96. // addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。
  97. // 预定义字符是:
  98. // 单引号(')
  99. // 双引号(")
  100. // 反斜杠(\)
  101. func Addslashes(str string) string {
  102. tmpRune := []rune{}
  103. strRune := []rune(str)
  104. for _, ch := range strRune {
  105. switch ch {
  106. case []rune{'\\'}[0], []rune{'"'}[0], []rune{'\''}[0]:
  107. tmpRune = append(tmpRune, []rune{'\\'}[0])
  108. tmpRune = append(tmpRune, ch)
  109. default:
  110. tmpRune = append(tmpRune, ch)
  111. }
  112. }
  113. return string(tmpRune)
  114. }
  115. // stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。
  116. func Stripslashes(str string) string {
  117. dstRune := []rune{}
  118. strRune := []rune(str)
  119. strLenth := len(strRune)
  120. for i := 0; i < strLenth; i++ {
  121. if strRune[i] == []rune{'\\'}[0] {
  122. i++
  123. }
  124. dstRune = append(dstRune, strRune[i])
  125. }
  126. return string(dstRune)
  127. }
  128. // 将字符串的IP转化为数字
  129. func Ip4toInt(ip string) int64 {
  130. bits := strings.Split(ip, ".")
  131. if len(bits) == 4 {
  132. b0, _ := strconv.Atoi(bits[0])
  133. b1, _ := strconv.Atoi(bits[1])
  134. b2, _ := strconv.Atoi(bits[2])
  135. b3, _ := strconv.Atoi(bits[3])
  136. var sum int64
  137. sum += int64(b0) << 24
  138. sum += int64(b1) << 16
  139. sum += int64(b2) << 8
  140. sum += int64(b3)
  141. return sum
  142. } else {
  143. return 0
  144. }
  145. }
  146. // 得到当前时间到下一天零点的延时
  147. func NextDayDuration() time.Duration {
  148. year, month, day := time.Now().Add(time.Hour * 24).Date()
  149. next := time.Date(year, month, day, 0, 0, 0, 0, conf.SysTimeLocation)
  150. return next.Sub(time.Now())
  151. }
  152. // 从接口类型安全获取到int64
  153. func GetInt64(i interface{}, d int64) int64 {
  154. if i == nil {
  155. return d
  156. }
  157. switch i.(type) {
  158. case string:
  159. num, err := strconv.Atoi(i.(string))
  160. if err != nil {
  161. return d
  162. } else {
  163. return int64(num)
  164. }
  165. case []byte:
  166. bits := i.([]byte)
  167. if len(bits) == 8 {
  168. return int64(binary.LittleEndian.Uint64(bits))
  169. } else if len(bits) <= 4 {
  170. num, err := strconv.Atoi(string(bits))
  171. if err != nil {
  172. return d
  173. } else {
  174. return int64(num)
  175. }
  176. }
  177. case uint:
  178. return int64(i.(uint))
  179. case uint8:
  180. return int64(i.(uint8))
  181. case uint16:
  182. return int64(i.(uint16))
  183. case uint32:
  184. return int64(i.(uint32))
  185. case uint64:
  186. return int64(i.(uint64))
  187. case int:
  188. return int64(i.(int))
  189. case int8:
  190. return int64(i.(int8))
  191. case int16:
  192. return int64(i.(int16))
  193. case int32:
  194. return int64(i.(int32))
  195. case int64:
  196. return i.(int64)
  197. case float32:
  198. return int64(i.(float32))
  199. case float64:
  200. return int64(i.(float64))
  201. }
  202. return d
  203. }
  204. // 从接口类型安全获取到字符串类型
  205. func GetString(str interface{}, d string) string {
  206. if str == nil {
  207. return d
  208. }
  209. switch str.(type) {
  210. case string:
  211. return str.(string)
  212. case []byte:
  213. return string(str.([]byte))
  214. }
  215. return fmt.Sprintf("%s", str)
  216. }
  217. // 从map中得到指定的key
  218. func GetInt64FromMap(dm map[string]interface{}, key string, dft int64) int64 {
  219. data, ok := dm[key]
  220. if !ok {
  221. return dft
  222. }
  223. return GetInt64(data, dft)
  224. }
  225. // 从map中得到指定的key
  226. func GetInt64FromStringMap(dm map[string]string, key string, dft int64) int64 {
  227. data, ok := dm[key]
  228. if !ok {
  229. return dft
  230. }
  231. return GetInt64(data, dft)
  232. }
  233. // 从map中得到指定的key
  234. func GetStringFromMap(dm map[string]interface{}, key string, dft string) string {
  235. data, ok := dm[key]
  236. if !ok {
  237. return dft
  238. }
  239. return GetString(data, dft)
  240. }
  241. // 从map中得到指定的key
  242. func GetStringFromStringMap(dm map[string]string, key string, dft string) string {
  243. data, ok := dm[key]
  244. if !ok {
  245. return dft
  246. }
  247. return data
  248. }