lib.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. package lib
  2. import (
  3. "ColdP_server/models/Account"
  4. "encoding/json"
  5. "fmt"
  6. beego "github.com/beego/beego/v2/server/web"
  7. "github.com/nats-io/nats.go"
  8. "log"
  9. "math"
  10. "math/rand"
  11. "os"
  12. "path/filepath"
  13. "runtime"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. var Run_My_Server = false // 运行当期服务
  20. type Cl_ struct {
  21. Uuid_list map[string]string // 泛型
  22. }
  23. var CountrySnMap map[string]Cl_ /*创建集合 */
  24. var DeviceRealSnMap map[string]int /*创建集合 */
  25. var Nats *nats.Conn
  26. func init() {
  27. CountrySnMap = make(map[string]Cl_)
  28. DeviceRealSnMap = make(map[string]int)
  29. }
  30. // PageHelper 分页结构体
  31. type PageHelper struct {
  32. //TotalCount 总记录数
  33. TotalCount int `json:"totalCount"`
  34. //TotalPage 总页数
  35. TotalPage int `json:"totalPage"`
  36. //CurrentPage 当前页
  37. CurrentPage int `json:"currentPage"`
  38. //NextPage 是否存在下一页
  39. NextPage bool `json:"nextPage"`
  40. //PreviousPage 是否存在上一页
  41. PreviousPage bool `json:"previousPage"`
  42. //List 数据
  43. List interface{} `json:"list"`
  44. }
  45. type JSONS struct {
  46. //必须的大写开头
  47. Code int16
  48. Msg string
  49. Data interface{} // 泛型
  50. }
  51. type R_JSONS_List struct {
  52. //必须的大写开头
  53. Data []interface{}
  54. Num int64
  55. Page int
  56. Page_size int
  57. }
  58. type R_JSONS struct {
  59. //必须的大写开头
  60. Data interface{}
  61. Num int64
  62. Page int
  63. Page_size int
  64. }
  65. type R1_JSONS struct {
  66. //必须的大写开头
  67. List interface{}
  68. Num int
  69. Page int
  70. Page_size int
  71. Pages []Page_T
  72. }
  73. // 登录验证
  74. //
  75. // func Verification(GetCookie string, GetString string) (bool, Account.Admin) {
  76. // Run_My_Server = true // 运行当期服务
  77. // // 自适应 参数
  78. // User_tokey := GetCookie
  79. // if len(User_tokey) == 0 {
  80. // User_tokey = GetString
  81. // }
  82. // if len(User_tokey) == 0 {
  83. // return false, Account.Admin{}
  84. // }
  85. // tokey, is := Account.Redis_Tokey_Get(User_tokey)
  86. // if !is {
  87. // return false, Account.Admin{}
  88. // }
  89. // admin_r, err := Account.Read_Admin_ByUuid(tokey)
  90. // if err != nil {
  91. // return false, Account.Admin{}
  92. // }
  93. // log.Println("登录 Admin_name 为:", admin_r.T_name)
  94. // return true, admin_r
  95. // }
  96. //
  97. // // VerificationController 登录验证 需要控制器对象
  98. //
  99. // func VerificationController(c *beego.Controller) (bool, Account.Admin) {
  100. // Run_My_Server = true // 运行当期服务
  101. // GetString := c.GetString("User_tokey")
  102. // GetCookie := c.Ctx.GetCookie("User_tokey")
  103. // // 自适应 参数
  104. // User_tokey := GetCookie
  105. // if len(User_tokey) == 0 {
  106. // User_tokey = GetString
  107. // }
  108. // if len(User_tokey) == 0 {
  109. // c.Data["json"] = JSONS{Code: 201, Msg: "User_tokey Err!"}
  110. // c.ServeJSON()
  111. // return false, Account.Admin{}
  112. // }
  113. // tokey, is := Account.Redis_Tokey_Get(User_tokey)
  114. // if !is {
  115. // c.Data["json"] = JSONS{Code: 201, Msg: "User_tokey Err!"}
  116. // c.ServeJSON()
  117. // return false, Account.Admin{}
  118. // }
  119. // admin_r, err := Account.Read_Admin_ByUuid(tokey)
  120. // if err != nil {
  121. // c.Data["json"] = JSONS{Code: 201, Msg: "User_tokey Err!"}
  122. // c.ServeJSON()
  123. // return false, Account.Admin{}
  124. // }
  125. // log.Println("登录 Admin_name 为:", admin_r.T_name)
  126. // return true, admin_r
  127. // }
  128. func Verification(GetCookie string, GetString string) (bool, Account.ColdpUser) {
  129. Run_My_Server = true // 运行当期服务
  130. // 自适应 参数
  131. User_tokey := GetCookie
  132. if len(User_tokey) == 0 {
  133. User_tokey = GetString
  134. }
  135. if len(User_tokey) == 0 {
  136. return false, Account.ColdpUser{}
  137. }
  138. tokey, is := Account.Redis_Tokey_Get(User_tokey)
  139. if !is {
  140. return false, Account.ColdpUser{}
  141. }
  142. admin_r, err := Account.ReadColdpUserByUuid(tokey)
  143. if err != nil {
  144. return false, Account.ColdpUser{}
  145. }
  146. log.Println("登录 Admin_name 为:", admin_r.T_name)
  147. return true, admin_r
  148. }
  149. // VerificationController 登录验证 需要控制器对象
  150. func VerificationController(c *beego.Controller) (bool, Account.ColdpUser) {
  151. Run_My_Server = true // 运行当期服务
  152. GetString := c.GetString("User_tokey")
  153. GetCookie := c.Ctx.GetCookie("User_tokey")
  154. // 自适应 参数
  155. User_tokey := GetCookie
  156. if len(User_tokey) == 0 {
  157. User_tokey = GetString
  158. }
  159. if len(User_tokey) == 0 {
  160. c.Data["json"] = JSONS{Code: 201, Msg: "User_tokey Err!"}
  161. c.ServeJSON()
  162. return false, Account.ColdpUser{}
  163. }
  164. tokey, is := Account.Redis_Tokey_Get(User_tokey)
  165. if !is {
  166. c.Data["json"] = JSONS{Code: 201, Msg: "User_tokey Err!"}
  167. c.ServeJSON()
  168. return false, Account.ColdpUser{}
  169. }
  170. admin_r, err := Account.ReadColdpUserByUuid(tokey)
  171. if err != nil {
  172. c.Data["json"] = JSONS{Code: 201, Msg: "User_tokey Err!"}
  173. c.ServeJSON()
  174. return false, Account.ColdpUser{}
  175. }
  176. log.Println("登录 Admin_name 为:", admin_r.T_name)
  177. return true, admin_r
  178. }
  179. // 登录验证
  180. func Verification_Tokey(GetCookie string, GetString string) bool {
  181. Run_My_Server = true // 运行当期服务
  182. // 自适应 参数
  183. User_tokey := GetCookie
  184. if len(User_tokey) == 0 {
  185. User_tokey = GetString
  186. }
  187. if len(User_tokey) == 0 {
  188. return false
  189. }
  190. _, is := Account.Redis_Tokey_Get(User_tokey)
  191. if !is {
  192. return false
  193. }
  194. return is
  195. }
  196. // func_page 分页 [{3 1} {4 2} {4 3} {4 4} {4 5} {4 6} {4 7} {4 8} {4 9} {5 2}]-
  197. type Page_T struct {
  198. A int
  199. V int64
  200. }
  201. func Func_page(Page int64, Page_size int64) (page_t_list []Page_T) {
  202. if Page > 1 {
  203. page_t_list = append(page_t_list, Page_T{A: 1, V: Page - 1})
  204. }
  205. i := int64(0)
  206. for aa := int64(1); aa < 5; aa++ {
  207. if Page-aa <= 0 {
  208. break
  209. }
  210. page_t_list = append(page_t_list, Page_T{A: 2, V: Page - aa})
  211. i++
  212. }
  213. page_t_list = append(page_t_list, Page_T{A: 3, V: Page})
  214. for aa := int64(1); aa < 10-i; aa++ {
  215. if Page_size < Page+aa {
  216. break
  217. }
  218. page_t_list = append(page_t_list, Page_T{A: 4, V: Page + aa})
  219. }
  220. sort.Slice(page_t_list, func(i, j int) bool {
  221. if page_t_list[i].V < page_t_list[j].V {
  222. return true
  223. }
  224. return false
  225. })
  226. sort.Slice(page_t_list, func(i, j int) bool {
  227. if page_t_list[i].A < page_t_list[j].A {
  228. return true
  229. }
  230. return false
  231. })
  232. if Page < Page_size {
  233. page_t_list = append(page_t_list, Page_T{A: 5, V: Page + 1})
  234. }
  235. return page_t_list
  236. }
  237. func Strval(value interface{}) string {
  238. var key string
  239. if value == nil {
  240. return key
  241. }
  242. switch value.(type) {
  243. case float64:
  244. ft := value.(float64)
  245. key = strconv.FormatFloat(ft, 'f', -1, 64)
  246. case float32:
  247. ft := value.(float32)
  248. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  249. case int:
  250. it := value.(int)
  251. key = strconv.Itoa(it)
  252. case uint:
  253. it := value.(uint)
  254. key = strconv.Itoa(int(it))
  255. case int8:
  256. it := value.(int8)
  257. key = strconv.Itoa(int(it))
  258. case uint8:
  259. it := value.(uint8)
  260. key = strconv.Itoa(int(it))
  261. case int16:
  262. it := value.(int16)
  263. key = strconv.Itoa(int(it))
  264. case uint16:
  265. it := value.(uint16)
  266. key = strconv.Itoa(int(it))
  267. case int32:
  268. it := value.(int32)
  269. key = strconv.Itoa(int(it))
  270. case uint32:
  271. it := value.(uint32)
  272. key = strconv.Itoa(int(it))
  273. case int64:
  274. it := value.(int64)
  275. key = strconv.FormatInt(it, 10)
  276. case uint64:
  277. it := value.(uint64)
  278. key = strconv.FormatUint(it, 10)
  279. case string:
  280. key = value.(string)
  281. case []byte:
  282. key = string(value.([]byte))
  283. default:
  284. newValue, _ := json.Marshal(value)
  285. key = string(newValue)
  286. }
  287. return key
  288. }
  289. func To_int(value interface{}) int {
  290. var key int
  291. if value == nil {
  292. return key
  293. }
  294. switch value.(type) {
  295. case float64:
  296. key = int(value.(float64))
  297. case float32:
  298. key = int(value.(float32))
  299. case int:
  300. key = int(value.(int))
  301. case uint:
  302. key = int(value.(uint))
  303. case int8:
  304. key = int(value.(int8))
  305. case uint8:
  306. key = int(value.(uint8))
  307. case int16:
  308. key = int(value.(int16))
  309. case uint16:
  310. key = int(value.(uint16))
  311. case int32:
  312. key = int(value.(int32))
  313. case uint32:
  314. key = int(value.(uint32))
  315. case int64:
  316. key = int(value.(int64))
  317. case uint64:
  318. key = int(value.(uint64))
  319. case string:
  320. key, _ = strconv.Atoi(value.(string))
  321. case []byte:
  322. key, _ = strconv.Atoi(string(value.([]byte)))
  323. default:
  324. newValue, _ := json.Marshal(value)
  325. key, _ = strconv.Atoi(string(newValue))
  326. }
  327. return key
  328. }
  329. func To_float32(value interface{}) float32 {
  330. var key float32
  331. if value == nil {
  332. return key
  333. }
  334. switch value.(type) {
  335. case float64:
  336. key = float32(value.(float64))
  337. case float32:
  338. key = float32(value.(float32))
  339. case int:
  340. key = float32(value.(int))
  341. case uint:
  342. key = float32(value.(uint))
  343. case int8:
  344. key = float32(value.(int8))
  345. case uint8:
  346. key = float32(value.(uint8))
  347. case int16:
  348. key = float32(value.(int16))
  349. case uint16:
  350. key = float32(value.(uint16))
  351. case int32:
  352. key = float32(value.(int32))
  353. case uint32:
  354. key = float32(value.(uint32))
  355. case int64:
  356. key = float32(value.(int64))
  357. case uint64:
  358. key = float32(value.(uint64))
  359. case string:
  360. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  361. key = float32(key_float64)
  362. case []byte:
  363. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  364. key = float32(key_float64)
  365. default:
  366. newValue, _ := json.Marshal(value)
  367. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  368. key = float32(key_float64)
  369. }
  370. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  371. key = float32(key_float64)
  372. return key
  373. }
  374. func To_string(value interface{}) string {
  375. var key string
  376. if value == nil {
  377. return key
  378. }
  379. switch value.(type) {
  380. case float64:
  381. ft := value.(float64)
  382. key = strconv.FormatFloat(ft, 'f', -1, 64)
  383. case float32:
  384. ft := value.(float32)
  385. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  386. case int:
  387. it := value.(int)
  388. key = strconv.Itoa(it)
  389. case uint:
  390. it := value.(uint)
  391. key = strconv.Itoa(int(it))
  392. case int8:
  393. it := value.(int8)
  394. key = strconv.Itoa(int(it))
  395. case uint8:
  396. it := value.(uint8)
  397. key = strconv.Itoa(int(it))
  398. case int16:
  399. it := value.(int16)
  400. key = strconv.Itoa(int(it))
  401. case uint16:
  402. it := value.(uint16)
  403. key = strconv.Itoa(int(it))
  404. case int32:
  405. it := value.(int32)
  406. key = strconv.Itoa(int(it))
  407. case uint32:
  408. it := value.(uint32)
  409. key = strconv.Itoa(int(it))
  410. case int64:
  411. it := value.(int64)
  412. key = strconv.FormatInt(it, 10)
  413. case uint64:
  414. it := value.(uint64)
  415. key = strconv.FormatUint(it, 10)
  416. case string:
  417. key = value.(string)
  418. case []byte:
  419. key = string(value.([]byte))
  420. default:
  421. newValue, _ := json.Marshal(value)
  422. key = string(newValue)
  423. }
  424. return key
  425. }
  426. func Random(min, max int) int {
  427. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  428. return rand.Intn(max-min) + min
  429. }
  430. // 取文本(字符串)中间
  431. func GetBetweenStr(str, start, end string) string {
  432. n := strings.Index(str, start)
  433. if n == -1 {
  434. n = 0
  435. } else {
  436. n = n + len(start) // 增加了else,不加的会把start带上
  437. }
  438. str = string([]byte(str)[n:])
  439. m := strings.Index(str, end)
  440. if m == -1 {
  441. m = len(str)
  442. }
  443. str = string([]byte(str)[:m])
  444. return str
  445. }
  446. // getYearMonthToDay 查询指定年份指定月份有多少天
  447. // @params year int 指定年份
  448. // @params month int 指定月份
  449. func GetYearMonthToDay(year int, month int) int {
  450. // 有31天的月份
  451. day31 := map[int]bool{
  452. 1: true,
  453. 3: true,
  454. 5: true,
  455. 7: true,
  456. 8: true,
  457. 10: true,
  458. 12: true,
  459. }
  460. if day31[month] == true {
  461. return 31
  462. }
  463. // 有30天的月份
  464. day30 := map[int]bool{
  465. 4: true,
  466. 6: true,
  467. 9: true,
  468. 11: true,
  469. }
  470. if day30[month] == true {
  471. return 30
  472. }
  473. // 计算是平年还是闰年
  474. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  475. // 得出2月的天数
  476. return 29
  477. }
  478. // 得出2月的天数
  479. return 28
  480. }
  481. func Decimal(value float64) float64 {
  482. value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64)
  483. return value
  484. }
  485. func Strconv_Atoi(string string) int {
  486. int, _ := strconv.Atoi(string)
  487. return int
  488. }
  489. // golang获取程序运行路径
  490. func GetCurrentDirectory() string {
  491. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  492. return strings.Replace(dir, "\\", "/", -1)
  493. }
  494. // 获取正在运行的函数名
  495. func FuncName() string {
  496. pc := make([]uintptr, 1)
  497. runtime.Callers(2, pc)
  498. f := runtime.FuncForPC(pc[0])
  499. return f.Name()
  500. }
  501. func SplitStringIds(str string, prefix string) (r []string) {
  502. Ids_str := strings.TrimRight(str, "|")
  503. Ids := strings.Split(Ids_str, "|")
  504. for _, v := range Ids {
  505. r = append(r, strings.TrimLeft(v, prefix))
  506. }
  507. return r
  508. }
  509. // 过滤sql特殊字符
  510. func ReplaceSQL(name string) string {
  511. name = strings.Replace(name, ";", "", -1)
  512. name = strings.Replace(name, ",", "", -1)
  513. name = strings.Replace(name, "?", "", -1)
  514. name = strings.Replace(name, "<", "", -1)
  515. name = strings.Replace(name, ">", "", -1)
  516. name = strings.Replace(name, "(", "", -1)
  517. name = strings.Replace(name, ")", "", -1)
  518. name = strings.Replace(name, "@", "", -1)
  519. name = strings.Replace(name, "=", "", -1)
  520. name = strings.Replace(name, "+", "", -1)
  521. name = strings.Replace(name, "*", "", -1)
  522. name = strings.Replace(name, "&", "", -1)
  523. name = strings.Replace(name, "#", "", -1)
  524. name = strings.Replace(name, "%", "", -1)
  525. name = strings.Replace(name, "$", "", -1)
  526. return name
  527. }
  528. func StringListToDotStr(str []string) (r string) {
  529. //for _, v := range str {
  530. // r += v + ","
  531. //}
  532. //r = strings.TrimRight(r, ",")
  533. return strings.Join(str, ",")
  534. }
  535. func IntListToDotStr(list []int) (r string) {
  536. for _, v := range list {
  537. r += fmt.Sprintf("%d,", v)
  538. }
  539. r = strings.TrimRight(r, ",")
  540. return r
  541. }
  542. func SplitStringToDotStr(str string, prefix string) (r string) {
  543. Ids_str := strings.TrimRight(str, "|")
  544. Ids := strings.Split(Ids_str, "|")
  545. for _, v := range Ids {
  546. r += strings.TrimLeft(v, prefix) + ","
  547. }
  548. r = strings.TrimRight(r, ",")
  549. return r
  550. }
  551. // ["a","b","c"] ==> "'a','b','c'"
  552. func StringListToQuotesDotStr(str []string) (r string) {
  553. for _, v := range str {
  554. r += fmt.Sprintf("'%s',", v)
  555. }
  556. r = strings.TrimRight(r, ",")
  557. return r
  558. }
  559. // 四舍五入保留小数点后几位
  560. func RoundToDecimal(num float64, decimalPlaces int) float64 {
  561. shift := math.Pow(10, float64(decimalPlaces))
  562. return math.Round(num*shift) / shift
  563. }