github.com/jtzjtz/kit@v1.0.2/net/url.go (about) 1 package net 2 3 import ( 4 "github.com/jtzjtz/kit/array" 5 "net/url" 6 "reflect" 7 "strings" 8 ) 9 10 //map转url 11 12 func MapToUrl(m map[string]string) string { 13 vs := url.Values{} 14 for k, v := range m { 15 vs.Add(k, v) 16 } 17 return vs.Encode() 18 } 19 20 //url转map 21 22 func UrlToMap(u string) map[string][]string { 23 m, _ := url.ParseQuery(u) 24 return m 25 } 26 27 //从post数据中取要更新为空的字段 返回值为 更新为空的entity字段名字 28 29 func GetEmptyEntityFieldFromPost(postData url.Values, entity interface{}) []string { 30 var emptyFields, result []string 31 for k, v := range postData { 32 vStr := strings.Join(v, "") 33 if vStr == "" { 34 emptyFields = append(emptyFields, k) 35 } else if vStr == "0" { 36 emptyFields = append(emptyFields, k) 37 } 38 } 39 getType := reflect.TypeOf(entity) 40 41 // 获取方法字段 42 // 1. 先获取interface的reflect.Type,然后通过NumField进行遍历 43 // 2. 再通过reflect.Type的Field获取其Field 44 // 3. 最后通过Field的Interface()得到对应的value 45 for i := 0; i < getType.NumField(); i++ { 46 field := getType.Field(i) 47 // value := getValue.Field(i).Interface() 48 formTag := field.Tag.Get("json") 49 tags := strings.Split(formTag, ",") 50 if len(tags) > 0 { 51 if isHave, err := array.Contains(tags[0], emptyFields); err == nil && isHave == true { 52 result = append(result, field.Name) 53 } 54 } 55 56 } 57 58 return result 59 } 60 61 // 从url数据中获取 SQL 的 Sort 和 Where,返回值为 Sort,Where 62 63 func GetSortAndWhereFormUrl(entity interface{}, values url.Values) (map[string]string, string) { 64 var sort = map[string]string{} 65 var where = "" 66 var fieldWhereKey []string 67 var fieldSortKey []string 68 var filedSortMap = map[string]string{} 69 for k, v := range values { 70 vStr := strings.Join(v, "") 71 if k == "sort" { // sort 72 if strings.Index(vStr, "|") != -1 { 73 for _, sv := range strings.Split(vStr, "|") { 74 if strings.Index(sv, ",") != -1 { 75 sortOneArr := strings.Split(sv, ",") 76 if len(sortOneArr) == 2 && (sortOneArr[1] == "asc" || sortOneArr[1] == "desc") { 77 fieldSortKey = append(fieldSortKey, sortOneArr[0]) 78 filedSortMap[sortOneArr[0]] = sortOneArr[1] 79 } 80 } 81 } 82 } else { 83 if strings.Index(vStr, ",") != -1 { 84 sortOneArr := strings.Split(vStr, ",") 85 if len(sortOneArr) == 2 && (sortOneArr[1] == "asc" || sortOneArr[1] == "desc") { 86 fieldSortKey = append(fieldSortKey, sortOneArr[0]) 87 filedSortMap[sortOneArr[0]] = sortOneArr[1] 88 } 89 } 90 } 91 } 92 93 if k != "sort" && k != "page" && k != "page_num" { // where 94 fieldWhereKey = append(fieldWhereKey, k) 95 } 96 } 97 98 v := reflect.TypeOf(entity) 99 if v.NumField() > 0 { 100 var isHave = true 101 var err error 102 for i := 0; i < v.NumField(); i++ { 103 field := v.Field(i) 104 formTag := "" 105 formTagStr := field.Tag.Get("form") 106 formTagArr := strings.Split(formTagStr, ",") 107 if len(formTagArr) > 0 { 108 formTag = formTagArr[0] 109 } 110 // 拼接 OrderBy 111 isHave, err = array.Contains(formTag, fieldSortKey) 112 if isHave && err == nil { 113 sort[formTag] = filedSortMap[formTag] 114 } 115 // 拼接 Where 116 isHave, err = array.Contains(formTag, fieldWhereKey) 117 if isHave && err == nil { 118 kind := field.Type.Kind() 119 formTagValueStr := values.Get(formTag) 120 var formTagValueArr []string 121 if strings.Index(formTagValueStr, "|") != -1 { 122 // in 123 formTagValueArr = strings.Split(formTagValueStr, "|in") 124 if len(formTagValueArr) == 2 && formTagValueArr[0] != "" { 125 var inArr []string 126 for _, v := range strings.Split(formTagValueArr[0], ",") { 127 inArr = append(inArr, v) 128 } 129 where += formTag + " IN ('" + strings.Join(inArr, "','") + "') AND " 130 } 131 // like 132 formTagValueArr = strings.Split(formTagValueStr, "|like") 133 if len(formTagValueArr) == 2 && formTagValueArr[0] != "" { 134 where += formTag + " LIKE '%" + formTagValueArr[0] + "%' AND " 135 } 136 // llike 137 formTagValueArr = strings.Split(formTagValueStr, "|llike") 138 if len(formTagValueArr) == 2 && formTagValueArr[0] != "" { 139 where += formTag + " LIKE '%" + formTagValueArr[0] + "' AND " 140 } 141 // rlike 142 formTagValueArr = strings.Split(formTagValueStr, "|rlike") 143 if len(formTagValueArr) == 2 && formTagValueArr[0] != "" { 144 where += formTag + " LIKE '" + formTagValueArr[0] + "%' AND " 145 } 146 // gt 147 formTagValueArr = strings.Split(formTagValueStr, "|gt") 148 if len(formTagValueArr) == 2 && formTagValueArr[0] != "" { 149 where += formTag + " > '" + formTagValueArr[0] + "' AND " 150 } 151 // gte 152 formTagValueArr = strings.Split(formTagValueStr, "|gte") 153 if len(formTagValueArr) == 2 && formTagValueArr[0] != "" { 154 where += formTag + " >= '" + formTagValueArr[0] + "' AND " 155 } 156 // lt 157 formTagValueArr = strings.Split(formTagValueStr, "|lt") 158 if len(formTagValueArr) == 2 && formTagValueArr[0] != "" { 159 where += formTag + " < '" + formTagValueArr[0] + "' AND " 160 } 161 // lte 162 formTagValueArr = strings.Split(formTagValueStr, "|lte") 163 if len(formTagValueArr) == 2 && formTagValueArr[0] != "" { 164 where += formTag + " <= '" + formTagValueArr[0] + "' AND " 165 } 166 // neq 167 formTagValueArr = strings.Split(formTagValueStr, "|neq") 168 if len(formTagValueArr) == 2 && formTagValueArr[0] != "" { 169 where += formTag + " != '" + formTagValueArr[0] + "' AND " 170 } 171 } else { 172 if kind == reflect.Int || kind == reflect.Float32 || kind == reflect.Float64 { 173 where += formTag + " = " + formTagValueStr + " AND " 174 } 175 if kind == reflect.String { 176 where += formTag + " = '" + formTagValueStr + "' AND " 177 } 178 } 179 } 180 } 181 if where != "" { 182 where = where[0 : len(where)-4] 183 } 184 } 185 return sort, where 186 }