dcsunny 3 年之前
父節點
當前提交
9b69c8a98e
共有 3 個文件被更改,包括 22 次插入8 次删除
  1. 16 1
      codes/code.go
  2. 1 2
      common/context.go
  3. 5 5
      http/handle.go

+ 16 - 1
codes/code.go

@@ -37,7 +37,7 @@ func SystemErr(msg string) error {
 	return Error(10500, msg)
 }
 
-//Customize 自定义返回值. 如自己重定向
+// Customize 自定义返回值. 如自己重定向
 func Customize() error {
 	return Error(1000, "")
 }
@@ -65,3 +65,18 @@ func ParseErrorWithData(err error, data interface{}) (int, string) {
 func ParseError(err error) (int, string) {
 	return ParseErrorWithData(err, nil)
 }
+
+func ErrorEqual(err1 error, err2 error) bool {
+	code1, msg1 := ParseError(err1)
+	code2, msg2 := ParseError(err2)
+	if code1 == 0 && code2 == 0 {
+		if msg1 == msg2 {
+			return true
+		}
+		return false
+	}
+	if code1 == code2 {
+		return true
+	}
+	return false
+}

+ 1 - 2
common/context.go

@@ -14,8 +14,7 @@ func NewContext(parentCtx context.Context) context.Context {
 
 func NewContextWithAccountID(parentCtx context.Context, accountID int64) context.Context {
 	ctx := &AccountContext{ctx: parentCtx}
-	newCtx := context2.AppendToContext(ctx, "user_id", accountID)
-	newCtx = context2.AppendToContext(newCtx, "account_id", accountID)
+	newCtx := context2.AppendToContext(ctx, "account_id", accountID)
 	return newCtx
 }
 

+ 5 - 5
http/handle.go

@@ -3,7 +3,7 @@ package http
 import (
 	"bytes"
 	json2 "encoding/json"
-	"io/ioutil"
+	"io"
 	"net/http"
 	"strconv"
 	"strings"
@@ -22,7 +22,7 @@ import (
 	_ "github.com/go-kratos/kratos/v2/encoding/proto"
 )
 
-//DecodeRequest  decodeRequest decodes the request body to object.
+// DecodeRequest  decodeRequest decodes the request body to object.
 func DecodeRequest(req *http.Request, v interface{}) error {
 	method := strings.ToUpper(req.Method)
 	if method == "POST" || method == "PUT" || method == "DELETE" {
@@ -35,7 +35,7 @@ func DecodeRequest(req *http.Request, v interface{}) error {
 		}
 		subtype := contentSubtype(contextType)
 		if codec := encoding.GetCodec(subtype); codec != nil {
-			data, err := ioutil.ReadAll(req.Body)
+			data, err := io.ReadAll(req.Body)
 			if err != nil {
 				return err
 			}
@@ -82,7 +82,7 @@ func parseForm(req *http.Request, v interface{}) error {
 	return err
 }
 
-//EncodeResponse encodes the object to the HTTP response.
+// EncodeResponse encodes the object to the HTTP response.
 func EncodeResponse(w http.ResponseWriter, r *http.Request, v interface{}) error {
 	codec := codecForRequest(r)
 	data, err := codec.Marshal(v)
@@ -195,6 +195,6 @@ func contentSubtype(contentType string) string {
 }
 
 func GetBody(r *http.Request) []byte {
-	b, _ := ioutil.ReadAll(r.Body)
+	b, _ := io.ReadAll(r.Body)
 	return b
 }