消息服务RPC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. package msgrpc
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "strconv"
  7. "time"
  8. "git.tetele.net/tgo/crypter"
  9. "github.com/golang/protobuf/proto"
  10. )
  11. func SendMsg(site_id, dbname, business_id, domainname, msg_type, order_sn, memo string, data interface{}, url ...string) (*MsgSend, error) {
  12. conn, err := rpc_server_conn(url...)
  13. if err != nil {
  14. return nil, err
  15. }
  16. defer conn.Close()
  17. arg_data, err := json.Marshal(data)
  18. if err != nil {
  19. log.Println("json marshal error:", err)
  20. }
  21. arg := MsgBody{}
  22. arg.SiteId = site_id
  23. arg.Dbname = dbname
  24. arg.Platform = "1"
  25. arg.BusinessId = business_id
  26. arg.Domainname = domainname
  27. arg.Type = msg_type
  28. arg.Data = string(arg_data)
  29. arg.OrderSn = order_sn
  30. arg.Memo = memo
  31. data_json, err := json.Marshal(arg)
  32. if err != nil {
  33. return nil, err
  34. }
  35. now_int64 := time.Now().Unix()
  36. encryData := crypter.DesEn(string(data_json), DES_KEY)
  37. now := strconv.FormatInt(now_int64, 10)
  38. sign := Sign(encryData, now)
  39. req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}
  40. res := &Response{}
  41. err = conn.SendMsg(req, res)
  42. if err != nil {
  43. return nil, err
  44. }
  45. res_data, err := GetOrgData(res)
  46. var res_arr MsgSend
  47. err = json.Unmarshal([]byte(res_data), &res_arr)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return &res_arr, nil
  52. }
  53. // target_type:目标类型,商品或文章 target_id:目标ID
  54. func SendNotcie(site_id, dbname, domain, user_id, manager_id, target_type, target_id string, template_data map[string]string, url ...string) (bool, error) {
  55. if site_id == "" || dbname == "" || manager_id == "" || user_id == "" {
  56. return false, errors.New("参数错误")
  57. }
  58. conn, err := rpc_server_conn(url...)
  59. if err != nil {
  60. return false, err
  61. }
  62. defer conn.Close()
  63. arg := NoticeSend{}
  64. arg.SiteId = site_id
  65. arg.Dbname = dbname
  66. arg.Domain = domain
  67. arg.UserId = user_id
  68. arg.ManagerId = manager_id
  69. arg.TargetType = target_type
  70. arg.TargetId = target_id
  71. arg.TemplateData = template_data
  72. data_json, err := json.Marshal(arg)
  73. if err != nil {
  74. return false, err
  75. }
  76. now_int64 := time.Now().Unix()
  77. encryData := crypter.DesEn(string(data_json), DES_KEY)
  78. now := strconv.FormatInt(now_int64, 10)
  79. sign := Sign(encryData, now)
  80. req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}
  81. res := &Response{}
  82. err = conn.SendNotcie(req, res)
  83. if err != nil {
  84. log.Println("msgrpc SendNotcie error:", err)
  85. return false, err
  86. }
  87. res_data, err := GetOrgData(res)
  88. if err != nil {
  89. return false, err
  90. }
  91. if res_data == "" {
  92. return false, nil
  93. }
  94. var notice_res bool
  95. err = json.Unmarshal([]byte(res_data), &notice_res)
  96. if err != nil {
  97. return false, err
  98. }
  99. return notice_res, nil
  100. }