lib.go 8.3 KB

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