java后台关联微信公众号开发

1.需要配置到微信公众号的法控制类

注意:get是用于微信公众号的校验哒
post是用来接收公众号发送的请求进行处理

package com.youruan.logistics.controller;

import com.youruan.logistics.config.global.Mark;
import com.youruan.logistics.util.MessageUtil;
import com.youruan.logistics.util.SignUtil;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Enumeration;
import com.youruan.logistics.entity.TemplateParam;
import com.youruan.logistics.entity.WxTemplate;
import com.youruan.logistics.util.HttpUtilOfWx;
import com.youruan.logistics.util.SendWxMessage;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wechat")
public class WeChatController {
    private static Logger logger = Logger.getLogger(WeChatController.class);

    @RequestMapping(value = "/wx.do")
    public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {

        logger.info("WechatController   ----   WechatController");

        System.out.println("========WechatController========= ");
        logger.info("请求进来了...");

        Enumeration pNames = request.getParameterNames();
        while (pNames.hasMoreElements()) {
            String name = (String) pNames.nextElement();
            String value = request.getParameter(name);
            // out.print(name + "=" + value);

            String log = "name =" + name + "     value =" + value;
            logger.info(log);
        }

        String signature = request.getParameter("signature");/// 微信加密签名
        String timestamp = request.getParameter("timestamp");/// 时间戳
        String nonce = request.getParameter("nonce"); /// 随机数
        String echostr = request.getParameter("echostr"); // 随机字符串
        PrintWriter out = response.getWriter();

        if (SignUtil.checkSignature(signature, timestamp, nonce)) {
            out.print(echostr);
        }
        out.close();
        out = null;

    }
    @PostMapping(value = "/wx.do")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception,IOException {
        logger.info("接入成功,正在处理逻辑");
        //接受微信服务器发送过来的XML形式的消息
        InputStream in=request.getInputStream();
        BufferedReader reader=new BufferedReader(new InputStreamReader(in,"UTF-8"));
        String sReqData="";
        String itemStr="";//作为输出字符串的临时串,用于判断是否读取完毕
        while((itemStr=reader.readLine())!=null){
            sReqData+=itemStr;
        }
        in.close();
        reader.close();

        System.out.println("收到消息:"+sReqData);
        /*
        <xml><ToUserName><![CDATA[gh_14c875827291]]></ToUserName>
        <FromUserName><![CDATA[o8ulT1qcjL7B3BHnqwX8cQnaoVQo]]></FromUserName>
        <CreateTime>1565690913</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[subscribe]]></Event>
        <EventKey><![CDATA[]]></EventKey></xml>
         */
        //防止中文乱码
        response.setCharacterEncoding("UTF-8");
        MessageUtil.replyMessage(sReqData,response.getWriter());
        /*InputStream inputStream = request.getInputStream();
        Map<String, String> params = XMLUtil.parseStreamToMap(inputStream);
        String respXml = messageHandleService.handleMessage(params);
        if (StringUtils.isNotEmpty(respXml)) {
            // 输出流
            response.getWriter().write(respXml);
        }*/

    }

    /**
     * 給用戶推送消息通知
     * 參數:用戶openId  物流實體
     * @return
     * @throws Exception
     */
    @PostMapping("test")
    public String updateById() throws Exception {
        //根据bid去查询出bos_customer中的数据
        /*Bos_customer customer=projectService.findCustomerByBid(bid);
        System.out.println("Bos_customer:"+customer.getCid()+"-------"+customer.getCname()+"-------"+customer.getOpenId());
        */
        //
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值