lib.go 9.6 KB

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