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

feat:企业微信-身份验证-获取用户身份及敏感信息 #692

Merged
merged 1 commit into from
Jun 30, 2023
Merged
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
74 changes: 74 additions & 0 deletions work/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var (
oauthUserInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=%s&code=%s"
// oauthQrContentTargetURL 构造独立窗口登录二维码
oauthQrContentTargetURL = "https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=%s&agentid=%s&redirect_uri=%s&state=%s"
// getUserInfoURL 获取访问用户身份&获取用户登录身份
getUserInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s"
// getUserDetailURL 获取访问用户敏感信息
getUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserdetail?access_token=%s"
)

// NewOauth new init oauth
Expand Down Expand Up @@ -93,3 +97,73 @@ func (ctr *Oauth) UserFromCode(code string) (result ResUserInfo, err error) {
}
return
}

// GetUserInfoResponse 获取访问用户身份&获取用户登录身份响应
type GetUserInfoResponse struct {
util.CommonError
UserID string `json:"userid"`
UserTicket string `json:"user_ticket"`
OpenID string `json:"openid"`
ExternalUserID string `json:"external_userid"`
}

// GetUserInfo 获取访问用户身份&获取用户登录身份
// @see https://developer.work.weixin.qq.com/document/path/90213 获取访问用户身份
// @see https://developer.work.weixin.qq.com/document/path/98176 获取用户登录身份
func (ctr *Oauth) GetUserInfo(code string) (*GetUserInfoResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = ctr.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.HTTPGet(fmt.Sprintf(getUserInfoURL, accessToken, code)); err != nil {
return nil, err
}
result := &GetUserInfoResponse{}
if err = util.DecodeWithError(response, result, "GetUserInfo"); err != nil {
return nil, err
}
return result, nil
}

// GetUserDetailRequest 获取访问用户敏感信息请求
type GetUserDetailRequest struct {
UserTicket string `json:"user_ticket"`
}

// GetUserDetailResponse 获取访问用户敏感信息响应
type GetUserDetailResponse struct {
util.CommonError
UserID string `json:"userid"`
Gender string `json:"gender"`
Avatar string `json:"avatar"`
QrCode string `json:"qr_code"`
Mobile string `json:"mobile"`
Email string `json:"email"`
BizMail string `json:"biz_mail"`
Address string `json:"address"`
}

// GetUserDetail 获取访问用户敏感信息
// @see https://developer.work.weixin.qq.com/document/path/95833
func (ctr *Oauth) GetUserDetail(req *GetUserDetailRequest) (*GetUserDetailResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = ctr.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getUserDetailURL, accessToken), req); err != nil {
return nil, err
}
result := &GetUserDetailResponse{}
if err = util.DecodeWithError(response, result, "GetUserDetail"); err != nil {
return nil, err
}
return result, nil
}