lib.go 8.6 KB

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