微信接口的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.

79 lines
1.2 KiB

  1. package weixinrpc
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "time"
  6. "git.tetele.net/tgo/crypter"
  7. "github.com/golang/protobuf/proto"
  8. )
  9. type AccessTokenReq struct {
  10. Appid string
  11. Secret string
  12. }
  13. func GetAccessToken(dbname, site_id string, appid, secret string, url ...string) (*AccessTokenRes, error) {
  14. conn, err := rpc_server_conn(url...)
  15. data["dbname"] = dbname
  16. data["site_id"] = site_id
  17. data["appid"] = appid
  18. data["secret"] = secret
  19. data_json, err := json.Marshal(data)
  20. if err != nil {
  21. return nil, err
  22. }
  23. encryData := crypter.DesEn(string(data_json), DES_KEY)
  24. now_int64 := time.Now().Unix()
  25. now := strconv.FormatInt(now_int64, 10)
  26. sign := Sign(encryData, now)
  27. req := &Request{
  28. proto.String(encryData),
  29. proto.String(now),
  30. proto.String(sign),
  31. nil}
  32. res := &Response{}
  33. err = conn.GetAccessToken(req, res)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return HandleAccessToken(res)
  38. }
  39. /**
  40. * 处理返回结果
  41. */
  42. func HandleAccessToken(res *Response) (*AccessTokenRes, error) {
  43. //解密
  44. res_data_de, err := GetOrgData(res)
  45. if err != nil {
  46. return nil, err
  47. }
  48. var res_arr AccessTokenRes
  49. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &res_arr, nil
  54. }