lib.go 8.9 KB

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