123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package Nats
- import (
- "ERP_user/conf"
- "ERP_user/models/Account"
- "ERP_user/models/System"
- "fmt"
- powerlibs "git.baozhida.cn/ERP_libs/Power"
- userlibs "git.baozhida.cn/ERP_libs/User"
- "github.com/nats-io/nats.go"
- "github.com/vmihailenco/msgpack/v5"
- )
- var Nats *nats.Conn
- func init() {
- fmt.Println("============Nats init============")
- var err error
- // 连接Nats服务器
- Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
- if err != nil {
- fmt.Println("nats 连接失败!")
- panic(err)
- }
- fmt.Println("nats OK!")
- go NatsInit()
- }
- func NatsInit() {
- // 请求-响应, 响应 test3 消息。
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_User_verification", func(m *nats.Msg) {
- fmt.Printf("ERP_User_verification message: %v\n", string(m.Data))
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data userlibs.User `xml:"Data"` // 泛型
- }
- var t_R T_R
- // 验证登录
- b_, admin_r := Account.Verification(string(m.Data), "")
- if !b_ {
- t_R.Code = 201
- t_R.Msg = "请重新登陆!"
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- t_R.Code = 200
- t_R.Msg = "ok"
- t_R.Data = Account.UserToUser_Libs(admin_r)
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- // 请求-响应 获取所有用户列表
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_User_Read_User_List_All", func(m *nats.Msg) {
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data []Account.User_R `xml:"Data"` // 泛型
- }
- var t_R T_R
- t_R.Code = 200
- t_R.Msg = "ok"
- t_R.Data = Account.Read_User_List_All()
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- // 请求-响应 获取用户列表
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_User_Read_User_List_T_uuid", func(m *nats.Msg) {
- type T_S struct {
- T_uuid []string
- T_name string
- Page int
- Page_z int
- }
- var t_S T_S
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data []Account.User_R `xml:"Data"`
- Num int64 `xml:"Num"`
- }
- var t_R T_R
- err := msgpack.Unmarshal(m.Data, &t_S)
- if err != nil {
- t_R.Code = 202
- t_R.Msg = "msgpack unmarshal err!"
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- return
- }
- t_R.Code = 200
- t_R.Msg = "ok"
- list, R_cnt := Account.Read_User_List_ByT_uuid(t_S.T_name, t_S.T_uuid, t_S.Page, t_S.Page_z)
- t_R.Data, t_R.Num = list, R_cnt
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- // 请求-响应 获取所有权限列表
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_User_Read_Power_List_All", func(m *nats.Msg) {
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Count int64 `xml:"Count"`
- Data []powerlibs.Power_R `xml:"Data"` // 泛型
- }
- var t_R T_R
- t_R.Code = 200
- t_R.Msg = "ok"
- t_R.Data, t_R.Count = Account.Read_Power_List("", 0, 9999)
- b, _ := msgpack.Marshal(&t_R)
- _ = Nats.Publish(m.Reply, b)
- })
- // 发布-订阅 模式,异步订阅 系统日志
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_AddSysLogs", func(m *nats.Msg) {
- fmt.Printf("ERP_AddSysLogs message: %v\n", string(m.Data))
- type T_S struct {
- T_class string
- T_title string
- T_txt string
- }
- var t_S T_S
- err := msgpack.Unmarshal(m.Data, &t_S)
- if err != nil {
- System.Add_SysLogs("Nats", "Nats AddSysLogs 解析失败", string(m.Data))
- return
- }
- System.Add_SysLogs(t_S.T_class, t_S.T_title, t_S.T_txt)
- })
- // 发布-订阅 模式,异步订阅 用户日志
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_AddUserLogs", func(m *nats.Msg) {
- fmt.Printf("ERP_AddUserLogs message: %v\n", string(m.Data))
- type T_S struct {
- T_uuid string
- T_class string
- T_title string
- T_txt string
- }
- var t_S T_S
- err := msgpack.Unmarshal(m.Data, &t_S)
- if err != nil {
- System.Add_SysLogs("Nats", "Nats AddUserLogs 解析失败", string(m.Data))
- return
- }
- System.Add_UserLogs(t_S.T_uuid, t_S.T_class, t_S.T_title, t_S.T_txt)
- })
- // 发布-订阅 模式,异步订阅 消息
- _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+"ERP_AddNews", func(m *nats.Msg) {
- fmt.Printf("ERP_AddNews message: %v\n", string(m.Data))
- type T_S struct {
- T_uuid string
- T_Title string
- T_Url string
- }
- var t_S T_S
- err := msgpack.Unmarshal(m.Data, &t_S)
- if err != nil {
- System.Add_SysLogs("Nats", "Nats AddNews 解析失败", string(m.Data))
- return
- }
- _, err = System.Add_News(System.News{T_uuid: t_S.T_uuid, T_Title: t_S.T_Title, T_Url: t_S.T_Url})
- if err != nil {
- System.Add_SysLogs("Nats", "Nats AddNews 添加失败", string(m.Data))
- return
- }
- })
- }
|