|
@@ -1,44 +1,32 @@
|
|
|
package com.miaxis.system.controller.test;
|
|
|
|
|
|
-import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.extra.qrcode.QrCodeUtil;
|
|
|
-import cn.hutool.json.XML;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
import com.miaxis.common.constant.Constants;
|
|
|
import com.miaxis.common.core.domain.Response;
|
|
|
-import com.miaxis.common.utils.wx.SignUtil;
|
|
|
-import com.miaxis.feign.dto.WxTicket;
|
|
|
import com.miaxis.feign.service.IWxMpService;
|
|
|
import com.miaxis.feign.service.IWxSendService;
|
|
|
import com.miaxis.wx.service.IWxGzhService;
|
|
|
+import com.qcloud.cos.COSClient;
|
|
|
+import com.qcloud.cos.model.ObjectMetadata;
|
|
|
+import com.qcloud.cos.model.PutObjectRequest;
|
|
|
+import com.qcloud.cos.model.PutObjectResult;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
-import org.w3c.dom.Node;
|
|
|
-import org.w3c.dom.NodeList;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import javax.xml.parsers.DocumentBuilder;
|
|
|
-import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
-import javax.xml.transform.OutputKeys;
|
|
|
-import javax.xml.transform.Transformer;
|
|
|
-import javax.xml.transform.TransformerFactory;
|
|
|
-import javax.xml.transform.dom.DOMSource;
|
|
|
-import javax.xml.transform.stream.StreamResult;
|
|
|
-import java.io.*;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.Calendar;
|
|
|
|
|
|
/**
|
|
|
* @author wwl
|
|
@@ -65,285 +53,62 @@ public class TestController {
|
|
|
@Autowired
|
|
|
HttpServletRequest request;
|
|
|
|
|
|
- /**
|
|
|
- * 生成带参数的二维码
|
|
|
- */
|
|
|
- @GetMapping(value = "/generateTicket")
|
|
|
- @ApiOperation("生成带参数的二维码")
|
|
|
- public Response generateTicket() throws Exception{
|
|
|
- String xcxMessageToken = wxGzhService.getGzhToken();
|
|
|
- WxTicket wxTicket = new WxTicket();
|
|
|
- wxTicket.setExpire_seconds(86400);//一天
|
|
|
- wxTicket.setAction_name("QR_SCENE");
|
|
|
- JSONObject jsonObject1 = new JSONObject();
|
|
|
- JSONObject jsonObject2 = new JSONObject();
|
|
|
- jsonObject1.put("scene_id","123");
|
|
|
- jsonObject2.put("scene",jsonObject1);
|
|
|
- wxTicket.setAction_info(jsonObject2);
|
|
|
- String result = wxSendService.generateTicket(xcxMessageToken,wxTicket);
|
|
|
- System.out.println("生成ticket:" + result);
|
|
|
- JSONObject jsonStr = JSONObject.parseObject(result);
|
|
|
-
|
|
|
- QrCodeUtil.generate(
|
|
|
- jsonStr.getString("url"), //二维码内容
|
|
|
- 300, 300,
|
|
|
- //QrConfig.create().setImg("C:\\Users\\wwl\\Desktop\\二维码\\微信图片_20211021120216.jpg")
|
|
|
- FileUtil.file("/data/test/qrimg.jpg")//写出到的文件
|
|
|
- );
|
|
|
-
|
|
|
- return Response.success();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理服务器推送消息
|
|
|
- * @param request
|
|
|
- * @param response
|
|
|
- * @return
|
|
|
- */
|
|
|
- @RequestMapping(value = "/notifyMsg",method = { RequestMethod.GET, RequestMethod.POST })
|
|
|
- @ApiOperation("处理服务器推送消息")
|
|
|
- public String checkSign(HttpServletRequest request, HttpServletResponse response) {
|
|
|
- log.info("-----------进入---------");
|
|
|
- try {
|
|
|
- String signature = request.getParameter("signature");
|
|
|
- String timestamp = request.getParameter("timestamp");
|
|
|
- String nonce = request.getParameter("nonce");
|
|
|
- String echostr = request.getParameter("echostr");
|
|
|
- log.info("本身" + signature);
|
|
|
- if(request.getMethod().equals("GET")){
|
|
|
- log.info("-----------微信验签---------");
|
|
|
- return SignUtil.checkSignature(signature, timestamp, nonce) == true ? echostr : null;
|
|
|
- }else{
|
|
|
- log.info("-----------推送消息处理---------");
|
|
|
- return handlePublicMsg(request);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("验证公众号token失败", e);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //微信功能处理
|
|
|
- public String WexHandeler(HttpServletRequest request) throws Exception {
|
|
|
- try {
|
|
|
- cn.hutool.json.JSONObject info = XML.toJSONObject(IOUtils.toString(request.getInputStream()));
|
|
|
- log.info("info:" + info);
|
|
|
- if (info.get("xml") != null) {
|
|
|
- JSONObject event = (JSONObject) info.get("xml");
|
|
|
- if (event.get("MsgType") != null) {
|
|
|
- String Event = (String) event.get("Event");//事件类型 关注事件还是取消关注等
|
|
|
- //如果是事件
|
|
|
- if (!StringUtils.isEmpty(Event)) try {
|
|
|
- if ("subscribe".equals(Event)) { //带参数二维码的关注事件
|
|
|
- //关注方法
|
|
|
- String toUserName = (String) event.get("ToUserName");//开发者微信号
|
|
|
- String fromUserName = (String) event.get("FromUserName");//发送方帐号(一个OpenID)
|
|
|
- String eventKey = (String) event.get("EventKey");//事件KEY值,是一个32位无符号整数,即创建二维码时的二维码scene_id
|
|
|
- String ticket = (String) event.get("Ticket");//二维码的ticket,可用来换取二维码图片
|
|
|
- log.info(">>>>>>>>>>>>>>>>>>接受的数据为:");
|
|
|
- log.info("toUserName:" + toUserName);
|
|
|
- log.info("fromUserName:" + fromUserName);
|
|
|
- log.info("scene_id:" + eventKey);
|
|
|
- log.info("ticket:" + ticket);
|
|
|
- //解析完执行操作
|
|
|
-
|
|
|
- } else if ("SCAN".equals(Event)) {//扫描带参数二维码事件 用户已关注时的事件推送
|
|
|
- String toUserName = (String) event.get("ToUserName");//开发者微信号
|
|
|
- String fromUserName = (String) event.get("FromUserName");//发送方帐号(一个OpenID)
|
|
|
- String eventKey = (String) event.get("EventKey");//事件KEY值,是一个32位无符号整数,即创建二维码时的二维码scene_id
|
|
|
- String ticket = (String) event.get("Ticket");//二维码的ticket,可用来换取二维码图片
|
|
|
- log.info(">>>>>>>>>>>>>>>>>>接受的数据为:");
|
|
|
- log.info("toUserName:" + toUserName);
|
|
|
- log.info("fromUserName:" + fromUserName);
|
|
|
- log.info("scene_id:" + eventKey);
|
|
|
- log.info("ticket:" + ticket);
|
|
|
- //解析完执行操作
|
|
|
-
|
|
|
- } else if ("unsubscribe".equals(Event)) { //取消订阅事件
|
|
|
-
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- return e.getMessage();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- return request.getParameter("echostr");
|
|
|
- }
|
|
|
- return request.getParameter("echostr");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ private COSClient cosClient;
|
|
|
|
|
|
- /**
|
|
|
- * 处理微信公众号请求信息
|
|
|
- */
|
|
|
- public String handlePublicMsg(HttpServletRequest request) throws Exception {
|
|
|
- log.info("1....");
|
|
|
- // 获得微信端返回的xml数据
|
|
|
- InputStream is = null;
|
|
|
- InputStreamReader isr = null;
|
|
|
- BufferedReader br = null;
|
|
|
- try {
|
|
|
- is = request.getInputStream();
|
|
|
- isr = new InputStreamReader(is, "utf-8");
|
|
|
- br = new BufferedReader(isr);
|
|
|
- log.info("2....");
|
|
|
- String str = null;
|
|
|
- StringBuffer returnXml= new StringBuffer();
|
|
|
- while ((str = br.readLine()) != null) {
|
|
|
- log.info("3....");
|
|
|
- //返回的是xml数据
|
|
|
- returnXml.append(str);
|
|
|
- }
|
|
|
-// Map<String, String> decryptMap = xmlToMap(returnXml.toString());
|
|
|
- Map<String, String> decryptMap = xmlToMap(returnXml.toString());
|
|
|
- log.info("-----decryptMap------"+decryptMap);
|
|
|
-// // 得到公众号传来的加密信息并解密,得到的是明文xml数据
|
|
|
-// String decryptXml = WXPublicUtils.decrypt(encryptMap.get("Encrypt"));
|
|
|
- // 将xml数据转换为map
|
|
|
- //Map<String, String> decryptMap = xmlToMap(encryptMap);
|
|
|
- log.info("5....");
|
|
|
+ @Value("${cos.bucketName}")
|
|
|
+ private String bucketName;
|
|
|
+ @Value("${cos.path}")
|
|
|
+ private String path;
|
|
|
+ @Value("${cos.preffix}")
|
|
|
+ private String preffix;
|
|
|
|
|
|
- // 区分消息类型
|
|
|
- String msgType = decryptMap.get("MsgType");
|
|
|
- log.info("5.1...");
|
|
|
- // 普通消息
|
|
|
- if ("text".equals(msgType)) { // 文本消息
|
|
|
- // todo 处理文本消息
|
|
|
- } else if ("image".equals(msgType)) { // 图片消息
|
|
|
- // todo 处理图片消息
|
|
|
- }
|
|
|
+ @Value("${file.ticketPath}")
|
|
|
+ private String ticketPath;
|
|
|
|
|
|
- // 事件推送
|
|
|
- else if ("event".equals(msgType)) { // 事件消息
|
|
|
- // 区分事件推送
|
|
|
- String event = decryptMap.get("Event");
|
|
|
- if ("subscribe".equals(event)) { // 订阅事件 或 未关注扫描二维码事件
|
|
|
- log.info("5.2...");
|
|
|
- // 返回消息时ToUserName的值与FromUserName的互换
|
|
|
- Map<String, String> returnMap = new HashMap<>();
|
|
|
- returnMap.put("ToUserName", decryptMap.get("FromUserName"));
|
|
|
- returnMap.put("FromUserName", decryptMap.get("ToUserName"));
|
|
|
- returnMap.put("CreateTime", new Date().getTime()+"");
|
|
|
- returnMap.put("MsgType", "text");
|
|
|
- returnMap.put("Content", "测试哈哈哈哈");
|
|
|
- log.info("5.2...----returnMap-----"+returnMap);
|
|
|
- String encryptMsg = mapToXml(returnMap);
|
|
|
- return encryptMsg;
|
|
|
- } else if ("unsubscribe".equals(event)) { // 取消订阅事件
|
|
|
- // todo 处理取消订阅事件
|
|
|
- } else if ("SCAN".equals(event)) { // 已关注扫描二维码事件
|
|
|
- log.info("5.3...");
|
|
|
- // 返回消息时ToUserName的值与FromUserName的互换
|
|
|
- Map<String, String> returnMap = new HashMap<>();
|
|
|
- returnMap.put("ToUserName", decryptMap.get("FromUserName"));
|
|
|
- returnMap.put("FromUserName", decryptMap.get("ToUserName"));
|
|
|
- returnMap.put("CreateTime", new Date().getTime()+"");
|
|
|
- returnMap.put("MsgType", "text");
|
|
|
- returnMap.put("Content", "测试哈哈哈哈");
|
|
|
- log.info("5.3...----returnMap-----"+returnMap);
|
|
|
- String encryptMsg = mapToXml(returnMap);
|
|
|
- return encryptMsg;
|
|
|
- } else if ("LOCATION".equals(event)) { // 上报地理位置事件
|
|
|
- // todo 处理上报地理位置事件
|
|
|
- } else if ("CLICK".equals(event)) { // 点击菜单拉取消息时的事件推送事件
|
|
|
- // todo 处理点击菜单拉取消息时的事件推送事件
|
|
|
- } else if ("VIEW".equals(event)) { // 点击菜单跳转链接时的事件推送
|
|
|
- // todo 处理点击菜单跳转链接时的事件推送
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("处理微信公众号请求信息,失败", e);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
+ @Value("${wxgzh.materialUrl}")
|
|
|
+ private String materialUrl;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * XML格式字符串转换为Map
|
|
|
- *
|
|
|
- * @param strXML XML字符串
|
|
|
- * @return XML数据转换后的Map
|
|
|
- * @throws Exception
|
|
|
+ * cos上传二维码
|
|
|
*/
|
|
|
- public static Map<String, String> xmlToMap(String strXML) throws Exception {
|
|
|
- log.info("4....");
|
|
|
- try {
|
|
|
- Map<String, String> data = new HashMap<>();
|
|
|
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
|
|
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
|
|
- InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
|
|
|
- org.w3c.dom.Document doc = documentBuilder.parse(stream);
|
|
|
- doc.getDocumentElement().normalize();
|
|
|
- NodeList nodeList = doc.getDocumentElement().getChildNodes();
|
|
|
- for (int idx = 0; idx < nodeList.getLength(); ++idx) {
|
|
|
- Node node = nodeList.item(idx);
|
|
|
- if (node.getNodeType() == Node.ELEMENT_NODE) {
|
|
|
- org.w3c.dom.Element element = (org.w3c.dom.Element) node;
|
|
|
- data.put(element.getNodeName(), element.getTextContent());
|
|
|
- }
|
|
|
- }
|
|
|
- try {
|
|
|
- stream.close();
|
|
|
- } catch (Exception ex) {
|
|
|
- // do nothing
|
|
|
- }
|
|
|
- return data;
|
|
|
- } catch (Exception ex) {
|
|
|
- throw ex;
|
|
|
- }
|
|
|
+ @GetMapping(value = "/test01")
|
|
|
+ @ApiOperation("cos上传二维码")
|
|
|
+ public Response test01() throws Exception{
|
|
|
+
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+
|
|
|
+ ImageIO.write(QrCodeUtil.generate(
|
|
|
+ "二维码内容", //二维码内容
|
|
|
+ 300, 300
|
|
|
+ ), "JPG", os);
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
|
|
|
+
|
|
|
+ ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
+ // 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据,存在内存OOM的情况
|
|
|
+ objectMetadata.setContentLength(os.size());
|
|
|
+
|
|
|
+ // bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
|
|
|
+ String bucketName = this.bucketName;
|
|
|
+
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ int year = cal.get(Calendar.YEAR);
|
|
|
+ int month=cal.get(Calendar.MONTH)+ 1;
|
|
|
+ int day=cal.get(Calendar.DATE);
|
|
|
+
|
|
|
+ //存储的文件名
|
|
|
+ String storagefileName = System.currentTimeMillis() + ".png";
|
|
|
+ // 指定要上传到 COS 上的路径
|
|
|
+ String key = "/"+this.preffix+"/"+ "testTicket"+"/"+year+"/"+month+"/"+day+"/"+storagefileName;
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream,objectMetadata);
|
|
|
+ PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
|
|
+ String fileUrl=this.path + putObjectRequest.getKey();
|
|
|
+
|
|
|
+ return Response.success(fileUrl);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 将Map转换为XML格式的字符串
|
|
|
- *
|
|
|
- * @param data Map类型数据
|
|
|
- * @return XML格式的字符串
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static String mapToXml(Map<String, String> data) throws Exception {
|
|
|
- log.info("----mapToXml-----"+data);
|
|
|
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
|
|
- DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
|
|
|
- org.w3c.dom.Document document = documentBuilder.newDocument();
|
|
|
- org.w3c.dom.Element root = document.createElement("xml");
|
|
|
- document.appendChild(root);
|
|
|
- for (String key: data.keySet()) {
|
|
|
- String value = data.get(key);
|
|
|
- if (value == null) {
|
|
|
- value = "";
|
|
|
- }
|
|
|
- value = value.trim();
|
|
|
- org.w3c.dom.Element filed = document.createElement(key);
|
|
|
- filed.appendChild(document.createTextNode(value));
|
|
|
- root.appendChild(filed);
|
|
|
- }
|
|
|
- TransformerFactory tf = TransformerFactory.newInstance();
|
|
|
- Transformer transformer = tf.newTransformer();
|
|
|
- DOMSource source = new DOMSource(document);
|
|
|
- transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
|
|
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
|
- StringWriter writer = new StringWriter();
|
|
|
- StreamResult result = new StreamResult(writer);
|
|
|
- transformer.transform(source, result);
|
|
|
- String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
|
|
|
- try {
|
|
|
- writer.close();
|
|
|
- } catch (Exception ex) {
|
|
|
-
|
|
|
- }
|
|
|
- log.info("----mapToXml-return-----"+output);
|
|
|
- return output;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
|
|
}
|