hcmp/weapp/wxmp.go
2024-07-31 15:48:23 +08:00

88 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package weapp
import (
"hc/dbop"
log "github.com/sirupsen/logrus"
"github.com/shockliu/logger"
wechat "github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/miniprogram"
"github.com/silenceper/wechat/v2/miniprogram/auth"
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
//"github.com/silenceper/wechat/v2/officialaccount/material"
minimsg "github.com/silenceper/wechat/v2/miniprogram/message"
)
var (
// openID chan string
// oa *officialaccount.OfficialAccount
// kf *message.Manager
// tm *message.Template
//mt *material.Material
// miniprogram
mp *miniprogram.MiniProgram
au *auth.Auth
qr *qrcode.QRCode
cm *minimsg.Manager // 维护小程序
)
func init() {
// 设置log "github.com/sirupsen/logrus"的日志级别
// log.SetLevel(log.WarnLevel)
log.SetLevel(log.InfoLevel)
// openID = make(chan string, 10)
wc := wechat.NewWechat()
//这里本地内存保存access_token也可选择redismemcache或者自定cache
// redisOpts := &cache.RedisOpts{
// Host: "127.0.0.1:6379", // redis host
// Password: "DqklFshop8848", //redis password
// Database: 1, // redis db
// MaxActive: 10, // 连接池最大活跃连接数
// MaxIdle: 10, //连接池最大空闲连接数
// IdleTimeout: 60, //空闲连接超时时间,单位:second
// }
// redisCache := cache.NewRedis(context.Background(), redisOpts)
// //memory := cache.NewMemory()
// // 事集公众号相关配置
// cfg := &offConfig.Config{
// AppID: "wx0d77e0af930eacc1",
// AppSecret: "119554764ba746d8d6e882c098bf78fe",
// Token: "DqklShijiWLSC8848",
// EncodingAESKey: "Sujw4wE0mbxfiDx40A0gLAzvRhgVnT4Y2KSTyVMVKoH",
// Cache: redisCache,
// }
// oa = wc.GetOfficialAccount(cfg)
// kf = oa.GetCustomerMessageManager()
// tm = oa.GetTemplate()
// //mt = oa.GetMaterial()
// go UserInit()
// 康养小程序
SelfCache := dbop.NewSelfCache("HCWXMAPP:")
minicfg := &miniConfig.Config{
AppID: "wxe9c749a836dee2a8",
AppSecret: "51cbc34547a97c661af7e8855a48d808",
Cache: SelfCache,
}
mp = wc.GetMiniProgram(minicfg)
au = mp.GetAuth()
qr = mp.GetQRCode()
cm = mp.GetCustomerMessage() // 康养小程序客服信息
}
func GetAuth(code string) (string, string, string, bool) {
rst, err := au.Code2Session(code)
if err != nil {
logger.Warnf("Code2Session Err:%s\n", err)
return "", "", "", false
} else {
return rst.SessionKey, rst.OpenID, rst.UnionID, true
}
}