wangchuanjin il y a 9 mois
Parent
commit
7290c8b67c

+ 5 - 0
common/gatecode/errcode.go

@@ -14,6 +14,10 @@ type ErrCode int
 //go:generate stringer -type ErrCode -linecomment
 
 // 定义错误码 *代码后错误注释,即返回用户的错误信息
+const (
+	GLOBAL_SUCCESS ErrCode = 0 + iota
+)
+
 const (
 	// GLOBAL_ERR_NIL 常用全局异常定义
 	GLOBAL_ERR_ERR          ErrCode = 1000 + iota //代理服务异常
@@ -27,6 +31,7 @@ const (
 )
 
 var globalErrMap = map[ErrCode]string{
+	GLOBAL_SUCCESS:          "请求成功",
 	GLOBAL_ERR_ERR:          "代理服务异常",
 	GLOBAL_ERR_SIGN:         "无效的签名",
 	GLOBAL_ERR_NOBALANCE:    "余额不足",

+ 3 - 1
core/proxy/middleware/filterFuncs.go

@@ -58,7 +58,9 @@ func filterBefore(r *ghttp.Request) error {
 	}
 	//校验余额
 	gCtx := router.GetGContext(r.GetCtx())
-	if gCtx.RouterRule.Price <= 0 {
+	if gCtx == nil {
+		return NewErrorWithCode(GLOBAL_ERR_ERR)
+	} else if gCtx.RouterRule.Price <= 0 {
 		return nil
 	} else if amount := gconv.Int64((*users)[0]["amount"]); amount-gCtx.RouterRule.Price < 0 {
 		return NewErrorWithCode(GLOBAL_ERR_NOBALANCE, fmt.Sprint(amount))

+ 20 - 13
core/proxy/proxyServer.go

@@ -3,6 +3,7 @@ package proxy
 import (
 	"bytes"
 	"database/sql"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"io/ioutil"
@@ -11,7 +12,6 @@ import (
 	"strings"
 
 	log "app.yhyue.com/moapp/jylog"
-	"github.com/gogf/gf/v2/encoding/gjson"
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/net/ghttp"
 	"github.com/gogf/gf/v2/net/gtrace"
@@ -99,19 +99,26 @@ var proxyHandler = func(r *ghttp.Request) {
 		result := ""
 		if contentType := resp.Header.Get("content-type"); contentType == "application/json" {
 			b, e := ioutil.ReadAll(resp.Body)
-			if e == nil {
-				resp.Body = ioutil.NopCloser(bytes.NewReader(b))
-			}
-			jn, err := gjson.DecodeToJson(b)
-			if err != nil {
-				log.WithContext(r.Context()).Error("ChangeError", err)
-			} else if !jn.Contains("error_code") {
-				log.WithContext(r.Context()).Error("ChangeError:缺少error_code", string(b))
-			} else if jn.Get("error_code").Int64() == 0 {
-				isCharging = true
-				result = string(b)
+			jn := make(map[string]interface{})
+			if e != nil {
+				log.WithContext(r.Context()).Error("ChangeError", e)
 			} else {
-				status = 1
+				if err := json.Unmarshal(b, &jn); err != nil {
+					log.WithContext(r.Context()).Error("ChangeError", err)
+				} else if _, ok := jn["error_code"]; !ok {
+					log.WithContext(r.Context()).Error("ChangeError:缺少error_code", string(b))
+				} else if gconv.Int64(jn["error_code"]) == gconv.Int64(GLOBAL_SUCCESS) {
+					if error_msg, _ := jn["error_msg"].(string); error_msg == "" {
+						jn["error_msg"] = "请求成功"
+						b, _ = json.Marshal(jn)
+						resp.Header.Set("content-length", fmt.Sprint(len(b)))
+					}
+					result = string(b)
+					isCharging = true
+				} else {
+					status = 1
+				}
+				resp.Body = ioutil.NopCloser(bytes.NewReader(b))
 			}
 		} else if strings.Contains(resp.Header.Get("Content-Disposition"), "attachment") && resp.ContentLength > 0 { //下载文件
 			isCharging = true

+ 2 - 2
core/router/manager.go

@@ -24,7 +24,7 @@ type Manager struct {
 func InitRouterManager() (*Manager, error) {
 	//加载规则
 	res := db.GateWatMySql.Query(`SELECT * FROM front_proxy`)
-	if res == nil || len(*res) == 0 {
+	if res == nil {
 		return nil, errors.New("未发现可用路由")
 	}
 	// 初始化 routerManager
@@ -76,7 +76,7 @@ func (m *Manager) GetRouterRule(url string) (*Router, error) {
 		}
 	}
 	if routerRule == nil || routerRule.Status != 1 {
-		return nil, NewErrorWithCode(GLOBAL_ERR_NOTFIND, fmt.Sprintf("未找到请求地址%s", url))
+		return nil, NewErrorWithCode(GLOBAL_ERR_NOTFIND)
 	}
 	return routerRule, nil
 }