订单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.

98 lines
1.7 KiB

  1. package orderrpc
  2. import (
  3. "errors"
  4. "strconv"
  5. "time"
  6. "git.tetele.net/tgo/crypter"
  7. )
  8. const DES_KEY = "ordernew"
  9. func rpc_server_conn(url ...string) (*OrderServiceClient, error) {
  10. var order_rpc_url string = "127.0.0.1:7973"
  11. if len(url) > 0 && url[0] != "" {
  12. order_rpc_url = url[0]
  13. }
  14. conn, _, err := DialOrderService("tcp", order_rpc_url)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return conn, nil
  19. }
  20. /**
  21. * 处理返回结果
  22. */
  23. func HandleResponse(res *CreateResponse) (string, error) {
  24. res_data := res.GetData()
  25. if res_data == "" {
  26. return "", errors.New("未收到收据")
  27. }
  28. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  29. if err != nil {
  30. return "", err
  31. }
  32. now_int64 := time.Now().Unix()
  33. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  34. //时间误差前后10秒,返回
  35. return "", errors.New("返回时间错误")
  36. }
  37. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  38. if !check_sign {
  39. return "", errors.New("返回数据签名错误")
  40. }
  41. //解密
  42. res_data_de := crypter.DesDe(res_data, "ordernew")
  43. return res_data_de, nil
  44. }
  45. /**
  46. * 处理返回结果
  47. */
  48. func GetOrgData(res *Response) (string, error) {
  49. res_data := res.GetData()
  50. if res_data == "" {
  51. return "", errors.New("未收到收据")
  52. }
  53. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  54. if err != nil {
  55. return "", err
  56. }
  57. now_int64 := time.Now().Unix()
  58. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  59. //时间误差前后10秒,返回
  60. return "", errors.New("返回时间错误")
  61. }
  62. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  63. if !check_sign {
  64. return "", errors.New("返回数据签名错误")
  65. }
  66. //解密
  67. res_data_de := crypter.DesDe(res_data, DES_KEY)
  68. return res_data_de, nil
  69. }