123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package wx_app
- import (
- "fmt"
- wechat "github.com/silenceper/wechat/v2"
- "github.com/silenceper/wechat/v2/cache"
- "github.com/silenceper/wechat/v2/miniprogram"
- "github.com/silenceper/wechat/v2/miniprogram/auth"
- "github.com/silenceper/wechat/v2/miniprogram/config"
- "github.com/silenceper/wechat/v2/miniprogram/encryptor"
- "github.com/silenceper/wechat/v2/miniprogram/qrcode"
- "hongze/hongze_yb/global"
- "hongze/hongze_yb/services/wx_app/security"
- )
- // 微信小程序配置信息
- var (
- WxId string //微信原始ID
- WxAppId string
- WxAppSecret string
- WxPlatform int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
- EnvVersion string // 小程序版本, release-正式版; trial-体验版; develop-开发版
- SendWxTemplateMsgUrl string
- )
- func init() {
- WxAppId = `wxb059c872d79b9967`
- WxId = `gh_75abb562a946`
- WxAppSecret = `1737c73e9f69a21de1a345b8f0800258`
- WxPlatform = 6 //弘则研报来源
- if global.CONFIG.Serve.RunMode == "release" {
- EnvVersion = "release"
- SendWxTemplateMsgUrl = "http://127.0.0.1:8086/v1/wechat/send_template_msg"
- } else {
- EnvVersion = "trial"
- SendWxTemplateMsgUrl = "http://127.0.0.1:8086/v1/wechat/send_template_msg"
- }
- }
- func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
- wc := wechat.NewWechat()
- memory := cache.NewMemory()
- //memory := cache.NewRedis(global.Redis)
- cfg := &config.Config{
- AppID: WxAppId,
- AppSecret: WxAppSecret,
- Cache: memory,
- }
- miniprogram = wc.GetMiniProgram(cfg)
- return
- }
- // GetSession 获取用户详情
- func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
- wechatClient := GetWxApp()
- authClient := wechatClient.GetAuth()
- userInfo, err = authClient.Code2Session(code)
- return
- }
- // GetSession 获取用户详情
- func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
- wechatClient := GetWxApp()
- authClient := wechatClient.GetAuth()
- fmt.Println("code:", code)
- userInfo, err = authClient.Code2Session(code)
- return
- }
- // 获取解密信息 GetDecryptInfo
- func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
- wechatClient := GetWxApp()
- encryptorClient := wechatClient.GetEncryptor()
- decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
- return
- }
- // GetSunCode 获取太阳码
- func GetSunCode(page, scene string) (resp []byte, err error) {
- codePars := qrcode.QRCoder{
- Page: page,
- Scene: scene,
- EnvVersion: EnvVersion,
- }
- wechatClient := GetWxApp()
- qr := wechatClient.GetQRCode()
- return qr.GetWXACodeUnlimit(codePars)
- }
- // MsgSecCheck 检查一段文本是否含有违法违规内容。
- func MsgSecCheck(openid string, content string) (result security.Result, err error) {
- wechatClient := GetWxApp()
- myMiniprogram := security.NewMyMiniprogram(wechatClient)
- bodyContent := &security.BodyContent{
- Version: 2,
- Content: content,
- Openid: openid,
- Scene: 2,
- }
- return myMiniprogram.MsgSecCheckWithResult(bodyContent)
- }
- // GetSunCode 获取太阳码
- func GetSunCodeV2(page string) (resp []byte, err error) {
- codePars := qrcode.QRCoder{
- Path: page,
- EnvVersion: EnvVersion,
- Width: 256,
- }
- wechatClient := GetWxApp()
- qr := wechatClient.GetQRCode()
- return qr.GetWXACode(codePars)
- }
|