Althars123 3 years ago
parent
commit
3a01d3dd0e

+ 49 - 5
twzd-admin/src/test/java/com/miaxis/test/NormalTest.java

@@ -1,17 +1,21 @@
 package com.miaxis.test;
 package com.miaxis.test;
 
 
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.alibaba.fastjson.JSONObject;
 import com.miaxis.TwzdApplication;
 import com.miaxis.TwzdApplication;
+import com.miaxis.common.constant.Constants;
+import com.miaxis.common.utils.StringUtils;
+import com.miaxis.feign.service.IWxSendService;
 import com.miaxis.question.mapper.QuestionInfoMapper;
 import com.miaxis.question.mapper.QuestionInfoMapper;
 import com.miaxis.question.vo.QuestionInfoExcelTypeVo;
 import com.miaxis.question.vo.QuestionInfoExcelTypeVo;
 import com.miaxis.question.vo.QuestionInfoExcelVo;
 import com.miaxis.question.vo.QuestionInfoExcelVo;
-import com.miaxis.wx.domain.WxForeverCode;
 import com.miaxis.wx.service.IWxForeverCodeService;
 import com.miaxis.wx.service.IWxForeverCodeService;
 import com.miaxis.wx.service.impl.WxGzhServiceImpl;
 import com.miaxis.wx.service.impl.WxGzhServiceImpl;
 import org.apache.poi.hssf.usermodel.*;
 import org.apache.poi.hssf.usermodel.*;
 import org.junit.Test;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.test.context.ActiveProfiles;
 import org.springframework.test.context.ActiveProfiles;
 import org.springframework.test.context.junit4.SpringRunner;
 import org.springframework.test.context.junit4.SpringRunner;
 
 
@@ -19,6 +23,7 @@ import javax.annotation.Resource;
 import java.io.FileOutputStream;
 import java.io.FileOutputStream;
 import java.io.OutputStream;
 import java.io.OutputStream;
 import java.util.List;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 
 @ActiveProfiles("prod")
 @ActiveProfiles("prod")
 @SpringBootTest(classes = TwzdApplication.class)
 @SpringBootTest(classes = TwzdApplication.class)
@@ -35,11 +40,50 @@ public class NormalTest {
     @Resource
     @Resource
     IWxForeverCodeService wxForeverCodeService;
     IWxForeverCodeService wxForeverCodeService;
 
 
+
+    @Resource
+    IWxSendService wxSendService;
+
+    @Resource
+    RedisTemplate redisTemplate;
+
+
+    @Value("${app.appid}")
+    private String appid;
+
+    @Value("${app.appSecret}")
+    private String secret;
+
+    public String getGzhToken() {
+        String token = "";
+        Integer expiresIn;
+        if (redisTemplate.hasKey(Constants.GZH_MESSAGE_TOKEN)){
+            token = (String)redisTemplate.opsForValue().get(Constants.GZH_MESSAGE_TOKEN);
+        }
+        if (StringUtils.isEmpty(token)){
+            String result = wxSendService.getAccessToken("client_credential",appid,secret);
+            JSONObject json = JSONObject.parseObject(result);
+            token = json.getString("access_token");
+            expiresIn = json.getIntValue("expires_in");
+            if (expiresIn <= 0){
+                expiresIn = 7000;
+            }
+
+            redisTemplate.opsForValue().set(Constants.GZH_MESSAGE_TOKEN,token,expiresIn, TimeUnit.SECONDS);
+        }
+
+        return token;
+    }
+
     @Test
     @Test
     public void test11() throws Exception {
     public void test11() throws Exception {
-        String  content = "k3vvps";
-        WxForeverCode wxForeverCode = wxForeverCodeService.getOne(new QueryWrapper<WxForeverCode>().eq("code", content).isNull("openid"));
-        System.out.println(11);
+        //获取用户信息
+        String userInfoResult = wxSendService.userInfo(this.getGzhToken(), "ovKTX5-FKLF6_sgTtCIXpG_lz3PY", "zh_CN");
+        JSONObject jsonObj = JSONObject.parseObject(userInfoResult);
+        System.out.println(jsonObj);
+//        String  content = "k3vvps";
+//        WxForeverCode wxForeverCode = wxForeverCodeService.getOne(new QueryWrapper<WxForeverCode>().eq("code", content).isNull("openid"));
+//        System.out.println(11);
     }
     }
 
 
 
 

+ 4 - 5
twzd-service/src/main/java/com/miaxis/wx/domain/WxForeverCode.java

@@ -46,11 +46,10 @@ public class WxForeverCode extends BaseBusinessEntity{
     @ApiModelProperty(value = "微信昵称")
     @ApiModelProperty(value = "微信昵称")
     private String nickName;
     private String nickName;
 
 
-    /** 微信昵称 */
-    @Excel(name = "素材id")
-    @TableField("media_id")
-    @ApiModelProperty(value = "素材id")
-    private String mediaId;
+
+    @TableField("url")
+    @ApiModelProperty(value = "url")
+    private String url;
 
 
     /** 状态  1:有效(默认) 0:失效  */
     /** 状态  1:有效(默认) 0:失效  */
     @Excel(name = "状态  1:有效(默认) 0:失效 ")
     @Excel(name = "状态  1:有效(默认) 0:失效 ")

+ 4 - 1
twzd-service/src/main/java/com/miaxis/wx/service/IWxMessageEvenService.java

@@ -1,5 +1,7 @@
 package com.miaxis.wx.service;
 package com.miaxis.wx.service;
 
 
+import com.miaxis.wx.domain.WxForeverCode;
+
 /**
 /**
  * 微信消息事件处理
  * 微信消息事件处理
  * @author wwl
  * @author wwl
@@ -25,9 +27,10 @@ public interface IWxMessageEvenService {
      * @param fromUserName 发送方帐号(一个OpenID)
      * @param fromUserName 发送方帐号(一个OpenID)
      * @param token 凭证
      * @param token 凭证
      * @param isForever 是否是永久二维码
      * @param isForever 是否是永久二维码
+     * @param wxForeverCode
      * @return
      * @return
      */
      */
-    String gainTicketEvent(String fromUserName, String token, boolean isForever);
+    String gainTicketEvent(String fromUserName, String token, boolean isForever, WxForeverCode wxForeverCode);
 
 
 
 
 }
 }

