lib.go 8.6 KB

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