Browse Source

增加获取小程序openid接口调用

master v0.1.9
guzeng 3 years ago
parent
commit
27950743ff
4 changed files with 102 additions and 0 deletions
  1. +19
    -0
      common.go
  2. +78
    -0
      miniapp_openid.client.go
  3. +4
    -0
      weixin.pb.go
  4. +1
    -0
      weixin.proto

+ 19
- 0
common.go View File

@ -34,6 +34,25 @@ type UniformMessageReq struct {
Data interface{}
}
/**
* 获取小程序openid请求参数
*/
type MiniAppOpenidReq struct {
Appid string
Secret string
JsCode string
}
/**
* 获取小程序openid返回参数
*/
type MiniAppOpenidReqRes struct {
Openid string
SessionKey string
Unionid string
WxApiRes
}
func rpc_server_conn(url ...string) (*WeixinRpcServiceClient, error) {
var wx_rpc_url string = "127.0.0.1:7969"


+ 78
- 0
miniapp_openid.client.go View File

@ -0,0 +1,78 @@
package weixinrpc
import (
"encoding/json"
"strconv"
"time"
"git.tetele.net/tgo/crypter"
"github.com/golang/protobuf/proto"
)
func GetMiniAppOpenid(appid, secret, js_code string, url ...string) (*AccessTokenRes, error) {
conn, err := rpc_server_conn(url...)
if err != nil {
return nil, err
}
defer conn.Close()
data := MiniAppOpenidReq{}
data.Appid = appid
data.Secret = secret
data.JsCode = js_code
data_json, err := json.Marshal(data)
if err != nil {
return nil, err
}
encryData := crypter.DesEn(string(data_json), DES_KEY)
now_int64 := time.Now().Unix()
now := strconv.FormatInt(now_int64, 10)
sign := Sign(encryData, now)
req := &Request{
proto.String(encryData),
proto.String(now),
proto.String(sign),
nil}
res := &Response{}
err = conn.GetMiniAppOpenid(req, res)
if err != nil {
return nil, err
}
return HandleMiniAppOpenid(res)
}
/**
* 处理返回结果
*/
func HandleMiniAppOpenid(res *Response) (*MiniAppOpenidReqRes, error) {
//解密
res_data_de, err := GetOrgData(res)
if err != nil {
return nil, err
}
var res_arr MiniAppOpenidReqRes
err = json.Unmarshal([]byte(res_data_de), &res_arr)
if err != nil {
return nil, err
}
return &res_arr, nil
}

+ 4
- 0
weixin.pb.go View File

@ -101,6 +101,7 @@ type WeixinRpcService interface {
GetAccessToken(in *Request, out *Response) error
SendMiniprogramSubscribeMessage(in *Request, out *Response) error
SendUniformMessage(in *Request, out *Response) error
GetMiniAppOpenid(in *Request, out *Response) error
}
// AcceptWeixinRpcServiceClient accepts connections on the listener and serves requests
@ -181,6 +182,9 @@ func (c *WeixinRpcServiceClient) SendMiniprogramSubscribeMessage(in *Request, ou
func (c *WeixinRpcServiceClient) SendUniformMessage(in *Request, out *Response) error {
return c.Call("WeixinRpcService.SendUniformMessage", in, out)
}
func (c *WeixinRpcServiceClient) GetMiniAppOpenid(in *Request, out *Response) error {
return c.Call("WeixinRpcService.GetMiniAppOpenid", in, out)
}
// DialWeixinRpcService connects to an WeixinRpcService at the specified network address.
func DialWeixinRpcService(network, addr string) (*WeixinRpcServiceClient, *rpc.Client, error) {


+ 1
- 0
weixin.proto View File

@ -21,4 +21,5 @@ service WeixinRpcService {
rpc getAccessToken (Request) returns (Response); // access token
rpc sendMiniprogramSubscribeMessage (Request) returns (Response); //
rpc sendUniformMessage(Request) returns (Response); //
rpc getMiniAppOpenid(Request) returns (Response); // openid
}

Loading…
Cancel
Save