用户接口远程调用
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.

102 lines
1.7 KiB

  1. package userrpc
  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. /**
  11. * 查找商户信息
  12. * 2021/04/06
  13. * gz
  14. */
  15. func GetBusiness(site_id, dbname, business_id string, url ...string) (*Business, error) {
  16. conn, err := rpc_server_conn(url...)
  17. if err != nil {
  18. return nil, err
  19. }
  20. defer conn.Close()
  21. data := GetBusinessReq{}
  22. data.SiteId = site_id
  23. data.Dbname = dbname
  24. data.BusinessId = business_id
  25. data_json, err := json.Marshal(data)
  26. if err != nil {
  27. return nil, err
  28. }
  29. encryData := crypter.DesEn(string(data_json), DES_KEY)
  30. now_int64 := time.Now().Unix()
  31. now := strconv.FormatInt(now_int64, 10)
  32. sign := Sign(encryData, now)
  33. req := &Request{
  34. proto.String(encryData),
  35. proto.String(now),
  36. proto.String(sign),
  37. nil}
  38. res := &Response{}
  39. err = conn.GetBusiness(req, res)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return HandleGetBusinessRes(res)
  44. }
  45. /**
  46. * 处理返回结果
  47. */
  48. func HandleGetBusinessRes(res *Response) (*Business, error) {
  49. res_data := res.GetData()
  50. if res_data == "" {
  51. return nil, errors.New("未收到收据")
  52. }
  53. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  54. if err != nil {
  55. return nil, err
  56. }
  57. now_int64 := time.Now().Unix()
  58. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  59. //时间误差前后10秒,返回
  60. return nil, errors.New("返回时间错误")
  61. }
  62. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  63. if !check_sign {
  64. return nil, errors.New("返回数据签名错误")
  65. }
  66. //解密
  67. res_data_de := crypter.DesDe(res_data, DES_KEY)
  68. var res_arr Business
  69. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return &res_arr, nil
  74. }