lib.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. package lib
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web/context"
  6. "github.com/mssola/user_agent"
  7. "math/rand"
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. const timeFormat = "2006-01-02 15:04:05"
  17. const timezone = "Asia/Shanghai"
  18. const dateFormat = "2006-01-02"
  19. type JSONS struct {
  20. //必须的大写开头
  21. Code int16
  22. Msg string
  23. Data interface{} // 泛型
  24. }
  25. type R_JSONS_List struct {
  26. //必须的大写开头
  27. Data []interface{}
  28. Num int64
  29. Page int
  30. Page_size int
  31. }
  32. type R_JSONS struct {
  33. //必须的大写开头
  34. Data interface{}
  35. Num int64
  36. Page int
  37. Page_size int
  38. }
  39. type R1_JSONS struct {
  40. //必须的大写开头
  41. List interface{}
  42. Num int
  43. Page int
  44. Page_size int
  45. Pages []Page_T
  46. }
  47. // func_page 分页 [{3 1} {4 2} {4 3} {4 4} {4 5} {4 6} {4 7} {4 8} {4 9} {5 2}]-
  48. type Page_T struct {
  49. A int
  50. V int64
  51. }
  52. func Func_page(Page int64, Page_size int64) (page_t_list []Page_T) {
  53. if Page > 1 {
  54. page_t_list = append(page_t_list, Page_T{A: 1, V: Page - 1})
  55. }
  56. i := int64(0)
  57. for aa := int64(1); aa < 5; aa++ {
  58. if Page-aa <= 0 {
  59. break
  60. }
  61. page_t_list = append(page_t_list, Page_T{A: 2, V: Page - aa})
  62. i++
  63. }
  64. page_t_list = append(page_t_list, Page_T{A: 3, V: Page})
  65. for aa := int64(1); aa < 10-i; aa++ {
  66. if Page_size < Page+aa {
  67. break
  68. }
  69. page_t_list = append(page_t_list, Page_T{A: 4, V: Page + aa})
  70. }
  71. sort.Slice(page_t_list, func(i, j int) bool {
  72. if page_t_list[i].V < page_t_list[j].V {
  73. return true
  74. }
  75. return false
  76. })
  77. sort.Slice(page_t_list, func(i, j int) bool {
  78. if page_t_list[i].A < page_t_list[j].A {
  79. return true
  80. }
  81. return false
  82. })
  83. if Page < Page_size {
  84. page_t_list = append(page_t_list, Page_T{A: 5, V: Page + 1})
  85. }
  86. return page_t_list
  87. }
  88. func Strval(value interface{}) string {
  89. var key string
  90. if value == nil {
  91. return key
  92. }
  93. switch value.(type) {
  94. case float64:
  95. ft := value.(float64)
  96. key = strconv.FormatFloat(ft, 'f', -1, 64)
  97. case float32:
  98. ft := value.(float32)
  99. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  100. case int:
  101. it := value.(int)
  102. key = strconv.Itoa(it)
  103. case uint:
  104. it := value.(uint)
  105. key = strconv.Itoa(int(it))
  106. case int8:
  107. it := value.(int8)
  108. key = strconv.Itoa(int(it))
  109. case uint8:
  110. it := value.(uint8)
  111. key = strconv.Itoa(int(it))
  112. case int16:
  113. it := value.(int16)
  114. key = strconv.Itoa(int(it))
  115. case uint16:
  116. it := value.(uint16)
  117. key = strconv.Itoa(int(it))
  118. case int32:
  119. it := value.(int32)
  120. key = strconv.Itoa(int(it))
  121. case uint32:
  122. it := value.(uint32)
  123. key = strconv.Itoa(int(it))
  124. case int64:
  125. it := value.(int64)
  126. key = strconv.FormatInt(it, 10)
  127. case uint64:
  128. it := value.(uint64)
  129. key = strconv.FormatUint(it, 10)
  130. case string:
  131. key = value.(string)
  132. case []byte:
  133. key = string(value.([]byte))
  134. default:
  135. newValue, _ := json.Marshal(value)
  136. key = string(newValue)
  137. }
  138. return key
  139. }
  140. func To_int(value interface{}) int {
  141. var key int
  142. if value == nil {
  143. return key
  144. }
  145. switch value.(type) {
  146. case float64:
  147. key = int(value.(float64))
  148. case float32:
  149. key = int(value.(float32))
  150. case int:
  151. key = int(value.(int))
  152. case uint:
  153. key = int(value.(uint))
  154. case int8:
  155. key = int(value.(int8))
  156. case uint8:
  157. key = int(value.(uint8))
  158. case int16:
  159. key = int(value.(int16))
  160. case uint16:
  161. key = int(value.(uint16))
  162. case int32:
  163. key = int(value.(int32))
  164. case uint32:
  165. key = int(value.(uint32))
  166. case int64:
  167. key = int(value.(int64))
  168. case uint64:
  169. key = int(value.(uint64))
  170. case string:
  171. key, _ = strconv.Atoi(value.(string))
  172. case []byte:
  173. key, _ = strconv.Atoi(string(value.([]byte)))
  174. default:
  175. newValue, _ := json.Marshal(value)
  176. key, _ = strconv.Atoi(string(newValue))
  177. }
  178. return key
  179. }
  180. func To_float32(value interface{}) float32 {
  181. var key float32
  182. if value == nil {
  183. return key
  184. }
  185. switch value.(type) {
  186. case float64:
  187. key = float32(value.(float64))
  188. case float32:
  189. key = float32(value.(float32))
  190. case int:
  191. key = float32(value.(int))
  192. case uint:
  193. key = float32(value.(uint))
  194. case int8:
  195. key = float32(value.(int8))
  196. case uint8:
  197. key = float32(value.(uint8))
  198. case int16:
  199. key = float32(value.(int16))
  200. case uint16:
  201. key = float32(value.(uint16))
  202. case int32:
  203. key = float32(value.(int32))
  204. case uint32:
  205. key = float32(value.(uint32))
  206. case int64:
  207. key = float32(value.(int64))
  208. case uint64:
  209. key = float32(value.(uint64))
  210. case string:
  211. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  212. key = float32(key_float64)
  213. case []byte:
  214. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  215. key = float32(key_float64)
  216. default:
  217. newValue, _ := json.Marshal(value)
  218. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  219. key = float32(key_float64)
  220. }
  221. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  222. key = float32(key_float64)
  223. return key
  224. }
  225. func To_string(value interface{}) string {
  226. var key string
  227. if value == nil {
  228. return key
  229. }
  230. switch value.(type) {
  231. case float64:
  232. ft := value.(float64)
  233. key = strconv.FormatFloat(ft, 'f', -1, 64)
  234. case float32:
  235. ft := value.(float32)
  236. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  237. case int:
  238. it := value.(int)
  239. key = strconv.Itoa(it)
  240. case uint:
  241. it := value.(uint)
  242. key = strconv.Itoa(int(it))
  243. case int8:
  244. it := value.(int8)
  245. key = strconv.Itoa(int(it))
  246. case uint8:
  247. it := value.(uint8)
  248. key = strconv.Itoa(int(it))
  249. case int16:
  250. it := value.(int16)
  251. key = strconv.Itoa(int(it))
  252. case uint16:
  253. it := value.(uint16)
  254. key = strconv.Itoa(int(it))
  255. case int32:
  256. it := value.(int32)
  257. key = strconv.Itoa(int(it))
  258. case uint32:
  259. it := value.(uint32)
  260. key = strconv.Itoa(int(it))
  261. case int64:
  262. it := value.(int64)
  263. key = strconv.FormatInt(it, 10)
  264. case uint64:
  265. it := value.(uint64)
  266. key = strconv.FormatUint(it, 10)
  267. case string:
  268. key = value.(string)
  269. case []byte:
  270. key = string(value.([]byte))
  271. default:
  272. newValue, _ := json.Marshal(value)
  273. key = string(newValue)
  274. }
  275. return key
  276. }
  277. func Random(min, max int) int {
  278. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  279. return rand.Intn(max-min) + min
  280. }
  281. // 取文本(字符串)中间
  282. func GetBetweenStr(str, start, end string) string {
  283. n := strings.Index(str, start)
  284. if n == -1 {
  285. n = 0
  286. } else {
  287. n = n + len(start) // 增加了else,不加的会把start带上
  288. }
  289. str = string([]byte(str)[n:])
  290. m := strings.Index(str, end)
  291. if m == -1 {
  292. m = len(str)
  293. }
  294. str = string([]byte(str)[:m])
  295. return str
  296. }
  297. // getYearMonthToDay 查询指定年份指定月份有多少天
  298. // @params year int 指定年份
  299. // @params month int 指定月份
  300. func GetYearMonthToDay(year int, month int) int {
  301. // 有31天的月份
  302. day31 := map[int]bool{
  303. 1: true,
  304. 3: true,
  305. 5: true,
  306. 7: true,
  307. 8: true,
  308. 10: true,
  309. 12: true,
  310. }
  311. if day31[month] == true {
  312. return 31
  313. }
  314. // 有30天的月份
  315. day30 := map[int]bool{
  316. 4: true,
  317. 6: true,
  318. 9: true,
  319. 11: true,
  320. }
  321. if day30[month] == true {
  322. return 30
  323. }
  324. // 计算是平年还是闰年
  325. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  326. // 得出2月的天数
  327. return 29
  328. }
  329. // 得出2月的天数
  330. return 28
  331. }
  332. func Decimal(value float64) float64 {
  333. value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64)
  334. return value
  335. }
  336. func Strconv_Atoi(string string) int {
  337. int, _ := strconv.Atoi(string)
  338. return int
  339. }
  340. // golang获取程序运行路径
  341. func GetCurrentDirectory() string {
  342. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  343. return strings.Replace(dir, "\\", "/", -1)
  344. }
  345. // 获取正在运行的函数名
  346. func FuncName() string {
  347. pc := make([]uintptr, 1)
  348. runtime.Callers(2, pc)
  349. f := runtime.FuncForPC(pc[0])
  350. return f.Name()
  351. }
  352. // 获取两个时间相差的天数,0表同一天,正数表t1>t2,负数表t1<t2
  353. func GetDiffDays(t1, t2 time.Time) int {
  354. t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, time.Local)
  355. t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local)
  356. return int(t1.Sub(t2).Hours() / 24)
  357. }
  358. // 获取用户登录信息
  359. func GetUserLoginInfo(ctx *context.Context) map[string]interface{} {
  360. //Ipaddr ip地址
  361. //Browser 浏览器
  362. //Os 系统
  363. //Platform 固件
  364. l := make(map[string]interface{})
  365. ua := user_agent.New(ctx.Request.UserAgent())
  366. l["ipaddr"] = ctx.Input.IP()
  367. l["remark"] = ctx.Request.UserAgent()
  368. browserName, browserVersion := ua.Browser()
  369. l["browser"] = browserName + " " + browserVersion
  370. l["os"] = ua.OS()
  371. l["platform"] = ua.Platform()
  372. return l
  373. }
  374. func StringListCompare(list1, list2 []string) (isEqual bool, common, onlyList1, onlyList2 []string) {
  375. if len(list1) != len(list2) {
  376. isEqual = false
  377. }
  378. // 创建map来存储list1列表中的元素
  379. AMap := make(map[string]int)
  380. for _, item := range list1 {
  381. AMap[item]++
  382. }
  383. // 检查B列表中的元素是否在AMap中
  384. for _, item := range list2 {
  385. if count, exists := AMap[item]; !exists || count == 0 {
  386. isEqual = false
  387. }
  388. AMap[item]--
  389. }
  390. if !isEqual {
  391. // 创建一个map来存储A列表中的元素
  392. list1Map := make(map[string]struct{})
  393. for _, a := range list1 {
  394. list1Map[a] = struct{}{}
  395. }
  396. // 查找list2列表中存在但list1列表中不存在的元素
  397. for _, v := range list2 {
  398. if _, exists := list1Map[v]; !exists {
  399. onlyList2 = append(onlyList2, v)
  400. } else {
  401. common = append(common, v)
  402. }
  403. }
  404. // 创建一个map来存储B列表中的元素
  405. list2Map := make(map[string]struct{})
  406. for _, b := range list2 {
  407. list2Map[b] = struct{}{}
  408. }
  409. // 查找list1列表中存在但list2列表中不存在的元素
  410. for _, a := range list1 {
  411. if _, exists := list2Map[a]; !exists {
  412. onlyList1 = append(onlyList1, a)
  413. }
  414. }
  415. return
  416. }
  417. isEqual = true
  418. return
  419. }
  420. func IntListCompare(list1, list2 []int) (isEqual bool, common, onlyList1, onlyList2 []int) {
  421. if len(list1) != len(list2) {
  422. isEqual = false
  423. }
  424. // 创建map来存储list1列表中的元素
  425. AMap := make(map[int]int)
  426. for _, item := range list1 {
  427. AMap[item]++
  428. }
  429. // 检查B列表中的元素是否在AMap中
  430. for _, item := range list2 {
  431. if count, exists := AMap[item]; !exists || count == 0 {
  432. isEqual = false
  433. }
  434. AMap[item]--
  435. }
  436. if !isEqual {
  437. // 创建一个map来存储A列表中的元素
  438. list1Map := make(map[int]struct{})
  439. for _, a := range list1 {
  440. list1Map[a] = struct{}{}
  441. }
  442. // 查找list2列表中存在但list1列表中不存在的元素
  443. for _, v := range list2 {
  444. if _, exists := list1Map[v]; !exists {
  445. onlyList2 = append(onlyList2, v)
  446. } else {
  447. common = append(common, v)
  448. }
  449. }
  450. // 创建一个map来存储B列表中的元素
  451. list2Map := make(map[int]struct{})
  452. for _, b := range list2 {
  453. list2Map[b] = struct{}{}
  454. }
  455. // 查找list1列表中存在但list2列表中不存在的元素
  456. for _, a := range list1 {
  457. if _, exists := list2Map[a]; !exists {
  458. onlyList1 = append(onlyList1, a)
  459. }
  460. }
  461. }
  462. isEqual = true
  463. return
  464. }
  465. func ChunkBy[T any](list []T, size int) [][]T {
  466. var chunks [][]T
  467. for size < len(list) {
  468. list, chunks = list[size:], append(chunks, list[0:size:size])
  469. }
  470. return append(chunks, list)
  471. }
  472. // GetFirstDayOfLastMonth 获取上个月第一天的日期
  473. // 返回格式: 2006-01-02
  474. func GetFirstDayOfLastMonth() string {
  475. loc, _ := time.LoadLocation(timezone)
  476. now := time.Now().In(loc)
  477. // 获取上个月
  478. lastMonth := now.AddDate(0, -1, 0)
  479. // 设置为上个月的第一天
  480. firstDay := time.Date(lastMonth.Year(), lastMonth.Month(), 1, 0, 0, 0, 0, loc)
  481. // 按照日期格式输出
  482. return firstDay.Format(dateFormat)
  483. }
  484. // GetLastDayOfLastMonth 获取上个月最后一天的日期
  485. // 返回格式: 2006-01-02
  486. func GetLastDayOfLastMonth() string {
  487. loc, _ := time.LoadLocation(timezone)
  488. now := time.Now().In(loc)
  489. // 获取本月的第一天
  490. firstDayOfCurrentMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, loc)
  491. // 本月第一天减去一天,就是上个月的最后一天
  492. lastDayOfLastMonth := firstDayOfCurrentMonth.Add(-time.Hour * 24)
  493. // 按照日期格式输出
  494. return lastDayOfLastMonth.Format(dateFormat)
  495. }