NatsUser.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package NatsServer
  2. import (
  3. "Cold_GoodsOrder/lib"
  4. "Cold_GoodsOrder/models/Account"
  5. "errors"
  6. "fmt"
  7. "github.com/astaxie/beego/logs"
  8. "github.com/vmihailenco/msgpack/v5"
  9. "log"
  10. "strconv"
  11. "time"
  12. )
  13. // 验证TOKEY
  14. func Verification(GetCookie string, GetString string) (bool, Account.User, int) {
  15. User_tokey := GetCookie
  16. if len(User_tokey) == 0 {
  17. User_tokey = GetString
  18. }
  19. if len(User_tokey) == 0 {
  20. return false, Account.User{}, 0
  21. }
  22. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  23. msg, err := lib.Nats.Request("Cold_User_verification", []byte(User_tokey), 3*time.Second)
  24. if err != nil {
  25. return false, Account.User{}, 0
  26. }
  27. fmt.Printf("Cold_User_verification : %s\n", string(msg.Data))
  28. type T_R struct {
  29. Code int16 `xml:"Code"`
  30. Msg string `xml:"Msg"`
  31. Pid int `xml:"Pid"` // 公司id
  32. Data Account.User `xml:"Data"` // 泛型
  33. }
  34. var t_R T_R
  35. err = msgpack.Unmarshal(msg.Data, &t_R)
  36. if err != nil {
  37. return false, Account.User{}, 0
  38. }
  39. return true, t_R.Data, t_R.Pid
  40. }
  41. func CheckUserPermissions(Power_Id int, Req_Url string) bool {
  42. log.Println("Power_Id:", Power_Id, "Req_Url:", Req_Url)
  43. type T_Req struct {
  44. Power_Id int `xml:"Power_Id"` // 权限id
  45. Req_Url string `xml:"Req_Url"` // 请求url
  46. }
  47. t_Req := T_Req{
  48. Power_Id: Power_Id,
  49. Req_Url: Req_Url,
  50. }
  51. b, _ := msgpack.Marshal(&t_Req)
  52. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  53. msg, err := lib.Nats.Request("Cold_User_CheckUserPermissions", b, 3*time.Second)
  54. //fmt.Printf("Cold_User_CheckUserPermissions : %s\n", string(msg.Data))
  55. if err != nil {
  56. return false
  57. }
  58. type T_R struct {
  59. Code int16 `xml:"Code"`
  60. Msg string `xml:"Msg"`
  61. Pass bool `xml:"Pass"` // 泛型
  62. }
  63. var t_R T_R
  64. err = msgpack.Unmarshal(msg.Data, &t_R)
  65. if err != nil {
  66. return false
  67. }
  68. return t_R.Pass
  69. }
  70. func Read_Company_ById(T_id int) (d Account.Company, err error) {
  71. msg, err := lib.Nats.Request("Cold_ReadCompanyByT_id", []byte(strconv.Itoa(T_id)), 3*time.Second)
  72. if err != nil {
  73. return d, err
  74. }
  75. type T_R struct {
  76. Code int16 `xml:"Code"`
  77. Msg string `xml:"Msg"`
  78. Data Account.Company `xml:"Data"` // 泛型
  79. }
  80. var t_R T_R
  81. err = msgpack.Unmarshal(msg.Data, &t_R)
  82. if err != nil {
  83. logs.Error(lib.FuncName(), err)
  84. return d, err
  85. }
  86. if t_R.Code != 200 {
  87. return d, errors.New(t_R.Msg)
  88. }
  89. return t_R.Data, nil
  90. }