utils.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // 获得员工组
  21. func GetAccountGroup() map[int]string {
  22. groupDate := map[int]string{
  23. 1: "建设单位",
  24. 2: "监理单位",
  25. 3: "施工单位",
  26. 4: "设计单位",
  27. }
  28. return groupDate
  29. }
  30. // 字符串转换MAP
  31. func StringToMap(str string, split string) map[string]string {
  32. entries := strings.Split(str, split)
  33. strMap := make(map[string]string)
  34. for _, e := range entries {
  35. parts := strings.Split(e, ":")
  36. if len(parts) <= 1 {
  37. strMap["error"] = strings.Replace(parts[0], " ", "", -1)
  38. } else {
  39. strMap[strings.Replace(parts[0], " ", "", -1)] = strings.Replace(parts[1], " ", "", -1)
  40. }
  41. }
  42. return strMap
  43. }
  44. // 获得项目ID
  45. func GetBackstageKey(ctx iris.Context, key string) (string, error) {
  46. //account := ctx.Values().Get("account").(*models.CmProjectAccount)
  47. // fmt.Println(account)
  48. value := ctx.Values().GetString(key)
  49. if value == "" {
  50. return "", errors.New("员工名称不存在")
  51. }
  52. return value, nil
  53. }
  54. // 获得项目ID
  55. func GetProjectId(ctx iris.Context) (int, error) {
  56. //account := ctx.Values().Get("account").(*models.CmProjectAccount)
  57. // fmt.Println(account)
  58. projectId, err := ctx.Values().GetInt("projectId")
  59. if err != nil {
  60. return 0, errors.New("项目不存在")
  61. }
  62. return projectId, nil
  63. }
  64. // 获得项目账号ID
  65. func GetProjectAccountId(ctx iris.Context) (int, error) {
  66. identityIdInt, err := ctx.Values().GetInt("accountId")
  67. if err != nil {
  68. return 0, errors.New("项目账号不存在")
  69. }
  70. return identityIdInt, nil
  71. }
  72. // 获得解密后的ID
  73. func GetDecryptId(id string) (int, error) {
  74. if id == "" {
  75. return 0, errors.New("ID 解析错误")
  76. }
  77. id, err := comm.AesDecrypt(id, conf.SignSecret)
  78. if err != nil {
  79. return 0, errors.New("ID 解析错误")
  80. }
  81. idInt, err := strconv.Atoi(id)
  82. if err != nil {
  83. return 0, errors.New("ID 转换错误")
  84. }
  85. return idInt, nil
  86. }
  87. // 金额转字符串 保留2位小数
  88. func PriceToStringFormat(price float64) string {
  89. // fmt.Println("price=========================")
  90. // fmt.Println(price)
  91. return fmt.Sprintf("%.2f", price)
  92. // return fmt.Sprintf("%g", price)
  93. }
  94. // 生成code
  95. func CreateRuleCode(code int64, count int64, len int) string {
  96. // fmt.Println("rule", rule, "counts", count)
  97. // egRule := strings.Split(rule.Eg, "-")
  98. // for _, value := range egRule {
  99. // if strings.Contains(value, "_") {
  100. // // fmt.Println("rule", rule.Code, count)
  101. // code := fmt.Printf("%0*d", rule.Code, count+1)
  102. // strings.Replace(rule.Eg, )
  103. // }
  104. // }
  105. // fmt.Println("egRule:", egRule)
  106. // return "123"
  107. // var newCode string
  108. if code == 0 {
  109. a := fmt.Sprintf("%0*d", len, count)
  110. return a
  111. } else {
  112. b := fmt.Sprintf("%0*d", len, count+code)
  113. return b
  114. }
  115. }
  116. // // 获得项目ID
  117. // func GetProjectId(ctx iris.Context) (int, error) {
  118. // jwtInfo := ctx.Values().Get("jwt").(*jwt.Token)
  119. // project := jwtInfo.Claims.(jwt.MapClaims)["project"].(string)
  120. // projectId, err := comm.AesDecrypt(project, conf.CookieSecret)
  121. // if err != nil {
  122. // return 0, errors.New("项目不存在")
  123. // }
  124. // projectIdInt, err := strconv.Atoi(projectId)
  125. // if err != nil {
  126. // return 0, errors.New("项目不存在")
  127. // }
  128. // return projectIdInt, nil
  129. // }
  130. // // 获得项目账号ID
  131. // func GetProjectAccountId(ctx iris.Context) (int, error) {
  132. // jwtInfo := ctx.Values().Get("jwt").(*jwt.Token)
  133. // identity := jwtInfo.Claims.(jwt.MapClaims)["identity"].(string)
  134. // identityId, err := comm.AesDecrypt(identity, conf.CookieSecret)
  135. // if err != nil {
  136. // return 0, errors.New("项目账号不存在")
  137. // }
  138. // identityIdInt, err := strconv.Atoi(identityId)
  139. // if err != nil {
  140. // return 0, errors.New("项目账号不存在")
  141. // }
  142. // return identityIdInt, nil
  143. // }