Skip to content

增加开放平台授权方小程序登录 #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions openplatform/miniprogram/auth/auth.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions openplatform/miniprogram/miniprogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
miniContext "github.com/silenceper/wechat/v2/miniprogram/context"
"github.com/silenceper/wechat/v2/miniprogram/urllink"
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/component"
)
Expand Down Expand Up @@ -72,6 +73,11 @@ func (miniProgram *MiniProgram) GetURLLink() *urllink.URLLink {
})
}

// GetAuth 登录/用户信息相关接口
func (miniProgram *MiniProgram) GetAuth() *auth.Auth {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是不是需要使用单例呢?

Suggested change
func (miniProgram *MiniProgram) GetAuth() *auth.Auth {
func (miniProgram *MiniProgram) GetAuth() *auth.Auth {

return auth.NewAuth(miniProgram.openContext, miniProgram.AppID)
}

// DefaultAuthrAccessToken 默认获取授权ak的方法
type DefaultAuthrAccessToken struct {
opCtx *openContext.Context
Expand Down