• 对接微信消息


    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 "";
        }
    }

    有追求,才有动力!

    向每一个软件工程师致敬!

    by wujf

    mail:921252375@qq.com

  • 相关阅读:
    Linux安装telnet
    linux 打压缩包
    JAVA读写文件
    LINUX安装NGINX
    Linux安装JDK
    java判断字符串中是否包含中文 过滤中文
    操作MySQL
    Java序列化对象-字符串转换
    nginx
    linux操作命令
  • 原文地址:https://www.cnblogs.com/wujf/p/9040739.html
Copyright © 2020-2023  润新知