zwxi 10 月之前
父節點
當前提交
17f9266532
共有 2 個文件被更改,包括 48 次插入30 次删除
  1. 26 20
      controller/public.go
  2. 22 10
      services/wx_app/wx_app.go

+ 26 - 20
controller/public.go

@@ -5,7 +5,6 @@ import (
 	"errors"
 	"fmt"
 	"github.com/gin-gonic/gin"
-	"github.com/skip2/go-qrcode"
 	"hongze/hongze_yb/controller/response"
 	"hongze/hongze_yb/global"
 	"hongze/hongze_yb/logic"
@@ -23,6 +22,7 @@ import (
 	"hongze/hongze_yb/services"
 	"hongze/hongze_yb/services/alarm_msg"
 	"hongze/hongze_yb/services/user"
+	"hongze/hongze_yb/services/wx_app"
 	"hongze/hongze_yb/utils"
 	"io/ioutil"
 	"net/url"
@@ -542,35 +542,41 @@ func BannerGetQRCode(c *gin.Context) {
 	}
 	companyName := companyInfo.CompanyName
 
-	userInfo := user.GetInfoByClaims(c)
-	userDetail, err, errMsg := userLogic.GetUserInfo(userInfo)
-	if err != nil {
-		if errMsg != "" {
-			errMsg = "获取失败"
-		}
-		response.Fail(errMsg, c)
-		return
-	}
-	randStr := utils.GetRandStringNoSpecialChar(28)
-	filePath := "./static/" + randStr + ".png"
-	fileName := randStr + ".png"
-
 	companyNameCode := url.QueryEscape(companyName) // 进行URL编码
 	remarkCode := url.QueryEscape(remark) // 进行URL编码
 	url := "pages-report/signUpPage/signUpPage?RealName=%s&CompanyName=%s&Mobile=%s&BannerId=%d&Title=%s"
-	url = fmt.Sprintf(url, wxUserInfo.RealName, companyNameCode, userDetail.Mobile, bannerId, remarkCode)
+	url = fmt.Sprintf(url, wxUserInfo.RealName, companyNameCode, wxUserInfo.Mobile, bannerId, remarkCode)
 
-	err = qrcode.WriteFile(url, qrcode.Medium, 256, filePath)
+	picByte, err := wx_app.GetSunCodeV2(url)
 	if err != nil {
-		response.FailData("生成二维码失败", "生成二维码失败,Err:"+err.Error(), c)
+		return
+	}
+	// 生成图片
+	localPath := "./static/img"
+	fileName := utils.GetRandStringNoSpecialChar(28) + ".png"
+	fpath := fmt.Sprint(localPath, "/", fileName)
+	f, err := os.Create(fpath)
+	if err != nil {
+		return
+	}
+	if _, err = f.Write(picByte); err != nil {
+		return
 	}
-
 	defer func() {
-		os.Remove(filePath)
+		f.Close()
+		os.Remove(fpath)
 	}()
+	//err = qrcode.WriteFile(url, qrcode.Medium, 256, filePath)
+	//if err != nil {
+	//	response.FailData("生成二维码失败", "生成二维码失败,Err:"+err.Error(), c)
+	//}
+	//
+	//defer func() {
+	//	os.Remove(filePath)
+	//}()
 
 	//上传到阿里云
-	resourceUrl, err := services.UploadAliyun(fileName, filePath)
+	resourceUrl, err := services.UploadAliyun(fileName, fpath)
 	if err != nil {
 		response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
 		return

+ 22 - 10
services/wx_app/wx_app.go

@@ -13,13 +13,13 @@ import (
 	"hongze/hongze_yb/services/wx_app/security"
 )
 
-//微信小程序配置信息
+// 微信小程序配置信息
 var (
-	WxId        string //微信原始ID
-	WxAppId     string
-	WxAppSecret string
-	WxPlatform  int    //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
-	EnvVersion  string // 小程序版本, release-正式版; trial-体验版; develop-开发版
+	WxId                 string //微信原始ID
+	WxAppId              string
+	WxAppSecret          string
+	WxPlatform           int    //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
+	EnvVersion           string // 小程序版本, release-正式版; trial-体验版; develop-开发版
 	SendWxTemplateMsgUrl string
 )
 
@@ -89,14 +89,26 @@ func GetSunCode(page, scene string) (resp []byte, err error) {
 }
 
 // MsgSecCheck 检查一段文本是否含有违法违规内容。
-func MsgSecCheck(openid string,content string) (result security.Result, err error) {
+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,
+		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)
+}