stringutil.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package utils
  2. import (
  3. "crypto/md5"
  4. cryptoRand "crypto/rand"
  5. "encoding/hex"
  6. "fmt"
  7. "io"
  8. "math/big"
  9. "math/rand"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/dchest/captcha"
  15. )
  16. func FilterXSS(str string) string {
  17. str = strings.Replace(str, "<", "&#60;", -1)
  18. str = strings.Replace(str, ">", "&#62;", -1)
  19. str = strings.Replace(str, "%3C", "&#60;", -1)
  20. str = strings.Replace(str, "%3E", "&#62;", -1)
  21. str = strings.Replace(str, "expression", "expression", -1)
  22. str = strings.Replace(str, "javascript", "javascript", -1)
  23. return str
  24. }
  25. func MakeSimpleCaptcha(n int) string {
  26. var idChars = []byte("0123456789")
  27. b := captcha.RandomDigits(n)
  28. for i, c := range b {
  29. b[i] = idChars[c]
  30. }
  31. return string(b)
  32. }
  33. /**
  34. 获取当前时间的毫秒值
  35. */
  36. func Now4Millis() int64 {
  37. return time.Now().UnixNano() / 1e6
  38. }
  39. /*产生n以内的随机数*/
  40. func MakeIntRand(n int) int {
  41. rand.Seed(time.Now().UnixNano())
  42. return rand.Intn(n)
  43. }
  44. //生成32位md5字串
  45. func GetMd5String(s string) string {
  46. h := md5.New()
  47. h.Write([]byte(s))
  48. return hex.EncodeToString(h.Sum(nil))
  49. }
  50. func GenerateSimpleToken() string {
  51. rand.Seed(time.Now().UnixNano())
  52. str := fmt.Sprintf("%d%d", time.Now().UnixNano(), rand.Intn(999999))
  53. h := md5.New()
  54. h.Write([]byte(str))
  55. return hex.EncodeToString(h.Sum(nil))
  56. }
  57. //var pool = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$"
  58. //获取复杂的随机数
  59. func GetLetterRandom(length int, flag ...bool) string {
  60. var idChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
  61. var mod byte = 52
  62. if len(flag) > 0 && flag[0] {
  63. idChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  64. mod = 26
  65. }
  66. b := make([]byte, length)
  67. maxrb := byte(256 - (256 % int(mod)))
  68. i := 0
  69. EXIT:
  70. for {
  71. r := make([]byte, length+(length/4))
  72. if _, err := io.ReadFull(cryptoRand.Reader, r); err != nil {
  73. panic("GetLetterRandom: error reading random source: " + err.Error())
  74. }
  75. for _, c := range r {
  76. if c > maxrb {
  77. continue
  78. }
  79. b[i] = c % mod
  80. i++
  81. if i == length {
  82. break EXIT
  83. }
  84. }
  85. }
  86. for i, c := range b {
  87. b[i] = idChars[c]
  88. }
  89. return string(b)
  90. }
  91. /*获取复杂的随机数,数字和字母的组合
  92. * c > 2 数字的个数和字母的个数随机分配
  93. * n 数字的个数
  94. * l 字母的个数
  95. */
  96. func GetComplexRandom(c, n, l int) string {
  97. if c < 2 && (n < 1 || l < 1) {
  98. return "--"
  99. }
  100. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  101. myCommonMethod := func(flag bool) int {
  102. if flag {
  103. return r.Intn(c-1) + 1
  104. } else {
  105. return r.Intn(c)
  106. }
  107. }
  108. if c >= 2 {
  109. n = myCommonMethod(true)
  110. l = c - n
  111. } else {
  112. c = l + n
  113. }
  114. value := MakeSimpleCaptcha(n) + GetLetterRandom(l)
  115. var array = strings.Split(value, "")
  116. for i := 0; i < c/2; i++ {
  117. r1 := myCommonMethod(false)
  118. r2 := myCommonMethod(false)
  119. o := array[r1]
  120. array[r1] = array[r2]
  121. array[r2] = o
  122. }
  123. return strings.Join(array, "")
  124. }
  125. func GetRandom(n int) string {
  126. var idChars = []byte("0123456789")
  127. b := captcha.RandomDigits(n)
  128. for i, c := range b {
  129. b[i] = idChars[c]
  130. }
  131. return string(b)
  132. }
  133. func ObjToString(old interface{}) string {
  134. if nil == old {
  135. return ""
  136. } else {
  137. r, _ := old.(string)
  138. return r
  139. }
  140. }
  141. func IntAll(num interface{}) int {
  142. return IntAllDef(num, 0)
  143. }
  144. func IntAllDef(num interface{}, defaultNum int) int {
  145. if i, ok := num.(int); ok {
  146. return int(i)
  147. } else if i0, ok0 := num.(int32); ok0 {
  148. return int(i0)
  149. } else if i1, ok1 := num.(float64); ok1 {
  150. return int(i1)
  151. } else if i2, ok2 := num.(int64); ok2 {
  152. return int(i2)
  153. } else if i3, ok3 := num.(float32); ok3 {
  154. return int(i3)
  155. } else if i4, ok4 := num.(string); ok4 {
  156. in, _ := strconv.Atoi(i4)
  157. return int(in)
  158. } else if i5, ok5 := num.(int16); ok5 {
  159. return int(i5)
  160. } else if i6, ok6 := num.(int8); ok6 {
  161. return int(i6)
  162. } else if i7, ok7 := num.(*big.Int); ok7 {
  163. in, _ := strconv.Atoi(fmt.Sprint(i7))
  164. return int(in)
  165. } else if i8, ok8 := num.(*big.Float); ok8 {
  166. in, _ := strconv.Atoi(fmt.Sprint(i8))
  167. return int(in)
  168. } else {
  169. return defaultNum
  170. }
  171. }
  172. func ObjArrToMapArr(old []interface{}) []map[string]interface{} {
  173. if old != nil {
  174. new := make([]map[string]interface{}, len(old))
  175. for i, v := range old {
  176. new[i], _ = v.(map[string]interface{})
  177. }
  178. return new
  179. } else {
  180. return nil
  181. }
  182. }
  183. func Int64All(num interface{}) int64 {
  184. if i, ok := num.(int64); ok {
  185. return int64(i)
  186. } else if i0, ok0 := num.(int32); ok0 {
  187. return int64(i0)
  188. } else if i1, ok1 := num.(float64); ok1 {
  189. return int64(i1)
  190. } else if i2, ok2 := num.(int); ok2 {
  191. return int64(i2)
  192. } else if i3, ok3 := num.(float32); ok3 {
  193. return int64(i3)
  194. } else if i4, ok4 := num.(string); ok4 {
  195. i64, _ := strconv.ParseInt(i4, 10, 64)
  196. //in, _ := strconv.Atoi(i4)
  197. return i64
  198. } else if i5, ok5 := num.(int16); ok5 {
  199. return int64(i5)
  200. } else if i6, ok6 := num.(int8); ok6 {
  201. return int64(i6)
  202. } else if i7, ok7 := num.(*big.Int); ok7 {
  203. in, _ := strconv.ParseInt(fmt.Sprint(i7), 10, 64)
  204. return int64(in)
  205. } else if i8, ok8 := num.(*big.Float); ok8 {
  206. in, _ := strconv.ParseInt(fmt.Sprint(i8), 10, 64)
  207. return int64(in)
  208. } else {
  209. return 0
  210. }
  211. }
  212. func Float64All(num interface{}) float64 {
  213. if i, ok := num.(float64); ok {
  214. return float64(i)
  215. } else if i0, ok0 := num.(int32); ok0 {
  216. return float64(i0)
  217. } else if i1, ok1 := num.(int64); ok1 {
  218. return float64(i1)
  219. } else if i2, ok2 := num.(int); ok2 {
  220. return float64(i2)
  221. } else if i3, ok3 := num.(float32); ok3 {
  222. return float64(i3)
  223. } else if i4, ok4 := num.(string); ok4 {
  224. in, _ := strconv.ParseFloat(i4, 64)
  225. return in
  226. } else if i5, ok5 := num.(int16); ok5 {
  227. return float64(i5)
  228. } else if i6, ok6 := num.(int8); ok6 {
  229. return float64(i6)
  230. } else if i6, ok6 := num.(uint); ok6 {
  231. return float64(i6)
  232. } else if i6, ok6 := num.(uint8); ok6 {
  233. return float64(i6)
  234. } else if i6, ok6 := num.(uint16); ok6 {
  235. return float64(i6)
  236. } else if i6, ok6 := num.(uint32); ok6 {
  237. return float64(i6)
  238. } else if i6, ok6 := num.(uint64); ok6 {
  239. return float64(i6)
  240. } else if i7, ok7 := num.(*big.Float); ok7 {
  241. in, _ := strconv.ParseFloat(fmt.Sprint(i7), 64)
  242. return float64(in)
  243. } else if i8, ok8 := num.(*big.Int); ok8 {
  244. in, _ := strconv.ParseFloat(fmt.Sprint(i8), 64)
  245. return float64(in)
  246. } else {
  247. return 0
  248. }
  249. }
  250. //通用加密
  251. func CommonEncodeArticle(stype string, keys ...string) (id string) {
  252. //正文
  253. var SE = &SimpleEncrypt{Key: "topnet2015topnet2015"}
  254. var SE2 = &SimpleEncrypt{Key: "2017jianyu"}
  255. if stype == "content" {
  256. id = BEncodeArticleId2ByCheck("A", SE, SE2, keys...)
  257. }
  258. return
  259. }
  260. //短地址加密,二次加密带校验和
  261. func BEncodeArticleId2ByCheck(h string, s1, s2 *SimpleEncrypt, keys ...string) string {
  262. kstr := strings.Join(keys, ",")
  263. kstr = s1.EncodeStringByCheck(kstr)
  264. return url.QueryEscape(h + GetLetterRandom(2) + s2.EncodeStringByCheck(kstr))
  265. }
  266. /**
  267. 产生一般订单编号方法
  268. */
  269. func CreateOrderCode() string {
  270. return fmt.Sprint(time.Now().Unix()) + fmt.Sprint(GetRandom(6))
  271. }
  272. func GetDayMinMax(t time.Time) (int64, int64) {
  273. min := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local).Unix()
  274. return min, min + 86400
  275. }