lib.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. package lib
  2. import (
  3. "AIOTCOER/logs"
  4. "bytes"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/nats-io/nats.go"
  10. "github.com/thinkeridea/go-extend/exunicode/exutf8"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "golang.org/x/text/encoding/simplifiedchinese"
  13. "golang.org/x/text/transform"
  14. "io/ioutil"
  15. "math/rand"
  16. "reflect"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "time"
  22. "unicode/utf8"
  23. )
  24. var Nats *nats.Conn
  25. type SingleMutex struct {
  26. Mu sync.RWMutex
  27. Num int64
  28. }
  29. var PullHandleTime SingleMutex
  30. //耗时统计函数
  31. func (c *SingleMutex) TimeCost(start time.Time){
  32. c.Mu.Lock()
  33. c.Num = time.Since(start).Milliseconds()
  34. c.Mu.Unlock()
  35. }
  36. func init() {
  37. }
  38. const Success = 200
  39. const Error = 201
  40. type JSONR struct {
  41. //必须的大写开头
  42. Code int16
  43. Msg string
  44. Data interface{} // 泛型
  45. }
  46. type JSONS struct {
  47. //必须的大写开头
  48. Code int16
  49. Msg string
  50. List interface{}
  51. Total int16
  52. PageIndex int
  53. PageSize int
  54. }
  55. func C_Page(list interface{}, PageIndex, PageSize int, Total int64) (Jsons JSONS) {
  56. Jsons.List = list
  57. Jsons.Total = int16(Total)
  58. Jsons.PageIndex = PageIndex
  59. Jsons.PageSize = PageSize
  60. //Jsons.PageSize = int16(math.Ceil(float64(Total) / float64(PageSize)))
  61. return Jsons
  62. }
  63. func Strval(value interface{}) string {
  64. var key string
  65. if value == nil {
  66. return key
  67. }
  68. switch value.(type) {
  69. case float64:
  70. ft := value.(float64)
  71. key = strconv.FormatFloat(ft, 'f', -1, 64)
  72. case float32:
  73. ft := value.(float32)
  74. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  75. case int:
  76. it := value.(int)
  77. key = strconv.Itoa(it)
  78. case uint:
  79. it := value.(uint)
  80. key = strconv.Itoa(int(it))
  81. case int8:
  82. it := value.(int8)
  83. key = strconv.Itoa(int(it))
  84. case uint8:
  85. it := value.(uint8)
  86. key = strconv.Itoa(int(it))
  87. case int16:
  88. it := value.(int16)
  89. key = strconv.Itoa(int(it))
  90. case uint16:
  91. it := value.(uint16)
  92. key = strconv.Itoa(int(it))
  93. case int32:
  94. it := value.(int32)
  95. key = strconv.Itoa(int(it))
  96. case uint32:
  97. it := value.(uint32)
  98. key = strconv.Itoa(int(it))
  99. case int64:
  100. it := value.(int64)
  101. key = strconv.FormatInt(it, 10)
  102. case uint64:
  103. it := value.(uint64)
  104. key = strconv.FormatUint(it, 10)
  105. case string:
  106. key = value.(string)
  107. case []byte:
  108. key = string(value.([]byte))
  109. default:
  110. newValue, _ := json.Marshal(value)
  111. key = string(newValue)
  112. }
  113. return key
  114. }
  115. // 取一个
  116. func Take_one(a, b string) string {
  117. if len(a) == 0 {
  118. return b
  119. }
  120. return a
  121. }
  122. func To_int(value interface{}) int {
  123. var key int
  124. if value == nil {
  125. return key
  126. }
  127. switch value.(type) {
  128. case float64:
  129. key = int(value.(float64))
  130. case float32:
  131. key = int(value.(float32))
  132. case int:
  133. key = int(value.(int))
  134. case uint:
  135. key = int(value.(uint))
  136. case int8:
  137. key = int(value.(int8))
  138. case uint8:
  139. key = int(value.(uint8))
  140. case int16:
  141. key = int(value.(int16))
  142. case uint16:
  143. key = int(value.(uint16))
  144. case int32:
  145. key = int(value.(int32))
  146. case uint32:
  147. key = int(value.(uint32))
  148. case int64:
  149. key = int(value.(int64))
  150. case uint64:
  151. key = int(value.(uint64))
  152. case string:
  153. key, _ = strconv.Atoi(value.(string))
  154. case []byte:
  155. key, _ = strconv.Atoi(string(value.([]byte)))
  156. default:
  157. newValue, _ := json.Marshal(value)
  158. key, _ = strconv.Atoi(string(newValue))
  159. }
  160. return key
  161. }
  162. func To_int64(value interface{}) int64 {
  163. var key int64
  164. if value == nil {
  165. return key
  166. }
  167. switch value.(type) {
  168. case float64:
  169. key = int64(value.(float64))
  170. case float32:
  171. key = int64(value.(float32))
  172. case int:
  173. key = int64(value.(int))
  174. case uint:
  175. key = int64(value.(uint))
  176. case int8:
  177. key = int64(value.(int8))
  178. case uint8:
  179. key = int64(value.(uint8))
  180. case int16:
  181. key = int64(value.(int16))
  182. case uint16:
  183. key = int64(value.(uint16))
  184. case int32:
  185. key = int64(value.(int32))
  186. case uint32:
  187. key = int64(value.(uint32))
  188. case int64:
  189. key = int64(value.(int64))
  190. case uint64:
  191. key = int64(value.(uint64))
  192. case string:
  193. key, _ = strconv.ParseInt(value.(string), 10, 64)
  194. case []byte:
  195. key, _ = strconv.ParseInt(string(value.([]byte)), 10, 64)
  196. default:
  197. newValue, _ := json.Marshal(value)
  198. key, _ = strconv.ParseInt(string(newValue), 10, 64)
  199. }
  200. return key
  201. }
  202. func To_float32(value interface{}) float32 {
  203. var key float32
  204. if value == nil {
  205. return key
  206. }
  207. switch value.(type) {
  208. case float64:
  209. key = float32(value.(float64))
  210. case float32:
  211. key = float32(value.(float32))
  212. case int:
  213. key = float32(value.(int))
  214. case uint:
  215. key = float32(value.(uint))
  216. case int8:
  217. key = float32(value.(int8))
  218. case uint8:
  219. key = float32(value.(uint8))
  220. case int16:
  221. key = float32(value.(int16))
  222. case uint16:
  223. key = float32(value.(uint16))
  224. case int32:
  225. key = float32(value.(int32))
  226. case uint32:
  227. key = float32(value.(uint32))
  228. case int64:
  229. key = float32(value.(int64))
  230. case uint64:
  231. key = float32(value.(uint64))
  232. case string:
  233. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  234. key = float32(key_float64)
  235. case []byte:
  236. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  237. key = float32(key_float64)
  238. default:
  239. newValue, _ := json.Marshal(value)
  240. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  241. key = float32(key_float64)
  242. }
  243. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  244. key = float32(key_float64)
  245. return key
  246. }
  247. func To_string(value interface{}) string {
  248. var key string
  249. if value == nil {
  250. return key
  251. }
  252. switch value.(type) {
  253. case float64:
  254. ft := value.(float64)
  255. key = strconv.FormatFloat(ft, 'f', -1, 64)
  256. case float32:
  257. ft := value.(float32)
  258. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  259. case int:
  260. it := value.(int)
  261. key = strconv.Itoa(it)
  262. case uint:
  263. it := value.(uint)
  264. key = strconv.Itoa(int(it))
  265. case int8:
  266. it := value.(int8)
  267. key = strconv.Itoa(int(it))
  268. case uint8:
  269. it := value.(uint8)
  270. key = strconv.Itoa(int(it))
  271. case int16:
  272. it := value.(int16)
  273. key = strconv.Itoa(int(it))
  274. case uint16:
  275. it := value.(uint16)
  276. key = strconv.Itoa(int(it))
  277. case int32:
  278. it := value.(int32)
  279. key = strconv.Itoa(int(it))
  280. case uint32:
  281. it := value.(uint32)
  282. key = strconv.Itoa(int(it))
  283. case int64:
  284. it := value.(int64)
  285. key = strconv.FormatInt(it, 10)
  286. case uint64:
  287. it := value.(uint64)
  288. key = strconv.FormatUint(it, 10)
  289. case string:
  290. key = value.(string)
  291. case []byte:
  292. key = string(value.([]byte))
  293. default:
  294. newValue, _ := json.Marshal(value)
  295. key = string(newValue)
  296. }
  297. return key
  298. }
  299. func Random(min, max int) int {
  300. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  301. return rand.Intn(max-min) + min
  302. }
  303. // 取文本(字符串)中间
  304. func GetBetweenStr(str, start, end string) string {
  305. n := strings.Index(str, start)
  306. if n == -1 {
  307. n = 0
  308. } else {
  309. n = n + len(start) // 增加了else,不加的会把start带上
  310. }
  311. str = string([]byte(str)[n:])
  312. m := strings.Index(str, end)
  313. if m == -1 {
  314. m = len(str)
  315. }
  316. str = string([]byte(str)[:m])
  317. return str
  318. }
  319. // getYearMonthToDay 查询指定年份指定月份有多少天
  320. // @params year int 指定年份
  321. // @params month int 指定月份
  322. func GetYearMonthToDay(year int, month int) int {
  323. // 有31天的月份
  324. day31 := map[int]bool{
  325. 1: true,
  326. 3: true,
  327. 5: true,
  328. 7: true,
  329. 8: true,
  330. 10: true,
  331. 12: true,
  332. }
  333. if day31[month] == true {
  334. return 31
  335. }
  336. // 有30天的月份
  337. day30 := map[int]bool{
  338. 4: true,
  339. 6: true,
  340. 9: true,
  341. 11: true,
  342. }
  343. if day30[month] == true {
  344. return 30
  345. }
  346. // 计算是平年还是闰年
  347. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  348. // 得出2月的天数
  349. return 29
  350. }
  351. // 得出2月的天数
  352. return 28
  353. }
  354. func Decimal(value float64) float64 {
  355. value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64)
  356. return value
  357. }
  358. func Strconv_Atoi(string string) int {
  359. int, _ := strconv.Atoi(string)
  360. return int
  361. }
  362. // 获取正在运行的函数名
  363. func FuncName() string {
  364. pc := make([]uintptr, 1)
  365. runtime.Callers(2, pc)
  366. f := runtime.FuncForPC(pc[0])
  367. return f.Name()
  368. }
  369. func Limit_len(str string, lenx int) string {
  370. if utf8.RuneCountInString(str) > lenx {
  371. return exutf8.RuneSubString(str, 0, lenx-3) + "..."
  372. }
  373. return exutf8.RuneSubString(str, 0, lenx)
  374. }
  375. func Utf8ToGbk(s []byte) ([]byte, error) {
  376. reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
  377. d, e := ioutil.ReadAll(reader)
  378. if e != nil {
  379. return nil, e
  380. }
  381. return d, nil
  382. }
  383. // #取得随机字符串:通过打乱slice来操作
  384. func GetRandstring(length int, char string, rand_x int64) string {
  385. if length < 1 {
  386. return ""
  387. }
  388. if len(char) <= 6 || len(char) <= length {
  389. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  390. }
  391. charArr := strings.Split(char, "")
  392. ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
  393. l := len(charArr)
  394. for i := l - 1; i > 0; i-- {
  395. r := ran.Intn(i)
  396. charArr[r], charArr[i] = charArr[i], charArr[r]
  397. }
  398. rchar := charArr[:length]
  399. return strings.Join(rchar, "")
  400. }
  401. // Sha256加密
  402. func Sha256(src string) string {
  403. m := sha256.New()
  404. m.Write([]byte(src))
  405. res := hex.EncodeToString(m.Sum(nil))
  406. return res
  407. }
  408. // 判断时间是当年的第几周
  409. func WeekByDate() string {
  410. t := time.Now()
  411. yearDay := t.YearDay()
  412. yearFirstDay := t.AddDate(0, 0, -yearDay+1)
  413. firstDayInWeek := int(yearFirstDay.Weekday())
  414. //今年第一周有几天
  415. firstWeekDays := 1
  416. if firstDayInWeek != 0 {
  417. firstWeekDays = 7 - firstDayInWeek + 1
  418. }
  419. var week int
  420. if yearDay <= firstWeekDays {
  421. week = 1
  422. } else {
  423. week = (yearDay-firstWeekDays)/7 + 2
  424. }
  425. return fmt.Sprintf("%d%02d", t.Year(), week) // 202253
  426. }
  427. // 获取json指定参数 [AAAA BBBB],0,JSON !!!如果失败,返回已找到的全部json
  428. func Json_key(topicNameList []string, topicNameI int, articleSlide map[string]interface{}) interface{} {
  429. if len(topicNameList) > topicNameI {
  430. a, is := articleSlide[topicNameList[topicNameI]]
  431. if !is {
  432. return articleSlide
  433. }
  434. if reflect.TypeOf(a).String() == "map[string]interface {}" {
  435. return Json_key(topicNameList, topicNameI+1, a.(map[string]interface{}))
  436. }
  437. }
  438. if len(topicNameList) == topicNameI {
  439. return articleSlide
  440. }
  441. return articleSlide[topicNameList[topicNameI]]
  442. }
  443. // 导出map到文件
  444. func MapToFile(data map[string]map[string]string, filePath string) error {
  445. jsonData, err := json.Marshal(data)
  446. if err != nil {
  447. return err
  448. }
  449. err = ioutil.WriteFile(filePath, jsonData, 0644)
  450. if err != nil {
  451. return err
  452. }
  453. return nil
  454. }
  455. // 从文件中导入map数据
  456. func MapFromFile(filePath string) map[string]map[string]string {
  457. jsonData, err := ioutil.ReadFile(filePath)
  458. if err != nil {
  459. return nil
  460. }
  461. var importedMap map[string]map[string]string
  462. err = json.Unmarshal(jsonData, &importedMap)
  463. if err != nil {
  464. logs.PrintlnError("Error unmarshalling JSON:", err)
  465. return nil
  466. }
  467. return importedMap
  468. }
  469. func Hande_Tab_Tfilter(ArticleSlide []map[string]interface{}, JointTab string, t_map *[]string) {
  470. for _, value := range ArticleSlide {
  471. // 存在下一级
  472. if len(JointTab) == 0 || len(value["children"].([]interface{})) != 0 {
  473. key := JointTab + value["T_tab"].(string)
  474. if value["T_filter"] == nil {
  475. continue
  476. }
  477. if reflect.TypeOf(value["T_filter"]).String() != "bool" {
  478. continue
  479. }
  480. key_v := value["T_filter"].(bool)
  481. if key_v {
  482. //(*t_map)[key] = key_v
  483. (*t_map) = append((*t_map), key)
  484. }
  485. if len(value["children"].([]interface{})) != 0 {
  486. var valuex_map []map[string]interface{}
  487. for _, valuex := range value["children"].([]interface{}) {
  488. if reflect.TypeOf(valuex).String() == "map[string]interface {}" {
  489. valuex_map = append(valuex_map, valuex.(map[string]interface{}))
  490. }
  491. }
  492. Hande_Tab_Tfilter(valuex_map, key+".", t_map)
  493. }
  494. }
  495. }
  496. }
  497. func Hande_Tab_Trenew(ArticleSlide []map[string]interface{}, JointTab string, t_map *[]string) {
  498. for _, value := range ArticleSlide {
  499. // 存在下一级
  500. key := JointTab + value["T_tab"].(string)
  501. if len(value["children"].([]interface{})) != 0 {
  502. var valuex_map []map[string]interface{}
  503. for _, valuex := range value["children"].([]interface{}) {
  504. if reflect.TypeOf(valuex).String() == "map[string]interface {}" {
  505. valuex_map = append(valuex_map, valuex.(map[string]interface{}))
  506. }
  507. }
  508. Hande_Tab_Trenew(valuex_map, key+".", t_map)
  509. }
  510. if value["T_renew"] == nil {
  511. continue
  512. }
  513. if reflect.TypeOf(value["T_renew"]).String() != "bool" {
  514. continue
  515. }
  516. key_v := value["T_renew"].(bool)
  517. if key_v {
  518. //(*t_map)[key] = key_v
  519. (*t_map) = append((*t_map), key)
  520. }
  521. }
  522. }
  523. func Hande_Tab_Tsave(ArticleSlide []map[string]interface{}, JointTab string, t_map *map[string]int) {
  524. for _, value := range ArticleSlide {
  525. // 存在下一级
  526. if len(JointTab) == 0 || len(value["children"].([]interface{})) != 0 {
  527. key := JointTab + value["T_tab"].(string)
  528. if value["T_save"] == nil {
  529. continue
  530. }
  531. key_v := To_int(value["T_save"])
  532. if key_v > 0 {
  533. (*t_map)[key] = key_v
  534. }
  535. if len(value["children"].([]interface{})) != 0 {
  536. var valuex_map []map[string]interface{}
  537. for _, valuex := range value["children"].([]interface{}) {
  538. if reflect.TypeOf(valuex).String() == "map[string]interface {}" {
  539. valuex_map = append(valuex_map, valuex.(map[string]interface{}))
  540. }
  541. }
  542. Hande_Tab_Tsave(valuex_map, key+".", t_map)
  543. }
  544. }
  545. }
  546. }
  547. // 通过 "aa.bb.cc" 获取map指定参数
  548. func GetMapRecursion(str string, nestedMap map[string]interface{}) interface{} {
  549. keys := strings.Split(str, ".")
  550. result := nestedMap
  551. for _, key := range keys {
  552. if value, ok := result[key]; ok {
  553. switch reflect.TypeOf(value).String() {
  554. case "map[string]interface {}":
  555. result = value.(map[string]interface{})
  556. break
  557. default:
  558. return value
  559. }
  560. } else {
  561. logs.Println("Key not found")
  562. return nil
  563. }
  564. }
  565. return result
  566. }
  567. // 将 时间格式为 bson.M 时间格式
  568. func MongdbJsonHtime(ArticleSlide bson.M) bson.M {
  569. bson_M := bson.M{}
  570. for key, value := range ArticleSlide {
  571. //logs.Println(reflect.TypeOf(value).String())
  572. switch reflect.TypeOf(value).String() {
  573. case "map[string]interface {}":
  574. jsonFind_M := bson.M{}
  575. data, _ := json.Marshal(value.(map[string]interface{}))
  576. err := json.Unmarshal(data, &jsonFind_M)
  577. if err == nil {
  578. bson_M[key] = MongdbJsonHtime(jsonFind_M)
  579. }
  580. break
  581. case "[]interface {}":
  582. bson_M_list := []bson.M{}
  583. for _, valuex := range value.([]interface{}) {
  584. if reflect.TypeOf(valuex).String() == "map[string]interface {}" {
  585. jsonFind_M := bson.M{}
  586. data, _ := json.Marshal(valuex.(map[string]interface{}))
  587. err := json.Unmarshal(data, &jsonFind_M)
  588. if err == nil {
  589. bson_M_list = append(bson_M_list, MongdbJsonHtime(jsonFind_M))
  590. }
  591. }
  592. }
  593. bson_M[key] = bson_M_list
  594. //return json_r
  595. break
  596. default:
  597. t1, err := time.ParseInLocation("2006-01-02 15:04:05", value.(string), time.Local) // +8 时差
  598. if err == nil {
  599. bson_M[key] = t1
  600. } else {
  601. bson_M[key] = value
  602. }
  603. break
  604. }
  605. }
  606. return bson_M
  607. }
  608. // 判断字符串是否在数组中
  609. func StringExistsInSlice(str string, slice []string) bool {
  610. for _, s := range slice {
  611. if s == str {
  612. return true
  613. }
  614. }
  615. return false
  616. }
  617. // 获取 JSON 指定位置 field : "AAA.BBB.d_name"
  618. func JsonGetField(data map[string]interface{}, field string) interface{} {
  619. fields := strings.Split(field, ".")
  620. result := data
  621. for _, f := range fields {
  622. if val, ok := result[f]; ok {
  623. switch val.(type) {
  624. case map[string]interface{}:
  625. result = val.(map[string]interface{})
  626. default:
  627. return val
  628. }
  629. } else {
  630. return nil
  631. }
  632. }
  633. return result
  634. }
  635. // 获取 JSON 指定位置 data:修改的field必须存在 field : "AAA.BBB.d_name" value: 123
  636. func JsonSetField(data map[string]interface{}, field string, value interface{}) {
  637. fields := strings.Split(field, ".")
  638. current := data
  639. for i, fieldv := range fields {
  640. if i == len(fields)-1 {
  641. current[fieldv] = value
  642. } else {
  643. next, ok := current[fieldv].(map[string]interface{})
  644. if !ok {
  645. return
  646. }
  647. current = next
  648. }
  649. }
  650. }
  651. // 复制一份 Map
  652. func CopyMap(inputMap map[string]interface{}) map[string]interface{} {
  653. copiedMap := make(map[string]interface{})
  654. for key, value := range inputMap {
  655. copiedMap[key] = value
  656. }
  657. return copiedMap
  658. }