lib.go 12 KB

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