|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- package zhios_order_relate_utils
-
- import (
- "fmt"
- "github.com/syyongx/php2go"
- "math/rand"
- "reflect"
- "sort"
- "strings"
- "time"
- )
-
- func Implode(glue string, args ...interface{}) string {
- data := make([]string, len(args))
- for i, s := range args {
- data[i] = fmt.Sprint(s)
- }
- return strings.Join(data, glue)
- }
-
- //字符串是否在数组里
- func InArr(target string, str_array []string) bool {
- for _, element := range str_array {
- if target == element {
- return true
- }
- }
- return false
- }
- func InArrToInt(target int, str_array []int) bool {
- for _, element := range str_array {
- if target == element {
- return true
- }
- }
- return false
- }
-
- //把数组的值放到key里
- func ArrayColumn(array interface{}, key string) (result map[string]interface{}, err error) {
- result = make(map[string]interface{})
- t := reflect.TypeOf(array)
- v := reflect.ValueOf(array)
- if t.Kind() != reflect.Slice {
- return nil, nil
- }
- if v.Len() == 0 {
- return nil, nil
- }
- for i := 0; i < v.Len(); i++ {
- indexv := v.Index(i)
- if indexv.Type().Kind() != reflect.Struct {
- return nil, nil
- }
- mapKeyInterface := indexv.FieldByName(key)
- if mapKeyInterface.Kind() == reflect.Invalid {
- return nil, nil
- }
- mapKeyString, err := InterfaceToString(mapKeyInterface.Interface())
- if err != nil {
- return nil, err
- }
- result[mapKeyString] = indexv.Interface()
- }
- return result, err
- }
-
- //转string
- func InterfaceToString(v interface{}) (result string, err error) {
- switch reflect.TypeOf(v).Kind() {
- case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
- result = fmt.Sprintf("%v", v)
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- result = fmt.Sprintf("%v", v)
- case reflect.String:
- result = v.(string)
- default:
- err = nil
- }
- return result, err
- }
-
- func HideTrueName(name string) string {
- res := "**"
- if name != "" {
- runs := []rune(name)
- leng := len(runs)
- if leng <= 3 {
- res = string(runs[0:1]) + res
- } else if leng < 5 {
- res = string(runs[0:2]) + res
- } else if leng < 10 {
- res = string(runs[0:2]) + "***" + string(runs[leng-2:leng])
- } else if leng < 16 {
- res = string(runs[0:3]) + "****" + string(runs[leng-3:leng])
- } else {
- res = string(runs[0:4]) + "*****" + string(runs[leng-4:leng])
- }
- }
- return res
- }
-
- func GetQueryParam(uri string) map[string]string {
- //根据问号分割路由还是query参数
- uriList := strings.Split(uri, "?")
- var query = make(map[string]string, 0)
- //有参数才处理
- if len(uriList) == 2 {
- //分割query参数
- var queryList = strings.Split(uriList[1], "&")
- if len(queryList) > 0 {
- //key value 分别赋值
- for _, v := range queryList {
- var valueList = strings.Split(v, "=")
- if len(valueList) == 2 {
- value, _ := php2go.URLDecode(valueList[1])
- if value == "" {
- value = valueList[1]
- }
- query[valueList[0]] = value
- }
- }
- }
- }
- return query
- }
-
- //JoinStringsInASCII 按照规则,参数名ASCII码从小到大排序后拼接
- //data 待拼接的数据
- //sep 连接符
- //onlyValues 是否只包含参数值,true则不包含参数名,否则参数名和参数值均有
- //includeEmpty 是否包含空值,true则包含空值,否则不包含,注意此参数不影响参数名的存在
- //exceptKeys 被排除的参数名,不参与排序及拼接
- func JoinStringsInASCII(data map[string]string, sep string, onlyValues, includeEmpty bool, exceptKeys ...string) string {
- var list []string
- var keyList []string
- m := make(map[string]int)
- if len(exceptKeys) > 0 {
- for _, except := range exceptKeys {
- m[except] = 1
- }
- }
- for k := range data {
- if _, ok := m[k]; ok {
- continue
- }
- value := data[k]
- if !includeEmpty && value == "" {
- continue
- }
- if onlyValues {
- keyList = append(keyList, k)
- } else {
- list = append(list, fmt.Sprintf("%s=%s", k, value))
- }
- }
- if onlyValues {
- sort.Strings(keyList)
- for _, v := range keyList {
- list = append(list, AnyToString(data[v]))
- }
- } else {
- sort.Strings(list)
- }
- return strings.Join(list, sep)
- }
-
- //x的y次方
- func RandPow(l int) string {
- var i = "1"
- for j := 0; j < l; j++ {
- i += "0"
- }
- k := StrToInt64(i)
- n := rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(k)
- ls := "%0" + IntToStr(l) + "v"
- str := fmt.Sprintf(ls, n)
- //min := int(math.Pow10(l - 1))
- //max := int(math.Pow10(l) - 1)
- return str
- }
-
- //根据显示长度截取字符串
- func ShowSubstr(s string, l int) string {
- if len(s) <= l {
- return s
- }
- ss, sl, rl, rs := "", 0, 0, []rune(s)
- for _, r := range rs {
- rint := int(r)
- if rint < 128 {
- rl = 1
- } else {
- rl = 2
- }
- if sl+rl > l {
- break
- }
- sl += rl
- ss += string(r)
- }
- return ss
- }
|