智莺生活mysql模型库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

204 lines
4.6 KiB

  1. package zhios_order_relate_utils
  2. import (
  3. "fmt"
  4. "github.com/syyongx/php2go"
  5. "math/rand"
  6. "reflect"
  7. "sort"
  8. "strings"
  9. "time"
  10. )
  11. func Implode(glue string, args ...interface{}) string {
  12. data := make([]string, len(args))
  13. for i, s := range args {
  14. data[i] = fmt.Sprint(s)
  15. }
  16. return strings.Join(data, glue)
  17. }
  18. //字符串是否在数组里
  19. func InArr(target string, str_array []string) bool {
  20. for _, element := range str_array {
  21. if target == element {
  22. return true
  23. }
  24. }
  25. return false
  26. }
  27. func InArrToInt(target int, str_array []int) bool {
  28. for _, element := range str_array {
  29. if target == element {
  30. return true
  31. }
  32. }
  33. return false
  34. }
  35. //把数组的值放到key里
  36. func ArrayColumn(array interface{}, key string) (result map[string]interface{}, err error) {
  37. result = make(map[string]interface{})
  38. t := reflect.TypeOf(array)
  39. v := reflect.ValueOf(array)
  40. if t.Kind() != reflect.Slice {
  41. return nil, nil
  42. }
  43. if v.Len() == 0 {
  44. return nil, nil
  45. }
  46. for i := 0; i < v.Len(); i++ {
  47. indexv := v.Index(i)
  48. if indexv.Type().Kind() != reflect.Struct {
  49. return nil, nil
  50. }
  51. mapKeyInterface := indexv.FieldByName(key)
  52. if mapKeyInterface.Kind() == reflect.Invalid {
  53. return nil, nil
  54. }
  55. mapKeyString, err := InterfaceToString(mapKeyInterface.Interface())
  56. if err != nil {
  57. return nil, err
  58. }
  59. result[mapKeyString] = indexv.Interface()
  60. }
  61. return result, err
  62. }
  63. //转string
  64. func InterfaceToString(v interface{}) (result string, err error) {
  65. switch reflect.TypeOf(v).Kind() {
  66. case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
  67. result = fmt.Sprintf("%v", v)
  68. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  69. result = fmt.Sprintf("%v", v)
  70. case reflect.String:
  71. result = v.(string)
  72. default:
  73. err = nil
  74. }
  75. return result, err
  76. }
  77. func HideTrueName(name string) string {
  78. res := "**"
  79. if name != "" {
  80. runs := []rune(name)
  81. leng := len(runs)
  82. if leng <= 3 {
  83. res = string(runs[0:1]) + res
  84. } else if leng < 5 {
  85. res = string(runs[0:2]) + res
  86. } else if leng < 10 {
  87. res = string(runs[0:2]) + "***" + string(runs[leng-2:leng])
  88. } else if leng < 16 {
  89. res = string(runs[0:3]) + "****" + string(runs[leng-3:leng])
  90. } else {
  91. res = string(runs[0:4]) + "*****" + string(runs[leng-4:leng])
  92. }
  93. }
  94. return res
  95. }
  96. func GetQueryParam(uri string) map[string]string {
  97. //根据问号分割路由还是query参数
  98. uriList := strings.Split(uri, "?")
  99. var query = make(map[string]string, 0)
  100. //有参数才处理
  101. if len(uriList) == 2 {
  102. //分割query参数
  103. var queryList = strings.Split(uriList[1], "&")
  104. if len(queryList) > 0 {
  105. //key value 分别赋值
  106. for _, v := range queryList {
  107. var valueList = strings.Split(v, "=")
  108. if len(valueList) == 2 {
  109. value, _ := php2go.URLDecode(valueList[1])
  110. if value == "" {
  111. value = valueList[1]
  112. }
  113. query[valueList[0]] = value
  114. }
  115. }
  116. }
  117. }
  118. return query
  119. }
  120. //JoinStringsInASCII 按照规则,参数名ASCII码从小到大排序后拼接
  121. //data 待拼接的数据
  122. //sep 连接符
  123. //onlyValues 是否只包含参数值,true则不包含参数名,否则参数名和参数值均有
  124. //includeEmpty 是否包含空值,true则包含空值,否则不包含,注意此参数不影响参数名的存在
  125. //exceptKeys 被排除的参数名,不参与排序及拼接
  126. func JoinStringsInASCII(data map[string]string, sep string, onlyValues, includeEmpty bool, exceptKeys ...string) string {
  127. var list []string
  128. var keyList []string
  129. m := make(map[string]int)
  130. if len(exceptKeys) > 0 {
  131. for _, except := range exceptKeys {
  132. m[except] = 1
  133. }
  134. }
  135. for k := range data {
  136. if _, ok := m[k]; ok {
  137. continue
  138. }
  139. value := data[k]
  140. if !includeEmpty && value == "" {
  141. continue
  142. }
  143. if onlyValues {
  144. keyList = append(keyList, k)
  145. } else {
  146. list = append(list, fmt.Sprintf("%s=%s", k, value))
  147. }
  148. }
  149. if onlyValues {
  150. sort.Strings(keyList)
  151. for _, v := range keyList {
  152. list = append(list, AnyToString(data[v]))
  153. }
  154. } else {
  155. sort.Strings(list)
  156. }
  157. return strings.Join(list, sep)
  158. }
  159. //x的y次方
  160. func RandPow(l int) string {
  161. var i = "1"
  162. for j := 0; j < l; j++ {
  163. i += "0"
  164. }
  165. k := StrToInt64(i)
  166. n := rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(k)
  167. ls := "%0" + IntToStr(l) + "v"
  168. str := fmt.Sprintf(ls, n)
  169. //min := int(math.Pow10(l - 1))
  170. //max := int(math.Pow10(l) - 1)
  171. return str
  172. }
  173. //根据显示长度截取字符串
  174. func ShowSubstr(s string, l int) string {
  175. if len(s) <= l {
  176. return s
  177. }
  178. ss, sl, rl, rs := "", 0, 0, []rune(s)
  179. for _, r := range rs {
  180. rint := int(r)
  181. if rint < 128 {
  182. rl = 1
  183. } else {
  184. rl = 2
  185. }
  186. if sl+rl > l {
  187. break
  188. }
  189. sl += rl
  190. ss += string(r)
  191. }
  192. return ss
  193. }