// Copyright 2019 utimes.cc. All rights reserved. // Use of this source code is governed by utimes.cc. package utils import ( "gd_auth_check/apis" "gd_auth_check/errors" "encoding/json" "fmt" "reflect" "strings" "gd_auth_check/common.in/jsonrpc2" ) // checkRequiredParam 检查必选参数name的值是否为空,m为所有参数map,merchantUsedName为商户配置的别名 func checkRequiredParam(m map[string]interface{}, name string, merchantUsedName string) error { if value, ok := m[name]; ok == false || value == nil { return jsonrpc2.NewJsonError(1103, fmt.Sprintf("参数错误 %s为空", merchantUsedName)) } val := reflect.ValueOf(m[name]) if val.Kind() != reflect.Bool && reflect.DeepEqual(m[name], reflect.Zero(val.Type()).Interface()) { return jsonrpc2.NewJsonError(1103, fmt.Sprintf("参数错误 %s为空", merchantUsedName)) } return nil } // filterRuleFuncMap 参数过滤规则检查函数map var filterRuleFuncMap = map[string]func(string, []string) error{ "vin_norecord": checkRuleVinNorecord, "contain": checkRuleContain, "equal": checkRuleEqual, "prefix": checkRulePrefix, } // checkRuleVinNorecord 当value值为targets中的一个时返回查无 func checkRuleVinNorecord(value string, targets []string) error { for _, v := range targets { if v == value { return errors.NoRecord } } return nil } // checkRuleContain 当value不包含targets中的任何一个时返回参数错误 func checkRuleContain(value string, targets []string) error { for _, v := range targets { if strings.Contains(value, v) { return nil } } return jsonrpc2.NewJsonError(10021, fmt.Sprintf("不支持的查询参数:%s", value)) } // checkRuleContain 当value不等于targets中的任何一个时返回参数错误 func checkRuleEqual(value string, targets []string) error { for _, v := range targets { if value == v { return nil } } return jsonrpc2.NewJsonError(10021, fmt.Sprintf("不支持的查询参数:%s", value)) } // checkRuleContain 当value不以targets中的任何一个为前缀时返回参数错误 func checkRulePrefix(value string, targets []string) error { for _, v := range targets { if strings.HasPrefix(value, v) { return nil } } return jsonrpc2.NewJsonError(10021, fmt.Sprintf("不支持的查询参数:%s", value)) } // checkSpecialParam 特殊参数检查及过滤规则检查,m为所有参数map,name 为参数名字,filterMap为配置的过滤条件, ruleMap为过滤规则 func checkSpecialParam(m map[string]interface{}, name string, filterMap map[string][]apis.BaseApiParamFilter, ruleMap map[int64]apis.FilterRule) (string, string, error) { plateNo := "" plateType := "" val := reflect.ValueOf(m[name]) if val.Kind() != reflect.String { return "", "", nil } value, _ := m[name].(string) value = strings.TrimSpace(value) m[name] = value switch name { case "plate_no": plateNo = value case "plate_type": plateType = value case "vin": if value != "" { if realVin, err := CheckVinFormat(value); err != nil { return "", "", err } else { m["vin"] = realVin } } case "id_card": if value != "" { if realIdCert, success := CheckIDCert(value); success == false { return "", "", jsonrpc2.NewJsonError(1102, "身份证格式错误") } else { m["id_card"] = realIdCert } } } // 过滤字段检查 if len(filterMap) != 0 && value != "" { finfos, ok := filterMap[name] if ok == false { return plateNo, plateType, nil } var ferr error for _, finfo := range finfos { rule, ok := ruleMap[finfo.RuleId] if ok == false { if finfo.RuleId != 0 { continue } rule.RuleName = "contain" if name == "vin" { rule.RuleName = "vin_norecord" } } if rule.EffectiveParam != "" && rule.EffectiveParam != name { continue } function, ok := filterRuleFuncMap[rule.RuleName] if ok == false { continue } array := strings.Split(finfo.Value, ",") ferr = function(value, array) if ferr != nil { break } } return plateNo, plateType, ferr } return plateNo, plateType, nil } // GetRequestParamNew 获取解析后的参数,其中包含参数检查。checkApiResult为auth_check模块的结果,filters为商户配置的参数过滤条件,ruleMap为参数过滤规则 func GetRequestParamNew(checkApiResult apis.CheckMerchantApiResult, filters []apis.BaseApiParamFilter, param []byte, ruleMap map[int64]apis.FilterRule) ([]byte, error) { plateNo := "" plateType := "" var err error // origin 为商户输入的原始参数map var origin = map[string]interface{}{} // m 为参数名转化后的map var m = map[string]interface{}{} // retMap为最终解析后的参数map var retMap = map[string]interface{}{} // 原始参数解析 json.Unmarshal(param, &origin) // 参数名称大小写转换 for k, v := range origin { name := strings.TrimSpace(k) m[name] = v } // 构建参数过滤条件map filterMap := map[string][]apis.BaseApiParamFilter{} for _, f := range filters { filterMap[f.Name] = append(filterMap[f.Name], f) } // 参数解析及检查 for _, v := range checkApiResult.RequstParamConf { // 删除未勾选字段 if v.Selected == false { continue } // 处理参数名称 v.Name = strings.TrimSpace(v.Name) v.In = strings.TrimSpace(v.In) merchantUsedName := v.Name // 字段名替换 if v.In != "" { merchantUsedName = v.In if _, ok := m[v.In]; ok { m[v.Name] = m[v.In] } } // 必选参数检查 if v.Required == true { if err = checkRequiredParam(m, v.Name, merchantUsedName); err != nil { return nil, err } } if _, ok := m[v.Name]; ok == false { continue } // 结果赋值 retMap[v.Name] = m[v.Name] // 特殊参数格式检查及规则过滤检查 plateNoValue, plateTypeValue, err := checkSpecialParam(retMap, v.Name, filterMap, ruleMap) if plateNoValue != "" { plateNo = plateNoValue } if plateTypeValue != "" { plateType = plateTypeValue } if err != nil { return nil, err } } // 车牌号检查 if plateNo != "" { retMap["origin_plate_type"] = plateType plateType, err := CheckPlateNoFormat(&plateNo, plateType) if err != nil { return nil, err } retMap["plate_no"] = plateNo if plateType != "" { retMap["plate_type"] = plateType } } bytes, err := json.Marshal(retMap) return bytes, err }