diff --git a/Readme.md b/Readme.md index 894201f..8b27931 100644 --- a/Readme.md +++ b/Readme.md @@ -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) diff --git a/auth/auth.go b/auth/auth.go new file mode 100644 index 0000000..1b0a3b5 --- /dev/null +++ b/auth/auth.go @@ -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 +} diff --git a/auth/check_encrypted_data.go b/auth/check_encrypted_data.go new file mode 100644 index 0000000..980eb94 --- /dev/null +++ b/auth/check_encrypted_data.go @@ -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 +} diff --git a/auth/get_paid_union_id.go b/auth/get_paid_union_id.go new file mode 100644 index 0000000..962894c --- /dev/null +++ b/auth/get_paid_union_id.go @@ -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 +} diff --git a/livebroadcast/get_liveInfo.go b/livebroadcast/get_liveInfo.go index 62c7665..a3e9c83 100644 --- a/livebroadcast/get_liveInfo.go +++ b/livebroadcast/get_liveInfo.go @@ -8,7 +8,7 @@ type PriceType uint8 const ( _ PriceType = iota - PriceTypeOne // 一口价 + PriceTypeNormal // 一口价 PriceTypeRange // 区间价格 PriceTypeDiscount // 折扣价格 ) diff --git a/livebroadcast/get_shared_code.go b/livebroadcast/get_shared_code.go index 3debd25..e654f97 100644 --- a/livebroadcast/get_shared_code.go +++ b/livebroadcast/get_shared_code.go @@ -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"` @@ -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 diff --git a/livebroadcast/get_sub_anchor.go b/livebroadcast/get_sub_anchor.go new file mode 100644 index 0000000..15c3374 --- /dev/null +++ b/livebroadcast/get_sub_anchor.go @@ -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 +} diff --git a/livebroadcast/goods_add.go b/livebroadcast/goods_add.go new file mode 100644 index 0000000..3b61bc7 --- /dev/null +++ b/livebroadcast/goods_add.go @@ -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 +} diff --git a/weapp.go b/weapp.go index 3bb2f0f..78d3eab 100644 --- a/weapp.go +++ b/weapp.go @@ -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" @@ -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)