lib.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. package lib
  2. import (
  3. "ColdVerify_local/logs"
  4. "ColdVerify_local/models/Account"
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "github.com/beego/beego/v2/server/web/context"
  10. "github.com/nats-io/nats.go"
  11. "github.com/signintech/gopdf"
  12. "io/ioutil"
  13. "log"
  14. "math/rand"
  15. "os"
  16. "os/exec"
  17. "runtime"
  18. "sort"
  19. "strconv"
  20. "strings"
  21. "time"
  22. "unicode"
  23. )
  24. var Nats *nats.Conn
  25. type JSONS struct {
  26. //必须的大写开头
  27. Code int16
  28. Msg string
  29. Data interface{} // 泛型
  30. }
  31. type JSONSSE struct {
  32. State int8 // 状态 0 进行中 1 成功 2 失败
  33. Msg string // 内容
  34. }
  35. type R_JSONS struct {
  36. //必须的大写开头
  37. List interface{}
  38. Num int
  39. Page int
  40. Page_size int
  41. Pages []Page_T
  42. }
  43. type R_JSONS_s struct {
  44. //必须的大写开头
  45. List []interface{}
  46. Num int
  47. Page int
  48. Page_size int
  49. Pages []Page_T
  50. }
  51. type LOG_JSONS struct {
  52. //必须的大写开头
  53. Class_1 string
  54. Class_List interface{}
  55. List interface{}
  56. Num int
  57. Page int
  58. Page_size int
  59. Pages []Page_T
  60. }
  61. // 获取登录用户uuid
  62. func GetAdminT_Uuid(GetCookie string, GetString string) (string, bool) {
  63. // 自适应 参数
  64. User_tokey := GetCookie
  65. if len(User_tokey) == 0 {
  66. User_tokey = GetString
  67. }
  68. if len(User_tokey) == 0 {
  69. return "", false
  70. }
  71. // 判断 tokey 是否存在
  72. tokey, is := Account.Read_Tokey(User_tokey)
  73. if !is {
  74. return "", false
  75. }
  76. log.Println("登录 User_uuid 为:", tokey)
  77. return tokey, true
  78. }
  79. // func_page 分页 [{3 1} {4 2} {4 3} {4 4} {4 5} {4 6} {4 7} {4 8} {4 9} {5 2}]-
  80. type Page_T struct {
  81. A int
  82. V int64
  83. }
  84. func Func_page(Page int64, Page_size int64) (page_t_list []Page_T) {
  85. if Page > 1 {
  86. page_t_list = append(page_t_list, Page_T{A: 1, V: Page - 1})
  87. }
  88. i := int64(0)
  89. for aa := int64(1); aa < 5; aa++ {
  90. if Page-aa <= 0 {
  91. break
  92. }
  93. page_t_list = append(page_t_list, Page_T{A: 2, V: Page - aa})
  94. i++
  95. }
  96. page_t_list = append(page_t_list, Page_T{A: 3, V: Page})
  97. for aa := int64(1); aa < 10-i; aa++ {
  98. if Page_size < Page+aa {
  99. break
  100. }
  101. page_t_list = append(page_t_list, Page_T{A: 4, V: Page + aa})
  102. }
  103. sort.Slice(page_t_list, func(i, j int) bool {
  104. if page_t_list[i].V < page_t_list[j].V {
  105. return true
  106. }
  107. return false
  108. })
  109. sort.Slice(page_t_list, func(i, j int) bool {
  110. if page_t_list[i].A < page_t_list[j].A {
  111. return true
  112. }
  113. return false
  114. })
  115. if Page < Page_size {
  116. page_t_list = append(page_t_list, Page_T{A: 5, V: Page + 1})
  117. }
  118. return page_t_list
  119. }
  120. func Strval(value interface{}) string {
  121. var key string
  122. if value == nil {
  123. return key
  124. }
  125. switch value.(type) {
  126. case float64:
  127. ft := value.(float64)
  128. key = strconv.FormatFloat(ft, 'f', -1, 64)
  129. case float32:
  130. ft := value.(float32)
  131. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  132. case int:
  133. it := value.(int)
  134. key = strconv.Itoa(it)
  135. case uint:
  136. it := value.(uint)
  137. key = strconv.Itoa(int(it))
  138. case int8:
  139. it := value.(int8)
  140. key = strconv.Itoa(int(it))
  141. case uint8:
  142. it := value.(uint8)
  143. key = strconv.Itoa(int(it))
  144. case int16:
  145. it := value.(int16)
  146. key = strconv.Itoa(int(it))
  147. case uint16:
  148. it := value.(uint16)
  149. key = strconv.Itoa(int(it))
  150. case int32:
  151. it := value.(int32)
  152. key = strconv.Itoa(int(it))
  153. case uint32:
  154. it := value.(uint32)
  155. key = strconv.Itoa(int(it))
  156. case int64:
  157. it := value.(int64)
  158. key = strconv.FormatInt(it, 10)
  159. case uint64:
  160. it := value.(uint64)
  161. key = strconv.FormatUint(it, 10)
  162. case string:
  163. key = value.(string)
  164. case []byte:
  165. key = string(value.([]byte))
  166. default:
  167. newValue, _ := json.Marshal(value)
  168. key = string(newValue)
  169. }
  170. return key
  171. }
  172. func To_int(value interface{}) int {
  173. var key int
  174. if value == nil {
  175. return key
  176. }
  177. switch value.(type) {
  178. case float64:
  179. key = int(value.(float64))
  180. case float32:
  181. key = int(value.(float32))
  182. case int:
  183. key = int(value.(int))
  184. case uint:
  185. key = int(value.(uint))
  186. case int8:
  187. key = int(value.(int8))
  188. case uint8:
  189. key = int(value.(uint8))
  190. case int16:
  191. key = int(value.(int16))
  192. case uint16:
  193. key = int(value.(uint16))
  194. case int32:
  195. key = int(value.(int32))
  196. case uint32:
  197. key = int(value.(uint32))
  198. case int64:
  199. key = int(value.(int64))
  200. case uint64:
  201. key = int(value.(uint64))
  202. case string:
  203. key, _ = strconv.Atoi(value.(string))
  204. case []byte:
  205. key, _ = strconv.Atoi(string(value.([]byte)))
  206. default:
  207. newValue, _ := json.Marshal(value)
  208. key, _ = strconv.Atoi(string(newValue))
  209. }
  210. return key
  211. }
  212. func To_float32(value interface{}) float32 {
  213. var key float32
  214. if value == nil {
  215. return key
  216. }
  217. switch value.(type) {
  218. case float64:
  219. key = float32(value.(float64))
  220. case float32:
  221. key = float32(value.(float32))
  222. case int:
  223. key = float32(value.(int))
  224. case uint:
  225. key = float32(value.(uint))
  226. case int8:
  227. key = float32(value.(int8))
  228. case uint8:
  229. key = float32(value.(uint8))
  230. case int16:
  231. key = float32(value.(int16))
  232. case uint16:
  233. key = float32(value.(uint16))
  234. case int32:
  235. key = float32(value.(int32))
  236. case uint32:
  237. key = float32(value.(uint32))
  238. case int64:
  239. key = float32(value.(int64))
  240. case uint64:
  241. key = float32(value.(uint64))
  242. case string:
  243. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  244. key = float32(key_float64)
  245. case []byte:
  246. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  247. key = float32(key_float64)
  248. default:
  249. newValue, _ := json.Marshal(value)
  250. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  251. key = float32(key_float64)
  252. }
  253. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  254. key = float32(key_float64)
  255. return key
  256. }
  257. // StringToFloat32 将字符串转换为 float32,支持数字、小数点和负数
  258. func StringToFloat32(s string) (float32, error) {
  259. cleanedStr := ""
  260. for i, char := range s {
  261. // 允许数字、小数点,以及第一个字符是负号
  262. if unicode.IsDigit(char) || char == '.' || (char == '-' && i == 0) {
  263. cleanedStr += string(char)
  264. }
  265. }
  266. // 检查字符串是否为空(例如输入是 "abc")
  267. if cleanedStr == "" {
  268. return 0, fmt.Errorf("无效的数字格式: %s", s)
  269. }
  270. // 转换为 float32
  271. f, err := strconv.ParseFloat(cleanedStr, 32)
  272. if err != nil {
  273. return 0, fmt.Errorf("转换失败: %v", err)
  274. }
  275. return float32(f), nil
  276. }
  277. func To_string(value interface{}) string {
  278. var key string
  279. if value == nil {
  280. return key
  281. }
  282. switch value.(type) {
  283. case float64:
  284. ft := value.(float64)
  285. key = strconv.FormatFloat(ft, 'f', -1, 64)
  286. case float32:
  287. ft := value.(float32)
  288. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  289. case int:
  290. it := value.(int)
  291. key = strconv.Itoa(it)
  292. case uint:
  293. it := value.(uint)
  294. key = strconv.Itoa(int(it))
  295. case int8:
  296. it := value.(int8)
  297. key = strconv.Itoa(int(it))
  298. case uint8:
  299. it := value.(uint8)
  300. key = strconv.Itoa(int(it))
  301. case int16:
  302. it := value.(int16)
  303. key = strconv.Itoa(int(it))
  304. case uint16:
  305. it := value.(uint16)
  306. key = strconv.Itoa(int(it))
  307. case int32:
  308. it := value.(int32)
  309. key = strconv.Itoa(int(it))
  310. case uint32:
  311. it := value.(uint32)
  312. key = strconv.Itoa(int(it))
  313. case int64:
  314. it := value.(int64)
  315. key = strconv.FormatInt(it, 10)
  316. case uint64:
  317. it := value.(uint64)
  318. key = strconv.FormatUint(it, 10)
  319. case string:
  320. key = value.(string)
  321. case []byte:
  322. key = string(value.([]byte))
  323. default:
  324. newValue, _ := json.Marshal(value)
  325. key = string(newValue)
  326. }
  327. return key
  328. }
  329. func Random(min, max int) int {
  330. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  331. return rand.Intn(max-min) + min
  332. }
  333. // 取文本(字符串)中间
  334. func GetBetweenStr(str, start, end string) string {
  335. n := strings.Index(str, start)
  336. if n == -1 {
  337. n = 0
  338. } else {
  339. n = n + len(start) // 增加了else,不加的会把start带上
  340. }
  341. str = string([]byte(str)[n:])
  342. m := strings.Index(str, end)
  343. if m == -1 {
  344. m = len(str)
  345. }
  346. str = string([]byte(str)[:m])
  347. return str
  348. }
  349. // getYearMonthToDay 查询指定年份指定月份有多少天
  350. // @params year int 指定年份
  351. // @params month int 指定月份
  352. func GetYearMonthToDay(year int, month int) int {
  353. // 有31天的月份
  354. day31 := map[int]bool{
  355. 1: true,
  356. 3: true,
  357. 5: true,
  358. 7: true,
  359. 8: true,
  360. 10: true,
  361. 12: true,
  362. }
  363. if day31[month] == true {
  364. return 31
  365. }
  366. // 有30天的月份
  367. day30 := map[int]bool{
  368. 4: true,
  369. 6: true,
  370. 9: true,
  371. 11: true,
  372. }
  373. if day30[month] == true {
  374. return 30
  375. }
  376. // 计算是平年还是闰年
  377. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  378. // 得出2月的天数
  379. return 29
  380. }
  381. // 得出2月的天数
  382. return 28
  383. }
  384. const (
  385. AlignLeft = 4
  386. AlignCenter = 5
  387. AlignRight = 6
  388. )
  389. const (
  390. ValignTop = 1
  391. ValignMiddle = 2
  392. ValignBottom = 3
  393. )
  394. func RectFillColor(pdf *gopdf.GoPdf,
  395. text string,
  396. fontSize int,
  397. x, y, w, h float64,
  398. r, g, b uint8,
  399. align, valign int,
  400. ) {
  401. pdf.SetLineWidth(0.1)
  402. pdf.SetFillColor(r, g, b) //setup fill color
  403. pdf.SetLineType("") // 线条样式
  404. pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
  405. pdf.SetFillColor(0, 0, 0)
  406. if align == AlignCenter {
  407. textw, _ := pdf.MeasureTextWidth(text)
  408. x = x + (w / 2) - (textw / 2)
  409. } else if align == AlignRight {
  410. textw, _ := pdf.MeasureTextWidth(text)
  411. x = x + w - textw
  412. }
  413. pdf.SetX(x)
  414. if valign == ValignMiddle {
  415. y = y + (h / 2) - (float64(fontSize) / 2)
  416. } else if valign == ValignBottom {
  417. y = y + h - float64(fontSize)
  418. }
  419. pdf.SetY(y)
  420. pdf.Cell(nil, text)
  421. }
  422. func Command(arg ...string) (result string, err error) {
  423. name := "/bin/bash"
  424. c := "-c"
  425. // 根据系统设定不同的命令name
  426. if runtime.GOOS == "windows" {
  427. name = "cmd"
  428. c = "/C"
  429. }
  430. arg = append([]string{c}, arg...)
  431. cmd := exec.Command(name, arg...)
  432. //var out bytes.Buffer
  433. //cmd.Stdout = &out
  434. var stderr bytes.Buffer
  435. cmd.Stderr = &stderr
  436. //创建获取命令输出管道
  437. stdout, err := cmd.StdoutPipe()
  438. if err != nil {
  439. logs.Println("Error:can not obtain stdout pipe for command:%s\n", err)
  440. return result, err
  441. }
  442. //执行命令
  443. if err = cmd.Start(); err != nil {
  444. logs.Println("Error:The command is err,", err)
  445. return result, err
  446. }
  447. //读取所有输出
  448. res, err := ioutil.ReadAll(stdout)
  449. if err != nil {
  450. logs.Println("ReadAll Stdout:", err.Error())
  451. return result, err
  452. }
  453. if err = cmd.Wait(); err != nil {
  454. e := errors.New(fmt.Sprintf("wait:%s:%s", err.Error(), stderr.String()))
  455. logs.Println(e.Error())
  456. return result, e
  457. }
  458. result = string(res)
  459. return result, nil
  460. }
  461. func Create_Dir(path string) error {
  462. _, err := os.Stat(path)
  463. // 如果返回的错误为nil,说明文件或文件夹存在
  464. if err == nil {
  465. return nil
  466. }
  467. if err = os.Mkdir(path, 0777); err != nil {
  468. return err
  469. }
  470. return nil
  471. }
  472. // 获取正在运行的函数名
  473. func FuncName() string {
  474. pc := make([]uintptr, 1)
  475. runtime.Callers(2, pc)
  476. f := runtime.FuncForPC(pc[0])
  477. return f.Name()
  478. }
  479. // 发送 数据
  480. func SseWriteJSON(client *context.Response, msg JSONSSE) error {
  481. datajson, _ := json.Marshal(msg)
  482. // Send data to the client
  483. client.Write(append(append([]byte("data: "), datajson...), []byte("\n\n")...))
  484. //client.Write([]byte("\n"))
  485. logs.Println("SseWriteJSON:", string(datajson))
  486. // Flush the response writer to send the data immediately
  487. client.Flush()
  488. return nil
  489. }
  490. // 格式化为保留两位小数的字符串
  491. func To2(number float32) string {
  492. formatted := fmt.Sprintf("%.2f", number)
  493. //fmt.Println(formatted) // 输出: 3.14
  494. return formatted
  495. }