lib.go 9.6 KB

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