lib.go 8.7 KB

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