NatsUser.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package NatsServer
  2. import (
  3. "Cold_GoodsOrder/lib"
  4. "Cold_GoodsOrder/models/Account"
  5. "fmt"
  6. "github.com/vmihailenco/msgpack/v5"
  7. "time"
  8. )
  9. // 验证TOKEY
  10. func Verification(GetCookie string, GetString string) (bool, Account.User) {
  11. User_tokey := GetCookie
  12. if len(User_tokey) == 0 {
  13. User_tokey = GetString
  14. }
  15. if len(User_tokey) == 0 {
  16. return false, Account.User{}
  17. }
  18. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  19. msg, err := lib.Nats.Request("Cold_User_verification", []byte(User_tokey), 3*time.Second)
  20. if err != nil {
  21. return false, Account.User{}
  22. }
  23. type T_R struct {
  24. Code int16 `xml:"Code"`
  25. Msg string `xml:"Msg"`
  26. Data Account.User `xml:"Data"` // 泛型
  27. }
  28. var t_R T_R
  29. err = msgpack.Unmarshal(msg.Data, &t_R)
  30. if err != nil || t_R.Code != 200 {
  31. return false, Account.User{}
  32. }
  33. fmt.Printf("Cold_User_verification : %+v\n", t_R)
  34. return true, t_R.Data
  35. }
  36. func CheckUserPermissions(Power_Id int, Req_Url string) bool {
  37. type T_Req struct {
  38. Power_Id int `xml:"Power_Id"` // 权限id
  39. Req_Url string `xml:"Req_Url"` // 请求url
  40. }
  41. t_Req := T_Req{
  42. Power_Id: Power_Id,
  43. Req_Url: Req_Url,
  44. }
  45. b, _ := msgpack.Marshal(&t_Req)
  46. // 请求-响应, 向 verification 发布一个 `ToKey` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  47. msg, err := lib.Nats.Request("Cold_User_CheckUserPermissions", b, 3*time.Second)
  48. //fmt.Printf("Cold_User_CheckUserPermissions : %s\n", string(msg.Data))
  49. if err != nil {
  50. return false
  51. }
  52. type T_R struct {
  53. Code int16 `xml:"Code"`
  54. Msg string `xml:"Msg"`
  55. Pass bool `xml:"Pass"` // 泛型
  56. }
  57. var t_R T_R
  58. err = msgpack.Unmarshal(msg.Data, &t_R)
  59. if err != nil {
  60. return false
  61. }
  62. fmt.Printf("Cold_User_CheckUserPermissions : %+v\n", t_R)
  63. return t_R.Pass
  64. }