lib.go 11 KB

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