lib.go 9.0 KB

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