request_param.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2019 utimes.cc. All rights reserved.
  2. // Use of this source code is governed by utimes.cc.
  3. package utils
  4. import (
  5. "gd_auth_check/apis"
  6. "gd_auth_check/errors"
  7. "encoding/json"
  8. "fmt"
  9. "reflect"
  10. "strings"
  11. "gd_auth_check/common.in/jsonrpc2"
  12. )
  13. // checkRequiredParam 检查必选参数name的值是否为空,m为所有参数map,merchantUsedName为商户配置的别名
  14. func checkRequiredParam(m map[string]interface{}, name string, merchantUsedName string) error {
  15. if value, ok := m[name]; ok == false || value == nil {
  16. return jsonrpc2.NewJsonError(1103, fmt.Sprintf("参数错误 %s为空", merchantUsedName))
  17. }
  18. val := reflect.ValueOf(m[name])
  19. if val.Kind() != reflect.Bool && reflect.DeepEqual(m[name], reflect.Zero(val.Type()).Interface()) {
  20. return jsonrpc2.NewJsonError(1103, fmt.Sprintf("参数错误 %s为空", merchantUsedName))
  21. }
  22. return nil
  23. }
  24. // filterRuleFuncMap 参数过滤规则检查函数map
  25. var filterRuleFuncMap = map[string]func(string, []string) error{
  26. "vin_norecord": checkRuleVinNorecord,
  27. "contain": checkRuleContain,
  28. "equal": checkRuleEqual,
  29. "prefix": checkRulePrefix,
  30. }
  31. // checkRuleVinNorecord 当value值为targets中的一个时返回查无
  32. func checkRuleVinNorecord(value string, targets []string) error {
  33. for _, v := range targets {
  34. if v == value {
  35. return errors.NoRecord
  36. }
  37. }
  38. return nil
  39. }
  40. // checkRuleContain 当value不包含targets中的任何一个时返回参数错误
  41. func checkRuleContain(value string, targets []string) error {
  42. for _, v := range targets {
  43. if strings.Contains(value, v) {
  44. return nil
  45. }
  46. }
  47. return jsonrpc2.NewJsonError(10021, fmt.Sprintf("不支持的查询参数:%s", value))
  48. }
  49. // checkRuleContain 当value不等于targets中的任何一个时返回参数错误
  50. func checkRuleEqual(value string, targets []string) error {
  51. for _, v := range targets {
  52. if value == v {
  53. return nil
  54. }
  55. }
  56. return jsonrpc2.NewJsonError(10021, fmt.Sprintf("不支持的查询参数:%s", value))
  57. }
  58. // checkRuleContain 当value不以targets中的任何一个为前缀时返回参数错误
  59. func checkRulePrefix(value string, targets []string) error {
  60. for _, v := range targets {
  61. if strings.HasPrefix(value, v) {
  62. return nil
  63. }
  64. }
  65. return jsonrpc2.NewJsonError(10021, fmt.Sprintf("不支持的查询参数:%s", value))
  66. }
  67. // checkSpecialParam 特殊参数检查及过滤规则检查,m为所有参数map,name 为参数名字,filterMap为配置的过滤条件, ruleMap为过滤规则
  68. func checkSpecialParam(m map[string]interface{}, name string, filterMap map[string][]apis.BaseApiParamFilter, ruleMap map[int64]apis.FilterRule) (string, string, error) {
  69. plateNo := ""
  70. plateType := ""
  71. val := reflect.ValueOf(m[name])
  72. if val.Kind() != reflect.String {
  73. return "", "", nil
  74. }
  75. value, _ := m[name].(string)
  76. value = strings.TrimSpace(value)
  77. m[name] = value
  78. switch name {
  79. case "plate_no":
  80. plateNo = value
  81. case "plate_type":
  82. plateType = value
  83. case "vin":
  84. if value != "" {
  85. if realVin, err := CheckVinFormat(value); err != nil {
  86. return "", "", err
  87. } else {
  88. m["vin"] = realVin
  89. }
  90. }
  91. case "id_card":
  92. if value != "" {
  93. if realIdCert, success := CheckIDCert(value); success == false {
  94. return "", "", jsonrpc2.NewJsonError(1102, "身份证格式错误")
  95. } else {
  96. m["id_card"] = realIdCert
  97. }
  98. }
  99. }
  100. // 过滤字段检查
  101. if len(filterMap) != 0 && value != "" {
  102. finfos, ok := filterMap[name]
  103. if ok == false {
  104. return plateNo, plateType, nil
  105. }
  106. var ferr error
  107. for _, finfo := range finfos {
  108. rule, ok := ruleMap[finfo.RuleId]
  109. if ok == false {
  110. if finfo.RuleId != 0 {
  111. continue
  112. }
  113. rule.RuleName = "contain"
  114. if name == "vin" {
  115. rule.RuleName = "vin_norecord"
  116. }
  117. }
  118. if rule.EffectiveParam != "" && rule.EffectiveParam != name {
  119. continue
  120. }
  121. function, ok := filterRuleFuncMap[rule.RuleName]
  122. if ok == false {
  123. continue
  124. }
  125. array := strings.Split(finfo.Value, ",")
  126. ferr = function(value, array)
  127. if ferr != nil {
  128. break
  129. }
  130. }
  131. return plateNo, plateType, ferr
  132. }
  133. return plateNo, plateType, nil
  134. }
  135. // GetRequestParamNew 获取解析后的参数,其中包含参数检查。checkApiResult为auth_check模块的结果,filters为商户配置的参数过滤条件,ruleMap为参数过滤规则
  136. func GetRequestParamNew(checkApiResult apis.CheckMerchantApiResult, filters []apis.BaseApiParamFilter, param []byte, ruleMap map[int64]apis.FilterRule) ([]byte, error) {
  137. plateNo := ""
  138. plateType := ""
  139. var err error
  140. // origin 为商户输入的原始参数map
  141. var origin = map[string]interface{}{}
  142. // m 为参数名转化后的map
  143. var m = map[string]interface{}{}
  144. // retMap为最终解析后的参数map
  145. var retMap = map[string]interface{}{}
  146. // 原始参数解析
  147. json.Unmarshal(param, &origin)
  148. // 参数名称大小写转换
  149. for k, v := range origin {
  150. name := strings.TrimSpace(k)
  151. m[name] = v
  152. }
  153. // 构建参数过滤条件map
  154. filterMap := map[string][]apis.BaseApiParamFilter{}
  155. for _, f := range filters {
  156. filterMap[f.Name] = append(filterMap[f.Name], f)
  157. }
  158. // 参数解析及检查
  159. for _, v := range checkApiResult.RequstParamConf {
  160. // 删除未勾选字段
  161. if v.Selected == false {
  162. continue
  163. }
  164. // 处理参数名称
  165. v.Name = strings.TrimSpace(v.Name)
  166. v.In = strings.TrimSpace(v.In)
  167. merchantUsedName := v.Name
  168. // 字段名替换
  169. if v.In != "" {
  170. merchantUsedName = v.In
  171. if _, ok := m[v.In]; ok {
  172. m[v.Name] = m[v.In]
  173. }
  174. }
  175. // 必选参数检查
  176. if v.Required == true {
  177. if err = checkRequiredParam(m, v.Name, merchantUsedName); err != nil {
  178. return nil, err
  179. }
  180. }
  181. if _, ok := m[v.Name]; ok == false {
  182. continue
  183. }
  184. // 结果赋值
  185. retMap[v.Name] = m[v.Name]
  186. // 特殊参数格式检查及规则过滤检查
  187. plateNoValue, plateTypeValue, err := checkSpecialParam(retMap, v.Name, filterMap, ruleMap)
  188. if plateNoValue != "" {
  189. plateNo = plateNoValue
  190. }
  191. if plateTypeValue != "" {
  192. plateType = plateTypeValue
  193. }
  194. if err != nil {
  195. return nil, err
  196. }
  197. }
  198. // 车牌号检查
  199. if plateNo != "" {
  200. retMap["origin_plate_type"] = plateType
  201. plateType, err := CheckPlateNoFormat(&plateNo, plateType)
  202. if err != nil {
  203. return nil, err
  204. }
  205. retMap["plate_no"] = plateNo
  206. if plateType != "" {
  207. retMap["plate_type"] = plateType
  208. }
  209. }
  210. bytes, err := json.Marshal(retMap)
  211. return bytes, err
  212. }