1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright (c) 2022 Yansen Zhang
  3. * wxcomponent is licensed under Mulan PSL v2.
  4. * You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. * You may obtain a copy of Mulan PSL v2 at:
  6. * http://license.coscl.org.cn/MulanPSL2
  7. * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. * See the Mulan PSL v2 for more details.
  11. **/
  12. package utils
  13. import (
  14. "encoding/xml"
  15. "math/rand"
  16. "strings"
  17. "time"
  18. "gitee.com/yansen_zh/wxcomponent/utils/encrypt"
  19. )
  20. // RandStr 获取指定 n 长度的随机字符串
  21. func RandStr(n int) string {
  22. arr := strings.Split("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
  23. arrLen := len(arr)
  24. seed := time.Now().Unix()
  25. r := rand.New(rand.NewSource(seed))
  26. dst := make([]string, n)
  27. for i := range dst {
  28. p := r.Intn(arrLen)
  29. dst[i] = arr[p]
  30. }
  31. return strings.Join(dst, "")
  32. }
  33. // GetExpireTime 获取过期时间
  34. func GetExpireTime(sec int) time.Time {
  35. return time.Now().Add(time.Duration(sec) * time.Second)
  36. }
  37. // DeCodeXML 解析XML到MAP
  38. func DeCodeXML(src []byte) (*encrypt.XMLMap, error) {
  39. dst := new(encrypt.XMLMap)
  40. err := xml.Unmarshal(src, dst)
  41. return dst, err
  42. }