lib.go 9.8 KB

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