lib.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. package lib
  2. import (
  3. "Yunlot/lib/TcpServerLib"
  4. "Yunlot/logs"
  5. "Yunlot/models/Account"
  6. "bytes"
  7. "crypto/sha256"
  8. "encoding/hex"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/nats-io/nats.go"
  12. "github.com/thinkeridea/go-extend/exunicode/exutf8"
  13. "github.com/yosssi/gmq/mqtt"
  14. "github.com/yosssi/gmq/mqtt/client"
  15. "golang.org/x/text/encoding/simplifiedchinese"
  16. "golang.org/x/text/transform"
  17. "io/ioutil"
  18. "math/rand"
  19. "plugin"
  20. "reflect"
  21. "runtime"
  22. "strconv"
  23. "strings"
  24. "time"
  25. "unicode/utf8"
  26. )
  27. var Nats *nats.Conn
  28. var MqttClient *client.Client
  29. var TopicMap map[string]bool
  30. var PluginMap map[string]*plugin.Plugin
  31. var TcpMap map[string]*TcpServerLib.Session
  32. func init() {
  33. TopicMap = make(map[string]bool)
  34. PluginMap = make(map[string]*plugin.Plugin)
  35. TcpMap = make(map[string]*TcpServerLib.Session)
  36. }
  37. const Success = 200
  38. const Error = 201
  39. type JSONR struct {
  40. //必须的大写开头
  41. Code int16
  42. Msg string
  43. Data interface{} // 泛型
  44. }
  45. type JSONS struct {
  46. //必须的大写开头
  47. List interface{}
  48. Total int16
  49. PageIndex int
  50. PageSize int
  51. }
  52. func C_Page(list interface{}, PageIndex, PageSize int, Total int64) (Jsons JSONS) {
  53. Jsons.List = list
  54. Jsons.Total = int16(Total)
  55. Jsons.PageIndex = PageIndex
  56. Jsons.PageSize = PageSize
  57. //Jsons.PageSize = int16(math.Ceil(float64(Total) / float64(PageSize)))
  58. return Jsons
  59. }
  60. func Strval(value interface{}) string {
  61. var key string
  62. if value == nil {
  63. return key
  64. }
  65. switch value.(type) {
  66. case float64:
  67. ft := value.(float64)
  68. key = strconv.FormatFloat(ft, 'f', -1, 64)
  69. case float32:
  70. ft := value.(float32)
  71. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  72. case int:
  73. it := value.(int)
  74. key = strconv.Itoa(it)
  75. case uint:
  76. it := value.(uint)
  77. key = strconv.Itoa(int(it))
  78. case int8:
  79. it := value.(int8)
  80. key = strconv.Itoa(int(it))
  81. case uint8:
  82. it := value.(uint8)
  83. key = strconv.Itoa(int(it))
  84. case int16:
  85. it := value.(int16)
  86. key = strconv.Itoa(int(it))
  87. case uint16:
  88. it := value.(uint16)
  89. key = strconv.Itoa(int(it))
  90. case int32:
  91. it := value.(int32)
  92. key = strconv.Itoa(int(it))
  93. case uint32:
  94. it := value.(uint32)
  95. key = strconv.Itoa(int(it))
  96. case int64:
  97. it := value.(int64)
  98. key = strconv.FormatInt(it, 10)
  99. case uint64:
  100. it := value.(uint64)
  101. key = strconv.FormatUint(it, 10)
  102. case string:
  103. key = value.(string)
  104. case []byte:
  105. key = string(value.([]byte))
  106. default:
  107. newValue, _ := json.Marshal(value)
  108. key = string(newValue)
  109. }
  110. return key
  111. }
  112. func To_int(value interface{}) int {
  113. var key int
  114. if value == nil {
  115. return key
  116. }
  117. switch value.(type) {
  118. case float64:
  119. key = int(value.(float64))
  120. case float32:
  121. key = int(value.(float32))
  122. case int:
  123. key = int(value.(int))
  124. case uint:
  125. key = int(value.(uint))
  126. case int8:
  127. key = int(value.(int8))
  128. case uint8:
  129. key = int(value.(uint8))
  130. case int16:
  131. key = int(value.(int16))
  132. case uint16:
  133. key = int(value.(uint16))
  134. case int32:
  135. key = int(value.(int32))
  136. case uint32:
  137. key = int(value.(uint32))
  138. case int64:
  139. key = int(value.(int64))
  140. case uint64:
  141. key = int(value.(uint64))
  142. case string:
  143. key, _ = strconv.Atoi(value.(string))
  144. case []byte:
  145. key, _ = strconv.Atoi(string(value.([]byte)))
  146. default:
  147. newValue, _ := json.Marshal(value)
  148. key, _ = strconv.Atoi(string(newValue))
  149. }
  150. return key
  151. }
  152. func To_int64(value interface{}) int64 {
  153. var key int64
  154. if value == nil {
  155. return key
  156. }
  157. switch value.(type) {
  158. case float64:
  159. key = int64(value.(float64))
  160. case float32:
  161. key = int64(value.(float32))
  162. case int:
  163. key = int64(value.(int))
  164. case uint:
  165. key = int64(value.(uint))
  166. case int8:
  167. key = int64(value.(int8))
  168. case uint8:
  169. key = int64(value.(uint8))
  170. case int16:
  171. key = int64(value.(int16))
  172. case uint16:
  173. key = int64(value.(uint16))
  174. case int32:
  175. key = int64(value.(int32))
  176. case uint32:
  177. key = int64(value.(uint32))
  178. case int64:
  179. key = int64(value.(int64))
  180. case uint64:
  181. key = int64(value.(uint64))
  182. case string:
  183. key, _ = strconv.ParseInt(value.(string), 10, 64)
  184. case []byte:
  185. key, _ = strconv.ParseInt(string(value.([]byte)), 10, 64)
  186. default:
  187. newValue, _ := json.Marshal(value)
  188. key, _ = strconv.ParseInt(string(newValue), 10, 64)
  189. }
  190. return key
  191. }
  192. func To_float32(value interface{}) float32 {
  193. var key float32
  194. if value == nil {
  195. return key
  196. }
  197. switch value.(type) {
  198. case float64:
  199. key = float32(value.(float64))
  200. case float32:
  201. key = float32(value.(float32))
  202. case int:
  203. key = float32(value.(int))
  204. case uint:
  205. key = float32(value.(uint))
  206. case int8:
  207. key = float32(value.(int8))
  208. case uint8:
  209. key = float32(value.(uint8))
  210. case int16:
  211. key = float32(value.(int16))
  212. case uint16:
  213. key = float32(value.(uint16))
  214. case int32:
  215. key = float32(value.(int32))
  216. case uint32:
  217. key = float32(value.(uint32))
  218. case int64:
  219. key = float32(value.(int64))
  220. case uint64:
  221. key = float32(value.(uint64))
  222. case string:
  223. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  224. key = float32(key_float64)
  225. case []byte:
  226. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  227. key = float32(key_float64)
  228. default:
  229. newValue, _ := json.Marshal(value)
  230. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  231. key = float32(key_float64)
  232. }
  233. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  234. key = float32(key_float64)
  235. return key
  236. }
  237. func To_string(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 Random(min, max int) int {
  290. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  291. return rand.Intn(max-min) + min
  292. }
  293. // 取文本(字符串)中间
  294. func GetBetweenStr(str, start, end string) string {
  295. n := strings.Index(str, start)
  296. if n == -1 {
  297. n = 0
  298. } else {
  299. n = n + len(start) // 增加了else,不加的会把start带上
  300. }
  301. str = string([]byte(str)[n:])
  302. m := strings.Index(str, end)
  303. if m == -1 {
  304. m = len(str)
  305. }
  306. str = string([]byte(str)[:m])
  307. return str
  308. }
  309. // getYearMonthToDay 查询指定年份指定月份有多少天
  310. // @params year int 指定年份
  311. // @params month int 指定月份
  312. func GetYearMonthToDay(year int, month int) int {
  313. // 有31天的月份
  314. day31 := map[int]bool{
  315. 1: true,
  316. 3: true,
  317. 5: true,
  318. 7: true,
  319. 8: true,
  320. 10: true,
  321. 12: true,
  322. }
  323. if day31[month] == true {
  324. return 31
  325. }
  326. // 有30天的月份
  327. day30 := map[int]bool{
  328. 4: true,
  329. 6: true,
  330. 9: true,
  331. 11: true,
  332. }
  333. if day30[month] == true {
  334. return 30
  335. }
  336. // 计算是平年还是闰年
  337. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  338. // 得出2月的天数
  339. return 29
  340. }
  341. // 得出2月的天数
  342. return 28
  343. }
  344. func Decimal(value float64) float64 {
  345. value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64)
  346. return value
  347. }
  348. func Strconv_Atoi(string string) int {
  349. int, _ := strconv.Atoi(string)
  350. return int
  351. }
  352. // 获取正在运行的函数名
  353. func FuncName() string {
  354. pc := make([]uintptr, 1)
  355. runtime.Callers(2, pc)
  356. f := runtime.FuncForPC(pc[0])
  357. return f.Name()
  358. }
  359. func Limit_len(str string, lenx int) string {
  360. if utf8.RuneCountInString(str) > lenx {
  361. return exutf8.RuneSubString(str, 0, lenx-3) + "..."
  362. }
  363. return exutf8.RuneSubString(str, 0, lenx)
  364. }
  365. func Utf8ToGbk(s []byte) ([]byte, error) {
  366. reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
  367. d, e := ioutil.ReadAll(reader)
  368. if e != nil {
  369. return nil, e
  370. }
  371. return d, nil
  372. }
  373. // #取得随机字符串:通过打乱slice来操作
  374. func GetRandstring(length int, char string, rand_x int64) string {
  375. if length < 1 {
  376. return ""
  377. }
  378. if len(char) <= 6 || len(char) <= length {
  379. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  380. }
  381. charArr := strings.Split(char, "")
  382. ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
  383. l := len(charArr)
  384. for i := l - 1; i > 0; i-- {
  385. r := ran.Intn(i)
  386. charArr[r], charArr[i] = charArr[i], charArr[r]
  387. }
  388. rchar := charArr[:length]
  389. return strings.Join(rchar, "")
  390. }
  391. // Sha256加密
  392. func Sha256(src string) string {
  393. m := sha256.New()
  394. m.Write([]byte(src))
  395. res := hex.EncodeToString(m.Sum(nil))
  396. return res
  397. }
  398. // 判断时间是当年的第几周
  399. func WeekByDate() string {
  400. t := time.Now()
  401. yearDay := t.YearDay()
  402. yearFirstDay := t.AddDate(0, 0, -yearDay+1)
  403. firstDayInWeek := int(yearFirstDay.Weekday())
  404. //今年第一周有几天
  405. firstWeekDays := 1
  406. if firstDayInWeek != 0 {
  407. firstWeekDays = 7 - firstDayInWeek + 1
  408. }
  409. var week int
  410. if yearDay <= firstWeekDays {
  411. week = 1
  412. } else {
  413. week = (yearDay-firstWeekDays)/7 + 2
  414. }
  415. return fmt.Sprintf("%d%02d", t.Year(), week) // 202253
  416. }
  417. // 登录验证
  418. func Verification(GetCookie string, GetString string) (bool, Account.User) {
  419. // 自适应 参数
  420. User_tokey := GetCookie
  421. if len(User_tokey) == 0 {
  422. User_tokey = GetString
  423. }
  424. if len(User_tokey) == 0 {
  425. return false, Account.User{}
  426. }
  427. T_uuid, is := Account.Redis_Tokey_Get(User_tokey)
  428. if !is {
  429. return false, Account.User{}
  430. }
  431. admin_r := Account.User{T_uuid: T_uuid}
  432. if !admin_r.Read() {
  433. return false, Account.User{}
  434. }
  435. //log.Println("登录 Admin_name 为:", admin_r.Admin_name)
  436. return true, admin_r
  437. }
  438. // 发送数据
  439. func Mqtt_publish(topic string, b []byte) {
  440. TopicMap[topic] = true
  441. // Publish a message.
  442. err := MqttClient.Publish(&client.PublishOptions{
  443. QoS: mqtt.QoS0,
  444. TopicName: []byte(topic),
  445. Message: b,
  446. })
  447. logs.PrintlnMqtt("Mqtt-> " + topic + ":" + string(b))
  448. if err != nil {
  449. logs.PrintlnError("MqttServer", "发送消息失败 [Mqtt_publish]", "-> "+topic+" "+string(b))
  450. }
  451. }
  452. // 获取json指定参数 [AAAA BBBB],0,JSON !!!如果失败,返回已找到的全部json
  453. func Json_key(topicNameList []string, topicNameI int, articleSlide map[string]interface{}) interface{} {
  454. if len(topicNameList) > topicNameI {
  455. a, is := articleSlide[topicNameList[topicNameI]]
  456. if !is {
  457. return articleSlide
  458. }
  459. if reflect.TypeOf(a).String() == "map[string]interface {}" {
  460. return Json_key(topicNameList, topicNameI+1, a.(map[string]interface{}))
  461. }
  462. }
  463. if len(topicNameList) == topicNameI {
  464. return articleSlide
  465. }
  466. return articleSlide[topicNameList[topicNameI]]
  467. }
  468. // 发送数据
  469. func TCP_publish(T_sn string, byte_r []byte) {
  470. logs.PrintlnMqtt("TCP-> " + T_sn + ":" + string(byte_r))
  471. if v, is := TcpMap[T_sn]; is {
  472. err := v.Send(string(byte_r))
  473. if err != nil {
  474. logs.PrintlnError("TCP", "发送消息失败 [TCP_publish]", "-> "+T_sn+" "+string(byte_r))
  475. }
  476. } else {
  477. logs.PrintlnError("TCP", "TcpMap[T_sn] 不存在,发送消息失败 [TCP_publish]", "-> "+T_sn+" "+string(byte_r))
  478. }
  479. }