common.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package gateway
  2. import (
  3. "git.sxidc.com/go-tools/utils/strutils"
  4. )
  5. func AddJsonBodyTenantIDAndUserInfo(requestBuilder *RequestBuilder, tenantIDParamsName string, userIDParamsName string, operatorUserNameParamsName string) error {
  6. jsonBody, err := requestBuilder.c.apiContext.GetJsonBody()
  7. if err != nil {
  8. return err
  9. }
  10. if strutils.IsStringNotEmpty(tenantIDParamsName) {
  11. tenantID := jsonBody.Get(tenantIDParamsName)
  12. if tenantID == nil {
  13. tenantInfo := requestBuilder.c.apiContext.GetTenantInfo()
  14. if tenantInfo != nil {
  15. jsonBody.Set(tenantIDParamsName, tenantInfo.GetID())
  16. } else {
  17. jsonBody.Set(tenantIDParamsName, "guest-"+strutils.SimpleUUID()[:26])
  18. }
  19. }
  20. }
  21. if strutils.IsStringNotEmpty(userIDParamsName) {
  22. userID := jsonBody.Get(userIDParamsName)
  23. if userID == nil {
  24. userInfo := requestBuilder.c.apiContext.GetUserInfo()
  25. if userInfo != nil {
  26. jsonBody.Set(userIDParamsName, userInfo.GetID())
  27. } else {
  28. jsonBody.Set(userIDParamsName, "guest-"+strutils.SimpleUUID()[:26])
  29. }
  30. }
  31. }
  32. if strutils.IsStringNotEmpty(operatorUserNameParamsName) {
  33. operatorUserName := jsonBody.Get(operatorUserNameParamsName)
  34. if operatorUserName == nil {
  35. userInfo := requestBuilder.c.apiContext.GetUserInfo()
  36. if userInfo != nil {
  37. jsonBody.Set(operatorUserNameParamsName, userInfo.GetUserName())
  38. } else {
  39. jsonBody.Set(operatorUserNameParamsName, "guest-"+strutils.SimpleUUID()[:26])
  40. }
  41. }
  42. }
  43. return nil
  44. }
  45. func AddQueryParamsTenantIDAndUserInfo(requestBuilder *RequestBuilder, tenantIDParamsName string, userIDParamsName string, operatorUserNameParamsName string) error {
  46. queryParams := requestBuilder.c.apiContext.GetQueryParams()
  47. if requestBuilder.c.apiContext.GetTenantInfo() != nil && strutils.IsStringNotEmpty(tenantIDParamsName) {
  48. tenantID := queryParams.Get(tenantIDParamsName)
  49. if strutils.IsStringEmpty(tenantID) {
  50. queryParams.Set(tenantIDParamsName, requestBuilder.c.apiContext.GetTenantInfo().GetID())
  51. }
  52. }
  53. if requestBuilder.c.apiContext.GetUserInfo() != nil && strutils.IsStringNotEmpty(userIDParamsName) {
  54. userID := queryParams.Get(userIDParamsName)
  55. if strutils.IsStringEmpty(userID) {
  56. queryParams.Set(userIDParamsName, requestBuilder.c.apiContext.GetUserInfo().GetID())
  57. }
  58. }
  59. if requestBuilder.c.apiContext.GetUserInfo() != nil && strutils.IsStringNotEmpty(operatorUserNameParamsName) {
  60. operatorUserName := queryParams.Get(operatorUserNameParamsName)
  61. if strutils.IsStringEmpty(operatorUserName) {
  62. queryParams.Set(operatorUserNameParamsName, requestBuilder.c.apiContext.GetUserInfo().GetUserName())
  63. }
  64. }
  65. return nil
  66. }