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

140 lines
2.7 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, domain, user_id, manager_id, target_type, target_id string, template_data map[string]string, sms_data []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. arg.SmsData = sms_data
  73. data_json, err := json.Marshal(arg)
  74. if err != nil {
  75. return false, err
  76. }
  77. now_int64 := time.Now().Unix()
  78. encryData := crypter.DesEn(string(data_json), DES_KEY)
  79. now := strconv.FormatInt(now_int64, 10)
  80. sign := Sign(encryData, now)
  81. req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}
  82. res := &Response{}
  83. err = conn.SendNotcie(req, res)
  84. if err != nil {
  85. log.Println("msgrpc SendNotcie error:", err)
  86. return false, err
  87. }
  88. res_data, err := GetOrgData(res)
  89. if err != nil {
  90. return false, err
  91. }
  92. if res_data == "" {
  93. return false, nil
  94. }
  95. var notice_res bool
  96. err = json.Unmarshal([]byte(res_data), &notice_res)
  97. if err != nil {
  98. return false, err
  99. }
  100. return notice_res, nil
  101. }