lib.go 8.1 KB

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