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

80 lines
1.3 KiB

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