消息服务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.

137 lines
2.9 KiB

  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, manager_id, target_type, target_id string, template_Data map[string]string, url ...string) (bool, error) {
  55. if dbname == "" || dbname == "" || manager_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.ManagerId = manager_id
  67. arg.TargetType = target_type
  68. arg.TargetId = target_id
  69. arg.TemplateData = template_Data
  70. data_json, err := json.Marshal(arg)
  71. if err != nil {
  72. return false, err
  73. }
  74. now_int64 := time.Now().Unix()
  75. encryData := crypter.DesEn(string(data_json), DES_KEY)
  76. now := strconv.FormatInt(now_int64, 10)
  77. sign := Sign(encryData, now)
  78. req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}
  79. res := &Response{}
  80. err = conn.SendNotcie(req, res)
  81. if err != nil {
  82. log.Println("msgrpc SendNotcie error:", err)
  83. return false, err
  84. }
  85. res_data, err := GetOrgData(res)
  86. if err != nil {
  87. return false, err
  88. }
  89. if res_data == "" {
  90. return false, nil
  91. }
  92. var notice_res bool
  93. err = json.Unmarshal([]byte(res_data), &notice_res)
  94. if err != nil {
  95. return false, err
  96. }
  97. return notice_res, nil
  98. }