NatsUser.go 2.5 KB

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