You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package rpc
  2. import (
  3. "github.com/kataras/iris/v12"
  4. "LEIT_PM/container"
  5. "LEIT_PM/global"
  6. "LEIT_PM/web/middleware/jwts"
  7. "LEIT_PM/web/supports"
  8. "reflect"
  9. )
  10. var DefaultMethodInvokerBuilder = &RouteBuilder{&MethodInvokerBuilder{&ValueParserBuilder{}, map[reflect.Type]valueParser{container.RequestContextType: keepValue}}}
  11. type RouteBuilder struct {
  12. methodInvokerBuilder *MethodInvokerBuilder
  13. }
  14. func (builder *RouteBuilder) Build(callerFactory func(string, *global.User) (container.Caller, error), serviceMethodName string, methodType reflect.Type) (container.HttpMethod, string, container.Router, error) {
  15. router := func(context iris.Context) {
  16. var json interface{}
  17. var jsonArray []interface{}
  18. if context.GetContentLength() > 0 {
  19. if nil != context.ReadJSON(&json) {
  20. supports.Error(context, iris.StatusBadRequest, "JSON解析失败!", nil)
  21. return
  22. }
  23. if json != nil {
  24. if jsonObject, ok := json.([]interface{}); !ok {
  25. supports.Error(context, iris.StatusBadRequest, "JSON解析失败!", nil)
  26. return
  27. } else {
  28. jsonArray = jsonObject
  29. }
  30. }
  31. }
  32. user, signature, ok := jwts.ParseTokenAndSignature(context)
  33. if !ok {
  34. supports.Error(context, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
  35. return
  36. }
  37. requestContext, err := container.NewRequestContext("rc", context)
  38. if err != nil {
  39. supports.Error(context, iris.StatusInternalServerError, err.Error(), nil)
  40. return
  41. }
  42. jsonArray = append([]interface{}{requestContext}, jsonArray...)
  43. caller, err := callerFactory(user.UserId+signature, user)
  44. if err != nil {
  45. supports.Error(context, iris.StatusInternalServerError, err.Error(), nil)
  46. return
  47. }
  48. methodInvoker, err := builder.methodInvokerBuilder.buildMethodInvoker(serviceMethodName, methodType, caller)
  49. if err != nil {
  50. supports.Error(context, iris.StatusInternalServerError, err.Error(), nil)
  51. return
  52. }
  53. defer func() {
  54. if err := recover(); err != nil {
  55. supports.Error(context, iris.StatusInternalServerError, "内部错误", nil)
  56. return
  57. }
  58. }()
  59. if result, err := methodInvoker.Process(jsonArray); err != nil {
  60. supports.Error(context, iris.StatusInternalServerError, err.Error(), nil)
  61. } else {
  62. supports.Ok(context, supports.OptionSuccess, result)
  63. }
  64. }
  65. return container.HttpPost, "", router, nil
  66. }