lib.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package lib
  2. import (
  3. "ColdVerify_server/conf"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/beego/beego/v2/core/logs"
  7. "github.com/nats-io/nats.go"
  8. "github.com/signintech/gopdf"
  9. "math/rand"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. var Nats *nats.Conn
  19. type JSONS struct {
  20. //必须的大写开头
  21. Code int16
  22. Msg string
  23. Data interface{} // 泛型
  24. }
  25. type R_JSONS struct {
  26. //必须的大写开头
  27. List interface{}
  28. Num int
  29. Page int
  30. Page_size int
  31. Pages []Page_T
  32. }
  33. type R_JSONS_s struct {
  34. //必须的大写开头
  35. List []interface{}
  36. Num int
  37. Page int
  38. Page_size int
  39. Pages []Page_T
  40. }
  41. type LOG_JSONS struct {
  42. //必须的大写开头
  43. Class_1 string
  44. Class_List interface{}
  45. List interface{}
  46. Num int
  47. Page int
  48. Page_size int
  49. Pages []Page_T
  50. }
  51. // func_page 分页 [{3 1} {4 2} {4 3} {4 4} {4 5} {4 6} {4 7} {4 8} {4 9} {5 2}]-
  52. type Page_T struct {
  53. A int
  54. V int64
  55. }
  56. func Func_page(Page int64, Page_size int64) (page_t_list []Page_T) {
  57. if Page > 1 {
  58. page_t_list = append(page_t_list, Page_T{A: 1, V: Page - 1})
  59. }
  60. i := int64(0)
  61. for aa := int64(1); aa < 5; aa++ {
  62. if Page-aa <= 0 {
  63. break
  64. }
  65. page_t_list = append(page_t_list, Page_T{A: 2, V: Page - aa})
  66. i++
  67. }
  68. page_t_list = append(page_t_list, Page_T{A: 3, V: Page})
  69. for aa := int64(1); aa < 10-i; aa++ {
  70. if Page_size < Page+aa {
  71. break
  72. }
  73. page_t_list = append(page_t_list, Page_T{A: 4, V: Page + aa})
  74. }
  75. sort.Slice(page_t_list, func(i, j int) bool {
  76. if page_t_list[i].V < page_t_list[j].V {
  77. return true
  78. }
  79. return false
  80. })
  81. sort.Slice(page_t_list, func(i, j int) bool {
  82. if page_t_list[i].A < page_t_list[j].A {
  83. return true
  84. }
  85. return false
  86. })
  87. if Page < Page_size {
  88. page_t_list = append(page_t_list, Page_T{A: 5, V: Page + 1})
  89. }
  90. return page_t_list
  91. }
  92. func Strval(value interface{}) string {
  93. var key string
  94. if value == nil {
  95. return key
  96. }
  97. switch value.(type) {
  98. case float64:
  99. ft := value.(float64)
  100. key = strconv.FormatFloat(ft, 'f', -1, 64)
  101. case float32:
  102. ft := value.(float32)
  103. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  104. case int:
  105. it := value.(int)
  106. key = strconv.Itoa(it)
  107. case uint:
  108. it := value.(uint)
  109. key = strconv.Itoa(int(it))
  110. case int8:
  111. it := value.(int8)
  112. key = strconv.Itoa(int(it))
  113. case uint8:
  114. it := value.(uint8)
  115. key = strconv.Itoa(int(it))
  116. case int16:
  117. it := value.(int16)
  118. key = strconv.Itoa(int(it))
  119. case uint16:
  120. it := value.(uint16)
  121. key = strconv.Itoa(int(it))
  122. case int32:
  123. it := value.(int32)
  124. key = strconv.Itoa(int(it))
  125. case uint32:
  126. it := value.(uint32)
  127. key = strconv.Itoa(int(it))
  128. case int64:
  129. it := value.(int64)
  130. key = strconv.FormatInt(it, 10)
  131. case uint64:
  132. it := value.(uint64)
  133. key = strconv.FormatUint(it, 10)
  134. case string:
  135. key = value.(string)
  136. case []byte:
  137. key = string(value.([]byte))
  138. default:
  139. newValue, _ := json.Marshal(value)
  140. key = string(newValue)
  141. }
  142. return key
  143. }
  144. func To_int(value interface{}) int {
  145. var key int
  146. if value == nil {
  147. return key
  148. }
  149. switch value.(type) {
  150. case float64:
  151. key = int(value.(float64))
  152. case float32:
  153. key = int(value.(float32))
  154. case int:
  155. key = int(value.(int))
  156. case uint:
  157. key = int(value.(uint))
  158. case int8:
  159. key = int(value.(int8))
  160. case uint8:
  161. key = int(value.(uint8))
  162. case int16:
  163. key = int(value.(int16))
  164. case uint16:
  165. key = int(value.(uint16))
  166. case int32:
  167. key = int(value.(int32))
  168. case uint32:
  169. key = int(value.(uint32))
  170. case int64:
  171. key = int(value.(int64))
  172. case uint64:
  173. key = int(value.(uint64))
  174. case string:
  175. key, _ = strconv.Atoi(value.(string))
  176. case []byte:
  177. key, _ = strconv.Atoi(string(value.([]byte)))
  178. default:
  179. newValue, _ := json.Marshal(value)
  180. key, _ = strconv.Atoi(string(newValue))
  181. }
  182. return key
  183. }
  184. func To_float32(value interface{}) float32 {
  185. var key float32
  186. if value == nil {
  187. return key
  188. }
  189. switch value.(type) {
  190. case float64:
  191. key = float32(value.(float64))
  192. case float32:
  193. key = float32(value.(float32))
  194. case int:
  195. key = float32(value.(int))
  196. case uint:
  197. key = float32(value.(uint))
  198. case int8:
  199. key = float32(value.(int8))
  200. case uint8:
  201. key = float32(value.(uint8))
  202. case int16:
  203. key = float32(value.(int16))
  204. case uint16:
  205. key = float32(value.(uint16))
  206. case int32:
  207. key = float32(value.(int32))
  208. case uint32:
  209. key = float32(value.(uint32))
  210. case int64:
  211. key = float32(value.(int64))
  212. case uint64:
  213. key = float32(value.(uint64))
  214. case string:
  215. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  216. key = float32(key_float64)
  217. case []byte:
  218. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  219. key = float32(key_float64)
  220. default:
  221. newValue, _ := json.Marshal(value)
  222. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  223. key = float32(key_float64)
  224. }
  225. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  226. key = float32(key_float64)
  227. return key
  228. }
  229. func To_string(value interface{}) string {
  230. var key string
  231. if value == nil {
  232. return key
  233. }
  234. switch value.(type) {
  235. case float64:
  236. ft := value.(float64)
  237. key = strconv.FormatFloat(ft, 'f', -1, 64)
  238. case float32:
  239. ft := value.(float32)
  240. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  241. case int:
  242. it := value.(int)
  243. key = strconv.Itoa(it)
  244. case uint:
  245. it := value.(uint)
  246. key = strconv.Itoa(int(it))
  247. case int8:
  248. it := value.(int8)
  249. key = strconv.Itoa(int(it))
  250. case uint8:
  251. it := value.(uint8)
  252. key = strconv.Itoa(int(it))
  253. case int16:
  254. it := value.(int16)
  255. key = strconv.Itoa(int(it))
  256. case uint16:
  257. it := value.(uint16)
  258. key = strconv.Itoa(int(it))
  259. case int32:
  260. it := value.(int32)
  261. key = strconv.Itoa(int(it))
  262. case uint32:
  263. it := value.(uint32)
  264. key = strconv.Itoa(int(it))
  265. case int64:
  266. it := value.(int64)
  267. key = strconv.FormatInt(it, 10)
  268. case uint64:
  269. it := value.(uint64)
  270. key = strconv.FormatUint(it, 10)
  271. case string:
  272. key = value.(string)
  273. case []byte:
  274. key = string(value.([]byte))
  275. default:
  276. newValue, _ := json.Marshal(value)
  277. key = string(newValue)
  278. }
  279. return key
  280. }
  281. func Random(min, max int) int {
  282. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  283. return rand.Intn(max-min) + min
  284. }
  285. // 取文本(字符串)中间
  286. func GetBetweenStr(str, start, end string) string {
  287. n := strings.Index(str, start)
  288. if n == -1 {
  289. n = 0
  290. } else {
  291. n = n + len(start) // 增加了else,不加的会把start带上
  292. }
  293. str = string([]byte(str)[n:])
  294. m := strings.Index(str, end)
  295. if m == -1 {
  296. m = len(str)
  297. }
  298. str = string([]byte(str)[:m])
  299. return str
  300. }
  301. // getYearMonthToDay 查询指定年份指定月份有多少天
  302. // @params year int 指定年份
  303. // @params month int 指定月份
  304. func GetYearMonthToDay(year int, month int) int {
  305. // 有31天的月份
  306. day31 := map[int]bool{
  307. 1: true,
  308. 3: true,
  309. 5: true,
  310. 7: true,
  311. 8: true,
  312. 10: true,
  313. 12: true,
  314. }
  315. if day31[month] == true {
  316. return 31
  317. }
  318. // 有30天的月份
  319. day30 := map[int]bool{
  320. 4: true,
  321. 6: true,
  322. 9: true,
  323. 11: true,
  324. }
  325. if day30[month] == true {
  326. return 30
  327. }
  328. // 计算是平年还是闰年
  329. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  330. // 得出2月的天数
  331. return 29
  332. }
  333. // 得出2月的天数
  334. return 28
  335. }
  336. const (
  337. AlignLeft = 4
  338. AlignCenter = 5
  339. AlignRight = 6
  340. )
  341. const (
  342. ValignTop = 1
  343. ValignMiddle = 2
  344. ValignBottom = 3
  345. )
  346. func RectFillColor(pdf *gopdf.GoPdf,
  347. text string,
  348. fontSize int,
  349. x, y, w, h float64,
  350. r, g, b uint8,
  351. align, valign int,
  352. ) {
  353. pdf.SetLineWidth(0.1)
  354. pdf.SetFillColor(r, g, b) //setup fill color
  355. pdf.SetLineType("") // 线条样式
  356. pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
  357. pdf.SetFillColor(0, 0, 0)
  358. if align == AlignCenter {
  359. textw, _ := pdf.MeasureTextWidth(text)
  360. x = x + (w / 2) - (textw / 2)
  361. } else if align == AlignRight {
  362. textw, _ := pdf.MeasureTextWidth(text)
  363. x = x + w - textw
  364. }
  365. pdf.SetX(x)
  366. if valign == ValignMiddle {
  367. y = y + (h / 2) - (float64(fontSize) / 2)
  368. } else if valign == ValignBottom {
  369. y = y + h - float64(fontSize)
  370. }
  371. pdf.SetY(y)
  372. pdf.Cell(nil, text)
  373. }
  374. func Create_Dir(path string) error {
  375. _, err := os.Stat(path)
  376. // 如果返回的错误为nil,说明文件或文件夹存在
  377. if err == nil {
  378. return nil
  379. }
  380. if err = os.Mkdir(path, 0777); err != nil {
  381. return err
  382. }
  383. return nil
  384. }
  385. // 获取正在运行的函数名
  386. func FuncName() string {
  387. pc := make([]uintptr, 1)
  388. runtime.Callers(2, pc)
  389. f := runtime.FuncForPC(pc[0])
  390. return f.Name()
  391. }
  392. // golang获取程序运行路径
  393. func GetCurrentDirectory() string {
  394. if conf.RunMode == "dev" {
  395. currentPath, err := os.Getwd()
  396. if err != nil {
  397. logs.Error("获取程序运行路径失败", err)
  398. }
  399. return currentPath
  400. }
  401. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  402. return strings.Replace(dir, "\\", "/", -1)
  403. }
  404. func ConvertMinutesToDHM(minutes int) string {
  405. if minutes == 0 {
  406. return ""
  407. }
  408. days := minutes / 1440 // 1天 = 1440分钟
  409. hours := (minutes % 1440) / 60 // 1小时 = 60分钟
  410. remainingMinutes := minutes % 60 // 剩余分钟
  411. if minutes < 60 {
  412. return fmt.Sprintf("%dm", remainingMinutes)
  413. }
  414. if minutes < 1440 && minutes >= 60 {
  415. return fmt.Sprintf("%dh%dm", hours, remainingMinutes)
  416. }
  417. return fmt.Sprintf("%dd%dh%dm", days, hours, remainingMinutes)
  418. }