Browse Source

Merge branch 'yb/new_yb' into debug

hsun 2 years ago
parent
commit
45ced05535

+ 3 - 1
controller/comment/comment.go

@@ -12,13 +12,15 @@ import (
 
 // Comment 发布留言
 func Comment(c *gin.Context)  {
+	// 是否为备用小程序
+	copyYb := c.Request.Header.Get("CopyYb")
 	var req reqComment.ReqComment
 	if c.ShouldBind(&req) !=nil {
 		response.Fail("入参错误", c)
 		return
 	}
 	userInfo := userService.GetInfoByClaims(c)
-	data, err := commentService.Comment(userInfo, req)
+	data, err := commentService.Comment(userInfo, req, copyYb)
 	if err != nil {
 		response.Fail(err.Error(),c)
 		return

+ 3 - 1
controller/community/comment.go

@@ -14,6 +14,8 @@ import (
 
 // Comment 发布留言
 func Comment(c *gin.Context) {
+	// 是否为备用小程序
+	copyYb := c.Request.Header.Get("CopyYb")
 	var req request.ReqComment
 	if c.ShouldBind(&req) != nil {
 		response.Fail("入参错误", c)
@@ -39,7 +41,7 @@ func Comment(c *gin.Context) {
 		req.Source = 1
 	}
 
-	ybCommunityQuestionComment, err, errMsg := yb_community_question.Comment(userInfo, req.CommunityQuestionID, req.Content, req.SourceAgent, req.IsShowName, req.Source, qaAvatarUrl)
+	ybCommunityQuestionComment, err, errMsg := yb_community_question.Comment(userInfo, req.CommunityQuestionID, req.Content, req.SourceAgent, req.IsShowName, req.Source, qaAvatarUrl, copyYb)
 	if err != nil {
 		response.FailMsg(errMsg, err.Error(), c)
 		return

+ 3 - 1
controller/community/question.go

@@ -104,6 +104,8 @@ func QuestionDetail(c *gin.Context) {
 // @failure 400 {string} string "操作失败"
 // @Router /question/ask [post]
 func QuestionAsk(c *gin.Context) {
+	// 是否为备用小程序
+	copyYb := c.Request.Header.Get("CopyYb")
 	var req request.QuestionAskReq
 	if err := c.ShouldBind(&req); err != nil {
 		response.Fail("参数有误", c)
@@ -121,7 +123,7 @@ func QuestionAsk(c *gin.Context) {
 	// 敏感词校验, 只有小程序的用户才能走敏感词过滤接口
 	userinfo := user.GetInfoByClaims(c)
 	if userinfo.RecordInfo.OpenID != "" && (userinfo.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_YB || userinfo.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_COPY_YB) {
-		checkResult, e := wx_app.MsgSecCheck(userinfo.RecordInfo.OpenID, req.QuestionContent)
+		checkResult, e := wx_app.MsgSecCheck(userinfo.RecordInfo.OpenID, req.QuestionContent, copyYb)
 		if e == nil {
 			if checkResult.Result != nil && checkResult.Result.Suggest != "pass" {
 				errMsg := "含有违禁词, 不允许发布: " + checkResult.Result.Suggest + ", 命中标签: " + strconv.Itoa(checkResult.Result.Label)

+ 3 - 1
controller/public.go

@@ -101,6 +101,8 @@ func Upload(c *gin.Context) {
 // @failure 400 {string} string "获取失败"
 // @Router /public/get_share_poster [post]
 func GetSharePoster(c *gin.Context) {
+	// 是否为备用小程序
+	copyYb := c.Request.Header.Get("CopyYb")
 	var req services.SharePosterReq
 	if c.ShouldBind(&req) != nil {
 		response.Fail("参数异常", c)
@@ -110,7 +112,7 @@ func GetSharePoster(c *gin.Context) {
 		response.Fail("来源有误", c)
 		return
 	}
-	imgUrl, err := services.CreatePosterFromSourceV2(req.CodePage, req.CodeScene, req.Source, req.Version, req.Pars)
+	imgUrl, err := services.CreatePosterFromSourceV2(req.CodePage, req.CodeScene, req.Source, req.Version, req.Pars, copyYb)
 	if err != nil {
 		response.FailData("获取分享海报失败", "获取分享海报失败, Err: "+err.Error(), c)
 		return

+ 16 - 9
controller/wechat/wechat.go

@@ -9,6 +9,7 @@ import (
 	userService "hongze/hongze_yb/services/user"
 	"hongze/hongze_yb/services/wechat"
 	"hongze/hongze_yb/services/wx_app"
+	"hongze/hongze_yb/utils"
 	"strconv"
 )
 
@@ -29,9 +30,11 @@ func GetUserInfo(c *gin.Context) {
 
 func GetUserSession(c *gin.Context) {
 	code, _ := c.GetQuery("code")
+	// 是否为备用小程序
+	copyYb := c.Request.Header.Get("CopyYb")
 	//c.Sho
 	//fmt.Println(c.Request.)
-	userInfo, err := wx_app.GetSession(code)
+	userInfo, err := wx_app.GetSession(code, copyYb)
 	if err != nil {
 		response.Fail("获取失败,Err:"+err.Error(), c)
 		return
@@ -51,18 +54,20 @@ func GetUserSession(c *gin.Context) {
 func Login(c *gin.Context) {
 	//code, _ := c.GetQuery("code")
 	code := c.DefaultQuery("code", "")
-	wxUserInfo, err := wx_app.GetSession(code)
+	// 是否为备用小程序
+	copyYb := c.Request.Header.Get("CopyYb")
+
+	wxUserInfo, err := wx_app.GetSession(code, copyYb)
 	if err != nil {
 		response.Fail("获取失败,Err:"+err.Error(), c)
 		return
 	}
-	appConf, e := wx_app.GetWxAppConf()
-	if e != nil {
-		response.FailMsg("获取失败", "获取小程序配置失败, Err: " + e.Error(), c)
-		return
+	wxPlatform := utils.USER_RECORD_PLATFORM_YB
+	if copyYb == "true" {
+		wxPlatform = utils.USER_RECORD_PLATFORM_COPY_YB
 	}
 
-	token, userId, isBind, err := user.WxLogin(appConf.WxPlatform, wxUserInfo)
+	token, userId, isBind, err := user.WxLogin(wxPlatform, wxUserInfo)
 	if err != nil {
 		response.Fail("登录失败,Err:"+err.Error(), c)
 		return
@@ -94,6 +99,8 @@ type EncryptReq struct {
 // @Success 200 {string} string "获取成功"
 // @Router /wechat/getEncryptInfo [post]
 func GetEncryptInfo(c *gin.Context) {
+	// 是否为备用小程序
+	copyYb := c.Request.Header.Get("CopyYb")
 	var req EncryptReq
 	err := c.ShouldBind(&req)
 	if err != nil {
@@ -114,7 +121,7 @@ func GetEncryptInfo(c *gin.Context) {
 		response.Fail("请重新登录", c)
 		return
 	}
-	decryptData, err := wx_app.GetDecryptInfo(userInfo.RecordInfo.SessionKey, req.EncryptedData, req.Iv)
+	decryptData, err := wx_app.GetDecryptInfo(userInfo.RecordInfo.SessionKey, req.EncryptedData, req.Iv, copyYb)
 	if err != nil {
 		response.Fail("获取失败,Err:"+err.Error(), c)
 		return
@@ -128,7 +135,7 @@ func GetEncryptInfo(c *gin.Context) {
 	purePhoneNumber := ""
 	if req.Code != "" {
 		// 新版code获取手机号
-		if phoneInfo, err := wx_app.GetPhoneNumber(req.Code); err != nil {
+		if phoneInfo, err := wx_app.GetPhoneNumber(req.Code, copyYb); err != nil {
 			response.Fail("code获取手机号信息失败, Err:" + err.Error(), c)
 			return
 		} else {

+ 2 - 2
logic/yb_community_question/yb_community_question_comment.go

@@ -24,7 +24,7 @@ import (
 )
 
 // Comment 发布留言
-func Comment(user user.UserInfo, communityQuestionID uint32, content string, sourceAgent, isShowName, source int8, qaAvatarUrl string) (ybCommunityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment, err error, errMsg string) {
+func Comment(user user.UserInfo, communityQuestionID uint32, content string, sourceAgent, isShowName, source int8, qaAvatarUrl, copyYb string) (ybCommunityQuestionComment *yb_community_question_comment.YbCommunityQuestionComment, err error, errMsg string) {
 	errMsg = "发布留言失败"
 	defer func() {
 		if err != nil {
@@ -60,7 +60,7 @@ func Comment(user user.UserInfo, communityQuestionID uint32, content string, sou
 
 	// 敏感词过滤
 	if user.RecordInfo.OpenID != "" && (user.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_YB || user.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_COPY_YB) { //只有小程序的用户才能走敏感词过滤接口
-		checkResult, tErr := wx_app.MsgSecCheck(user.RecordInfo.OpenID, content)
+		checkResult, tErr := wx_app.MsgSecCheck(user.RecordInfo.OpenID, content, copyYb)
 		/*if tErr != nil {
 			errMsg = "敏感词过滤失败" + tErr.Error()
 			err = errors.New("敏感词过滤失败")

+ 2 - 2
services/comment/comment.go

@@ -17,7 +17,7 @@ import (
 )
 
 // Comment 发布留言
-func Comment(user user.UserInfo, req reqComment.ReqComment) (ret response.RespCommentAdd, err error) {
+func Comment(user user.UserInfo, req reqComment.ReqComment, copyYb string) (ret response.RespCommentAdd, err error) {
 	var errMsg string
 	defer func() {
 		if err != nil {
@@ -50,7 +50,7 @@ func Comment(user user.UserInfo, req reqComment.ReqComment) (ret response.RespCo
 	}
 	// 敏感词过滤
 	if user.RecordInfo.OpenID != "" && (user.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_YB || user.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_COPY_YB) {   //只有小程序的用户才能走敏感词过滤接口
-		checkResult, tErr := wx_app.MsgSecCheck(user.RecordInfo.OpenID, req.Content)
+		checkResult, tErr := wx_app.MsgSecCheck(user.RecordInfo.OpenID, req.Content, copyYb)
 		/*if tErr != nil {
 			errMsg = "敏感词过滤失败" + tErr.Error()
 			err = errors.New("敏感词过滤失败")

+ 4 - 4
services/share_poster.go

@@ -81,7 +81,7 @@ func Html2ImgHttpPost(url, postData string, params ...string) ([]byte, error) {
 }
 
 // CreateAndUploadSunCode 生成太阳码并上传OSS
-func CreateAndUploadSunCode(page, scene, version string) (imgUrl string, err error) {
+func CreateAndUploadSunCode(page, scene, version, copyYb string) (imgUrl string, err error) {
 	if page == "" {
 		err = errors.New("page不能为空")
 		return
@@ -99,7 +99,7 @@ func CreateAndUploadSunCode(page, scene, version string) (imgUrl string, err err
 	if scene != "" {
 		sceneMD5 = utils.MD5(scene)
 	}
-	picByte, err := wx_app.GetSunCode(page, sceneMD5)
+	picByte, err := wx_app.GetSunCode(page, sceneMD5, copyYb)
 	if err != nil {
 		return
 	}
@@ -149,7 +149,7 @@ func CreateAndUploadSunCode(page, scene, version string) (imgUrl string, err err
 }
 
 // CreatePosterFromSourceV2 生成分享海报(通过配置获取相关信息)
-func CreatePosterFromSourceV2(codePage, codeScene, source, version, pars string) (imgUrl string, err error) {
+func CreatePosterFromSourceV2(codePage, codeScene, source, version, pars, copyYb string) (imgUrl string, err error) {
 	var errMsg string
 	defer func() {
 		if err != nil {
@@ -189,7 +189,7 @@ func CreatePosterFromSourceV2(codePage, codeScene, source, version, pars string)
 	width := ybPosterConfig.Width
 	height := ybPosterConfig.Hight
 	//生成太阳码
-	sunCodeUrl, err := CreateAndUploadSunCode(codePage, codeScene, version)
+	sunCodeUrl, err := CreateAndUploadSunCode(codePage, codeScene, version, copyYb)
 	if err != nil {
 		return
 	}

+ 10 - 1
services/sun_code.go

@@ -3,6 +3,7 @@ package services
 import (
 	"errors"
 	"fmt"
+	"hongze/hongze_yb/models/tables/yb_config"
 	"hongze/hongze_yb/models/tables/yb_pc_suncode"
 	"hongze/hongze_yb/models/tables/yb_suncode_pars"
 	"hongze/hongze_yb/services/wx_app"
@@ -29,7 +30,15 @@ func PcCreateAndUploadSunCode(scene, page string) (imgUrl string, err error) {
 	if scene != "" {
 		sceneMD5 = utils.MD5(scene)
 	}
-	picByte, err := wx_app.GetSunCode(page, sceneMD5)
+
+	// 根据配置选择小程序
+	conf, e := yb_config.GetConfigByCode(yb_config.KeyUseCopyYb)
+	if e != nil {
+		err = errors.New("获取小程序配置失败, Err: " + e.Error())
+		return
+	}
+
+	picByte, err := wx_app.GetSunCode(page, sceneMD5, conf.ConfigValue)
 	if err != nil {
 		return
 	}

+ 23 - 28
services/wx_app/wx_app.go

@@ -41,17 +41,16 @@ func init() {
 	}
 }
 
-func GetWxApp() (mp *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, e := GetWxAppConf()
-	if e != nil {
-		return mp
-	}
+	appConf := getWxAppConfByRequest(copyYb)
 
 	cfg := &config.Config{
 		AppID:     appConf.WxAppId,
@@ -64,25 +63,16 @@ func GetWxApp() (mp *miniprogram.MiniProgram) {
 }
 
 // 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()
+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
@@ -108,12 +98,12 @@ type PhoneInfo struct {
 	} `json:"watermark"` // 数据水印
 }
 
-func GetPhoneNumber(code string) (result GetPhoneNumberResponse, err error) {
+func GetPhoneNumber(code, copyYb string) (result GetPhoneNumberResponse, err error) {
 	var response []byte
 	var (
 		at string
 	)
-	wechatClient := GetWxApp()
+	wechatClient := GetWxApp(copyYb)
 	authClient := wechatClient.GetAuth()
 	if at, err = authClient.GetAccessToken(); err != nil {
 		return
@@ -131,20 +121,20 @@ func GetPhoneNumber(code string) (result GetPhoneNumberResponse, err error) {
 }
 
 // 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,
@@ -171,19 +161,24 @@ func GetWxAppConf() (appConf *WxAppConf, err error) {
 		err = errors.New("获取小程序配置失败, Err: " + e.Error())
 		return
 	}
-	appConf = &WxAppConf{
+	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 conf.ConfigValue == "true" {
-		// 备用小程序
+	// 备用小程序
+	if copyYb == "true" {
 		appConf.WxAppId = `wx9a2a9b49a00513a0`
 		appConf.WxAppSecret = `9feb793bd0a8756990a36ac2ade6978c`
 		appConf.WxPlatform = utils.USER_RECORD_PLATFORM_COPY_YB
 		appConf.MsgRedirectTarget = 4
 	}
-	return
+	return appConf
 }