+ 11 - 17
twzd-service/src/main/java/com/miaxis/wx/service/impl/WxGzhServiceImpl.java

@@ -110,8 +110,8 @@ public class WxGzhServiceImpl implements IWxGzhService {
             // 区分消息类型
             // 区分消息类型
             String msgType = jsonObjectData.getStr("MsgType");
             String msgType = jsonObjectData.getStr("MsgType");
             //获取用户信息
             //获取用户信息
-            String userInfoResult = wxSendService.userInfo(this.getGzhToken(), fromUserName, "zh_CN");
-            JSONObject jsonObj = JSONObject.parseObject(userInfoResult);
+//            String userInfoResult = wxSendService.userInfo(this.getGzhToken(), fromUserName, "zh_CN");
+//            JSONObject jsonObj = JSONObject.parseObject(userInfoResult);
 
 
             // 普通消息
             // 普通消息
             if (MessageUtil.MESSAGE_TEXT.equals(msgType)) { // 文本消息
             if (MessageUtil.MESSAGE_TEXT.equals(msgType)) { // 文本消息
@@ -121,12 +121,7 @@ public class WxGzhServiceImpl implements IWxGzhService {
                 //若该口令为未使用的口令
                 //若该口令为未使用的口令
                 if (wxForeverCode != null){
                 if (wxForeverCode != null){
                     log.info("2.1.1:口令有效,生成永久二维码");
                     log.info("2.1.1:口令有效,生成永久二维码");
-                    String mediaId = wxMessageEvenService.gainTicketEvent(fromUserName, this.getGzhToken(),true);
-                    //更新口令数据对象
-                    wxForeverCode.setOpenid(jsonObj.getString("openid"));
-                    wxForeverCode.setNickName(jsonObj.getString("nickname"));
-                    wxForeverCode.setMediaId(mediaId);
-                    wxForeverCodeService.updateById(wxForeverCode);
+                    String mediaId = wxMessageEvenService.gainTicketEvent(fromUserName, this.getGzhToken(),true,wxForeverCode);
                     return MessageUtil.initImage(fromUserName,toUserName,mediaId);
                     return MessageUtil.initImage(fromUserName,toUserName,mediaId);
                 }
                 }
                 else if (content.startsWith("你好")){
                 else if (content.startsWith("你好")){
@@ -146,15 +141,13 @@ public class WxGzhServiceImpl implements IWxGzhService {
                 String event = jsonObjectData.getStr("Event");
                 String event = jsonObjectData.getStr("Event");
                 if (MessageUtil.MESSAGE_SUBSCRIBE.equals(event)) { // 关注事件 或 扫描二维码关注事件
                 if (MessageUtil.MESSAGE_SUBSCRIBE.equals(event)) { // 关注事件 或 扫描二维码关注事件
                     log.info("3.1...");
                     log.info("3.1...");
-
-
                     UserInfo user = userService.getStudentByOpenId(fromUserName);
                     UserInfo user = userService.getStudentByOpenId(fromUserName);
                     UserInfo userInfo = new UserInfo();
                     UserInfo userInfo = new UserInfo();
                     if (user == null){
                     if (user == null){
                         //保存用户信息
                         //保存用户信息
-                        userInfo.setHeadImage(jsonObj.getString("headimgurl"));
-                        userInfo.setNickName(jsonObj.getString("nickname"));
-                        userInfo.setOpenid(jsonObj.getString("openid"));
+//                        userInfo.setHeadImage(jsonObj.getString("headimgurl"));
+//                        userInfo.setNickName(jsonObj.getString("nickname"));
+                        userInfo.setOpenid(fromUserName);
                         userService.saveUserInfo(userInfo);
                         userService.saveUserInfo(userInfo);
                     }
                     }
 
 
@@ -166,7 +159,7 @@ public class WxGzhServiceImpl implements IWxGzhService {
                         return MessageUtil.initText(fromUserName,toUserName,subscribeEvent);
                         return MessageUtil.initText(fromUserName,toUserName,subscribeEvent);
 
 
                     }else {
                     }else {
-                        return MessageUtil.initText(fromUserName, toUserName, jsonObj.getString("nickname")+":关注事件");
+                        return MessageUtil.initText(fromUserName, toUserName, "关注事件");
                     }
                     }
 
 
                 }  else if (MessageUtil.MESSAGE_UNSUBSCRIBE.equals(event)) { // 取消订阅事件
                 }  else if (MessageUtil.MESSAGE_UNSUBSCRIBE.equals(event)) { // 取消订阅事件
@@ -186,10 +179,11 @@ public class WxGzhServiceImpl implements IWxGzhService {
                         //先判定之前是否存在永久码,若存在,则返回相应的素材id
                         //先判定之前是否存在永久码,若存在,则返回相应的素材id
                         WxForeverCode wxForeverCode = wxForeverCodeService.getOne(new QueryWrapper<WxForeverCode>().eq("openid",fromUserName));
                         WxForeverCode wxForeverCode = wxForeverCodeService.getOne(new QueryWrapper<WxForeverCode>().eq("openid",fromUserName));
                         if (wxForeverCode!=null){
                         if (wxForeverCode!=null){
-                            log.info("3.4:直接返回永久码图片");
-                            return MessageUtil.initImage(fromUserName,toUserName,wxForeverCode.getMediaId());
+                            log.info("3.4:返回永久码图片");
+                            String mediaId = wxMessageEvenService.gainTicketEvent(fromUserName, this.getGzhToken(),true,wxForeverCode);
+                            return MessageUtil.initImage(fromUserName,toUserName,mediaId);
                         }
                         }
-                        String mediaId = wxMessageEvenService.gainTicketEvent(fromUserName, this.getGzhToken(), false);
+                        String mediaId = wxMessageEvenService.gainTicketEvent(fromUserName, this.getGzhToken(), false, wxForeverCode);
                         return MessageUtil.initImage(fromUserName,toUserName,mediaId);
                         return MessageUtil.initImage(fromUserName,toUserName,mediaId);
                     }
                     }
 
 

+ 31 - 11
twzd-service/src/main/java/com/miaxis/wx/service/impl/WxMessageEvenServiceImpl.java

@@ -16,6 +16,8 @@ import com.miaxis.feign.dto.WxQrTicket;
 import com.miaxis.feign.service.IWxSendService;
 import com.miaxis.feign.service.IWxSendService;
 import com.miaxis.spread.domain.WxSpreadRelation;
 import com.miaxis.spread.domain.WxSpreadRelation;
 import com.miaxis.spread.service.IWxSpreadRelationService;
 import com.miaxis.spread.service.IWxSpreadRelationService;
+import com.miaxis.wx.domain.WxForeverCode;
+import com.miaxis.wx.service.IWxForeverCodeService;
 import com.miaxis.wx.service.IWxMessageEvenService;
 import com.miaxis.wx.service.IWxMessageEvenService;
 import com.miaxis.wx.service.IWxTicketService;
 import com.miaxis.wx.service.IWxTicketService;
 import lombok.RequiredArgsConstructor;
 import lombok.RequiredArgsConstructor;
@@ -50,6 +52,8 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
 
 
     private final IWxSpreadRelationService wxSpreadRelationService;
     private final IWxSpreadRelationService wxSpreadRelationService;
 
 
+    private final IWxForeverCodeService wxForeverCodeService;
+
     @Value("${file.ticketPath}")
     @Value("${file.ticketPath}")
     private String ticketPath;
     private String ticketPath;
 
 
@@ -106,10 +110,11 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
      * click获取推广二维码事件
      * click获取推广二维码事件
      * @param fromUserName 发送方帐号(一个OpenID)
      * @param fromUserName 发送方帐号(一个OpenID)
      * @param token 凭证
      * @param token 凭证
+     * @param wxForeverCode
      * @return
      * @return
      */
      */
     @Override
     @Override
-    public String gainTicketEvent(String fromUserName, String token, boolean isForever) {
+    public String gainTicketEvent(String fromUserName, String token, boolean isForever, WxForeverCode wxForeverCode) {
 
 
         //发送客服信息
         //发送客服信息
         messageCustomSend(fromUserName,token);
         messageCustomSend(fromUserName,token);
@@ -119,18 +124,33 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
 //            String mediaId = (String) redisTemplate.opsForValue().get(Constants.GZH_MATERIAL_KEY + fromUserName);
 //            String mediaId = (String) redisTemplate.opsForValue().get(Constants.GZH_MATERIAL_KEY + fromUserName);
 //            return mediaId;
 //            return mediaId;
 //        }
 //        }
-
-        //生成带参数临时二维码ticket
-        WxQrTicket wxQrTicket = null ;
-        if (isForever){
-            wxQrTicket = createForeverTicket(fromUserName);
+        String url = null ;
+        if (wxForeverCode == null && wxForeverCode.getUrl() != null){
+            url = wxForeverCode.getUrl();
         }else{
         }else{
-            wxQrTicket = createShortTimeTicket(fromUserName);
+            //生成带参数临时二维码ticket
+
+            WxQrTicket wxQrTicket = null ;
+            if (isForever){
+                wxQrTicket = createForeverTicket(fromUserName);
+            }else{
+                wxQrTicket = createShortTimeTicket(fromUserName);
+            }
+
+            String wxTicketResult = wxSendService.generateTicket(token, wxQrTicket);
+            JSONObject jsonStr = JSONObject.parseObject(wxTicketResult);
+            log.info("---------ticket-------" + jsonStr);
+            if (isForever){
+                //更新口令数据对象
+                wxForeverCode.setOpenid(fromUserName);
+                // wxForeverCode.setNickName(jsonObj.getString("nickname"));
+                wxForeverCode.setUrl(jsonStr.getString("url"));
+                wxForeverCodeService.updateById(wxForeverCode);
+            }
+
+            url = jsonStr.getString("url");
         }
         }
 
 
-        String wxTicketResult = wxSendService.generateTicket(token, wxQrTicket);
-        JSONObject jsonStr = JSONObject.parseObject(wxTicketResult);
-        log.info("---------ticket-------" + jsonStr);
 
 
         //获取用户信息
         //获取用户信息
         String userInfoResult = wxSendService.userInfo(token, fromUserName, "zh_CN");
         String userInfoResult = wxSendService.userInfo(token, fromUserName, "zh_CN");
@@ -139,7 +159,7 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
 
 
         //根据ticket生成二维码图片存储
         //根据ticket生成二维码图片存储
         File ticketFile = QrCodeUtil.generate(
         File ticketFile = QrCodeUtil.generate(
-                jsonStr.getString("url"), //二维码内容
+                url, //二维码内容
                 QrConfig.create().setImg(ticketPath + "logo.jpg"), //附带logo
                 QrConfig.create().setImg(ticketPath + "logo.jpg"), //附带logo
                 FileUtil.file(ticketPath + fromUserName + ".jpg")//写出到的文件
                 FileUtil.file(ticketPath + fromUserName + ".jpg")//写出到的文件
         );
         );