Przeglądaj źródła

通用结构&&需要校验权限则先判断此接口是否有此functionCode的方法

wangkaiyue 3 lat temu
rodzic
commit
4204227216

+ 1 - 0
common/gatecode/errcode.go

@@ -18,6 +18,7 @@ const (
 	GLOBAL_ERR_ENTACCOUNT_AUTH_AWAIT                          // 企业认证待审核
 	GLOBAL_ERR_ENTACCOUNT_AUTH_NOTPASS                        // 企业认证未通过
 	GLOBAL_ERR_ENTACCOUNT_AUTH_EXPIRED                        // 企业认证已过期
+	GLOBAL_ERR_RESOURCE_POWERCODE_ERR                         // 无效资源code
 	GLOBAL_ERR_RESOURCE_PORWE_FAIL                            // 权限校验失败
 	GLOBAL_ERR_RESOURCE_PORWE_EXPIRED                         // 权益已过期
 	GLOBAL_ERR_RESOURCE_PORWE_NOTENOUGH                       // 权益余额不足

+ 27 - 26
common/gatecode/errcode_string.go

@@ -18,34 +18,35 @@ func _() {
 	_ = x[GLOBAL_ERR_ENTACCOUNT_AUTH_AWAIT-1007]
 	_ = x[GLOBAL_ERR_ENTACCOUNT_AUTH_NOTPASS-1008]
 	_ = x[GLOBAL_ERR_ENTACCOUNT_AUTH_EXPIRED-1009]
-	_ = x[GLOBAL_ERR_RESOURCE_PORWE_FAIL-1010]
-	_ = x[GLOBAL_ERR_RESOURCE_PORWE_EXPIRED-1011]
-	_ = x[GLOBAL_ERR_RESOURCE_PORWE_NOTENOUGH-1012]
-	_ = x[GLOBAL_ERR_RESOURCE_PORWE_NOTHAS-1013]
-	_ = x[GATEWAY_ERR_NIL-2014]
-	_ = x[GATEWAY_MODULE_UNDEFINED-2015]
-	_ = x[GATEWAY_ROUTER_NOTFIND-2016]
-	_ = x[GATEWAY_ROUTER_UPHOLD-2017]
-	_ = x[GATEWAY_REGISTED_URL_ERR-2018]
-	_ = x[GATEWAY_PROXY_ERR-2019]
-	_ = x[GATEWAY_RPC_USERCENTER_ERR-2020]
-	_ = x[GATEWAY_RPC_RESOURCECENTER_ERR-2021]
-	_ = x[SERVER_ERR_NIL-3022]
-	_ = x[SERVER_DETAIL_TIMEOUT-3023]
-	_ = x[SERVER_DEDUCT_PARAM_LACK-3024]
-	_ = x[OTHER_ERR_NIL-4025]
-	_ = x[OTHER_ERR_UNDEFINED-4026]
+	_ = x[GLOBAL_ERR_RESOURCE_POWERCODE_ERR-1010]
+	_ = x[GLOBAL_ERR_RESOURCE_PORWE_FAIL-1011]
+	_ = x[GLOBAL_ERR_RESOURCE_PORWE_EXPIRED-1012]
+	_ = x[GLOBAL_ERR_RESOURCE_PORWE_NOTENOUGH-1013]
+	_ = x[GLOBAL_ERR_RESOURCE_PORWE_NOTHAS-1014]
+	_ = x[GATEWAY_ERR_NIL-2015]
+	_ = x[GATEWAY_MODULE_UNDEFINED-2016]
+	_ = x[GATEWAY_ROUTER_NOTFIND-2017]
+	_ = x[GATEWAY_ROUTER_UPHOLD-2018]
+	_ = x[GATEWAY_REGISTED_URL_ERR-2019]
+	_ = x[GATEWAY_PROXY_ERR-2020]
+	_ = x[GATEWAY_RPC_USERCENTER_ERR-2021]
+	_ = x[GATEWAY_RPC_RESOURCECENTER_ERR-2022]
+	_ = x[SERVER_ERR_NIL-3023]
+	_ = x[SERVER_DETAIL_TIMEOUT-3024]
+	_ = x[SERVER_DEDUCT_PARAM_LACK-3025]
+	_ = x[OTHER_ERR_NIL-4026]
+	_ = x[OTHER_ERR_UNDEFINED-4027]
 }
 
 const (
-	_ErrCode_name_0 = "global OK无用户身份无选择企业没有权限正在开发中企业账户已被冻结企业未认证企业认证待审核企业认证未通过企业认证已过期权限校验失败权益已过期权益余额不足没有权限"
+	_ErrCode_name_0 = "global OK无用户身份无选择企业没有权限正在开发中企业账户已被冻结企业未认证企业认证待审核企业认证未通过企业认证已过期无效资源code权限校验失败权益已过期权益余额不足没有权限"
 	_ErrCode_name_1 = "gateway ok无可用服务未注册路由地址接口维护中服务地址异常服务异常用户中心服务异常资源中心服务异常"
 	_ErrCode_name_2 = "server ok接口超时扣减异常"
 	_ErrCode_name_3 = "server ok未知异常"
 )
 
 var (
-	_ErrCode_index_0 = [...]uint8{0, 9, 24, 39, 51, 66, 90, 105, 126, 147, 168, 186, 201, 219, 231}
+	_ErrCode_index_0 = [...]uint8{0, 9, 24, 39, 51, 66, 90, 105, 126, 147, 168, 184, 202, 217, 235, 247}
 	_ErrCode_index_1 = [...]uint8{0, 10, 25, 46, 61, 79, 91, 115, 139}
 	_ErrCode_index_2 = [...]uint8{0, 9, 21, 33}
 	_ErrCode_index_3 = [...]uint8{0, 9, 21}
@@ -53,17 +54,17 @@ var (
 
 func (i ErrCode) String() string {
 	switch {
-	case 1000 <= i && i <= 1013:
+	case 1000 <= i && i <= 1014:
 		i -= 1000
 		return _ErrCode_name_0[_ErrCode_index_0[i]:_ErrCode_index_0[i+1]]
-	case 2014 <= i && i <= 2021:
-		i -= 2014
+	case 2015 <= i && i <= 2022:
+		i -= 2015
 		return _ErrCode_name_1[_ErrCode_index_1[i]:_ErrCode_index_1[i+1]]
-	case 3022 <= i && i <= 3024:
-		i -= 3022
+	case 3023 <= i && i <= 3025:
+		i -= 3023
 		return _ErrCode_name_2[_ErrCode_index_2[i]:_ErrCode_index_2[i+1]]
-	case 4025 <= i && i <= 4026:
-		i -= 4025
+	case 4026 <= i && i <= 4027:
+		i -= 4026
 		return _ErrCode_name_3[_ErrCode_index_3[i]:_ErrCode_index_3[i+1]]
 	default:
 		return "ErrCode(" + strconv.FormatInt(int64(i), 10) + ")"

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

@@ -34,7 +34,7 @@ func filterBefore(r *ghttp.Request) error {
 
 		//	用户身份注入请求体中
 		if (uCheck || eCheck) && r.Request.Method == http.MethodPost {
-			infusionIdentity(r, ctx.Sess.EntId, ctx.Sess.UserId, ctx.Sess.Phone, rule.FuncCode, rule.AppId)
+			infusionIdentity(r, ctx.Sess.EntId, ctx.Sess.UserId, ctx.Sess.Phone, rule.AppId)
 		}
 	}
 
@@ -47,7 +47,7 @@ func filterBefore(r *ghttp.Request) error {
 
 	//校验权益
 	if rule.PowerCheck == 1 {
-		if err := rpc.CheekResourcePower(ctx.Sess.EntId, -1, rule.FuncCode, rule.AppId); err != nil {
+		if err := rpc.CheekResourcePower(ctx.Sess.EntId, -1, rule.FuncCode, r.Get("functionCode").String(), rule.AppId); err != nil {
 			return err
 		}
 	}
@@ -91,7 +91,7 @@ func getPowerCheck(c int) (userIdCheck bool, entIdCheck bool) {
 }
 
 // infusionIdentity 用户身份注入
-func infusionIdentity(r *ghttp.Request, entId int64, userId, phone, funcCode, appId string) {
+func infusionIdentity(r *ghttp.Request, entId int64, userId, phone, appId string) {
 	//body-json请求方式
 	newBody := map[string]interface{}{}
 	logs.GInfo.Debug(r.GetCtx(), "请求内容-req", string(r.GetBody()))
@@ -100,7 +100,6 @@ func infusionIdentity(r *ghttp.Request, entId int64, userId, phone, funcCode, ap
 	newBody["entId"] = entId
 	newBody["phone"] = phone
 	newBody["appId"] = appId
-	newBody["functionCode"] = funcCode
 	nb, _ := json.Marshal(newBody)
 	logs.GInfo.Debug(r.GetCtx(), "请求内容-changed", string(nb))
 	r.Request.Body = ioutil.NopCloser(bytes.NewBuffer(nb))

+ 20 - 1
core/proxy/rpc/resourceCenter.go

@@ -9,6 +9,7 @@ import (
 	"github.com/gogf/gf/v2/os/gctx"
 	"github.com/zeromicro/go-zero/core/discov"
 	"github.com/zeromicro/go-zero/zrpc"
+	"strings"
 )
 
 var ResourceCenterRpc resource.Resource
@@ -26,7 +27,25 @@ func initResourceCenterRpc() {
 // eid 企业id
 // uId 新用户id;非mongodb中user表id
 // funcCode 业务代码
-func CheekResourcePower(eId, uId int64, funcCode, Appid string) error {
+// reqFuncCode 	 通用结构&&需要校验权限则先判断此接口是否有此functionCode的方法
+// appid 平台标识
+func CheekResourcePower(eId, uId int64, funcCodeRule, reqFuncCode, Appid string) error {
+	funcCode := funcCodeRule
+	funcCodeArr := strings.Split(funcCodeRule, ",")
+	if reqFuncCode != "" || len(funcCodeArr) > 1 {
+		var checkPass bool
+		for _, tFunc := range funcCodeArr {
+			if reqFuncCode == tFunc {
+				checkPass = true
+				break
+			}
+		}
+		if !checkPass {
+			return NewErrorWithCode(GLOBAL_ERR_RESOURCE_POWERCODE_ERR, fmt.Sprintf("账户 eId:%d,uId:%d 无效funccode:%s req:%s权限", eId, uId, funcCodeRule, reqFuncCode))
+		}
+		funcCode = reqFuncCode
+	}
+
 	res, err := ResourceCenterRpc.CheckPower(gctx.New(), &pb.CheckPowerReq{
 		Appid:        Appid,
 		FunctionCode: funcCode,