-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
增加企业微信 企业内部开发模块 #418
Merged
Merged
增加企业微信 企业内部开发模块 #418
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
497d4e9
增加小程序内容安全接口
b2b811f
内容安全接口 按照golint规范进行优化
d28316f
内容安全接口 按照golint规范进行优化
a4db929
删除CheckImage中的输出代码
b677d30
小程序内容安全接口
bbfa1eb
小程序内容安全接口
def94de
小程序内容安全接口
10ca1df
增加企业微信 企业内部开发模块
8a1db6b
增加企业微信 企业内部开发模块
b6090f8
修改参数为小写
22bb52e
优化参数格式
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,5 @@ _testmain.go | |
.vscode/ | ||
vendor | ||
.idea/ | ||
example/* | ||
example/* | ||
/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package content | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/silenceper/wechat/v2/miniprogram/context" | ||
"github.com/silenceper/wechat/v2/util" | ||
) | ||
|
||
const ( | ||
checkTextURL = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=%s" | ||
checkImageURL = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=%s" | ||
) | ||
|
||
//Content 内容安全 | ||
type Content struct { | ||
*context.Context | ||
} | ||
|
||
//NewContent 内容安全接口 | ||
func NewContent(ctx *context.Context) *Content { | ||
return &Content{ctx} | ||
} | ||
|
||
//CheckText 检测文字 | ||
//@text 需要检测的文字 | ||
func (content *Content) CheckText(text string) error { | ||
accessToken, err := content.GetAccessToken() | ||
if err != nil { | ||
return err | ||
} | ||
response, err := util.PostJSON( | ||
fmt.Sprintf(checkTextURL, accessToken), | ||
map[string]string{ | ||
"content": text, | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
return util.DecodeWithCommonError(response, "ContentCheckText") | ||
} | ||
|
||
//CheckImage 检测图片 | ||
//所传参数为要检测的图片文件的绝对路径,图片格式支持PNG、JPEG、JPG、GIF, 像素不超过 750 x 1334,同时文件大小以不超过 300K 为宜,否则可能报错 | ||
//@media 图片文件的绝对路径 | ||
func (content *Content) CheckImage(media string) error { | ||
accessToken, err := content.GetAccessToken() | ||
if err != nil { | ||
return err | ||
} | ||
response, err := util.PostFile( | ||
"media", | ||
media, | ||
fmt.Sprintf(checkImageURL, accessToken), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
return util.DecodeWithCommonError(response, "ContentCheckImage") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Package config 企业微信config配置 | ||
package config | ||
|
||
import ( | ||
"github.com/silenceper/wechat/v2/cache" | ||
) | ||
|
||
// Config config for 企业微信 | ||
type Config struct { | ||
CorpID string `json:"corp_id"` // corp_id | ||
CorpSecret string `json:"corp_secret"` // corp_secret | ||
AgentID string `json:"agent_id"` // agent_id | ||
Cache cache.Cache | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package context | ||
|
||
import ( | ||
"github.com/silenceper/wechat/v2/credential" | ||
"github.com/silenceper/wechat/v2/work/config" | ||
) | ||
|
||
// Context struct | ||
type Context struct { | ||
*config.Config | ||
credential.AccessTokenHandle | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package oauth | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/silenceper/wechat/v2/util" | ||
"github.com/silenceper/wechat/v2/work/context" | ||
) | ||
|
||
//Oauth auth | ||
type Oauth struct { | ||
*context.Context | ||
} | ||
|
||
var ( | ||
//oauthTargetURL 企业微信内跳转地址 | ||
oauthTargetURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect" | ||
//oauthUserInfoURL 获取用户信息地址 | ||
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" | ||
) | ||
|
||
//NewOauth new init oauth | ||
func NewOauth(ctx *context.Context) *Oauth { | ||
return &Oauth{ | ||
ctx, | ||
} | ||
} | ||
|
||
//GetTargetURL 获取授权地址 | ||
func (ctr *Oauth) GetTargetURL(callbackURL string) string { | ||
//url encode | ||
urlStr := url.QueryEscape(callbackURL) | ||
return fmt.Sprintf( | ||
oauthTargetURL, | ||
ctr.CorpID, | ||
urlStr, | ||
) | ||
} | ||
|
||
//GetQrContentTargetURL 构造独立窗口登录二维码 | ||
func (ctr *Oauth) GetQrContentTargetURL(callbackURL string) string { | ||
//url encode | ||
urlStr := url.QueryEscape(callbackURL) | ||
return fmt.Sprintf( | ||
oauthQrContentTargetURL, | ||
ctr.CorpID, | ||
ctr.AgentID, | ||
urlStr, | ||
util.RandomStr(16), | ||
) | ||
} | ||
|
||
//ResUserInfo 返回得用户信息 | ||
type ResUserInfo struct { | ||
util.CommonError | ||
//当用户为企业成员时返回 | ||
UserID string `json:"UserId"` | ||
DeviceID string `json:"DeviceId"` | ||
//非企业成员授权时返回 | ||
OpenID string `json:"OpenId"` | ||
} | ||
|
||
//UserFromCode 根据code获取用户信息 | ||
func (ctr *Oauth) UserFromCode(code string) (result ResUserInfo, err error) { | ||
var accessToken string | ||
accessToken, err = ctr.GetAccessToken() | ||
if err != nil { | ||
return | ||
} | ||
var response []byte | ||
response, err = util.HTTPGet( | ||
fmt.Sprintf(oauthUserInfoURL, accessToken, code), | ||
) | ||
if err != nil { | ||
return | ||
} | ||
err = json.Unmarshal(response, &result) | ||
if result.ErrCode != 0 { | ||
err = fmt.Errorf("GetUserAccessToken error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg) | ||
return | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package work | ||
|
||
import ( | ||
"github.com/silenceper/wechat/v2/credential" | ||
"github.com/silenceper/wechat/v2/work/config" | ||
"github.com/silenceper/wechat/v2/work/context" | ||
"github.com/silenceper/wechat/v2/work/oauth" | ||
) | ||
|
||
// Work 企业微信 | ||
type Work struct { | ||
ctx *context.Context | ||
} | ||
|
||
//NewWork init work | ||
func NewWork(cfg *config.Config) *Work { | ||
defaultAkHandle := credential.NewWorkAccessToken(cfg.CorpID, cfg.CorpSecret, credential.CacheKeyWorkPrefix, cfg.Cache) | ||
ctx := &context.Context{ | ||
Config: cfg, | ||
AccessTokenHandle: defaultAkHandle, | ||
} | ||
return &Work{ctx: ctx} | ||
} | ||
|
||
//GetContext get Context | ||
func (wk *Work) GetContext() *context.Context { | ||
return wk.ctx | ||
} | ||
|
||
//GetOauth get oauth | ||
func (wk *Work) GetOauth() *oauth.Oauth { | ||
return oauth.NewOauth(wk.ctx) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
参数Corp小写