Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
royalrick committed Sep 24, 2021
2 parents a475d35 + 8be0955 commit 4106d5c
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 7 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
}
2 changes: 1 addition & 1 deletion livebroadcast/get_liveInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type PriceType uint8

const (
_ PriceType = iota
PriceTypeOne // 一口价
PriceTypeNormal // 一口价
PriceTypeRange // 区间价格
PriceTypeDiscount // 折扣价格
)
Expand Down
12 changes: 6 additions & 6 deletions livebroadcast/get_shared_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package livebroadcast

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

const apigetSharedCode = "/wxaapi/broadcast/room/getsharedcode"
const apiGetSharedCode = "/wxaapi/broadcast/room/GetSharedCode"

type getSharedCodeRequest struct {
type GetSharedCodeRequest struct {
// 房间ID
RoomId int64 `json:"roomId"`
// 自定义参数
Params int64 `json:"params"`
}

type getSharedCodeResponse struct {
type GetSharedCodeResponse struct {
request.CommonError
// 分享二维码地址
CdnUrl string `json:"cdnUrl"`
Expand All @@ -22,14 +22,14 @@ type getSharedCodeResponse struct {
}

// 获取直播间分享二维码
func (cli *LiveBroadcast) GetSharedCode(req *getSharedCodeRequest) (*getSharedCodeResponse, error) {
func (cli *LiveBroadcast) GetSharedCode(req *GetSharedCodeRequest) (*GetSharedCodeResponse, error) {

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

res := new(getSharedCodeResponse)
res := new(GetSharedCodeResponse)
err = cli.request.Get(api, res)
if err != nil {
return nil, err
Expand Down
32 changes: 32 additions & 0 deletions livebroadcast/get_sub_anchor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package livebroadcast

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

const apiGetSubAnchor = "/wxaapi/broadcast/room/GetSubAnchor"

type GetSubAnchorRequest struct {
// 房间ID
RoomId int64 `json:"roomId"`
}

type GetSubAnchorResponse struct {
request.CommonError
Username string `json:"username"`
}

// 获取主播副号
func (cli *LiveBroadcast) GetSubAnchor(req *GetSubAnchorRequest) (*GetSubAnchorResponse, error) {

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

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

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

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

const apiGoodsAdd = "/wxaapi/broadcast/goods/add"

type GoodsAddRequest struct {
// 商品信息
GoodsInfo GoodsInfo `json:"goodsInfo"`
}

type GoodsInfo struct {
// 必填 填入mediaID(mediaID获取后,三天内有效);图片mediaID的获取,请参考以下文档: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/New_temporary_materials.html;图片规则:图片尺寸最大300像素*300像素;
CoverImgUrl string `json:"coverImgUrl"`
// 必填 商品名称,最长14个汉字,1个汉字相当于2个字符
Name string `json:"name"`
// 必填 价格类型,1:一口价(只需要传入price,price2不传) 2:价格区间(price字段为左边界,price2字段为右边界,price和price2必传) 3:显示折扣价(price字段为原价,price2字段为现价, price和price2必传)
PriceType PriceType `json:"priceType"`
// 必填 数字,最多保留两位小数,单位元
Price float64 `json:"price"`
// 非必填 数字,最多保留两位小数,单位元
Price2 float64 `json:"price2"`
// 必填 商品详情页的小程序路径,路径参数存在 url 的,该参数的值需要进行 encode 处理再填入
Url string `json:"url"`
// 非必填 当商品为第三方小程序的商品则填写为对应第三方小程序的appid,自身小程序商品则为''
ThirdPartyAppid string `json:"thirdPartyAppid"`
}

type GoodsAddResponse struct {
request.CommonError
// 商品ID
GoodsId string `json:"goodsId"`
// 审核单ID
AuditId string `json:"auditId"`
}

// 商品添加并提审
func (cli *LiveBroadcast) GoodsAdd(req *GoodsAddRequest) (*GoodsAddResponse, error) {

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

res := new(GoodsAddResponse)
err = cli.request.Post(api, req, 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 4106d5c

Please sign in to comment.