lib.go 7.9 KB

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