package ValidationUtil import ( "dsDataex/MyService/DataEX" "dsDataex/MyService/DataEX/DataexDAO" "dsDataex/MyService/MySwagger" "dsDataex/Utils/CommonUtil" "encoding/json" "fmt" "reflect" "regexp" "strings" "time" ) var dics []map[string]interface{} const TIME_LAYOUT = "2020-09-14 15:29:10" /** * @title ValidESDataContent * @Description 校验ESDataContent * @Author wangshuai * @Date 2020-09-16 * @Param datasourceCode string 数据源CODE * @Param datas []MySwagger.Data Data * @Return bool 校验是否完全通过 * @Return string 提示信息 * @Return []MySwagger.Data 校验通过数据集合 * @Return []string 校验失败ID集合 */ func ValidESDataContent(datasourceCode string, datas []MySwagger.Data) (bool, string, []MySwagger.Data, []map[string]string, []string) { var dataContent map[string]interface{} var failMessages string var failIDs []string var dataContentFailIDs []map[string]string var successDatas []MySwagger.Data var conditions string var esData DataEX.ESData var r bool var dicIds []string var dicIdsStr string fmt.Println("datas: ", datas) conditions = "datasource_id='" + datasourceCode + "'" // 获取该数据源对应的元数据集合 result, _, _, metadatas, _ := DataexDAO.GetMetadataResults(conditions) if result == true { for _, vv := range metadatas { // 从元数据集合获取该数据源对应的含有字典ID的字典ID集合 if vv["dic_id"].(string) != "" { dicIds = append(dicIds, "'" + vv["dic_id"].(string) + "'") } } // 字典ID集合字符串 dicIdsStr = strings.Join(dicIds, ", ") // 获取含有 _, _, _, dics, _ = DataexDAO.GetJyt2012ResultsByParentIds(dicIdsStr) for no := 0; no < len(datas) && no < 1000; no++ { r = true var jsonData map[string]interface{} json.Unmarshal([]byte(datas[no].Data), &jsonData) esData.DataContent = jsonData dataContent = esData.DataContent for _, v := range metadatas { // 判断集合中是否含有该项数据 if _, ok := dataContent[v["item_name"].(string)]; ok { // 校验数据 res, mes, _ := ValidESDataContentItem(v, v["item_name"].(string), dataContent[v["item_name"].(string)]) //fmt.Println(res, mes) if res == false { failMessages = v["item_name"].(string) + mes + " " r = false // 将校验失败的ID集合写入到failIDs //failIDs = append(failIDs, datas[no].DataID) // 将校验失败的ID和提示信息写入到fails集合 fails := make(map[string]string) fails["fail_id"] = datas[no].DataID fails["fail_msg"] = failMessages dataContentFailIDs = append(dataContentFailIDs,fails) failIDs = append(failIDs, datas[no].DataID) break } } } if r == true { // 将校验通过的数据集合写入到successDatas successDatas = append(successDatas, datas[no]) } } } // 首尾去空格 //fails = strings.TrimRight(fails, " ") return r, failMessages, successDatas, dataContentFailIDs, failIDs } /** * @title ValidESDataContentItem * @Description 校验ESDataContent每项元数据 * @Author wangshuai * @Date 2020-09-16 * @Param dataContentItem map[string]interface{} 数据源CODE * @Param itemName string 校验项名称 * @Param itemValue interface{} 校验项值 * @Return bool 校验是否通过 * @Return string 校验提示信息 * @Return error 错误信息 */ func ValidESDataContentItem(dataContentItem map[string]interface{}, itemName string, itemValue interface{}) (bool, string, error) { if dataContentItem != nil { dicId := dataContentItem["dic_id"].(string) itemLength := dataContentItem["item_length"].(int64) itemType := dataContentItem["item_type"].(string) itemPattern := dataContentItem["item_pattern"].(string) checkName := dataContentItem["check_name"].(int64) checkDic := dataContentItem["check_dic"].(int64) checkType := dataContentItem["check_type"].(int64) checkPattern := dataContentItem["check_pattern"].(int64) checkExist := dataContentItem["check_exist"].(int64) //fmt.Println("dataContentItem: ", dataContentItem) if itemLength > 0 { // 校验长度 if CommonUtil.ConvertIntToInt64(len(itemValue.(string))) > itemLength { fmt.Println(itemValue.(string) + "最大长度超出") return false, "最大长度超出", nil } } else if checkName > 0 { // 校验名称 } else if checkDic > 0 { // 校验字典 if CheckDic(itemValue, dicId, dics) == false { fmt.Println("字典校验不通过") return false, "字典校验不通过", nil } } else if checkType > 0 { // 校验类型 if CheckType(itemValue, itemType) == false { fmt.Println("类型校验不通过") return false, "类型校验不通过", nil } } else if checkPattern > 0 { // 校验规则 if CheckPattern(itemValue, itemPattern) == false { fmt.Println("数据项模式校验不通过") return false, "数据项模式校验不通过", nil } } else if checkExist > 0 { // 校验必填项 if itemValue == nil { fmt.Println("是必填项") return false, itemName + "是必填项", nil } } } else { fmt.Println("缺少dataContentItem") return false, "校验失败, 缺少dataContentItem", nil } return true, "ok", nil } /** * @title ValidGPSql * @Description 校验SQL注入 * @Author wangshuai * @Date 2021-02-26 * @Param params []string SQL参数 * @Return bool 校验是否通过 * @Return string 校验提示信息 * @Return error 错误信息 */ func ValidGPSql(params []string) (bool, string, error) { flag := true msg := "" var err error for _, v := range params { // 校验关键字 matchKeyword, _ := regexp.MatchString("SELECT|INSERT|DELETE|FROM|COUNT\\(|DROP TABLE|TRUNCATE|\\ASC|MID\\(|CHAR\\(|XP_CMDSHELL|EXEC MASTER|NETLOCALGROUP ADMINISTRATORS|\\:|NET USER|\"\"|OR|AND", strings.ToUpper(v)) // 校验关键字符 matchCharacter, _ := regexp.MatchString("[-|;|,|\\?|/|\\(|\\)|\\[|\\]|}|{|%|\\@|*|!|']", v) if matchKeyword == true || matchCharacter == true { flag = false msg = "SQL注入校验不通过" } } return flag, msg, err } /** * @title ValidHyperSql * @Description 校验SQL注入 * @Author wangshuai * @Date 2021-02-26 * @Param params []string SQL参数 * @Return bool 校验是否通过 * @Return string 校验提示信息 * @Return error 错误信息 */ func ValidHyperSql(params []string) (bool, string, error) { flag := true msg := "" var err error for _, v := range params { // 校验关键字 matchKeyword, _ := regexp.MatchString("SELECT|INSERT|DELETE|FROM|COUNT\\(|DROP TABLE|TRUNCATE|\\ASC|MID\\(|CHAR\\(|XP_CMDSHELL|EXEC MASTER|NETLOCALGROUP ADMINISTRATORS|\\:|NET USER|\"\"|OR|AND", strings.ToUpper(v)) // 校验关键字符 matchCharacter, _ := regexp.MatchString("[-|;|,|\\?|/|\\(|\\)|\\[|\\]|}|{|%|\\@|*]", v) if matchKeyword == true || matchCharacter == true { flag = false msg = "SQL注入校验不通过" } } return flag, msg, err } /** * @title CheckDic * @Description 字典校验 * @Author wangshuai * @Date 2020-09-16 * @Param itemValue interface{} 字典项值 * @Param dicId string 字典ID * @Param dics []map[string]interface{} 字典项值 * @Return bool 校验是否通过 */ func CheckDic(itemValue interface{}, dicId string, dics []map[string]interface{}) bool { flag := false for _, v := range dics { if v["dic_value"] == itemValue && v["parent_id"] == dicId { flag = true } } return flag } /** * @title CheckType * @Description 类型校验 * @Author wangshuai * @Date 2020-09-23 * @Param itemValue interface{} 项值 * @Param itemType string 类型 * @Return bool 校验是否通过 */ func CheckType(itemValue interface{}, itemType string) bool { flag := false fmt.Println("itemType: ", itemType) if itemType == "string" { // 校验是否是字符串 _, ok := itemValue.(string) if ok { flag = true } tp := reflect.TypeOf(itemValue) fmt.Println("tp: ", tp) } else if itemType == "float" { // 校验是否是浮点数 //tp := reflect.TypeOf(itemValue) //fmt.Println("tp: ", tp) _, ok := itemValue.(float32) if ok { flag = true } _, ok1 := itemValue.(float64) if ok1 { flag = true } //fmt.Println("flag: ", flag) } else if itemType == "datetime" { // 校验时间格式是否正确 _, e := time.Parse(TIME_LAYOUT, itemValue.(string)) if e == nil { flag = true } } else if itemType == "text" { // 校验是否是文本 _, ok := itemValue.(string) if ok { flag = true } } return flag } /** * @title CheckPattern * @Description 数据项模式校验 * @Author wangshuai * @Date 2020-09-23 * @Param itemValue interface{} 项值 * @Param itemPattern string 模式 * @Return bool 校验是否通过 */ func CheckPattern(itemValue interface{}, itemPattern string) bool { flag := false if itemPattern == "valid_email" { if ValidEmail(itemValue.(string)) { flag = true } } else if itemPattern == "valid_mobile" { if ValidMobile(itemValue.(string)) { flag = true } } return flag } /** * @title ValidTelephone * @Description 校验家用电话(不带前缀) 最高8位 * @Author wangshuai * @Date 2020-09-23 * @Param str ...string 项值 * @Return bool 校验是否通过 */ func ValidTelephone(str ...string) bool { var b bool for _, s := range str { b, _ = regexp.MatchString("^[0-9]{8}$", s) if false == b { return b } } return b } /** * @title ValidMobile * @Description 校验手提电话(不带前缀)最高11位 * @Author wangshuai * @Date 2020-09-23 * @Param str ...string 项值 * @Return bool 校验是否通过 */ func ValidMobile(str ...string) bool { var b bool for _, s := range str { b, _ = regexp.MatchString("^1[0-9]{10}$", s) if false == b { return b } } return b } /** * @title ValidEmail * @Description 校验邮箱 最高30位 * @Author wangshuai * @Date 2020-09-23 * @Param str ...string 项值 * @Return bool 校验是否通过 */ func ValidEmail(str ...string) bool { var b bool for _, s := range str { b, _ = regexp.MatchString("^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$", s) if false == b { return b } } return b } // 暂时作废 2020-09-16 func ValidESDataContentBak(datasourceCode string, dataContent map[string]interface{}) (bool, string) { var fails string var conditions string r := true conditions = " AND datasource_id=" + datasourceCode result, _, _, metadatas, _ := DataexDAO.GetMetadataResults(conditions) if result == true { for _, v := range metadatas { for _, vv := range v { if _, ok := dataContent[vv.(string)]; ok { res, mes, _ := ValidESDataContentItem(v, vv.(string), dataContent[vv.(string)]) if res == false { fails += vv.(string) + mes + " " r = false } } } } } fails = strings.TrimRight(fails, " ") return r, fails } // 暂时作废 2020-09-18 func CheckDicBak(dicId string, itemValue interface{}) bool { total := DataexDAO.GetJyt2012CountByParentIdAndDicValue(dicId, itemValue.(string)) if total > 0 { return true } else { return false } }