You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package CheckHandler
import (
"github.com/gin-gonic/gin"
"regexp"
"strings"
)
//在注释标签中声明的标识字符串X-TelLimit
var telLimit = "x-tellimit"
//形成一个map,用于O(1)速度获取到一个接口是不是需要进行判断拦截注意一个接口可以存在post+get两种形式所以map里面是一个数组需要遍历
type telLimitStruct struct {
parameterName string
httpType string
must bool
}
//长度限制的map,以string为key(key的命名是接口名+"_get" 或者 接口名+"_post"),里面是telLimitStruct形式的数组
var telLimitMap = make(map[string][]telLimitStruct)
/**
功能:功能函数,添当前的检查字典里
*/
func telAddToMap(d map[string]interface{}, t map[string]interface{}, k string, httpType string) {
var telLimitArray []telLimitStruct
d = t[httpType].(map[string]interface{})
if d != nil && d[telLimit] != nil {
for _, value := range d[telLimit].([]interface{}) {
var t telLimitStruct
t.parameterName = value.(string)
t.httpType = httpType
//此参数是否为必填写项
for _, v2 := range d["parameters"].([]interface{}) {
c2 := v2.(map[string]interface{})
for k2, v2 := range c2 {
if k2 == "required" {
t.must = v2.(bool) //标识是否是必须项
}
}
}
telLimitArray = append(telLimitArray, t)
}
telLimitMap[k+"_"+httpType] = telLimitArray
}
}
/**
功能:初始化手机号限制字典
*/
func telLimitInit(inter interface{}) map[string][]telLimitStruct {
//参数检查器
m := inter.(map[string]interface{})
n := m["paths"].(map[string]interface{})
for k, v := range n {
t := v.(map[string]interface{})
//参数长度检查器
var d map[string]interface{}
if t["get"] != nil {
telAddToMap(d, t, k, "get")
}
if t["post"] != nil {
telAddToMap(d, t, k, "post")
}
}
return telLimitMap
}
/**
功能:检查字符串是否是手机号
作者:黄海
时间2020-03-17
*/
func telLimitIsLegal(c *gin.Context, interName string) (bool, ResultStruct) {
var resultStruct ResultStruct
//接口传入的请求方式:get?post?
codeHttpType := strings.ToLower(c.Request.Method)
//1、如果此接口地址需要进行参数长度检查
if telLimitMap[interName+"_"+codeHttpType] != nil {
arr := telLimitMap[interName+"_"+codeHttpType]
resultStruct.InterfaceName = interName
//遍历arr
for i := 0; i < len(arr); i++ {
parameterName := arr[i].parameterName
httpType := arr[i].httpType
var p string
if httpType == "get" {
p = c.Query(parameterName)
} else {
p = c.PostForm(parameterName)
}
resultStruct.Parameter = parameterName
//为空,不需要检查,通过
if p == "" && !arr[i].must {
return true, resultStruct
}
//为空,需要检查,禁止
if p == "" && arr[i].must{
resultStruct.Message = "参数没有找到!"
return false, resultStruct
}
if !verifyMobileFormat(p) {
resultStruct.HttpType = httpType
resultStruct.Message = "不是合法手机号!"
return false, resultStruct
}
}
}
return true, resultStruct
}
func verifyMobileFormat(mobileNum string) bool {
regular := "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$"
reg := regexp.MustCompile(regular)
return reg.MatchString(mobileNum)
}