package com.qmtt.controller; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.qmtt.config.QmttConfig; import com.qmtt.mapper.LessonFormMapper; import com.qmtt.tools.HttpUtil; import com.qmtt.tools.JsonUtils; @RestController @RequestMapping("/api/contact") public class ContactController { private static final Logger log = LoggerFactory.getLogger(ContactController.class); @Autowired QmttConfig config; @Autowired LessonFormMapper lessonFormDao; @RequestMapping("/receiveMsg") public Object receiveMsg(HttpServletRequest request, HttpServletResponse response) throws Exception { String method = request.getMethod(); if (method.equals("GET")) { Map map = request.getParameterMap(); log.info(JsonUtils.obj2json(map)); return request.getParameter("echostr"); } if (method.equals("POST")) { BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream())); String line = null; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); } String jsonStr = sb.toString(); log.info(jsonStr); Map map = JsonUtils.json2map(jsonStr); String openid = map.get("FromUserName").toString(); String sessionFrom = ""; if (map.containsKey("SessionFrom")) { sessionFrom = map.get("SessionFrom").toString(); } String orderNo = sessionFrom; new Thread() { @Override public void run() { try { Thread.sleep(2000); String token = lessonFormDao.getWxToken(); if (token == null) { log.info("微信token不存在,不发消息"); return; } String url = String.format(config.getCustomUrl(), token); Map msgMap = new HashMap<String, Object>(); msgMap.put("touser", openid); msgMap.put("msgtype", "text"); Map textMap = new HashMap<String, Object>(); textMap.put("content", "订单信息" + orderNo); msgMap.put("text", textMap); String json = JsonUtils.toJsonString(msgMap); HttpUtil.postJson(url, json); } catch (Exception e) { log.error("线程异常", e); } } }.start(); return "success"; } return ""; } }