lib.go 9.2 KB

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