回调
CI / changes (push) Successful in 37s
CI / ad (push) Successful in 30s
CI / admin (push) Successful in 22s
CI / bff (push) Successful in 39s
CI / chore (push) Successful in 20s
CI / equipment (push) Successful in 19s
CI / express (push) Successful in 17s
CI / product (push) Successful in 16s
CI / record (push) Successful in 17s
CI / sale (push) Successful in 16s
CI / task (push) Successful in 16s
CI / user (push) Successful in 17s
CI / wecom (push) Successful in 18s

This commit is contained in:
2026-09-05 13:41:13 +08:00
parent be18a48148
commit 462d142a4c
9 changed files with 254 additions and 83 deletions
+28 -40
View File
@@ -1,7 +1,6 @@
package request
import (
"bytes"
"context"
"io"
"lone-services/pkg/utils"
@@ -17,24 +16,22 @@ import (
type ctxKey string
const (
CtxUserId ctxKey = "X-User-Id"
CtxUserName ctxKey = "X-User-Name"
CtxRefresh ctxKey = "X-Refresh"
CtxClientIP ctxKey = "X-Client-Ip"
CtxUserAgent ctxKey = "X-User-Agent"
CtxClientCode ctxKey = "X-Client-Code"
CtxSaleId ctxKey = "X-Sale-Id"
CtxSaleName ctxKey = "X-Sale-Name"
CtxSaleMobile ctxKey = "X-Sale-Mobile"
CtxSaleProvince ctxKey = "X-Sale-Province"
CtxStoreId ctxKey = "X-Store-Id"
CtxStoreName ctxKey = "X-Store-Name"
CtxGroupId ctxKey = "X-Group-Id"
CtxGroupName ctxKey = "X-Group-Name"
CtxUserType ctxKey = "X-User-Type"
CtxCallbackRawBody ctxKey = "callback_raw_body"
CtxCallbackHeaders ctxKey = "callback_headers"
CtxCallbackErr ctxKey = "callback_read_err"
CtxUserId ctxKey = "X-User-Id"
CtxUserName ctxKey = "X-User-Name"
CtxRefresh ctxKey = "X-Refresh"
CtxClientIP ctxKey = "X-Client-Ip"
CtxUserAgent ctxKey = "X-User-Agent"
CtxClientCode ctxKey = "X-Client-Code"
CtxSaleId ctxKey = "X-Sale-Id"
CtxSaleName ctxKey = "X-Sale-Name"
CtxSaleMobile ctxKey = "X-Sale-Mobile"
CtxSaleProvince ctxKey = "X-Sale-Province"
CtxStoreId ctxKey = "X-Store-Id"
CtxStoreName ctxKey = "X-Store-Name"
CtxGroupId ctxKey = "X-Group-Id"
CtxGroupName ctxKey = "X-Group-Name"
CtxUserType ctxKey = "X-User-Type"
CtxRawBody ctxKey = "X-Raw-Body"
)
var callbackPrefixes = []string{
@@ -60,6 +57,7 @@ type UserInfo struct {
StoreName string
GroupId string
GroupName string
RawBody string
Valid bool
}
@@ -79,6 +77,7 @@ func GetUserInfo(ctx context.Context) UserInfo {
storeName, _ := ctx.Value(CtxStoreName).(string)
groupId, _ := ctx.Value(CtxGroupId).(string)
groupName, _ := ctx.Value(CtxGroupName).(string)
rawBody, _ := ctx.Value(CtxRawBody).(string)
info := UserInfo{
RawUID: rawUID,
@@ -96,6 +95,7 @@ func GetUserInfo(ctx context.Context) UserInfo {
StoreName: storeName,
GroupId: groupId,
GroupName: groupName,
RawBody: rawBody,
}
if rawUID == "" {
return info
@@ -122,29 +122,17 @@ func getRealClientIP(r *http.Request) string {
return r.RemoteAddr
}
// UserReadMiddleware
/** 增加透传的方法
* 1、这里加上代码
* 2、UserClientInterceptor这个方法里也要写相应的代码对可以
*/
func UserReadMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
newCtx := r.Context()
if isCallbackPath(r.URL.Path) {
body, err := io.ReadAll(r.Body)
if err != nil {
newCtx = context.WithValue(newCtx, CtxCallbackRawBody, []byte(nil))
newCtx = context.WithValue(newCtx, CtxCallbackHeaders, http.Header(nil))
newCtx = context.WithValue(newCtx, CtxCallbackErr, err)
} else {
r.Body = io.NopCloser(bytes.NewBuffer(body))
header := make(http.Header)
for k, vv := range r.Header {
header[k] = vv
}
newCtx = context.WithValue(newCtx, CtxCallbackRawBody, body)
newCtx = context.WithValue(newCtx, CtxCallbackHeaders, header)
newCtx = context.WithValue(newCtx, CtxCallbackErr, error(nil))
}
next(w, r.WithContext(newCtx))
return
}
newCtx := r.Context()
body, _ := io.ReadAll(r.Body)
newCtx = context.WithValue(newCtx, CtxRawBody, string(body))
newCtx = context.WithValue(newCtx, CtxUserId, r.Header.Get("X-User-Id"))
newCtx = context.WithValue(newCtx, CtxUserName, r.Header.Get("X-User-Name"))
newCtx = context.WithValue(newCtx, CtxRefresh, r.Header.Get("X-Refresh"))
@@ -160,7 +148,6 @@ func UserReadMiddleware(next http.HandlerFunc) http.HandlerFunc {
newCtx = context.WithValue(newCtx, CtxStoreName, r.Header.Get("X-Store-Name"))
newCtx = context.WithValue(newCtx, CtxGroupId, r.Header.Get("X-Group-Id"))
newCtx = context.WithValue(newCtx, CtxGroupName, r.Header.Get("X-Group-Name"))
next(w, r.WithContext(newCtx))
}
}
@@ -189,6 +176,7 @@ func UserClientInterceptor(ctx context.Context, method string, req, reply any, c
md.Set("x-user-name", url.QueryEscape(user.Name))
}
setMDIfNotEmpty(md, "x-raw-body", user.RawBody)
setMDIfNotEmpty(md, "x-refresh", user.Refresh)
setMDIfNotEmpty(md, "x-client-ip", user.ClientIP)
if user.UserAgent != utils.StringEmpty {
+21 -1
View File
@@ -2,6 +2,7 @@ package response
import (
"bytes"
"lone-services/pkg/utils"
"net/http"
"strings"
@@ -16,6 +17,11 @@ type bodyWriter struct {
buf bytes.Buffer
}
type responseDataBack struct {
ReturnCode string `json:"returnCode"`
ReturnMsg string `json:"returnMsg"`
}
type responseData struct {
Code int32 `json:"code"`
Data any `json:"data"`
@@ -48,7 +54,21 @@ func Wrap(next http.HandlerFunc) http.HandlerFunc {
return
}
}
ctrl.OutPut(w, data.Code, unwrapJSONData(data.Data), data.Msg)
if data.Code < utils.NumberOne {
var back responseDataBack
if err := jsoniter.Unmarshal(raw, &back); err != nil {
ctrl.Fail(w)
return
}
if len(back.ReturnCode) < utils.NumberOne {
ctrl.Fail(w)
return
}
ctrl.OutPutBack(w, back)
} else {
ctrl.OutPut(w, data.Code, unwrapJSONData(data.Data), data.Msg)
}
}
}
+7
View File
@@ -53,3 +53,10 @@ func (b *BaseController) OutPut(w http.ResponseWriter, code int32, data any, msg
w.WriteHeader(http.StatusOK)
_ = jsoniter.NewEncoder(w).Encode(out)
}
func (b *BaseController) OutPutBack(w http.ResponseWriter, back responseDataBack) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_ = jsoniter.NewEncoder(w).Encode(back)
}