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

157 lines
2.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. * 绑定第三方平台openid
  12. * 2021/04/05
  13. * gz
  14. */
  15. func BindThird(site_id, dbname, platform, user_id, openid, openname, unionid, avatar, mobile string, url ...string) (*Res, error) {
  16. conn, err := rpc_server_conn(url...)
  17. if err != nil {
  18. return nil, err
  19. }
  20. defer conn.Close()
  21. data := BindThirdReq{}
  22. data.SiteId = site_id
  23. data.Dbname = dbname
  24. data.Openid = openid
  25. data.Openname = openname
  26. data.Platform = platform
  27. data.Unionid = unionid
  28. data.Avatar = avatar
  29. data.Mobile = mobile
  30. data.Userid = user_id
  31. data_json, err := json.Marshal(data)
  32. if err != nil {
  33. return nil, err
  34. }
  35. encryData := crypter.DesEn(string(data_json), DES_KEY)
  36. now_int64 := time.Now().Unix()
  37. now := strconv.FormatInt(now_int64, 10)
  38. sign := Sign(encryData, now)
  39. req := &Request{
  40. proto.String(encryData),
  41. proto.String(now),
  42. proto.String(sign),
  43. nil}
  44. res := &Response{}
  45. err = conn.BindThird(req, res)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return HandleRes(res)
  50. }
  51. /**
  52. * 查找第三方平台openid
  53. * 2021/04/06
  54. * gz
  55. */
  56. func GetThird(site_id, dbname, platform, user_id string, url ...string) (*Third, error) {
  57. conn, err := rpc_server_conn(url...)
  58. if err != nil {
  59. return nil, err
  60. }
  61. defer conn.Close()
  62. data := GetThirdReq{}
  63. data.SiteId = site_id
  64. data.Dbname = dbname
  65. data.Platform = platform
  66. data.Userid = user_id
  67. data_json, err := json.Marshal(data)
  68. if err != nil {
  69. return nil, err
  70. }
  71. encryData := crypter.DesEn(string(data_json), DES_KEY)
  72. now_int64 := time.Now().Unix()
  73. now := strconv.FormatInt(now_int64, 10)
  74. sign := Sign(encryData, now)
  75. req := &Request{
  76. proto.String(encryData),
  77. proto.String(now),
  78. proto.String(sign),
  79. nil}
  80. res := &Response{}
  81. err = conn.GetThird(req, res)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return HandleGetThirdRes(res)
  86. }
  87. /**
  88. * 处理返回结果
  89. */
  90. func HandleGetThirdRes(res *Response) (*Third, error) {
  91. res_data := res.GetData()
  92. if res_data == "" {
  93. return nil, errors.New("未收到收据")
  94. }
  95. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  96. if err != nil {
  97. return nil, err
  98. }
  99. now_int64 := time.Now().Unix()
  100. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  101. //时间误差前后10秒,返回
  102. return nil, errors.New("返回时间错误")
  103. }
  104. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  105. if !check_sign {
  106. return nil, errors.New("返回数据签名错误")
  107. }
  108. //解密
  109. res_data_de := crypter.DesDe(res_data, DES_KEY)
  110. var res_arr Third
  111. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return &res_arr, nil
  116. }