123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package utils
- import (
- "errors"
- "reflect"
- "regexp"
- "strconv"
- "strings"
- )
- type Rules map[string][]string
- type RulesMap map[string]Rules
- var CustomizeMap = make(map[string]Rules)
- var (
- LANG_CN = "cn" //中文
- LANG_EN = "english" //英文
- )
- var (
- VERIFY_NOT_EMPTY = "notEmpty" //不为空,必填
- VERIFY_LENGTH_NOT_LEGA = "lengthNotLega" //长度不合规
- )
- // 注册自定义规则方案建议在路由初始化层即注册
- func RegisterRule(key string, rule Rules) (err error) {
- if CustomizeMap[key] != nil {
- return errors.New(key + "已注册,无法重复注册")
- } else {
- CustomizeMap[key] = rule
- return nil
- }
- }
- // 非空 不能为其对应类型的0值
- func NotEmpty() string {
- return "notEmpty"
- }
- // 小于入参(<) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
- func Lt(mark string) string {
- return "lt=" + mark
- }
- // 小于等于入参(<=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
- func Le(mark string) string {
- return "le=" + mark
- }
- // 等于入参(==) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
- func Eq(mark string) string {
- return "eq=" + mark
- }
- // 不等于入参(!=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
- func Ne(mark string) string {
- return "ne=" + mark
- }
- // 大于等于入参(>=) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
- func Ge(mark string) string {
- return "ge=" + mark
- }
- // 大于入参(>) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
- func Gt(mark string) string {
- return "gt=" + mark
- }
- // 校验方法 接收两个参数 入参实例,规则map
- func Verify(st interface{}, roleMap Rules,lang string) (err error) {
- compareMap := map[string]bool{
- "lt": true,
- "le": true,
- "eq": true,
- "ne": true,
- "ge": true,
- "gt": true,
- }
- typ := reflect.TypeOf(st)
- val := reflect.ValueOf(st) // 获取reflect.Type类型
- kd := val.Kind() // 获取到st对应的类别
- if kd != reflect.Struct {
- return errors.New("expect struct")
- }
- num := val.NumField()
- // 遍历结构体的所有字段
- for i := 0; i < num; i++ {
- tagVal := typ.Field(i)
- val := val.Field(i)
- //如果有填写form字段的话,那么返回提示使用该字段
- tagName := tagVal.Tag.Get("form")
- if tagName == ""{
- tagName = tagVal.Name
- }
- if len(roleMap[tagVal.Name]) > 0 {
- for _, v := range roleMap[tagVal.Name] {
- switch {
- case v == "notEmpty":
- if isBlank(val) {
- return errors.New(tagName + getVerifyErrInfo(VERIFY_NOT_EMPTY,lang))
- }
- case compareMap[strings.Split(v, "=")[0]]:
- if !compareVerify(val, v) {
- return errors.New(tagName + getVerifyErrInfo(VERIFY_LENGTH_NOT_LEGA,lang) + v)
- }
- }
- }
- }
- }
- return nil
- }
- // 长度和数字的校验方法 根据类型自动校验
- func compareVerify(value reflect.Value, VerifyStr string) bool {
- switch value.Kind() {
- case reflect.String, reflect.Slice, reflect.Array:
- return compare(value.Len(), VerifyStr)
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return compare(value.Uint(), VerifyStr)
- case reflect.Float32, reflect.Float64:
- return compare(value.Float(), VerifyStr)
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return compare(value.Int(), VerifyStr)
- default:
- return false
- }
- }
- // 非空校验
- func isBlank(value reflect.Value) bool {
- switch value.Kind() {
- case reflect.String:
- return value.Len() == 0
- case reflect.Bool:
- return !value.Bool()
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- return value.Int() == 0
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- return value.Uint() == 0
- case reflect.Float32, reflect.Float64:
- return value.Float() == 0
- case reflect.Interface, reflect.Ptr:
- return value.IsNil()
- }
- return reflect.DeepEqual(value.Interface(), reflect.Zero(value.Type()).Interface())
- }
- func compare(value interface{}, VerifyStr string) bool {
- VerifyStrArr := strings.Split(VerifyStr, "=")
- val := reflect.ValueOf(value)
- switch val.Kind() {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- VInt, VErr := strconv.ParseInt(VerifyStrArr[1], 10, 64)
- if VErr != nil {
- return false
- }
- switch {
- case VerifyStrArr[0] == "lt":
- return val.Int() < VInt
- case VerifyStrArr[0] == "le":
- return val.Int() <= VInt
- case VerifyStrArr[0] == "eq":
- return val.Int() == VInt
- case VerifyStrArr[0] == "ne":
- return val.Int() != VInt
- case VerifyStrArr[0] == "ge":
- return val.Int() >= VInt
- case VerifyStrArr[0] == "gt":
- return val.Int() > VInt
- default:
- return false
- }
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
- VInt, VErr := strconv.Atoi(VerifyStrArr[1])
- if VErr != nil {
- return false
- }
- switch {
- case VerifyStrArr[0] == "lt":
- return val.Uint() < uint64(VInt)
- case VerifyStrArr[0] == "le":
- return val.Uint() <= uint64(VInt)
- case VerifyStrArr[0] == "eq":
- return val.Uint() == uint64(VInt)
- case VerifyStrArr[0] == "ne":
- return val.Uint() != uint64(VInt)
- case VerifyStrArr[0] == "ge":
- return val.Uint() >= uint64(VInt)
- case VerifyStrArr[0] == "gt":
- return val.Uint() > uint64(VInt)
- default:
- return false
- }
- case reflect.Float32, reflect.Float64:
- VFloat, VErr := strconv.ParseFloat(VerifyStrArr[1], 64)
- if VErr != nil {
- return false
- }
- switch {
- case VerifyStrArr[0] == "lt":
- return val.Float() < VFloat
- case VerifyStrArr[0] == "le":
- return val.Float() <= VFloat
- case VerifyStrArr[0] == "eq":
- return val.Float() == VFloat
- case VerifyStrArr[0] == "ne":
- return val.Float() != VFloat
- case VerifyStrArr[0] == "ge":
- return val.Float() >= VFloat
- case VerifyStrArr[0] == "gt":
- return val.Float() > VFloat
- default:
- return false
- }
- default:
- return false
- }
- }
- //获取错误信息(中文文)
- func getVerifyErrInfo(errType string,lang string) (errInfo string) {
- if lang == LANG_EN{
- switch errType {
- case VERIFY_NOT_EMPTY:
- errInfo = " Cannot be empty"
- case VERIFY_LENGTH_NOT_LEGA:
- errInfo = " Length or value is not in legal range,"
- }
- }else{
- switch errType {
- case VERIFY_NOT_EMPTY:
- errInfo = " 值不能为空"
- case VERIFY_LENGTH_NOT_LEGA:
- errInfo = " 长度或值不在合法范围,"
- }
- }
- return
- }
- func ContainsChinese(str string) bool {
- //设定一个含有中文的字符串
- var reg = regexp.MustCompile("^[\u4e00-\u9fa5]$")
- //接受正则表达式的范围
- for _, v := range str {
- //golang中string的底层是byte类型,所以单纯的for输出中文会出现乱码,这里选择for-range来输出
- if reg.MatchString(string(v)) {
- //判断是否为中文,如果是返回一个true,不是返回false。这俩面MatchString的参数要求是string
- return true
- }
- }
- return false
- }
|