utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "strconv"
  11. "strings"
  12. "github.com/kataras/iris/v12"
  13. "go.mod/comm"
  14. "go.mod/conf"
  15. )
  16. // 发送短信
  17. func SendSMS() {
  18. }
  19. // 字符串转换MAP
  20. func StringToMap(str string, split string) map[string]string {
  21. entries := strings.Split(str, split)
  22. strMap := make(map[string]string)
  23. for _, e := range entries {
  24. parts := strings.Split(e, ":")
  25. if len(parts) <= 1 {
  26. strMap["error"] = strings.Replace(parts[0], " ", "", -1)
  27. } else {
  28. strMap[strings.Replace(parts[0], " ", "", -1)] = strings.Replace(parts[1], " ", "", -1)
  29. }
  30. }
  31. return strMap
  32. }
  33. // 获得项目ID
  34. func GetProjectId(ctx iris.Context) (int, error) {
  35. //account := ctx.Values().Get("account").(*viewmodels.ProjectAccount)
  36. // fmt.Println(account)
  37. projectId, err := ctx.Values().GetInt("projectId")
  38. if err != nil {
  39. return 0, errors.New("项目不存在")
  40. }
  41. return projectId, nil
  42. }
  43. // 获得项目账号ID
  44. func GetProjectAccountId(ctx iris.Context) (int, error) {
  45. identityIdInt, err := ctx.Values().GetInt("accountId")
  46. if err != nil {
  47. return 0, errors.New("项目账号不存在")
  48. }
  49. return identityIdInt, nil
  50. }
  51. // 获得解密后的ID
  52. func GetDecryptId(id string) (int, error) {
  53. id, err := comm.AesDecrypt(id, conf.SignSecret)
  54. if err != nil {
  55. return 0, errors.New("ID 解析错误")
  56. }
  57. idInt, err := strconv.Atoi(id)
  58. if err != nil {
  59. return 0, errors.New("ID 转换错误")
  60. }
  61. return idInt, nil
  62. }
  63. // // 获得项目ID
  64. // func GetProjectId(ctx iris.Context) (int, error) {
  65. // jwtInfo := ctx.Values().Get("jwt").(*jwt.Token)
  66. // project := jwtInfo.Claims.(jwt.MapClaims)["project"].(string)
  67. // projectId, err := comm.AesDecrypt(project, conf.CookieSecret)
  68. // if err != nil {
  69. // return 0, errors.New("项目不存在")
  70. // }
  71. // projectIdInt, err := strconv.Atoi(projectId)
  72. // if err != nil {
  73. // return 0, errors.New("项目不存在")
  74. // }
  75. // return projectIdInt, nil
  76. // }
  77. // // 获得项目账号ID
  78. // func GetProjectAccountId(ctx iris.Context) (int, error) {
  79. // jwtInfo := ctx.Values().Get("jwt").(*jwt.Token)
  80. // identity := jwtInfo.Claims.(jwt.MapClaims)["identity"].(string)
  81. // identityId, err := comm.AesDecrypt(identity, conf.CookieSecret)
  82. // if err != nil {
  83. // return 0, errors.New("项目账号不存在")
  84. // }
  85. // identityIdInt, err := strconv.Atoi(identityId)
  86. // if err != nil {
  87. // return 0, errors.New("项目账号不存在")
  88. // }
  89. // return identityIdInt, nil
  90. // }