utils.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * @description:工具函数
  3. * @Author: CP
  4. * @Date: 2020-09-03 10:08:52
  5. * @FilePath: \construction_management\web\utils\utils.go
  6. */
  7. package utils
  8. import (
  9. "errors"
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "github.com/kataras/iris/v12"
  14. "go.mod/comm"
  15. "go.mod/conf"
  16. )
  17. // 发送短信
  18. func SendSMS() {
  19. }
  20. // 字符串转换MAP
  21. func StringToMap(str string, split string) map[string]string {
  22. entries := strings.Split(str, split)
  23. strMap := make(map[string]string)
  24. for _, e := range entries {
  25. parts := strings.Split(e, ":")
  26. if len(parts) <= 1 {
  27. strMap["error"] = strings.Replace(parts[0], " ", "", -1)
  28. } else {
  29. strMap[strings.Replace(parts[0], " ", "", -1)] = strings.Replace(parts[1], " ", "", -1)
  30. }
  31. }
  32. return strMap
  33. }
  34. // 获得项目ID
  35. func GetBackstageKey(ctx iris.Context, key string) (string, error) {
  36. //account := ctx.Values().Get("account").(*models.CmProjectAccount)
  37. // fmt.Println(account)
  38. value := ctx.Values().GetString(key)
  39. if value == "" {
  40. return "", errors.New("项目不存在")
  41. }
  42. return value, nil
  43. }
  44. // 获得项目ID
  45. func GetProjectId(ctx iris.Context) (int, error) {
  46. //account := ctx.Values().Get("account").(*models.CmProjectAccount)
  47. // fmt.Println(account)
  48. projectId, err := ctx.Values().GetInt("projectId")
  49. if err != nil {
  50. return 0, errors.New("项目不存在")
  51. }
  52. return projectId, nil
  53. }
  54. // 获得项目账号ID
  55. func GetProjectAccountId(ctx iris.Context) (int, error) {
  56. identityIdInt, err := ctx.Values().GetInt("accountId")
  57. if err != nil {
  58. return 0, errors.New("项目账号不存在")
  59. }
  60. return identityIdInt, nil
  61. }
  62. // 获得解密后的ID
  63. func GetDecryptId(id string) (int, error) {
  64. if id == "" {
  65. return 0, errors.New("ID 解析错误")
  66. }
  67. id, err := comm.AesDecrypt(id, conf.SignSecret)
  68. if err != nil {
  69. return 0, errors.New("ID 解析错误")
  70. }
  71. idInt, err := strconv.Atoi(id)
  72. if err != nil {
  73. return 0, errors.New("ID 转换错误")
  74. }
  75. return idInt, nil
  76. }
  77. // 金额转字符串 保留2位小数
  78. func PriceToStringFormat(price float64) string {
  79. // fmt.Println("price=========================")
  80. // fmt.Println(price)
  81. return fmt.Sprintf("%.2f", price)
  82. // return fmt.Sprintf("%g", price)
  83. }
  84. // 生成code
  85. func CreateRuleCode(code int64, count int64, len int) string {
  86. // fmt.Println("rule", rule, "counts", count)
  87. // egRule := strings.Split(rule.Eg, "-")
  88. // for _, value := range egRule {
  89. // if strings.Contains(value, "_") {
  90. // // fmt.Println("rule", rule.Code, count)
  91. // code := fmt.Printf("%0*d", rule.Code, count+1)
  92. // strings.Replace(rule.Eg, )
  93. // }
  94. // }
  95. // fmt.Println("egRule:", egRule)
  96. // return "123"
  97. // var newCode string
  98. if code == 0 {
  99. a := fmt.Sprintf("%0*d", len, count)
  100. return a
  101. } else {
  102. b := fmt.Sprintf("%0*d", len, count+code)
  103. return b
  104. }
  105. }
  106. // // 获得项目ID
  107. // func GetProjectId(ctx iris.Context) (int, error) {
  108. // jwtInfo := ctx.Values().Get("jwt").(*jwt.Token)
  109. // project := jwtInfo.Claims.(jwt.MapClaims)["project"].(string)
  110. // projectId, err := comm.AesDecrypt(project, conf.CookieSecret)
  111. // if err != nil {
  112. // return 0, errors.New("项目不存在")
  113. // }
  114. // projectIdInt, err := strconv.Atoi(projectId)
  115. // if err != nil {
  116. // return 0, errors.New("项目不存在")
  117. // }
  118. // return projectIdInt, nil
  119. // }
  120. // // 获得项目账号ID
  121. // func GetProjectAccountId(ctx iris.Context) (int, error) {
  122. // jwtInfo := ctx.Values().Get("jwt").(*jwt.Token)
  123. // identity := jwtInfo.Claims.(jwt.MapClaims)["identity"].(string)
  124. // identityId, err := comm.AesDecrypt(identity, conf.CookieSecret)
  125. // if err != nil {
  126. // return 0, errors.New("项目账号不存在")
  127. // }
  128. // identityIdInt, err := strconv.Atoi(identityId)
  129. // if err != nil {
  130. // return 0, errors.New("项目账号不存在")
  131. // }
  132. // return identityIdInt, nil
  133. // }