diff --git a/api/handler.tpl b/api/handler.tpl index fce5acc..4b62e9b 100644 --- a/api/handler.tpl +++ b/api/handler.tpl @@ -2,7 +2,7 @@ package {{.PkgName}} import ( "net/http" - + "context" "github.com/zeromicro/go-zero/rest/httpx" {{.ImportPackages}} ) @@ -15,7 +15,13 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc { httpx.ErrorCtx(r.Context(), w, err) return } - + // 获取请求头里的token + token := r.Header.Get("Authorization") + // 如果有token, 使用context传值到logic中 + if token != "" { + ctx := context.WithValue(r.Context(), "token", token) + r = r.WithContext(ctx) + } {{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx) {{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}}) if err != nil {