1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
token.go 3.61 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-10-09 14:13 . 企业微信 会话存档
package app3
import (
"gitee.com/h79/gothird/result"
"gitee.com/h79/gothird/token"
"gitee.com/h79/gothird/weixin/app3/authorizer"
"gitee.com/h79/gothird/weixin/app3/oauth2"
"gitee.com/h79/gothird/weixin/errors"
"gitee.com/h79/gothird/weixin/media"
"gitee.com/h79/gothird/weixin/offia/ticket"
"gitee.com/h79/goutils/common/data"
"mime/multipart"
"net/http"
"reflect"
)
var firstService *acsReq
var app3IdService *acsReq
var app3WebService *acsReq
func init() {
firstService = &acsReq{
get: GetComponentAccessToken,
ref: token.GetEmpty,
auth2: oauth2.WebGetAccessToken,
app3: oauth2.AuthorizerGetAccessToken,
}
app3IdService = &acsReq{
get: token.GetEmpty,
ref: oauth2.AuthorizerRefreshToken,
auth2: token.GetEmpty,
app3: token.GetEmpty,
}
app3WebService = &acsReq{
get: token.GetEmpty,
ref: oauth2.WebRefreshToken,
auth2: token.GetEmpty,
app3: token.GetEmpty,
}
}
func GetService() token.Service {
return firstService
}
func GetIdService() token.Service {
return app3IdService
}
func GetWebService() token.Service {
return app3WebService
}
func NewToken(app token.App) token.Token {
return token.New(&app, firstService)
}
func NewWebToken(app token.App) token.Token {
return token.New(&app, app3WebService)
}
func NewIdToken(app token.App) token.Token {
return token.New(&app, app3IdService)
}
var _ token.Service = (*acsReq)(nil)
type acsReq struct {
check token.CheckErrorFunc
get token.GenFunc
ref token.GenFunc
auth2 token.GenFunc
app3 token.GenFunc
}
// Execute
// token.Service interface
func (req *acsReq) Execute(tk token.Token, cmd string, d data.D) (interface{}, error) {
if cmd == token.NTicket {
return ticket.GetTicket(tk, d)
}
if cmd == token.NAccessToken {
return req.get(tk, d)
}
if cmd == token.NRefreshToken {
return req.ref(tk, d)
}
if cmd == token.NPreAuthCode {
api := token.NewApi(tk)
return authorizer.GetPreAuthCode(api)
}
if cmd == token.NOauth2App3 { //把公众号或小程序 授权给第三方应用
acs, err := req.app3(tk, d)
if err != nil {
return nil, err
}
if info, ok := acs.(*authorizer.AuthorizationInfo); ok {
id := info.Info.Appid
ch := &token.Child{
App: token.App{
ParentId: tk.GetId(),
IsCache: tk.IsCache(),
Type: token.KWxApp3IdType,
Id: token.GenId(tk.GetAppId(), id),
AppId: id,
Name: "app3:id",
},
VMap: token.VMap{},
}
token.SyncCreateChild(tk, app3IdService, ch, acs, []string{id}, func() {
}) //需要把授权的id 保存
}
return acs, err
}
if cmd == token.NOauth2 { //公众号,网页个人授权
acs, err := req.auth2(tk, d)
if err != nil {
return nil, err
}
return acs, err
}
return nil, result.ErrNotSupported
}
func (req *acsReq) GetUrl(uri string, acsKey string, d data.D) string {
return token.ComponentUrl(acsKey, uri)
}
func (req *acsReq) SetHead(h *http.Header, acsKey string) {
}
func (req *acsReq) CreateForm(w *multipart.Writer, field string, form interface{}) error {
img, ok := form.(*media.Image)
if !ok {
return result.Result{Code: -1, Msg: "file object not is Image"}
}
val := reflect.ValueOf(img)
if val.CanInterface() && val.Type().Implements(token.FormType) {
fm := val.Interface().(token.Form)
return fm.CreateForm(w, field)
}
return token.DefaultForm(w, field, img.FileName)
}
func (req *acsReq) SetCheckError(check token.CheckErrorFunc) {
req.check = check
}
func (req *acsReq) CheckError(err error) error {
if res, ok := err.(result.Result); ok {
if req.check != nil {
return req.check(&res)
}
if errors.IsTokenExpired(res.Code) {
return result.ErrExpired
}
}
return err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.0.56

搜索帮助

344bd9b3 5694891 D2dac590 5694891