lib.go 8.5 KB

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