Nats.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package Nats
  2. import (
  3. "ERP_user/conf"
  4. "ERP_user/models/Account"
  5. "ERP_user/models/System"
  6. "fmt"
  7. powerlibs "git.baozhida.cn/ERP_libs/Power"
  8. userlibs "git.baozhida.cn/ERP_libs/User"
  9. "github.com/nats-io/nats.go"
  10. "github.com/vmihailenco/msgpack/v5"
  11. )
  12. var Nats *nats.Conn
  13. func init() {
  14. fmt.Println("============Nats init============")
  15. var err error
  16. // 连接Nats服务器
  17. Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
  18. if err != nil {
  19. fmt.Println("nats 连接失败!")
  20. panic(err)
  21. }
  22. fmt.Println("nats OK!")
  23. go NatsInit()
  24. }
  25. func NatsInit() {
  26. // 请求-响应, 响应 test3 消息。
  27. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_User_verification", func(m *nats.Msg) {
  28. fmt.Printf("ERP_User_verification message: %v\n", string(m.Data))
  29. type T_R struct {
  30. Code int16 `xml:"Code"`
  31. Msg string `xml:"Msg"`
  32. Data userlibs.User `xml:"Data"` // 泛型
  33. }
  34. var t_R T_R
  35. // 验证登录
  36. b_, admin_r := Account.Verification(string(m.Data), "")
  37. if !b_ {
  38. t_R.Code = 201
  39. t_R.Msg = "请重新登陆!"
  40. b, _ := msgpack.Marshal(&t_R)
  41. _ = Nats.Publish(m.Reply, b)
  42. return
  43. }
  44. t_R.Code = 200
  45. t_R.Msg = "ok"
  46. t_R.Data = Account.UserToUser_Libs(admin_r)
  47. b, _ := msgpack.Marshal(&t_R)
  48. _ = Nats.Publish(m.Reply, b)
  49. })
  50. // 请求-响应 获取所有用户列表
  51. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_User_Read_User_List_All", func(m *nats.Msg) {
  52. type T_R struct {
  53. Code int16 `xml:"Code"`
  54. Msg string `xml:"Msg"`
  55. Data []Account.User_R `xml:"Data"` // 泛型
  56. }
  57. var t_R T_R
  58. t_R.Code = 200
  59. t_R.Msg = "ok"
  60. t_R.Data = Account.Read_User_List_All()
  61. b, _ := msgpack.Marshal(&t_R)
  62. _ = Nats.Publish(m.Reply, b)
  63. })
  64. // 请求-响应 获取用户列表
  65. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_User_Read_User_List_T_uuid", func(m *nats.Msg) {
  66. type T_S struct {
  67. T_uuid []string
  68. T_name string
  69. Page int
  70. Page_z int
  71. }
  72. var t_S T_S
  73. type T_R struct {
  74. Code int16 `xml:"Code"`
  75. Msg string `xml:"Msg"`
  76. Data []Account.User_R `xml:"Data"`
  77. Num int64 `xml:"Num"`
  78. }
  79. var t_R T_R
  80. err := msgpack.Unmarshal(m.Data, &t_S)
  81. if err != nil {
  82. t_R.Code = 202
  83. t_R.Msg = "msgpack unmarshal err!"
  84. b, _ := msgpack.Marshal(&t_R)
  85. _ = Nats.Publish(m.Reply, b)
  86. return
  87. }
  88. t_R.Code = 200
  89. t_R.Msg = "ok"
  90. list, R_cnt := Account.Read_User_List_ByT_uuid(t_S.T_name, t_S.T_uuid, t_S.Page, t_S.Page_z)
  91. t_R.Data, t_R.Num = list, R_cnt
  92. b, _ := msgpack.Marshal(&t_R)
  93. _ = Nats.Publish(m.Reply, b)
  94. })
  95. // 请求-响应 获取所有权限列表
  96. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_User_Read_Power_List_All", func(m *nats.Msg) {
  97. type T_R struct {
  98. Code int16 `xml:"Code"`
  99. Msg string `xml:"Msg"`
  100. Count int64 `xml:"Count"`
  101. Data []powerlibs.Power_R `xml:"Data"` // 泛型
  102. }
  103. var t_R T_R
  104. t_R.Code = 200
  105. t_R.Msg = "ok"
  106. t_R.Data, t_R.Count = Account.Read_Power_List("", 0, 9999)
  107. b, _ := msgpack.Marshal(&t_R)
  108. _ = Nats.Publish(m.Reply, b)
  109. })
  110. // 发布-订阅 模式,异步订阅 系统日志
  111. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_AddSysLogs", func(m *nats.Msg) {
  112. fmt.Printf("ERP_AddSysLogs message: %v\n", string(m.Data))
  113. type T_S struct {
  114. T_class string
  115. T_title string
  116. T_txt string
  117. }
  118. var t_S T_S
  119. err := msgpack.Unmarshal(m.Data, &t_S)
  120. if err != nil {
  121. System.Add_SysLogs("Nats", "Nats AddSysLogs 解析失败", string(m.Data))
  122. return
  123. }
  124. System.Add_SysLogs(t_S.T_class, t_S.T_title, t_S.T_txt)
  125. })
  126. // 发布-订阅 模式,异步订阅 用户日志
  127. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_AddUserLogs", func(m *nats.Msg) {
  128. fmt.Printf("ERP_AddUserLogs message: %v\n", string(m.Data))
  129. type T_S struct {
  130. T_uuid string
  131. T_class string
  132. T_title string
  133. T_txt string
  134. }
  135. var t_S T_S
  136. err := msgpack.Unmarshal(m.Data, &t_S)
  137. if err != nil {
  138. System.Add_SysLogs("Nats", "Nats AddUserLogs 解析失败", string(m.Data))
  139. return
  140. }
  141. System.Add_UserLogs(t_S.T_uuid, t_S.T_class, t_S.T_title, t_S.T_txt)
  142. })
  143. // 发布-订阅 模式,异步订阅 消息
  144. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_AddNews", func(m *nats.Msg) {
  145. fmt.Printf("ERP_AddNews message: %v\n", string(m.Data))
  146. type T_S struct {
  147. T_uuid string
  148. T_Title string
  149. T_Url string
  150. }
  151. var t_S T_S
  152. err := msgpack.Unmarshal(m.Data, &t_S)
  153. if err != nil {
  154. System.Add_SysLogs("Nats", "Nats AddNews 解析失败", string(m.Data))
  155. return
  156. }
  157. _, err = System.Add_News(System.News{T_uuid: t_S.T_uuid, T_Title: t_S.T_Title, T_Url: t_S.T_Url})
  158. if err != nil {
  159. System.Add_SysLogs("Nats", "Nats AddNews 添加失败", string(m.Data))
  160. return
  161. }
  162. })
  163. }