lib.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. package lib
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/nats-io/nats.go"
  9. "github.com/thinkeridea/go-extend/exunicode/exutf8"
  10. "golang.org/x/text/encoding/simplifiedchinese"
  11. "golang.org/x/text/transform"
  12. "io/ioutil"
  13. "math/rand"
  14. "reflect"
  15. "runtime"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "unicode/utf8"
  20. )
  21. var Nats *nats.Conn
  22. func init() {
  23. }
  24. const Success = 200
  25. const Error = 201
  26. type JSONR struct {
  27. //必须的大写开头
  28. Code int16
  29. Msg string
  30. Data interface{} // 泛型
  31. }
  32. type JSONS struct {
  33. //必须的大写开头
  34. Code int16
  35. Msg string
  36. List interface{}
  37. Total int16
  38. PageIndex int
  39. PageSize int
  40. }
  41. func C_Page(list interface{}, PageIndex, PageSize int, Total int64) (Jsons JSONS) {
  42. Jsons.List = list
  43. Jsons.Total = int16(Total)
  44. Jsons.PageIndex = PageIndex
  45. Jsons.PageSize = PageSize
  46. //Jsons.PageSize = int16(math.Ceil(float64(Total) / float64(PageSize)))
  47. return Jsons
  48. }
  49. func Strval(value interface{}) string {
  50. var key string
  51. if value == nil {
  52. return key
  53. }
  54. switch value.(type) {
  55. case float64:
  56. ft := value.(float64)
  57. key = strconv.FormatFloat(ft, 'f', -1, 64)
  58. case float32:
  59. ft := value.(float32)
  60. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  61. case int:
  62. it := value.(int)
  63. key = strconv.Itoa(it)
  64. case uint:
  65. it := value.(uint)
  66. key = strconv.Itoa(int(it))
  67. case int8:
  68. it := value.(int8)
  69. key = strconv.Itoa(int(it))
  70. case uint8:
  71. it := value.(uint8)
  72. key = strconv.Itoa(int(it))
  73. case int16:
  74. it := value.(int16)
  75. key = strconv.Itoa(int(it))
  76. case uint16:
  77. it := value.(uint16)
  78. key = strconv.Itoa(int(it))
  79. case int32:
  80. it := value.(int32)
  81. key = strconv.Itoa(int(it))
  82. case uint32:
  83. it := value.(uint32)
  84. key = strconv.Itoa(int(it))
  85. case int64:
  86. it := value.(int64)
  87. key = strconv.FormatInt(it, 10)
  88. case uint64:
  89. it := value.(uint64)
  90. key = strconv.FormatUint(it, 10)
  91. case string:
  92. key = value.(string)
  93. case []byte:
  94. key = string(value.([]byte))
  95. default:
  96. newValue, _ := json.Marshal(value)
  97. key = string(newValue)
  98. }
  99. return key
  100. }
  101. func To_int(value interface{}) int {
  102. var key int
  103. if value == nil {
  104. return key
  105. }
  106. switch value.(type) {
  107. case float64:
  108. key = int(value.(float64))
  109. case float32:
  110. key = int(value.(float32))
  111. case int:
  112. key = int(value.(int))
  113. case uint:
  114. key = int(value.(uint))
  115. case int8:
  116. key = int(value.(int8))
  117. case uint8:
  118. key = int(value.(uint8))
  119. case int16:
  120. key = int(value.(int16))
  121. case uint16:
  122. key = int(value.(uint16))
  123. case int32:
  124. key = int(value.(int32))
  125. case uint32:
  126. key = int(value.(uint32))
  127. case int64:
  128. key = int(value.(int64))
  129. case uint64:
  130. key = int(value.(uint64))
  131. case string:
  132. key, _ = strconv.Atoi(value.(string))
  133. case []byte:
  134. key, _ = strconv.Atoi(string(value.([]byte)))
  135. default:
  136. newValue, _ := json.Marshal(value)
  137. key, _ = strconv.Atoi(string(newValue))
  138. }
  139. return key
  140. }
  141. func To_int64(value interface{}) int64 {
  142. var key int64
  143. if value == nil {
  144. return key
  145. }
  146. switch value.(type) {
  147. case float64:
  148. key = int64(value.(float64))
  149. case float32:
  150. key = int64(value.(float32))
  151. case int:
  152. key = int64(value.(int))
  153. case uint:
  154. key = int64(value.(uint))
  155. case int8:
  156. key = int64(value.(int8))
  157. case uint8:
  158. key = int64(value.(uint8))
  159. case int16:
  160. key = int64(value.(int16))
  161. case uint16:
  162. key = int64(value.(uint16))
  163. case int32:
  164. key = int64(value.(int32))
  165. case uint32:
  166. key = int64(value.(uint32))
  167. case int64:
  168. key = int64(value.(int64))
  169. case uint64:
  170. key = int64(value.(uint64))
  171. case string:
  172. key, _ = strconv.ParseInt(value.(string), 10, 64)
  173. case []byte:
  174. key, _ = strconv.ParseInt(string(value.([]byte)), 10, 64)
  175. default:
  176. newValue, _ := json.Marshal(value)
  177. key, _ = strconv.ParseInt(string(newValue), 10, 64)
  178. }
  179. return key
  180. }
  181. func To_float32(value interface{}) float32 {
  182. var key float32
  183. if value == nil {
  184. return key
  185. }
  186. switch value.(type) {
  187. case float64:
  188. key = float32(value.(float64))
  189. case float32:
  190. key = float32(value.(float32))
  191. case int:
  192. key = float32(value.(int))
  193. case uint:
  194. key = float32(value.(uint))
  195. case int8:
  196. key = float32(value.(int8))
  197. case uint8:
  198. key = float32(value.(uint8))
  199. case int16:
  200. key = float32(value.(int16))
  201. case uint16:
  202. key = float32(value.(uint16))
  203. case int32:
  204. key = float32(value.(int32))
  205. case uint32:
  206. key = float32(value.(uint32))
  207. case int64:
  208. key = float32(value.(int64))
  209. case uint64:
  210. key = float32(value.(uint64))
  211. case string:
  212. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  213. key = float32(key_float64)
  214. case []byte:
  215. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  216. key = float32(key_float64)
  217. default:
  218. newValue, _ := json.Marshal(value)
  219. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  220. key = float32(key_float64)
  221. }
  222. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  223. key = float32(key_float64)
  224. return key
  225. }
  226. func To_string(value interface{}) string {
  227. var key string
  228. if value == nil {
  229. return key
  230. }
  231. switch value.(type) {
  232. case float64:
  233. ft := value.(float64)
  234. key = strconv.FormatFloat(ft, 'f', -1, 64)
  235. case float32:
  236. ft := value.(float32)
  237. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  238. case int:
  239. it := value.(int)
  240. key = strconv.Itoa(it)
  241. case uint:
  242. it := value.(uint)
  243. key = strconv.Itoa(int(it))
  244. case int8:
  245. it := value.(int8)
  246. key = strconv.Itoa(int(it))
  247. case uint8:
  248. it := value.(uint8)
  249. key = strconv.Itoa(int(it))
  250. case int16:
  251. it := value.(int16)
  252. key = strconv.Itoa(int(it))
  253. case uint16:
  254. it := value.(uint16)
  255. key = strconv.Itoa(int(it))
  256. case int32:
  257. it := value.(int32)
  258. key = strconv.Itoa(int(it))
  259. case uint32:
  260. it := value.(uint32)
  261. key = strconv.Itoa(int(it))
  262. case int64:
  263. it := value.(int64)
  264. key = strconv.FormatInt(it, 10)
  265. case uint64:
  266. it := value.(uint64)
  267. key = strconv.FormatUint(it, 10)
  268. case string:
  269. key = value.(string)
  270. case []byte:
  271. key = string(value.([]byte))
  272. default:
  273. newValue, _ := json.Marshal(value)
  274. key = string(newValue)
  275. }
  276. return key
  277. }
  278. func Random(min, max int) int {
  279. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  280. return rand.Intn(max-min) + min
  281. }
  282. // 取文本(字符串)中间
  283. func GetBetweenStr(str, start, end string) string {
  284. n := strings.Index(str, start)
  285. if n == -1 {
  286. n = 0
  287. } else {
  288. n = n + len(start) // 增加了else,不加的会把start带上
  289. }
  290. str = string([]byte(str)[n:])
  291. m := strings.Index(str, end)
  292. if m == -1 {
  293. m = len(str)
  294. }
  295. str = string([]byte(str)[:m])
  296. return str
  297. }
  298. // getYearMonthToDay 查询指定年份指定月份有多少天
  299. // @params year int 指定年份
  300. // @params month int 指定月份
  301. func GetYearMonthToDay(year int, month int) int {
  302. // 有31天的月份
  303. day31 := map[int]bool{
  304. 1: true,
  305. 3: true,
  306. 5: true,
  307. 7: true,
  308. 8: true,
  309. 10: true,
  310. 12: true,
  311. }
  312. if day31[month] == true {
  313. return 31
  314. }
  315. // 有30天的月份
  316. day30 := map[int]bool{
  317. 4: true,
  318. 6: true,
  319. 9: true,
  320. 11: true,
  321. }
  322. if day30[month] == true {
  323. return 30
  324. }
  325. // 计算是平年还是闰年
  326. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  327. // 得出2月的天数
  328. return 29
  329. }
  330. // 得出2月的天数
  331. return 28
  332. }
  333. func Decimal(value float64) float64 {
  334. value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64)
  335. return value
  336. }
  337. func Strconv_Atoi(string string) int {
  338. int, _ := strconv.Atoi(string)
  339. return int
  340. }
  341. // 获取正在运行的函数名
  342. func FuncName() string {
  343. pc := make([]uintptr, 1)
  344. runtime.Callers(2, pc)
  345. f := runtime.FuncForPC(pc[0])
  346. return f.Name()
  347. }
  348. func Limit_len(str string, lenx int) string {
  349. if utf8.RuneCountInString(str) > lenx {
  350. return exutf8.RuneSubString(str, 0, lenx-3) + "..."
  351. }
  352. return exutf8.RuneSubString(str, 0, lenx)
  353. }
  354. func Utf8ToGbk(s []byte) ([]byte, error) {
  355. reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
  356. d, e := ioutil.ReadAll(reader)
  357. if e != nil {
  358. return nil, e
  359. }
  360. return d, nil
  361. }
  362. // #取得随机字符串:通过打乱slice来操作
  363. func GetRandstring(length int, char string, rand_x int64) string {
  364. if length < 1 {
  365. return ""
  366. }
  367. if len(char) <= 6 || len(char) <= length {
  368. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  369. }
  370. charArr := strings.Split(char, "")
  371. ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
  372. l := len(charArr)
  373. for i := l - 1; i > 0; i-- {
  374. r := ran.Intn(i)
  375. charArr[r], charArr[i] = charArr[i], charArr[r]
  376. }
  377. rchar := charArr[:length]
  378. return strings.Join(rchar, "")
  379. }
  380. // Sha256加密
  381. func Sha256(src string) string {
  382. m := sha256.New()
  383. m.Write([]byte(src))
  384. res := hex.EncodeToString(m.Sum(nil))
  385. return res
  386. }
  387. // 判断时间是当年的第几周
  388. func WeekByDate() string {
  389. t := time.Now()
  390. yearDay := t.YearDay()
  391. yearFirstDay := t.AddDate(0, 0, -yearDay+1)
  392. firstDayInWeek := int(yearFirstDay.Weekday())
  393. //今年第一周有几天
  394. firstWeekDays := 1
  395. if firstDayInWeek != 0 {
  396. firstWeekDays = 7 - firstDayInWeek + 1
  397. }
  398. var week int
  399. if yearDay <= firstWeekDays {
  400. week = 1
  401. } else {
  402. week = (yearDay-firstWeekDays)/7 + 2
  403. }
  404. return fmt.Sprintf("%d%02d", t.Year(), week) // 202253
  405. }
  406. // 获取json指定参数 [AAAA BBBB],0,JSON !!!如果失败,返回已找到的全部json
  407. func Json_key(topicNameList []string, topicNameI int, articleSlide map[string]interface{}) interface{} {
  408. if len(topicNameList) > topicNameI {
  409. a, is := articleSlide[topicNameList[topicNameI]]
  410. if !is {
  411. return articleSlide
  412. }
  413. if reflect.TypeOf(a).String() == "map[string]interface {}" {
  414. return Json_key(topicNameList, topicNameI+1, a.(map[string]interface{}))
  415. }
  416. }
  417. if len(topicNameList) == topicNameI {
  418. return articleSlide
  419. }
  420. return articleSlide[topicNameList[topicNameI]]
  421. }
  422. // 导出map到文件
  423. func MapToFile(data map[string]map[string]string, filePath string) error {
  424. jsonData, err := json.Marshal(data)
  425. if err != nil {
  426. return err
  427. }
  428. err = ioutil.WriteFile(filePath, jsonData, 0644)
  429. if err != nil {
  430. return err
  431. }
  432. return nil
  433. }
  434. // 从文件中导入map数据
  435. func MapFromFile(filePath string) map[string]map[string]string {
  436. jsonData, err := ioutil.ReadFile(filePath)
  437. if err != nil {
  438. return nil
  439. }
  440. var importedMap map[string]map[string]string
  441. err = json.Unmarshal(jsonData, &importedMap)
  442. if err != nil {
  443. fmt.Println("Error unmarshalling JSON:", err)
  444. return nil
  445. }
  446. return importedMap
  447. }