123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package Nats
- import (
- "Cold_WorkOrder/conf"
- "Cold_WorkOrder/lib"
- "Cold_WorkOrder/models/Account"
- "Cold_WorkOrder/models/WorkOrder"
- "fmt"
- "github.com/nats-io/nats.go"
- "github.com/vmihailenco/msgpack/v5"
- "time"
- )
- func Init() {
- fmt.Println("============Nats init============")
- var err error
- // 连接Nats服务器
- lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
- if err != nil {
- fmt.Println("nats 连接失败!")
- panic(err)
- }
- fmt.Println("nats OK!")
- go NatsInit()
- }
- func NatsInit() {
- // 请求-响应 获取工单总数
- _, _ = lib.Nats.Subscribe("Cold_WorkOrder_Read_WorkOrderT_State_Count", func(m *nats.Msg) {
- fmt.Printf("Cold_WorkOrder_Read_WorkOrderT_State_Count message: %s\n", string(m.Data))
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- TodayWorkOrderNum int64 // 工单数量
- UntreatedWorkOrderNum int64 // 未处理工单数量
- }
- var t_R T_R
- today := time.Now().Format("2006-01-02") + " 00:00:00"
- t_R.TodayWorkOrderNum = WorkOrder.Read_WorkOrder_ALL_T_State_Count(string(m.Data), 0, today)
- t_R.UntreatedWorkOrderNum = WorkOrder.Read_WorkOrder_ALL_T_State_Count(string(m.Data), 2, "")
- t_R.Code = 200
- t_R.Msg = "ok"
- b, _ := msgpack.Marshal(&t_R)
- _ = lib.Nats.Publish(m.Reply, b)
- })
- }
- // 验证TOKEY
- func Verification(GetCookie string, GetString string) (bool, Account.User, int) {
- User_tokey := GetCookie
- if len(User_tokey) == 0 {
- User_tokey = GetString
- }
- if len(User_tokey) == 0 {
- return false, Account.User{}, 0
- }
- // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
- msg, err := lib.Nats.Request("Cold_User_verification", []byte(User_tokey), 3*time.Second)
- if err != nil {
- return false, Account.User{}, 0
- }
- fmt.Printf("Cold_User_verification : %s\n", string(msg.Data))
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Pid int `xml:"Pid"` // 公司id
- Data Account.User `xml:"Data"` // 泛型
- }
- var t_R T_R
- err = msgpack.Unmarshal(msg.Data, &t_R)
- if err != nil {
- return false, Account.User{}, 0
- }
- return true, t_R.Data, t_R.Pid
- }
- func CompanyListAllByT_name(T_name string) (list []Account.Company) {
- msg, err := lib.Nats.Request("Cold_User_CompanyListAllByT_name", []byte(T_name), 3*time.Second)
- if err != nil {
- return list
- }
- fmt.Printf("CompanyListAllByT_name: %s\n", string(msg.Data))
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data []Account.Company `xml:"Data"` // 泛型
- }
- var t_R T_R
- err = msgpack.Unmarshal(msg.Data, &t_R)
- if err != nil {
- return list
- }
- return t_R.Data
- }
- func UserListAll() (list []Account.User) {
- msg, err := lib.Nats.Request("Cold_User_UserListAll", []byte(""), 3*time.Second)
- if err != nil {
- return list
- }
- fmt.Printf("UserListAll: %v\n", string(msg.Data))
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data []Account.User `xml:"Data"` // 泛型
- }
- var t_R T_R
- err = msgpack.Unmarshal(msg.Data, &t_R)
- if err != nil {
- return list
- }
- return t_R.Data
- }
- // 添加系统日志
- func AddSysLogs(T_class, T_title string, T_txt interface{}) {
- type T_S struct {
- T_class string
- T_title string
- T_txt interface{}
- }
- b, _ := msgpack.Marshal(&T_S{T_class: T_class, T_title: T_title, T_txt: T_txt})
- // 发布-订阅 模式,向 test1 发布一个 `Hello World` 数据
- _ = lib.Nats.Publish("Cold_User_AddSysLogs", b)
- }
- // 添加用户日志
- func AddUserLogs(T_uuid, T_class, T_title string, T_txt interface{}) {
- type T_S struct {
- T_uuid string
- T_class string
- T_title string
- T_txt interface{}
- }
- b, _ := msgpack.Marshal(&T_S{T_uuid: T_uuid, T_class: T_class, T_title: T_title, T_txt: T_txt})
- // 发布-订阅 模式,向 test1 发布一个 `Hello World` 数据
- _ = lib.Nats.Publish("Cold_User_AddUserLogs", b)
- }
- func CheckUserPermissions(Power_Id int, Req_Url string) bool {
- type T_Req struct {
- Power_Id int `xml:"Power_Id"` // 权限id
- Req_Url string `xml:"Req_Url"` // 请求url
- }
- t_Req := T_Req{
- Power_Id: Power_Id,
- Req_Url: Req_Url,
- }
- b, _ := msgpack.Marshal(&t_Req)
- // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
- msg, err := lib.Nats.Request("Cold_User_CheckUserPermissions", b, 3*time.Second)
- fmt.Printf("CheckUserPermissions : %s\n", string(msg.Data))
- if err != nil {
- return false
- }
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Pass bool `xml:"Pass"` // 泛型
- }
- var t_R T_R
- err = msgpack.Unmarshal(msg.Data, &t_R)
- if err != nil {
- return false
- }
- return t_R.Pass
- }
|