lib.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package lib
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/nats-io/nats.go"
  7. "github.com/thinkeridea/go-extend/exunicode/exutf8"
  8. "golang.org/x/text/encoding/simplifiedchinese"
  9. "golang.org/x/text/transform"
  10. "io/ioutil"
  11. "math/rand"
  12. "runtime"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "unicode/utf8"
  18. )
  19. type Cl_ struct {
  20. Uuid_list map[string]string // 泛型
  21. }
  22. var Nats *nats.Conn
  23. func init() {
  24. }
  25. type JSONS struct {
  26. //必须的大写开头
  27. Code int16
  28. Msg string
  29. Data interface{} // 泛型
  30. }
  31. // func_page 分页 [{3 1} {4 2} {4 3} {4 4} {4 5} {4 6} {4 7} {4 8} {4 9} {5 2}]-
  32. type Page_T struct {
  33. A int
  34. V int64
  35. }
  36. func Func_page(Page int64, Page_size int64) (page_t_list []Page_T) {
  37. if Page > 1 {
  38. page_t_list = append(page_t_list, Page_T{A: 1, V: Page - 1})
  39. }
  40. i := int64(0)
  41. for aa := int64(1); aa < 5; aa++ {
  42. if Page-aa <= 0 {
  43. break
  44. }
  45. page_t_list = append(page_t_list, Page_T{A: 2, V: Page - aa})
  46. i++
  47. }
  48. page_t_list = append(page_t_list, Page_T{A: 3, V: Page})
  49. for aa := int64(1); aa < 10-i; aa++ {
  50. if Page_size < Page+aa {
  51. break
  52. }
  53. page_t_list = append(page_t_list, Page_T{A: 4, V: Page + aa})
  54. }
  55. sort.Slice(page_t_list, func(i, j int) bool {
  56. if page_t_list[i].V < page_t_list[j].V {
  57. return true
  58. }
  59. return false
  60. })
  61. sort.Slice(page_t_list, func(i, j int) bool {
  62. if page_t_list[i].A < page_t_list[j].A {
  63. return true
  64. }
  65. return false
  66. })
  67. if Page < Page_size {
  68. page_t_list = append(page_t_list, Page_T{A: 5, V: Page + 1})
  69. }
  70. return page_t_list
  71. }
  72. func Strval(value interface{}) string {
  73. var key string
  74. if value == nil {
  75. return key
  76. }
  77. switch value.(type) {
  78. case float64:
  79. ft := value.(float64)
  80. key = strconv.FormatFloat(ft, 'f', -1, 64)
  81. case float32:
  82. ft := value.(float32)
  83. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  84. case int:
  85. it := value.(int)
  86. key = strconv.Itoa(it)
  87. case uint:
  88. it := value.(uint)
  89. key = strconv.Itoa(int(it))
  90. case int8:
  91. it := value.(int8)
  92. key = strconv.Itoa(int(it))
  93. case uint8:
  94. it := value.(uint8)
  95. key = strconv.Itoa(int(it))
  96. case int16:
  97. it := value.(int16)
  98. key = strconv.Itoa(int(it))
  99. case uint16:
  100. it := value.(uint16)
  101. key = strconv.Itoa(int(it))
  102. case int32:
  103. it := value.(int32)
  104. key = strconv.Itoa(int(it))
  105. case uint32:
  106. it := value.(uint32)
  107. key = strconv.Itoa(int(it))
  108. case int64:
  109. it := value.(int64)
  110. key = strconv.FormatInt(it, 10)
  111. case uint64:
  112. it := value.(uint64)
  113. key = strconv.FormatUint(it, 10)
  114. case string:
  115. key = value.(string)
  116. case []byte:
  117. key = string(value.([]byte))
  118. default:
  119. newValue, _ := json.Marshal(value)
  120. key = string(newValue)
  121. }
  122. return key
  123. }
  124. func To_int(value interface{}) int {
  125. var key int
  126. if value == nil {
  127. return key
  128. }
  129. switch value.(type) {
  130. case float64:
  131. key = int(value.(float64))
  132. case float32:
  133. key = int(value.(float32))
  134. case int:
  135. key = int(value.(int))
  136. case uint:
  137. key = int(value.(uint))
  138. case int8:
  139. key = int(value.(int8))
  140. case uint8:
  141. key = int(value.(uint8))
  142. case int16:
  143. key = int(value.(int16))
  144. case uint16:
  145. key = int(value.(uint16))
  146. case int32:
  147. key = int(value.(int32))
  148. case uint32:
  149. key = int(value.(uint32))
  150. case int64:
  151. key = int(value.(int64))
  152. case uint64:
  153. key = int(value.(uint64))
  154. case string:
  155. key, _ = strconv.Atoi(value.(string))
  156. case []byte:
  157. key, _ = strconv.Atoi(string(value.([]byte)))
  158. default:
  159. newValue, _ := json.Marshal(value)
  160. key, _ = strconv.Atoi(string(newValue))
  161. }
  162. return key
  163. }
  164. func To_int64(value interface{}) int64 {
  165. var key int64
  166. if value == nil {
  167. return key
  168. }
  169. switch value.(type) {
  170. case float64:
  171. key = int64(value.(float64))
  172. case float32:
  173. key = int64(value.(float32))
  174. case int:
  175. key = int64(value.(int))
  176. case uint:
  177. key = int64(value.(uint))
  178. case int8:
  179. key = int64(value.(int8))
  180. case uint8:
  181. key = int64(value.(uint8))
  182. case int16:
  183. key = int64(value.(int16))
  184. case uint16:
  185. key = int64(value.(uint16))
  186. case int32:
  187. key = int64(value.(int32))
  188. case uint32:
  189. key = int64(value.(uint32))
  190. case int64:
  191. key = int64(value.(int64))
  192. case uint64:
  193. key = int64(value.(uint64))
  194. case string:
  195. key, _ = strconv.ParseInt(value.(string), 10, 64)
  196. case []byte:
  197. key, _ = strconv.ParseInt(string(value.([]byte)), 10, 64)
  198. default:
  199. newValue, _ := json.Marshal(value)
  200. key, _ = strconv.ParseInt(string(newValue), 10, 64)
  201. }
  202. return key
  203. }
  204. func To_float32(value interface{}) float32 {
  205. var key float32
  206. if value == nil {
  207. return key
  208. }
  209. switch value.(type) {
  210. case float64:
  211. key = float32(value.(float64))
  212. case float32:
  213. key = float32(value.(float32))
  214. case int:
  215. key = float32(value.(int))
  216. case uint:
  217. key = float32(value.(uint))
  218. case int8:
  219. key = float32(value.(int8))
  220. case uint8:
  221. key = float32(value.(uint8))
  222. case int16:
  223. key = float32(value.(int16))
  224. case uint16:
  225. key = float32(value.(uint16))
  226. case int32:
  227. key = float32(value.(int32))
  228. case uint32:
  229. key = float32(value.(uint32))
  230. case int64:
  231. key = float32(value.(int64))
  232. case uint64:
  233. key = float32(value.(uint64))
  234. case string:
  235. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  236. key = float32(key_float64)
  237. case []byte:
  238. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  239. key = float32(key_float64)
  240. default:
  241. newValue, _ := json.Marshal(value)
  242. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  243. key = float32(key_float64)
  244. }
  245. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  246. key = float32(key_float64)
  247. return key
  248. }
  249. func To_string(value interface{}) string {
  250. var key string
  251. if value == nil {
  252. return key
  253. }
  254. switch value.(type) {
  255. case float64:
  256. ft := value.(float64)
  257. key = strconv.FormatFloat(ft, 'f', -1, 64)
  258. case float32:
  259. ft := value.(float32)
  260. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  261. case int:
  262. it := value.(int)
  263. key = strconv.Itoa(it)
  264. case uint:
  265. it := value.(uint)
  266. key = strconv.Itoa(int(it))
  267. case int8:
  268. it := value.(int8)
  269. key = strconv.Itoa(int(it))
  270. case uint8:
  271. it := value.(uint8)
  272. key = strconv.Itoa(int(it))
  273. case int16:
  274. it := value.(int16)
  275. key = strconv.Itoa(int(it))
  276. case uint16:
  277. it := value.(uint16)
  278. key = strconv.Itoa(int(it))
  279. case int32:
  280. it := value.(int32)
  281. key = strconv.Itoa(int(it))
  282. case uint32:
  283. it := value.(uint32)
  284. key = strconv.Itoa(int(it))
  285. case int64:
  286. it := value.(int64)
  287. key = strconv.FormatInt(it, 10)
  288. case uint64:
  289. it := value.(uint64)
  290. key = strconv.FormatUint(it, 10)
  291. case string:
  292. key = value.(string)
  293. case []byte:
  294. key = string(value.([]byte))
  295. default:
  296. newValue, _ := json.Marshal(value)
  297. key = string(newValue)
  298. }
  299. return key
  300. }
  301. func Random(min, max int) int {
  302. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  303. return rand.Intn(max-min) + min
  304. }
  305. // 取文本(字符串)中间
  306. func GetBetweenStr(str, start, end string) string {
  307. n := strings.Index(str, start)
  308. if n == -1 {
  309. n = 0
  310. } else {
  311. n = n + len(start) // 增加了else,不加的会把start带上
  312. }
  313. str = string([]byte(str)[n:])
  314. m := strings.Index(str, end)
  315. if m == -1 {
  316. m = len(str)
  317. }
  318. str = string([]byte(str)[:m])
  319. return str
  320. }
  321. // getYearMonthToDay 查询指定年份指定月份有多少天
  322. // @params year int 指定年份
  323. // @params month int 指定月份
  324. func GetYearMonthToDay(year int, month int) int {
  325. // 有31天的月份
  326. day31 := map[int]bool{
  327. 1: true,
  328. 3: true,
  329. 5: true,
  330. 7: true,
  331. 8: true,
  332. 10: true,
  333. 12: true,
  334. }
  335. if day31[month] == true {
  336. return 31
  337. }
  338. // 有30天的月份
  339. day30 := map[int]bool{
  340. 4: true,
  341. 6: true,
  342. 9: true,
  343. 11: true,
  344. }
  345. if day30[month] == true {
  346. return 30
  347. }
  348. // 计算是平年还是闰年
  349. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  350. // 得出2月的天数
  351. return 29
  352. }
  353. // 得出2月的天数
  354. return 28
  355. }
  356. func Decimal(value float64) float64 {
  357. value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64)
  358. return value
  359. }
  360. func Strconv_Atoi(string string) int {
  361. int, _ := strconv.Atoi(string)
  362. return int
  363. }
  364. // 获取正在运行的函数名
  365. func FuncName() string {
  366. pc := make([]uintptr, 1)
  367. runtime.Callers(2, pc)
  368. f := runtime.FuncForPC(pc[0])
  369. return f.Name()
  370. }
  371. func Limit_len(str string, lenx int) string {
  372. if utf8.RuneCountInString(str) > lenx {
  373. return exutf8.RuneSubString(str, 0, lenx-3) + "..."
  374. }
  375. return exutf8.RuneSubString(str, 0, lenx)
  376. }
  377. func Utf8ToGbk(s []byte) ([]byte, error) {
  378. reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
  379. d, e := ioutil.ReadAll(reader)
  380. if e != nil {
  381. return nil, e
  382. }
  383. return d, nil
  384. }