lib.go 8.4 KB

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