商品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.

108 lines
2.1 KiB

  1. package productrpc
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strconv"
  6. "time"
  7. "git.tetele.net/tgo/crypter"
  8. "github.com/golang/protobuf/proto"
  9. )
  10. var DES_KEY = "pro%78gd"
  11. func SetResData(data interface{}, res *Response) {
  12. res_data_json, err := json.Marshal(data)
  13. if err == nil {
  14. encryData := crypter.DesEn(string(res_data_json), DES_KEY)
  15. now_str := strconv.FormatInt(time.Now().Unix(), 10)
  16. res_sign := Sign(encryData, now_str)
  17. res.Data = proto.String(encryData)
  18. res.Time = proto.String(now_str)
  19. res.Sign = proto.String(res_sign)
  20. }
  21. }
  22. func SetReqData(arg interface{}) (*Request, error) {
  23. data_json, err := json.Marshal(arg)
  24. if err != nil {
  25. return nil, err
  26. }
  27. now_int64 := time.Now().Unix()
  28. encryData := crypter.DesEn(string(data_json), DES_KEY)
  29. now := strconv.FormatInt(now_int64, 10)
  30. sign := Sign(encryData, now)
  31. return &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil
  32. }
  33. func GetReqData(req *Request) (string, error) {
  34. res_data := req.GetData()
  35. if res_data != "" {
  36. time_int64, err := strconv.ParseInt(req.GetTime(), 10, 64)
  37. if err != nil {
  38. return "", err
  39. }
  40. now_int64 := time.Now().Unix()
  41. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  42. //时间误差前后10秒,返回
  43. return "", errors.New("返回时间错误")
  44. }
  45. check_sign := CheckSign(req.GetSign(), res_data, req.GetTime())
  46. if !check_sign {
  47. return "", errors.New("返回数据签名错误")
  48. }
  49. //解密
  50. return crypter.DesDe(res_data, DES_KEY), nil
  51. }
  52. return "", nil
  53. }
  54. func GetResData(res *Response) (string, error) {
  55. res_data := res.GetData()
  56. if res_data != "" {
  57. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  58. if err != nil {
  59. return "", err
  60. }
  61. now_int64 := time.Now().Unix()
  62. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  63. //时间误差前后10秒,返回
  64. return "", errors.New("返回时间错误")
  65. }
  66. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  67. if !check_sign {
  68. return "", errors.New("返回数据签名错误")
  69. }
  70. //解密
  71. return crypter.DesDe(res_data, DES_KEY), nil
  72. }
  73. return "", nil
  74. }