|
@@ -1,6 +1,7 @@
|
|
|
package wx_app
|
|
|
|
|
|
import (
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
wechat "github.com/silenceper/wechat/v2"
|
|
|
"github.com/silenceper/wechat/v2/cache"
|
|
@@ -9,8 +10,11 @@ import (
|
|
|
"github.com/silenceper/wechat/v2/miniprogram/config"
|
|
|
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
|
|
|
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
|
|
|
+ "github.com/silenceper/wechat/v2/util"
|
|
|
"hongze/hongze_yb/global"
|
|
|
+ "hongze/hongze_yb/models/tables/yb_config"
|
|
|
"hongze/hongze_yb/services/wx_app/security"
|
|
|
+ "hongze/hongze_yb/utils"
|
|
|
)
|
|
|
|
|
|
// 微信小程序配置信息
|
|
@@ -37,60 +41,100 @@ func init() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
|
|
|
+// 注: 研报小程序及备用小程序在同时运行, 基础微信API通过前端请求Header判断来源
|
|
|
+// 模板消息等API则用配置控制具体使用哪一个小程序, 同一时间仅一个小程序可推消息
|
|
|
+func GetWxApp(copyYb string) (mp *miniprogram.MiniProgram) {
|
|
|
wc := wechat.NewWechat()
|
|
|
memory := cache.NewMemory()
|
|
|
//memory := cache.NewRedis(global.Redis)
|
|
|
+
|
|
|
+ mp = new(miniprogram.MiniProgram)
|
|
|
+
|
|
|
+ appConf := getWxAppConfByRequest(copyYb)
|
|
|
+
|
|
|
cfg := &config.Config{
|
|
|
- AppID: WxAppId,
|
|
|
- AppSecret: WxAppSecret,
|
|
|
+ AppID: appConf.WxAppId,
|
|
|
+ AppSecret: appConf.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)
|
|
|
+ mp = wc.GetMiniProgram(cfg)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// GetSession 获取用户详情
|
|
|
-func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
|
|
|
- wechatClient := GetWxApp()
|
|
|
+func GetSession(code, copyYb string) (userInfo auth.ResCode2Session, err error) {
|
|
|
+ wechatClient := GetWxApp(copyYb)
|
|
|
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()
|
|
|
+func GetDecryptInfo(sessionKey, encryptedData, iv, copyYb string) (decryptData *encryptor.PlainData, err error) {
|
|
|
+ wechatClient := GetWxApp(copyYb)
|
|
|
encryptorClient := wechatClient.GetEncryptor()
|
|
|
decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// 微信小程序新版code获取手机号
|
|
|
+const (
|
|
|
+ getPhoneNumberURL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
|
|
|
+)
|
|
|
+
|
|
|
+type GetPhoneNumberResponse struct {
|
|
|
+ util.CommonError
|
|
|
+ PhoneInfo PhoneInfo `json:"phone_info"`
|
|
|
+}
|
|
|
+
|
|
|
+type PhoneInfo struct {
|
|
|
+ PhoneNumber string `json:"phoneNumber"` // 用户绑定的手机号
|
|
|
+ PurePhoneNumber string `json:"purePhoneNumber"` // 没有区号的手机号
|
|
|
+ CountryCode string `json:"countryCode"` // 区号
|
|
|
+ WaterMark struct {
|
|
|
+ Timestamp int64 `json:"timestamp"`
|
|
|
+ AppID string `json:"appid"`
|
|
|
+ } `json:"watermark"` // 数据水印
|
|
|
+}
|
|
|
+
|
|
|
+func GetPhoneNumber(code, copyYb string) (result GetPhoneNumberResponse, err error) {
|
|
|
+ var response []byte
|
|
|
+ var (
|
|
|
+ at string
|
|
|
+ )
|
|
|
+ wechatClient := GetWxApp(copyYb)
|
|
|
+ authClient := wechatClient.GetAuth()
|
|
|
+ if at, err = authClient.GetAccessToken(); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ body := map[string]interface{}{
|
|
|
+ "code": code,
|
|
|
+ }
|
|
|
+ if response, err = util.PostJSON(fmt.Sprintf(getPhoneNumberURL, at), body); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
// GetSunCode 获取太阳码
|
|
|
-func GetSunCode(page, scene string) (resp []byte, err error) {
|
|
|
+func GetSunCode(page, scene, copyYb string) (resp []byte, err error) {
|
|
|
codePars := qrcode.QRCoder{
|
|
|
Page: page,
|
|
|
Scene: scene,
|
|
|
EnvVersion: EnvVersion,
|
|
|
}
|
|
|
- wechatClient := GetWxApp()
|
|
|
+ wechatClient := GetWxApp(copyYb)
|
|
|
qr := wechatClient.GetQRCode()
|
|
|
return qr.GetWXACodeUnlimit(codePars)
|
|
|
}
|
|
|
|
|
|
// MsgSecCheck 检查一段文本是否含有违法违规内容。
|
|
|
-func MsgSecCheck(openid string, content string) (result security.Result, err error) {
|
|
|
- wechatClient := GetWxApp()
|
|
|
+func MsgSecCheck(openid, content, copyYb string) (result security.Result, err error) {
|
|
|
+ wechatClient := GetWxApp(copyYb)
|
|
|
myMiniprogram := security.NewMyMiniprogram(wechatClient)
|
|
|
bodyContent := &security.BodyContent{
|
|
|
Version: 2,
|
|
@@ -101,6 +145,44 @@ func MsgSecCheck(openid string, content string) (result security.Result, err err
|
|
|
return myMiniprogram.MsgSecCheckWithResult(bodyContent)
|
|
|
}
|
|
|
|
|
|
+type WxAppConf struct {
|
|
|
+ WxId string `description:"微信原始ID"`
|
|
|
+ WxAppId string
|
|
|
+ WxAppSecret string
|
|
|
+ WxPlatform int `description:"app来源: 6-研报小程序; 9-研报备用小程序"`
|
|
|
+ MsgRedirectTarget int `description:"公共模板消息跳转类型:1-研报小程序; 4-备用研报小程序"`
|
|
|
+}
|
|
|
+
|
|
|
+// GetWxAppConf 获取小程序配置
|
|
|
+func GetWxAppConf() (appConf *WxAppConf, err error) {
|
|
|
+ // 根据配置选择小程序
|
|
|
+ conf, e := yb_config.GetConfigByCode(yb_config.KeyUseCopyYb)
|
|
|
+ if e != nil {
|
|
|
+ err = errors.New("获取小程序配置失败, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ appConf = getWxAppConfByRequest(conf.ConfigValue)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func getWxAppConfByRequest(copyYb string) *WxAppConf {
|
|
|
+ appConf := &WxAppConf{
|
|
|
+ WxId: `gh_75abb562a946`,
|
|
|
+ WxAppId: `wxb059c872d79b9967`,
|
|
|
+ WxAppSecret: `1737c73e9f69a21de1a345b8f0800258`,
|
|
|
+ WxPlatform: utils.USER_RECORD_PLATFORM_YB,
|
|
|
+ MsgRedirectTarget: 1,
|
|
|
+ }
|
|
|
+ // 备用小程序
|
|
|
+ if copyYb == "true" {
|
|
|
+ appConf.WxAppId = `wx9a2a9b49a00513a0`
|
|
|
+ appConf.WxAppSecret = `4dea76ad9482bdd4e71cf305f669d09f`
|
|
|
+ appConf.WxPlatform = utils.USER_RECORD_PLATFORM_COPY_YB
|
|
|
+ appConf.MsgRedirectTarget = 4
|
|
|
+ }
|
|
|
+ return appConf
|
|
|
+}
|
|
|
+
|
|
|
// GetSunCode 获取太阳码
|
|
|
func GetSunCodeV2(page string) (resp []byte, err error) {
|
|
|
codePars := qrcode.QRCoder{
|
|
@@ -108,7 +190,7 @@ func GetSunCodeV2(page string) (resp []byte, err error) {
|
|
|
EnvVersion: EnvVersion,
|
|
|
Width: 256,
|
|
|
}
|
|
|
- wechatClient := GetWxApp()
|
|
|
+ wechatClient := GetWxApp("false")
|
|
|
qr := wechatClient.GetQRCode()
|
|
|
return qr.GetWXACode(codePars)
|
|
|
}
|