Skip to content

Commit

Permalink
add: 增加用户信息模块
Browse files Browse the repository at this point in the history
  • Loading branch information
royalrick committed Sep 24, 2021
1 parent 2e785f3 commit 8be0955
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,45 @@ fmt.Printf("返回结果: %#v", res)

## 用户信息

- 初始化

```go
package main

import (
"fmt"
"log"

"github.com/medivhzhan/weapp/v3"
"github.com/medivhzhan/weapp/v3/auth"
)

func main() {
sdk := weapp.NewClient("your-appid", "your-secret")

cli := sdk.NewAuth()

// 用户支付完成后,获取该用户的 UnionId,无需用户授权。
rsp, err := cli.GetPaidUnionId(&auth.GetPaidUnionIdRequest{})
if err != nil {
log.Fatal(err)
}

// 检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
rsp, err := cli.CheckEncryptedData(&auth.CheckEncryptedDataRequest{})
if err != nil {
log.Fatal(err)
}

if err := rsp.GetResponseError(); err != nil {
log.Println(err)
}

fmt.Println(rsp)
}

```

### getPaidUnionId

[官方文档](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.getPaidUnionId.html)
Expand Down
20 changes: 20 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package auth

import "github.com/medivhzhan/weapp/v3/request"

// 用户信息
type Auth struct {
request *request.Request
// 组成完整的 URL 地址
// 默认包含 AccessToken
conbineURI func(url string, req interface{}) (string, error)
}

func NewAuth(request *request.Request, conbineURI func(url string, req interface{}) (string, error)) *Auth {
sm := Auth{
request: request,
conbineURI: conbineURI,
}

return &sm
}
35 changes: 35 additions & 0 deletions auth/check_encrypted_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package auth

import "github.com/medivhzhan/weapp/v3/request"

const apiCheckEncryptedData = "/wxa/business/checkencryptedmsg"

type CheckEncryptedDataRequest struct {
// 必填 加密数据的sha256,通过Hex(Base16)编码后的字符串
EncryptedMsgHash string `json:"encrypted_msg_hash"`
}

type CheckEncryptedDataResponse struct {
request.CommonError
// 是否是合法的数据
Valid bool `json:"vaild"`
// 加密数据生成的时间戳
CreateTime int64 `json:"create_time"`
}

// 检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
func (cli *Auth) CheckEncryptedData(req *CheckEncryptedDataRequest) (*CheckEncryptedDataResponse, error) {

api, err := cli.conbineURI(apiCheckEncryptedData, nil)
if err != nil {
return nil, err
}

res := new(CheckEncryptedDataResponse)
err = cli.request.Post(api, req, res)
if err != nil {
return nil, err
}

return res, nil
}
39 changes: 39 additions & 0 deletions auth/get_paid_union_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package auth

import "github.com/medivhzhan/weapp/v3/request"

const apiGetPaidUnionId = "/wxa/getpaidunionid"

type GetPaidUnionIdRequest struct {
// 必填 支付用户唯一标识
Openid string `json:"openid"`
// 非必填 微信支付订单号
TransactionId string `json:"transaction_id"`
// 非必填 微信支付分配的商户号,和商户订单号配合使用
MchId string `json:"mch_id"`
// 非必填 微信支付商户订单号,和商户号配合使用
OutTradeNo string `json:"out_trade_no"`
}

type GetPaidUnionIdResponse struct {
request.CommonError
// 用户唯一标识,调用成功后返回
UnionID string `json:"unionid"`
}

// 用户支付完成后,获取该用户的 UnionId,无需用户授权。
func (cli *Auth) GetPaidUnionId(req *GetPaidUnionIdRequest) (*GetPaidUnionIdResponse, error) {

api, err := cli.conbineURI(apiGetPaidUnionId, req)
if err != nil {
return nil, err
}

res := new(GetPaidUnionIdResponse)
err = cli.request.Get(api, res)
if err != nil {
return nil, err
}

return res, nil
}
6 changes: 6 additions & 0 deletions weapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"os"

"github.com/medivhzhan/weapp/v3/auth"
"github.com/medivhzhan/weapp/v3/cache"
"github.com/medivhzhan/weapp/v3/livebroadcast"
"github.com/medivhzhan/weapp/v3/logger"
Expand Down Expand Up @@ -163,6 +164,11 @@ func (cli *Client) conbineURI(url string, req interface{}) (string, error) {
return request.EncodeURL(baseURL+url, output)
}

// 用户信息
func (cli *Client) NewAuth() *auth.Auth {
return auth.NewAuth(cli.request, cli.conbineURI)
}

// 微信通知监听服务
func (cli *Client) NewServer(token, aesKey, mchID, apiKey string, validate bool, handler func(map[string]interface{}) map[string]interface{}) (*server.Server, error) {
return server.NewServer(cli.appid, token, aesKey, mchID, apiKey, validate, handler)
Expand Down

0 comments on commit 8be0955

Please sign in to comment.