Nats.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package Nats
  2. import (
  3. "Cold_WorkOrder/conf"
  4. "Cold_WorkOrder/lib"
  5. "Cold_WorkOrder/models/Account"
  6. "fmt"
  7. "github.com/nats-io/nats.go"
  8. "github.com/vmihailenco/msgpack/v5"
  9. "time"
  10. )
  11. func Init() {
  12. fmt.Println("============Nats init============")
  13. var err error
  14. // 连接Nats服务器
  15. lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
  16. if err != nil {
  17. fmt.Println("nats 连接失败!")
  18. panic(err)
  19. }
  20. fmt.Println("nats OK!")
  21. }
  22. // 验证TOKEY
  23. func Verification(GetCookie string, GetString string) (bool, Account.User) {
  24. User_tokey := GetCookie
  25. if len(User_tokey) == 0 {
  26. User_tokey = GetString
  27. }
  28. if len(User_tokey) == 0 {
  29. return false, Account.User{}
  30. }
  31. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  32. msg, err := lib.Nats.Request("Cold_User_verification", []byte(User_tokey), 3*time.Second)
  33. if err != nil {
  34. return false, Account.User{}
  35. }
  36. fmt.Printf("Cold_User_verification : %s\n", string(msg.Data))
  37. type T_R struct {
  38. Code int16 `xml:"Code"`
  39. Msg string `xml:"Msg"`
  40. Data Account.User `xml:"Data"` // 泛型
  41. }
  42. var t_R T_R
  43. err = msgpack.Unmarshal(msg.Data, &t_R)
  44. if err != nil {
  45. return false, Account.User{}
  46. }
  47. return true, t_R.Data
  48. }
  49. func CompanyListAllByT_name(T_name string) (list []Account.Company) {
  50. msg, err := lib.Nats.Request("Cold_User_CompanyListAllByT_name", []byte(T_name), 3*time.Second)
  51. if err != nil {
  52. return list
  53. }
  54. fmt.Printf("CompanyListAllByT_name: %s\n", string(msg.Data))
  55. type T_R struct {
  56. Code int16 `xml:"Code"`
  57. Msg string `xml:"Msg"`
  58. Data []Account.Company `xml:"Data"` // 泛型
  59. }
  60. var t_R T_R
  61. err = msgpack.Unmarshal(msg.Data, &t_R)
  62. if err != nil {
  63. return list
  64. }
  65. return t_R.Data
  66. }
  67. func UserListAll() (list []Account.User) {
  68. msg, err := lib.Nats.Request("Cold_User_UserListAll", []byte(""), 3*time.Second)
  69. if err != nil {
  70. return list
  71. }
  72. fmt.Printf("UserListAll: %s\n", string(msg.Data))
  73. type T_R struct {
  74. Code int16 `xml:"Code"`
  75. Msg string `xml:"Msg"`
  76. Data []Account.User `xml:"Data"` // 泛型
  77. }
  78. var t_R T_R
  79. err = msgpack.Unmarshal(msg.Data, &t_R)
  80. if err != nil {
  81. return list
  82. }
  83. return t_R.Data
  84. }
  85. // 添加系统日志
  86. func AddSysLogs(T_class, T_title string, T_txt interface{}) {
  87. type T_S struct {
  88. T_class string
  89. T_title string
  90. T_txt interface{}
  91. }
  92. b, _ := msgpack.Marshal(&T_S{T_class: T_class, T_title: T_title, T_txt: T_txt})
  93. // 发布-订阅 模式,向 test1 发布一个 `Hello World` 数据
  94. _ = lib.Nats.Publish("Cold_User_AddSysLogs", b)
  95. }
  96. // 添加用户日志
  97. func AddUserLogs(T_uuid, T_class, T_title string, T_txt interface{}) {
  98. type T_S struct {
  99. T_uuid string
  100. T_class string
  101. T_title string
  102. T_txt interface{}
  103. }
  104. b, _ := msgpack.Marshal(&T_S{T_uuid: T_uuid, T_class: T_class, T_title: T_title, T_txt: T_txt})
  105. // 发布-订阅 模式,向 test1 发布一个 `Hello World` 数据
  106. _ = lib.Nats.Publish("Cold_User_AddUserLogs", b)
  107. }
  108. func CheckUserPermissions(Power_Id int, Req_Url string) bool {
  109. type T_Req struct {
  110. Power_Id int `xml:"Power_Id"` // 权限id
  111. Req_Url string `xml:"Req_Url"` // 请求url
  112. }
  113. t_Req := T_Req{
  114. Power_Id: Power_Id,
  115. Req_Url: Req_Url,
  116. }
  117. b, _ := msgpack.Marshal(&t_Req)
  118. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  119. msg, err := lib.Nats.Request("Cold_User_CheckUserPermissions", b, 3*time.Second)
  120. fmt.Printf("CheckUserPermissions : %s\n", string(msg.Data))
  121. if err != nil {
  122. return false
  123. }
  124. type T_R struct {
  125. Code int16 `xml:"Code"`
  126. Msg string `xml:"Msg"`
  127. Pass bool `xml:"Pass"` // 泛型
  128. }
  129. var t_R T_R
  130. err = msgpack.Unmarshal(msg.Data, &t_R)
  131. if err != nil {
  132. return false
  133. }
  134. return t_R.Pass
  135. }