lib.go 11 KB

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