wx_app.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package wx_app
  2. import (
  3. "errors"
  4. "fmt"
  5. wechat "github.com/silenceper/wechat/v2"
  6. "github.com/silenceper/wechat/v2/cache"
  7. "github.com/silenceper/wechat/v2/miniprogram"
  8. "github.com/silenceper/wechat/v2/miniprogram/auth"
  9. "github.com/silenceper/wechat/v2/miniprogram/config"
  10. "github.com/silenceper/wechat/v2/miniprogram/encryptor"
  11. "github.com/silenceper/wechat/v2/miniprogram/qrcode"
  12. "github.com/silenceper/wechat/v2/util"
  13. "hongze/hongze_yb/global"
  14. "hongze/hongze_yb/models/tables/yb_config"
  15. "hongze/hongze_yb/services/wx_app/security"
  16. "hongze/hongze_yb/utils"
  17. )
  18. // 微信小程序配置信息
  19. var (
  20. WxId string //微信原始ID
  21. WxAppId string
  22. WxAppSecret string
  23. WxPlatform int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
  24. EnvVersion string // 小程序版本, release-正式版; trial-体验版; develop-开发版
  25. SendWxTemplateMsgUrl string
  26. )
  27. func init() {
  28. WxAppId = `wxb059c872d79b9967`
  29. WxId = `gh_75abb562a946`
  30. WxAppSecret = `1737c73e9f69a21de1a345b8f0800258`
  31. WxPlatform = 6 //弘则研报来源
  32. if global.CONFIG.Serve.RunMode == "release" {
  33. EnvVersion = "release"
  34. SendWxTemplateMsgUrl = "http://127.0.0.1:8086/v1/wechat/send_template_msg"
  35. } else {
  36. EnvVersion = "trial"
  37. SendWxTemplateMsgUrl = "http://127.0.0.1:8086/v1/wechat/send_template_msg"
  38. }
  39. }
  40. // 注: 研报小程序及备用小程序在同时运行, 基础微信API通过前端请求Header判断来源
  41. // 模板消息等API则用配置控制具体使用哪一个小程序, 同一时间仅一个小程序可推消息
  42. func GetWxApp(copyYb string) (mp *miniprogram.MiniProgram) {
  43. wc := wechat.NewWechat()
  44. memory := cache.NewMemory()
  45. //memory := cache.NewRedis(global.Redis)
  46. mp = new(miniprogram.MiniProgram)
  47. appConf := getWxAppConfByRequest(copyYb)
  48. cfg := &config.Config{
  49. AppID: appConf.WxAppId,
  50. AppSecret: appConf.WxAppSecret,
  51. Cache: memory,
  52. }
  53. mp = wc.GetMiniProgram(cfg)
  54. return
  55. }
  56. // GetSession 获取用户详情
  57. func GetSession(code, copyYb string) (userInfo auth.ResCode2Session, err error) {
  58. wechatClient := GetWxApp(copyYb)
  59. authClient := wechatClient.GetAuth()
  60. userInfo, err = authClient.Code2Session(code)
  61. return
  62. }
  63. // 获取解密信息 GetDecryptInfo
  64. func GetDecryptInfo(sessionKey, encryptedData, iv, copyYb string) (decryptData *encryptor.PlainData, err error) {
  65. wechatClient := GetWxApp(copyYb)
  66. encryptorClient := wechatClient.GetEncryptor()
  67. decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
  68. return
  69. }
  70. // 微信小程序新版code获取手机号
  71. const (
  72. getPhoneNumberURL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
  73. )
  74. type GetPhoneNumberResponse struct {
  75. util.CommonError
  76. PhoneInfo PhoneInfo `json:"phone_info"`
  77. }
  78. type PhoneInfo struct {
  79. PhoneNumber string `json:"phoneNumber"` // 用户绑定的手机号
  80. PurePhoneNumber string `json:"purePhoneNumber"` // 没有区号的手机号
  81. CountryCode string `json:"countryCode"` // 区号
  82. WaterMark struct {
  83. Timestamp int64 `json:"timestamp"`
  84. AppID string `json:"appid"`
  85. } `json:"watermark"` // 数据水印
  86. }
  87. func GetPhoneNumber(code, copyYb string) (result GetPhoneNumberResponse, err error) {
  88. var response []byte
  89. var (
  90. at string
  91. )
  92. wechatClient := GetWxApp(copyYb)
  93. authClient := wechatClient.GetAuth()
  94. if at, err = authClient.GetAccessToken(); err != nil {
  95. return
  96. }
  97. body := map[string]interface{}{
  98. "code": code,
  99. }
  100. if response, err = util.PostJSON(fmt.Sprintf(getPhoneNumberURL, at), body); err != nil {
  101. return
  102. }
  103. if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil {
  104. return
  105. }
  106. return
  107. }
  108. // GetSunCode 获取太阳码
  109. func GetSunCode(page, scene, copyYb string) (resp []byte, err error) {
  110. codePars := qrcode.QRCoder{
  111. Page: page,
  112. Scene: scene,
  113. EnvVersion: EnvVersion,
  114. }
  115. wechatClient := GetWxApp(copyYb)
  116. qr := wechatClient.GetQRCode()
  117. return qr.GetWXACodeUnlimit(codePars)
  118. }
  119. // MsgSecCheck 检查一段文本是否含有违法违规内容。
  120. func MsgSecCheck(openid, content, copyYb string) (result security.Result, err error) {
  121. wechatClient := GetWxApp(copyYb)
  122. myMiniprogram := security.NewMyMiniprogram(wechatClient)
  123. bodyContent := &security.BodyContent{
  124. Version: 2,
  125. Content: content,
  126. Openid: openid,
  127. Scene: 2,
  128. }
  129. return myMiniprogram.MsgSecCheckWithResult(bodyContent)
  130. }
  131. type WxAppConf struct {
  132. WxId string `description:"微信原始ID"`
  133. WxAppId string
  134. WxAppSecret string
  135. WxPlatform int `description:"app来源: 6-研报小程序; 9-研报备用小程序"`
  136. MsgRedirectTarget int `description:"公共模板消息跳转类型:1-研报小程序; 4-备用研报小程序"`
  137. }
  138. // GetWxAppConf 获取小程序配置
  139. func GetWxAppConf() (appConf *WxAppConf, err error) {
  140. // 根据配置选择小程序
  141. conf, e := yb_config.GetConfigByCode(yb_config.KeyUseCopyYb)
  142. if e != nil {
  143. err = errors.New("获取小程序配置失败, Err: " + e.Error())
  144. return
  145. }
  146. appConf = getWxAppConfByRequest(conf.ConfigValue)
  147. return
  148. }
  149. func getWxAppConfByRequest(copyYb string) *WxAppConf {
  150. appConf := &WxAppConf{
  151. WxId: `gh_75abb562a946`,
  152. WxAppId: `wxb059c872d79b9967`,
  153. WxAppSecret: `1737c73e9f69a21de1a345b8f0800258`,
  154. WxPlatform: utils.USER_RECORD_PLATFORM_YB,
  155. MsgRedirectTarget: 1,
  156. }
  157. // 备用小程序
  158. if copyYb == "true" {
  159. appConf.WxAppId = `wx9a2a9b49a00513a0`
  160. appConf.WxAppSecret = `4dea76ad9482bdd4e71cf305f669d09f`
  161. appConf.WxPlatform = utils.USER_RECORD_PLATFORM_COPY_YB
  162. appConf.MsgRedirectTarget = 4
  163. }
  164. return appConf
  165. }
  166. // GetSunCode 获取太阳码
  167. func GetSunCodeV2(page string) (resp []byte, err error) {
  168. codePars := qrcode.QRCoder{
  169. Path: page,
  170. EnvVersion: EnvVersion,
  171. Width: 256,
  172. }
  173. wechatClient := GetWxApp("false")
  174. qr := wechatClient.GetQRCode()
  175. return qr.GetWXACode(codePars)
  176. }