From e3157a3c367357cb48c1fb4cf8d4301a6f7eb0b3 Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Wed, 26 Jul 2023 14:18:34 +0800 Subject: [PATCH] 短信认证 --- src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java | 148 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 145 insertions(+), 3 deletions(-) diff --git a/src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java b/src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java index 4600070..cecefbd 100644 --- a/src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java +++ b/src/main/java/cc/mrbird/febs/pay/util/WechatEventUtil.java @@ -1,8 +1,23 @@ package cc.mrbird.febs.pay.util; +import cc.mrbird.febs.common.utils.MallUtils; +import cc.mrbird.febs.common.utils.RedisUtils; +import cc.mrbird.febs.rabbit.producter.AgentProducer; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.text.StrFormatter; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.json.JSONUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import org.dom4j.io.SAXReader; import javax.servlet.http.HttpServletRequest; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileOutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; @@ -11,6 +26,10 @@ import org.dom4j.Element; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + import java.io.InputStream; import java.util.*; @@ -21,7 +40,12 @@ * * @author shenshao */ +@Service(value="wechatEventUtil") public class WechatEventUtil { + + + @Autowired + private AgentProducer agentProducer; private static Logger log = LoggerFactory.getLogger(WechatEventUtil.class); @@ -31,7 +55,7 @@ * @param req 请求 * @return 校验结果 */ - public static String verification(HttpServletRequest req, String wechatToken) { + public String verification(HttpServletRequest req, String wechatToken) { // 接收微信服务器发送请求时传递过来的参数 String signature = req.getParameter("signature"); String timestamp = req.getParameter("timestamp"); @@ -57,7 +81,7 @@ * @param req * @return */ - public static String messageEvent(HttpServletRequest req) { + public String messageEvent(HttpServletRequest req) { String result = null; Map<String, String> map = xmlToMap(req); String fromUserName = map.get("FromUserName"); @@ -68,10 +92,42 @@ log.info("事件处理:event:{}、msgType:{}、toUserName:{}、fromUserName:{}", eventType, msgType, toUserName, fromUserName); if ("event".equals(msgType)) { if ("subscribe".equals(eventType)) { - result = imgTextMsg(toUserName, fromUserName, "新用户注册", "仅限上海地区手机号用户", "图片地址", "点击后跳转的路径"); + result = imgTextMsg(toUserName, fromUserName, "欢迎光临", "欢迎光临【药王谷铺子】,点击左下角【药王谷铺子】-【商城】,进入商城。", "图片地址", "点击后跳转的路径"); log.info("新增关注事件:toUserName{}、fromUserName{}", toUserName, fromUserName); + StringBuffer stringBuffer = new StringBuffer(); + if(map.containsKey("EventKey")){ + /** + * 扫带参数二维码 + */ + String eventKey = map.get("EventKey"); + if(StrUtil.isNotEmpty(eventKey)){ + stringBuffer.append(eventKey); + stringBuffer.append("-"); + stringBuffer.append(fromUserName); + agentProducer.sendMemberSubScanMsg(stringBuffer.toString()); + } + } + /** + * 普通关注事件 + */ + agentProducer.sendMemberSubMsg(fromUserName); } else if ("unsubscribe".equals(eventType)) { log.info("取消关注事件:toUserName{}、fromUserName{}", toUserName, fromUserName); + }else if ("SCAN".equals(eventType)) { + log.info("扫码关注事件:toUserName{}、fromUserName{}", toUserName, fromUserName); + if(map.containsKey("EventKey")){ + StringBuffer stringBuffer = new StringBuffer(); + /** + * 扫带参数二维码 + */ + String eventKey = map.get("EventKey"); + if(StrUtil.isNotEmpty(eventKey)){ + stringBuffer.append(eventKey); + stringBuffer.append("@"); + stringBuffer.append(fromUserName); + agentProducer.sendMemberSubScanMsg(stringBuffer.toString()); + } + } } } return result; @@ -184,4 +240,90 @@ return ""; } + + @Autowired + RedisUtils redisUtils; + + /** + * 获取ticket + * + * @param qrCodeParam + * @return + */ + public String getTicket(String qrCodeParam) { + RestTemplate restTemplate = new RestTemplate(); + String ticket = null; + // 拼接请求地址 + String CREATE_QRCODE_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN"; +// String requestUrl = CREATE_QRCODE_URL.replace("TOKEN", "70_ECdQaX-QtNXZP9ugU_JEJGRYEADWrlQZNYrT2I2IRHsx6hx4_O8RY4VasWI97_ixia8vANTdnNRI_cT00toK7CX98513sQI8535eTFw3b-VQChEFZrRqTfNPdSoGYXgAIAVNM"); + String requestUrl = CREATE_QRCODE_URL.replace("TOKEN", redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY).toString()); + // 创建临时带参二维码 + + JSONObject jsonObject = restTemplate.postForObject(requestUrl, + "{\"expire_seconds\": 604800, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": "+qrCodeParam+"}}}", + JSONObject.class); +// JSONObject jsonObject = HttpsUtil.request(requestUrl, "POST", "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}"); + if (ObjectUtil.isNotEmpty(jsonObject)) { + String response = JSON.toJSONString(jsonObject); + if (!jsonObject.containsKey("errcode")) { + ticket = jsonObject.getString("ticket"); + System.out.println("临时带参二维码ticket成功" + response); + } else { + log.error("临时带参二维码ticket失败:" + response); + } + } + return ticket; + } + + /** + * 通过ticket换取二维码并保存二维码图片 + * + * @param ticket + * @param savePath + * @return + */ + //图片上传路径 + public static final String IMG_UPLOAD_PATH="/home/javaweb/webresource/ywg/h5/wxcode"; + public String getQRcode(String ticket) { + + String SHOW_QRCODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET"; + String requestUrl = SHOW_QRCODE_URL.replace("TICKET", ticket); + + String randomNum = MallUtils.getRandomNum(5); + String imgName="/user_" + randomNum + "_acode_1.jpg"; + String urlPrefix="https://ywgouth.meiao.biz/wxcode"; + String imgPath=IMG_UPLOAD_PATH+imgName; + if(!FileUtil.exist(imgPath)){ + try { + HttpResponse execute = HttpRequest.get(requestUrl).execute(); + InputStream inputStream = execute.bodyStream(); + File file = new File(imgPath); + FileUtil.writeFromStream(inputStream, file); + long uploadUrl = FileUtil.size(file); + //小于10kb重新生成 + if(uploadUrl<=10240){ + log.error("生成微信小程序码失败:图片大小异常:{}",uploadUrl); + return null; + } + } catch (Exception e) { + log.error("生成微信小程序码失败",e); + return null; + } + }else{ + + //判断文件是否正常 不正常 删除 + File file = new File(imgPath); + long uploadUrl = FileUtil.size(file); + if(uploadUrl<=10240){ + FileUtil.del(file); + return null; + } + } + System.out.println("根据ticket换取二维码成功,路径:" + imgPath); + return urlPrefix+"/"+imgName; + + } + + public static void main(String[] args) { + } } -- Gitblit v1.9.1