im 管理后台
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// Package wechat
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2025 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
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 || get.IsNil() || get.IsEmpty() {
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Package wechat
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2025 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"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"
|
||||
"hotgo/internal/model"
|
||||
)
|
||||
|
||||
var (
|
||||
config *model.WechatConfig
|
||||
officialAccount *officialaccount.OfficialAccount // 微信公众号
|
||||
)
|
||||
|
||||
func SetConfig(c *model.WechatConfig) {
|
||||
config = c
|
||||
ctx := gctx.GetInitCtx()
|
||||
GetOfficialAccount(ctx, true)
|
||||
}
|
||||
|
||||
func GetConfig() *model.WechatConfig {
|
||||
return config
|
||||
}
|
||||
|
||||
// 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),
|
||||
UseStableAK: true,
|
||||
}
|
||||
wc := wechat.NewWechat()
|
||||
return wc.GetOfficialAccount(cfg)
|
||||
}
|
||||
|
||||
// GetOfficialAccount 微信公众号实例
|
||||
func GetOfficialAccount(ctx context.Context, refresh ...bool) *officialaccount.OfficialAccount {
|
||||
isRefresh := false
|
||||
if len(refresh) > 0 {
|
||||
isRefresh = refresh[0]
|
||||
}
|
||||
if officialAccount != nil && !isRefresh {
|
||||
return officialAccount
|
||||
}
|
||||
|
||||
officialAccount = NewOfficialAccount(ctx)
|
||||
return officialAccount
|
||||
}
|
||||
|
||||
// GetJsConfig 获取js配置
|
||||
func GetJsConfig(ctx context.Context, uri string) (config *officialJs.Config, err error) {
|
||||
return GetOfficialAccount(ctx).GetJs().GetConfig(uri)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Package wechat
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2025 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
officialOauth "github.com/silenceper/wechat/v2/officialaccount/oauth"
|
||||
)
|
||||
|
||||
// GetUserInfo 获取用户信息
|
||||
func GetUserInfo(ctx context.Context, token officialOauth.ResAccessToken) (info officialOauth.UserInfo, err error) {
|
||||
oauth := GetOfficialAccount(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 := GetOfficialAccount(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 := GetOfficialAccount(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
|
||||
}
|
||||
Reference in New Issue
Block a user