lib.go 8.7 KB

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