mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
增加企业微信 企业内部开发模块 (#418)
* 增加小程序内容安全接口 * 内容安全接口 按照golint规范进行优化 * 内容安全接口 按照golint规范进行优化 * 删除CheckImage中的输出代码 * 小程序内容安全接口 * 小程序内容安全接口 * 小程序内容安全接口 1:修改返回值 改为error异常统一返回 * 增加企业微信 企业内部开发模块 1:授权登录 * 增加企业微信 企业内部开发模块 * 修改参数为小写 * 优化参数格式 Co-authored-by: root <admin@example.com>
This commit is contained in:
14
work/config/config.go
Normal file
14
work/config/config.go
Normal file
@@ -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
|
||||
}
|
||||
12
work/context/context.go
Normal file
12
work/context/context.go
Normal file
@@ -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
|
||||
}
|
||||
87
work/oauth/oauth.go
Normal file
87
work/oauth/oauth.go
Normal file
@@ -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
|
||||
}
|
||||
33
work/work.go
Normal file
33
work/work.go
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user