wangchuanjin hace 3 meses
padre
commit
a48626a446
Se han modificado 2 ficheros con 33 adiciones y 22 borrados
  1. 28 21
      core/proxy/proxyServer.go
  2. 5 1
      core/router/manager.go

+ 28 - 21
core/proxy/proxyServer.go

@@ -97,6 +97,8 @@ var proxyHandler = func(r *ghttp.Request) {
 		status := 0
 		isCharging := false //是否扣费
 		result := ""
+		var error_code int64
+		error_msg := ""
 		if contentType := resp.Header.Get("content-type"); contentType == "application/json" {
 			b, e := ioutil.ReadAll(resp.Body)
 			jn := make(map[string]interface{})
@@ -107,16 +109,21 @@ var proxyHandler = func(r *ghttp.Request) {
 					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
+					error_code = gconv.Int64(jn["error_code"])
+					error_msg, _ = jn["error_msg"].(string)
+					if error_code == gconv.Int64(GLOBAL_SUCCESS) {
+						if error_msg == "" {
+							error_msg = "请求成功"
+							jn["error_msg"] = 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))
 			}
@@ -124,10 +131,11 @@ var proxyHandler = func(r *ghttp.Request) {
 			isCharging = true
 			result = fmt.Sprint(resp.ContentLength)
 		}
-		if isCharging {
-			if db.GateWatMySql.ExecTx("计费处理", func(tx *sql.Tx) bool {
-				deductMode := 2 //赠送扣除
-				appid := r.GetQuery("appid").String()
+		appid := r.GetQuery("appid").String()
+		if db.GateWatMySql.ExecTx("计费处理", func(tx *sql.Tx) bool {
+			var surplus, deductMode int64
+			if isCharging {
+				deductMode = 2 //赠送扣除
 				giftUpdate := db.GateWatMySql.UpdateOrDeleteBySqlByTx(tx, `UPDATE USER a INNER JOIN gift b ON (a.appid=? AND a.id=b.user_id and b.count>0)
 					INNER JOIN front_proxy c ON (c.url=? AND b.front_proxy_id=c.id)
 					SET b.count=b.count-1`, appid, gCtx.RouterRule.ReqUrl)
@@ -158,20 +166,19 @@ var proxyHandler = func(r *ghttp.Request) {
 					log.WithContext(r.Context()).Error(appid, "余额不足")
 					return false
 				}
-				var surplus int64
 				if deductMode == 1 {
 					surplus = amount
 				} else {
 					surplus = giftCount
 				}
-				logRes1, logRes2 := db.GateWatMySql.InsertBatchByTx(tx, "log", []string{"appid", "url", "param", "result", "price", "surplus", "mode", "ip", "create_time"}, []interface{}{appid, gCtx.RouterRule.ReqUrl, string(reqBody), result, gCtx.RouterRule.Price, surplus, deductMode, r.GetClientIp(), gtime.Now().String()})
-				if logRes1 <= 0 || logRes2 <= 0 {
-					return false
-				}
-				return true
-			}) {
-				status = 1
 			}
+			logRes1, logRes2 := db.GateWatMySql.InsertBatchByTx(tx, "log", []string{"appid", "url", "param", "result", "price", "surplus", "mode", "ip", "error_code", "error_msg", "create_time"}, []interface{}{appid, gCtx.RouterRule.ReqUrl, string(reqBody), result, gCtx.RouterRule.Price, surplus, deductMode, r.GetClientIp(), error_code, error_msg, gtime.Now().String()})
+			if logRes1 <= 0 || logRes2 <= 0 {
+				return false
+			}
+			return true
+		}) {
+			status = 1
 		}
 		if status == 0 {
 			return errors.New(fmt.Sprintf("RespError_%d", GLOBAL_ERR_SERVICE))

+ 5 - 1
core/router/manager.go

@@ -109,10 +109,14 @@ func (m *Manager) InfusionContext(r *ghttp.Request) (err error) {
 		Status:   router.Status,
 		ReqUrl:   router.ReqUrl,
 		TimeOut:  router.TimeOut,
+		Price:    router.Price,
 		Remark:   router.Remark,
 		Server:   router.Server,
-		Price:    gconv.Int64((*res)[0]["price"]),
 		ReqLimit: router.ReqLimit,
+		IsFree:   router.IsFree,
+	}
+	if price := gconv.Int64((*res)[0]["price"]); price >= 0 {
+		router.Price = price
 	}
 	rl := &util.ReqLimit{
 		Size: gconv.Int((*res)[0]["pool_size"]),