This commit is contained in:
孟帅
2023-05-10 23:54:50 +08:00
parent bbe655a4d8
commit 49a96750bf
314 changed files with 15138 additions and 6244 deletions

View File

@@ -0,0 +1,64 @@
package wechat
import (
"context"
"github.com/gogf/gf/v2/os/gcache"
"hotgo/internal/library/cache"
"time"
)
type Cache struct {
ctx context.Context
cache *gcache.Cache
}
// NewCache 实例化
func NewCache(ctx context.Context, name ...*gcache.Cache) *Cache {
var defaultCache = cache.Instance()
if len(name) > 0 {
defaultCache = name[0]
}
return &Cache{ctx: ctx, cache: defaultCache}
}
// SetCache 设置缓存驱动
func (r *Cache) SetCache(cache *gcache.Cache) {
r.cache = cache
}
// SetCtx 设置 ctx 参数
func (r *Cache) SetCtx(ctx context.Context) {
r.ctx = ctx
}
// Get 获取一个值
func (r *Cache) Get(key string) interface{} {
get, err := r.cache.Get(r.ctx, key)
if err != nil {
return nil
}
return get.Interface()
}
// Set 设置一个值
func (r *Cache) Set(key string, val interface{}, timeout time.Duration) error {
return r.cache.Set(r.ctx, key, val, timeout)
}
// IsExist 判断key是否存在
func (r *Cache) IsExist(key string) bool {
contains, err := r.cache.Contains(r.ctx, key)
if err != nil {
return false
}
return contains
}
// Delete 删除
func (r *Cache) Delete(key string) error {
_, err := r.cache.Remove(r.ctx, key)
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,13 @@
package wechat
import "hotgo/internal/model"
var config *model.WechatConfig
func SetConfig(c *model.WechatConfig) {
config = c
}
func GetConfig() *model.WechatConfig {
return config
}

View File

@@ -0,0 +1,108 @@
package wechat
import (
"context"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/officialaccount"
offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
officialJs "github.com/silenceper/wechat/v2/officialaccount/js"
officialOauth "github.com/silenceper/wechat/v2/officialaccount/oauth"
"github.com/silenceper/wechat/v2/openplatform"
openConfig "github.com/silenceper/wechat/v2/openplatform/config"
"hotgo/internal/consts"
)
// NewOfficialAccount 微信公众号实例
func NewOfficialAccount(ctx context.Context) *officialaccount.OfficialAccount {
cfg := &offConfig.Config{
AppID: config.OfficialAppID,
AppSecret: config.OfficialAppSecret,
Token: config.OfficialToken,
EncodingAESKey: config.OfficialEncodingAESKey,
Cache: NewCache(ctx),
}
return wechat.NewWechat().GetOfficialAccount(cfg)
}
// NewOpenPlatform 开放平台实例
func NewOpenPlatform(ctx context.Context) *openplatform.OpenPlatform {
cfg := &openConfig.Config{
AppID: config.OpenAppID,
AppSecret: config.OpenAppSecret,
Token: config.OpenToken,
EncodingAESKey: config.OpenEncodingAESKey,
Cache: NewCache(ctx),
}
return wechat.NewWechat().GetOpenPlatform(cfg)
}
// GetOpenOauthURL 代第三方公众号 - 获取网页授权地址
func GetOpenOauthURL(ctx context.Context, redirectURI, scope, state string) (location string, err error) {
op := NewOpenPlatform(ctx)
appid := config.OfficialAppID // 公众号appid
oauth := op.GetOfficialAccount(appid).PlatformOauth()
if scope == "" {
scope = consts.WechatScopeBase
}
location, err = oauth.GetRedirectURL(redirectURI, scope, state, appid)
return
}
// GetOpenUserAccessToken 代第三方公众号 - 通过网页授权的code 换取access_token
func GetOpenUserAccessToken(ctx context.Context, code string) (accessToken officialOauth.ResAccessToken, err error) {
op := NewOpenPlatform(ctx)
appid := config.OfficialAppID // 公众号appid
officialAccount := op.GetOfficialAccount(appid)
componentAccessToken, err := op.GetComponentAccessToken()
if err != nil {
return
}
accessToken, err = officialAccount.PlatformOauth().GetUserAccessToken(code, appid, componentAccessToken)
if err != nil {
return
}
if accessToken.ErrCode > 0 {
err = gerror.Newf("GetOpenUserAccessToken err:%+v", accessToken.ErrMsg)
return
}
return
}
// GetUserInfo 获取用户信息
func GetUserInfo(ctx context.Context, token officialOauth.ResAccessToken) (info officialOauth.UserInfo, err error) {
oauth := NewOfficialAccount(ctx).GetOauth()
info, err = oauth.GetUserInfo(token.AccessToken, token.OpenID, "")
return
}
// GetOauthURL 获取网页授权地址
func GetOauthURL(ctx context.Context, redirectURI, scope, state string) (location string, err error) {
oauth := NewOfficialAccount(ctx).GetOauth()
location, err = oauth.GetRedirectURL(redirectURI, scope, state)
return
}
// GetUserAccessToken 通过网页授权的code 换取access_token
func GetUserAccessToken(ctx context.Context, code string) (accessToken officialOauth.ResAccessToken, err error) {
oauth := NewOfficialAccount(ctx).GetOauth()
accessToken, err = oauth.GetUserAccessToken(code)
if err != nil {
return
}
if accessToken.ErrCode > 0 {
err = gerror.Newf("GetUserAccessToken err:%+v", accessToken.ErrMsg)
return
}
return
}
// GetJsConfig 获取js配置
func GetJsConfig(ctx context.Context, uri string) (config *officialJs.Config, err error) {
return NewOfficialAccount(ctx).GetJs().GetConfig(uri)
}