Skip to content
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

支持云托管的开放接口服务模式 #667

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions miniprogram/business/phone_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ type PhoneInfo struct {

// GetPhoneNumber code换取用户手机号。 每个code只能使用一次,code的有效期为5min
func (business *Business) GetPhoneNumber(in *GetPhoneNumberRequest) (info PhoneInfo, err error) {
accessToken, err := business.GetAccessToken()
if err != nil {
return
var uri string
if business.Config.NoAccessToken {
uri = getPhoneNumberURL[:len(getPhoneNumberURL)-16] // ?access_token=%s
Copy link
Owner

Choose a reason for hiding this comment

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

不建议这么搞,不然直接 getPhoneNumberURL 中就不保留参数;

Copy link
Author

Choose a reason for hiding this comment

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

@silenceper OK,那各个接口需要整云托管和外网版两个url 再根据开关选择使用,我再改下

Copy link
Author

Choose a reason for hiding this comment

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

后面我对接支付的时候他们的差异更大,url都发生了变化,不只是少一个accessToken,另外出入参数结构、字段和形式都有所不同(云托管走json)

Copy link
Owner

Choose a reason for hiding this comment

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

如果出入参都变化了,那这种就不建议 在原方法上改了。 走单独的逻辑

} else {
accessToken, err := business.GetAccessToken()
if err != nil {
return info, err
}

uri = fmt.Sprintf(getPhoneNumberURL, accessToken)
}

if business.Config.UsingHTTP {
uri = "http" + uri[5:]
}

uri := fmt.Sprintf(getPhoneNumberURL, accessToken)
response, err := util.PostJSON(uri, in)
if err != nil {
return
Expand Down
7 changes: 7 additions & 0 deletions miniprogram/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ type Config struct {
AppID string `json:"app_id"` // appid
AppSecret string `json:"app_secret"` // appSecret
Cache cache.Cache

//使用云托管开放接口方式访问接口,此时API调用不附着AccessToken
//参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloudrun/src/guide/weixin/open.html
NoAccessToken bool `json:"cloud_run_access"`
//是否使用云托管开放接口方式访问接口,此时API调用可以开启使用HTTP API
//参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloudrun/src/guide/weixin/open.html
UsingHTTP bool `json:"using_http"`
}
8 changes: 5 additions & 3 deletions miniprogram/miniprogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ type MiniProgram struct {

// NewMiniProgram 实例化小程序API
func NewMiniProgram(cfg *config.Config) *MiniProgram {
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyMiniProgramPrefix, cfg.Cache)
ctx := &context.Context{
Config: cfg,
AccessTokenHandle: defaultAkHandle,
Config: cfg,
}

if !cfg.NoAccessToken {
ctx.AccessTokenHandle = credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyMiniProgramPrefix, cfg.Cache)
}
return &MiniProgram{ctx}
}
Expand Down