mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-23 13:42:25 +08:00
Compare commits
2 Commits
feature/au
...
d4e6ac8620
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4e6ac8620 | ||
|
|
de92dc0dcd |
@@ -15,8 +15,6 @@ const (
|
|||||||
checkEncryptedDataURL = "https://api.weixin.qq.com/wxa/business/checkencryptedmsg?access_token=%s"
|
checkEncryptedDataURL = "https://api.weixin.qq.com/wxa/business/checkencryptedmsg?access_token=%s"
|
||||||
|
|
||||||
getPhoneNumber = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
|
getPhoneNumber = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
|
||||||
|
|
||||||
checkSessionURL = "https://api.weixin.qq.com/wxa/checksession?access_token=%s&openid=%s&signature=%s&sig_method=hmac_sha256"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Auth 登录/用户信息
|
// Auth 登录/用户信息
|
||||||
@@ -35,7 +33,7 @@ type ResCode2Session struct {
|
|||||||
|
|
||||||
OpenID string `json:"openid"` // 用户唯一标识
|
OpenID string `json:"openid"` // 用户唯一标识
|
||||||
SessionKey string `json:"session_key"` // 会话密钥
|
SessionKey string `json:"session_key"` // 会话密钥
|
||||||
UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符,在满足 UnionID 下发条件的情况下会返回
|
UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符,在满足UnionID下发条件的情况下会返回
|
||||||
}
|
}
|
||||||
|
|
||||||
// RspCheckEncryptedData .
|
// RspCheckEncryptedData .
|
||||||
@@ -72,12 +70,12 @@ func (auth *Auth) GetPaidUnionID() {
|
|||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckEncryptedData .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近 3 天生成的加密数据
|
// CheckEncryptedData .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
|
||||||
func (auth *Auth) CheckEncryptedData(encryptedMsgHash string) (result RspCheckEncryptedData, err error) {
|
func (auth *Auth) CheckEncryptedData(encryptedMsgHash string) (result RspCheckEncryptedData, err error) {
|
||||||
return auth.CheckEncryptedDataContext(context2.Background(), encryptedMsgHash)
|
return auth.CheckEncryptedDataContext(context2.Background(), encryptedMsgHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckEncryptedDataContext .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近 3 天生成的加密数据
|
// CheckEncryptedDataContext .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
|
||||||
func (auth *Auth) CheckEncryptedDataContext(ctx context2.Context, encryptedMsgHash string) (result RspCheckEncryptedData, err error) {
|
func (auth *Auth) CheckEncryptedDataContext(ctx context2.Context, encryptedMsgHash string) (result RspCheckEncryptedData, err error) {
|
||||||
var response []byte
|
var response []byte
|
||||||
var (
|
var (
|
||||||
@@ -87,7 +85,7 @@ func (auth *Auth) CheckEncryptedDataContext(ctx context2.Context, encryptedMsgHa
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 由于 GetPhoneNumberContext 需要传入 JSON,所以 HTTPPostContext 入参改为 []byte
|
// 由于GetPhoneNumberContext需要传入JSON,所以HTTPPostContext入参改为[]byte
|
||||||
if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(checkEncryptedDataURL, at), []byte("encrypted_msg_hash="+encryptedMsgHash), nil); err != nil {
|
if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(checkEncryptedDataURL, at), []byte("encrypted_msg_hash="+encryptedMsgHash), nil); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -115,62 +113,38 @@ type PhoneInfo struct {
|
|||||||
} `json:"watermark"` // 数据水印
|
} `json:"watermark"` // 数据水印
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPhoneNumberContext 小程序通过 code 获取用户手机号
|
// GetPhoneNumberContext 小程序通过code获取用户手机号
|
||||||
func (auth *Auth) GetPhoneNumberContext(ctx context2.Context, code string) (result *GetPhoneNumberResponse, err error) {
|
func (auth *Auth) GetPhoneNumberContext(ctx context2.Context, code string) (*GetPhoneNumberResponse, error) {
|
||||||
var accessToken string
|
var response []byte
|
||||||
if accessToken, err = auth.GetAccessToken(); err != nil {
|
var (
|
||||||
|
at string
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if at, err = auth.GetAccessToken(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
body := map[string]interface{}{
|
||||||
bodyBytes, err := json.Marshal(map[string]interface{}{
|
|
||||||
"code": code,
|
"code": code,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
bodyBytes, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
header := map[string]string{"Content-Type": "application/json;charset=utf-8"}
|
||||||
header = map[string]string{"Content-Type": "application/json;charset=utf-8"}
|
if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(getPhoneNumber, at), bodyBytes, header); err != nil {
|
||||||
response []byte
|
|
||||||
)
|
|
||||||
|
|
||||||
if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(getPhoneNumber, accessToken), bodyBytes, header); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber")
|
var result GetPhoneNumberResponse
|
||||||
return
|
if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPhoneNumber 小程序通过 code 获取用户手机号
|
// GetPhoneNumber 小程序通过code获取用户手机号
|
||||||
func (auth *Auth) GetPhoneNumber(code string) (*GetPhoneNumberResponse, error) {
|
func (auth *Auth) GetPhoneNumber(code string) (*GetPhoneNumberResponse, error) {
|
||||||
return auth.GetPhoneNumberContext(context2.Background(), code)
|
return auth.GetPhoneNumberContext(context2.Background(), code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// // CheckSession 检验登录态是否过期。
|
|
||||||
// func (auth *Auth) CheckSession(sessionKey, openID string) (result *CheckSessionResponse, err error) {
|
|
||||||
// return auth.CheckSessionContext(context2.Background(), sessionKey, openID)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // CheckSessionContext 检验登录态是否过期。
|
|
||||||
// func (auth *Auth) CheckSessionContext(ctx context2.Context, sessionKey, openID string) (result *CheckSessionResponse, err error) {
|
|
||||||
// var accessToken string
|
|
||||||
// if accessToken, err = auth.GetAccessToken(); err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// var (
|
|
||||||
// response []byte
|
|
||||||
// signature string = sessionKey
|
|
||||||
// )
|
|
||||||
// if response, err = util.HTTPGetContext(ctx, fmt.Sprintf(checkSessionURL, accessToken, openID, signature)); err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// err = util.DecodeWithError(response, &result, "CheckSessionContext")
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // CheckSessionResponse 检验登录态是否过期。
|
|
||||||
// type CheckSessionResponse struct {
|
|
||||||
// util.CommonError
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -117,24 +117,6 @@ const (
|
|||||||
|
|
||||||
// queryPublishGoods 查询批量发布道具任务状态
|
// queryPublishGoods 查询批量发布道具任务状态
|
||||||
queryPublishGoods = "/xpay/query_publish_goods"
|
queryPublishGoods = "/xpay/query_publish_goods"
|
||||||
|
|
||||||
// queryBizBalance 查询商家账户里的可提现余额
|
|
||||||
queryBizBalance = "/xpay/query_biz_balance"
|
|
||||||
|
|
||||||
// queryTransferAccount 查询广告金充值账户
|
|
||||||
queryTransferAccount = "/xpay/query_transfer_account"
|
|
||||||
|
|
||||||
// queryAdverFunds 查询广告金发放记录
|
|
||||||
queryAdverFunds = "/xpay/query_adver_funds"
|
|
||||||
|
|
||||||
// createFundsBill 充值广告金
|
|
||||||
createFundsBill = "/xpay/create_funds_bill"
|
|
||||||
|
|
||||||
// bindTransferAccount 绑定广告金充值账户
|
|
||||||
bindTransferAccount = "/xpay/bind_transfer_accout"
|
|
||||||
|
|
||||||
// defaultUnifiedOrderURL default unified order url
|
|
||||||
defaultUnifiedOrderURL = "requestVirtualPayment"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -145,8 +145,6 @@ type OrderItem struct {
|
|||||||
WxOrderID string `json:"wx_order_id"` // 微信内部单号
|
WxOrderID string `json:"wx_order_id"` // 微信内部单号
|
||||||
ChannelOrderID string `json:"channel_order_id"` // 渠道订单号,为用户微信支付详情页面上的商户单号
|
ChannelOrderID string `json:"channel_order_id"` // 渠道订单号,为用户微信支付详情页面上的商户单号
|
||||||
WxPayOrderID string `json:"wxpay_order_id"` // 微信支付交易单号,为用户微信支付详情页面上的交易单号
|
WxPayOrderID string `json:"wxpay_order_id"` // 微信支付交易单号,为用户微信支付详情页面上的交易单号
|
||||||
SettTime int64 `json:"sett_time"` // 结算时间,unix 秒级时间戳,结算时间的秒级时间戳,大于 0 表示结算成功
|
|
||||||
SettState uint `json:"sett_state"` // 结算状态 0-未开始结算 1-结算中 2-结算成功
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryOrderResponse 查询创建的订单(现金单,非代币单)响应参数
|
// QueryOrderResponse 查询创建的订单(现金单,非代币单)响应参数
|
||||||
|
|||||||
@@ -479,7 +479,6 @@ func (s *VirtualPayment) requestAddress(params URLParams) (url string, err error
|
|||||||
case queryUserBalance:
|
case queryUserBalance:
|
||||||
case currencyPay:
|
case currencyPay:
|
||||||
case cancelCurrencyPay:
|
case cancelCurrencyPay:
|
||||||
case defaultUnifiedOrderURL:
|
|
||||||
if params.PaySign, params.Signature, err = s.PaySignature(params.Path, params.Content); err != nil {
|
if params.PaySign, params.Signature, err = s.PaySignature(params.Path, params.Content); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
60
openplatform/miniprogram/auth/auth.go
Normal file
60
openplatform/miniprogram/auth/auth.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdContext "context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/openplatform/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
code2SessionURL = "https://api.weixin.qq.com/sns/component/jscode2session?appid=%s&js_code=%s&grant_type=authorization_code&component_appid=%s&component_access_token=%s"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Auth 登录/用户信息
|
||||||
|
type Auth struct {
|
||||||
|
*context.Context
|
||||||
|
authorizerAppID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAuth new auth (授权方appID)
|
||||||
|
func NewAuth(ctx *context.Context, appID string) *Auth {
|
||||||
|
return &Auth{ctx, appID}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResCode2Session 登录凭证校验的返回结果
|
||||||
|
type ResCode2Session struct {
|
||||||
|
util.CommonError
|
||||||
|
OpenID string `json:"openid"` // 用户唯一标识
|
||||||
|
SessionKey string `json:"session_key"` // 会话密钥
|
||||||
|
UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符,在满足UnionID下发条件的情况下会返回
|
||||||
|
}
|
||||||
|
|
||||||
|
// Code2Session 登录凭证校验。
|
||||||
|
func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error) {
|
||||||
|
return auth.Code2SessionContext(stdContext.Background(), jsCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Code2SessionContext 登录凭证校验。
|
||||||
|
func (auth *Auth) Code2SessionContext(ctx stdContext.Context, jsCode string) (result ResCode2Session, err error) {
|
||||||
|
var response []byte
|
||||||
|
var componentAccessToken string
|
||||||
|
componentAccessToken, err = auth.GetComponentAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
parse := fmt.Sprintf(code2SessionURL, auth.authorizerAppID, jsCode, auth.Context.AppID, componentAccessToken)
|
||||||
|
if response, err = util.HTTPGetContext(ctx, parse); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal(response, &result); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("Code2Session error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
miniContext "github.com/silenceper/wechat/v2/miniprogram/context"
|
miniContext "github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/urllink"
|
"github.com/silenceper/wechat/v2/miniprogram/urllink"
|
||||||
openContext "github.com/silenceper/wechat/v2/openplatform/context"
|
openContext "github.com/silenceper/wechat/v2/openplatform/context"
|
||||||
|
"github.com/silenceper/wechat/v2/openplatform/miniprogram/auth"
|
||||||
"github.com/silenceper/wechat/v2/openplatform/miniprogram/basic"
|
"github.com/silenceper/wechat/v2/openplatform/miniprogram/basic"
|
||||||
"github.com/silenceper/wechat/v2/openplatform/miniprogram/component"
|
"github.com/silenceper/wechat/v2/openplatform/miniprogram/component"
|
||||||
)
|
)
|
||||||
@@ -72,6 +73,11 @@ func (miniProgram *MiniProgram) GetURLLink() *urllink.URLLink {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAuth 登录/用户信息相关接口
|
||||||
|
func (miniProgram *MiniProgram) GetAuth() *auth.Auth {
|
||||||
|
return auth.NewAuth(miniProgram.openContext, miniProgram.AppID)
|
||||||
|
}
|
||||||
|
|
||||||
// DefaultAuthrAccessToken 默认获取授权ak的方法
|
// DefaultAuthrAccessToken 默认获取授权ak的方法
|
||||||
type DefaultAuthrAccessToken struct {
|
type DefaultAuthrAccessToken struct {
|
||||||
opCtx *openContext.Context
|
opCtx *openContext.Context
|
||||||
|
|||||||
Reference in New Issue
Block a user