wx_app.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package services
  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. "hongze/hz_crm_api/models"
  13. "hongze/hz_crm_api/utils"
  14. "os"
  15. "time"
  16. )
  17. // 微信小程序配置信息
  18. var (
  19. WxId string //微信原始ID
  20. WxAppId string
  21. WxAppSecret string
  22. WxPlatform int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
  23. EnvVersion string // 小程序版本, release-正式版; trial-体验版; develop-开发版
  24. )
  25. func init() {
  26. WxAppId = `wxb059c872d79b9967`
  27. WxId = `gh_75abb562a946`
  28. WxAppSecret = `1737c73e9f69a21de1a345b8f0800258`
  29. WxPlatform = 6 //弘则研报来源
  30. }
  31. func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
  32. wc := wechat.NewWechat()
  33. memory := cache.NewMemory()
  34. //memory := cache.NewRedis(global.Redis)
  35. cfg := &config.Config{
  36. AppID: WxAppId,
  37. AppSecret: WxAppSecret,
  38. Cache: memory,
  39. }
  40. miniprogram = wc.GetMiniProgram(cfg)
  41. return
  42. }
  43. // GetSession 获取用户详情
  44. func GetSession(code string) (userInfo auth.ResCode2Session, err error) {
  45. wechatClient := GetWxApp()
  46. authClient := wechatClient.GetAuth()
  47. userInfo, err = authClient.Code2Session(code)
  48. return
  49. }
  50. // GetSession 获取用户详情
  51. func GetUserInfo(code string) (userInfo auth.ResCode2Session, err error) {
  52. wechatClient := GetWxApp()
  53. authClient := wechatClient.GetAuth()
  54. fmt.Println("code:", code)
  55. userInfo, err = authClient.Code2Session(code)
  56. return
  57. }
  58. // 获取解密信息 GetDecryptInfo
  59. func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encryptor.PlainData, err error) {
  60. wechatClient := GetWxApp()
  61. encryptorClient := wechatClient.GetEncryptor()
  62. decryptData, err = encryptorClient.Decrypt(sessionKey, encryptedData, iv)
  63. return
  64. }
  65. // GetSunCode 获取太阳码
  66. func GetSunCode(page, scene string) (resp []byte, err error) {
  67. // 此处因初始化顺序问题不放在init中
  68. env := "trial"
  69. if utils.RunMode == "release" {
  70. env = "release"
  71. }
  72. codePars := qrcode.QRCoder{
  73. Page: page,
  74. Scene: scene,
  75. EnvVersion: env,
  76. }
  77. wechatClient := GetWxApp()
  78. qr := wechatClient.GetQRCode()
  79. return qr.GetWXACodeUnlimit(codePars)
  80. }
  81. // PcCreateAndUploadSunCode 生成太阳码并上传OSS
  82. func PcCreateAndUploadSunCode(scene, page string) (imgUrl string, err error) {
  83. if page == "" {
  84. err = errors.New("page不能为空")
  85. return
  86. }
  87. // scene超过32位会生成失败,md5处理至32位
  88. sceneMD5 := "a=1"
  89. if scene != "" {
  90. sceneMD5 = utils.MD5(scene)
  91. }
  92. picByte, err := GetSunCode(page, sceneMD5)
  93. if err != nil {
  94. return
  95. }
  96. // 生成图片
  97. localPath := "./static/imgs"
  98. fileName := utils.GetRandStringNoSpecialChar(28) + ".png"
  99. fpath := fmt.Sprint(localPath, "/", fileName)
  100. f, err := os.Create(fpath)
  101. if err != nil {
  102. fmt.Println("11111")
  103. return
  104. }
  105. if _, err = f.Write(picByte); err != nil {
  106. return
  107. }
  108. defer func() {
  109. f.Close()
  110. os.Remove(fpath)
  111. }()
  112. // 上传OSS
  113. fileDir := "yb/suncode/"
  114. imgUrl, err = UploadAliyunToDir(fileName, fpath, "", fileDir)
  115. if err != nil {
  116. return
  117. }
  118. if err != nil {
  119. return
  120. }
  121. // 记录参数
  122. if scene != "" {
  123. newSuncode := &models.YbPcSuncode{
  124. Scene: scene,
  125. SceneMd5: sceneMD5,
  126. CodePage: page,
  127. SuncodeUrl: imgUrl,
  128. CreateTime: time.Now(),
  129. }
  130. err = models.AddYbPcSunCode(newSuncode)
  131. }
  132. // 记录参数md5
  133. if scene != "" {
  134. newPars := &models.YbSuncodePars{
  135. Scene: scene,
  136. SceneKey: sceneMD5,
  137. CreateTime: time.Now(),
  138. }
  139. err = models.AddYbSuncodePars(newPars)
  140. }
  141. return
  142. }