Nats.go 4.7 KB

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