lib.go 9.0 KB

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