1. // @APIVersion 1.0.0
    2. // @Title 认证中心、用户中心
    3. // @Description token、用户、角色、权限
    4. package routers
    5. import (
    6. "fmt"
    7. "strings"
    8. "zhsq_go/code"
    9. "zhsq_go/user_center/controllers"
    10. "zhsq_go/user_center/models"
    11. "github.com/astaxie/beego"
    12. "github.com/astaxie/beego/context"
    13. "github.com/astaxie/beego/httplib"
    14. )
    15. func init() {
    16. // 所有接口访问之前的过滤器
    17. beego.InsertFilter("/*", beego.BeforeRouter, func(ctx *context.Context) {
    18. names := beego.AppConfig.Strings("proxy::names")
    19. for i := range names {
    20. //如果是静态资源,直接放行
    21. url := beego.AppConfig.String(fmt.Sprintf("proxy::%s_url", names[i]))
    22. if strings.HasPrefix(ctx.Request.RequestURI, url) {
    23. if strings.Contains(ctx.Request.RequestURI, "swagger") || strings.Contains(ctx.Request.RequestURI, "api-docs") {
    24. host := beego.AppConfig.String(fmt.Sprintf("proxy::%s_host", names[i]))
    25. request := httplib.Get(fmt.Sprintf("%s%v", host, ctx.Request.RequestURI))
    26. con, _ := request.Bytes()
    27. if strings.HasSuffix(ctx.Request.RequestURI, ".css") {
    28. ctx.Output.Header("Content-Type", "text/css")
    29. }
    30. ctx.Output.Body(con)
    31. return
    32. }
    33. }
    34. }
    35. // 校验Token是否有效
    36. result, userId := models.FilterValidateToken(ctx)
    37. if !result { //Token无效
    38. message := code.ApiResult{Code: code.UNAUTHORIZED, Message: "Token 无效!"}
    39. ctx.Output.JSON(message, false, false)
    40. return
    41. } else {
    42. //判断用户的请求
    43. if strings.HasPrefix(ctx.Request.RequestURI, "/api/user-center") { //用户中心
    44. // fmt.Println("userId :", userId)
    45. ctx.Input.SetData("userId", userId)
    46. } else {
    47. for i := range names {
    48. //实现代理
    49. url := beego.AppConfig.String(fmt.Sprintf("proxy::%s_url", names[i]))
    50. if strings.HasPrefix(ctx.Request.RequestURI, url) {
    51. // fmt.Println("其他模块接口")
    52. host := beego.AppConfig.String(fmt.Sprintf("proxy::%s_host", names[i]))
    53. // 请求方式
    54. method := ctx.Request.Method
    55. // 请求路径
    56. url := ctx.Request.RequestURI
    57. var req *httplib.BeegoHTTPRequest
    58. uri := fmt.Sprintf("%s%v", host, url)
    59. // fmt.Println("请求地址:", uri, " 请求方式:", method)
    60. if method == "POST" {
    61. req = httplib.Post(uri)
    62. // Post 请求参数
    63. req.Body(ctx.Input.RequestBody)
    64. } else if method == "GET" {
    65. req = httplib.Get(uri)
    66. }
    67. req.Param("userId", userId)
    68. req.Header("Content-Type", "application/json")
    69. response, _ := req.Response()
    70. // fmt.Println("response.Status :", response.Status)
    71. if strings.Contains(response.Status, "200") {
    72. con, _ := req.String()
    73. ctx.WriteString(con)
    74. } else {
    75. ctx.Output.JSON(code.ApiResult{Code: code.FAILED, Message: "操作失败!"}, false, false)
    76. }
    77. }
    78. }
    79. }
    80. }
    81. })
    82. ns := beego.NewNamespace("/api/user-center",
    83. beego.NSNamespace("/token",
    84. beego.NSInclude(
    85. &controllers.TokenController{},
    86. ),
    87. ),
    88. beego.NSNamespace("/user",
    89. beego.NSInclude(
    90. &controllers.UserController{},
    91. ),
    92. ),
    93. )
    94. beego.AddNamespace(ns)
    95. }
    1. appname = user_center
    2. httpaddr = 192.168.2.83
    3. httpport = 80
    4. runmode = dev
    5. autorender = false
    6. copyrequestbody = true
    7. EnableDocs = true
    8. [ignored]
    9. url=/token/authorize;/token/verification;/api/property/company/list
    10. [proxy]
    11. names=profit;property
    12. profit_host=http://127.0.0.1:8081
    13. profit_url=/api/profit
    14. profit_language=golang
    15. property_host=http://127.0.0.1:8090
    16. property_url=/api/property
    17. property_language=java