package jp.co.dreamarts;

import jp.co.dreamarts.platform.wx.WeixinMessage;
import jp.co.dreamarts.platform.wx.WeixinUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/restful/weixin.do")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String doGet(HttpServletRequest request) {
        String echostr = request.getParameter("echostr");
        request.setAttribute("echostr", echostr);
        return "hello";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String doPost(HttpServletRequest request) throws IOException {
        WeixinMessage wxMsg = null;
        try {
            wxMsg = WeixinUtils.parseXML(request.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(wxMsg.getContent());
        System.out.println("wxMsg.getReceiver()" + wxMsg.getReceiver());
        System.out.println("wxMsg.getSender()" + wxMsg.getSender());
        List list = new ArrayList();
        Map a1 = new HashMap();
        a1.put("name","李浩");
        a1.put("phone","18642698912");
        Map a2 = new HashMap();
        a2.put("name","李四");
        a2.put("phone","123123123");
        Map a3 = new HashMap();
        a3.put("name","刘鹏");
        a3.put("phone","33343434");
        Map a4 = new HashMap();
        a4.put("name","大爷");
        a4.put("phone","3123434");
        list.add(a1);
        list.add(a2);
        list.add(a3);
        list.add(a4);
        request.setAttribute("wxMsg", wxMsg.getReply());
        String phone = "";
        for(int i = 0 ; i<list.size();i++){
            Map result = (Map)list.get(i);
            String s = (String)result.get("name");
            String p = (String)result.get("phone");
            if(s.equals(wxMsg.getContent())) {
                phone = p;
            }
        }
        if(phone.length() == 0 ){
            phone = "查无此人";
        }
        request.setAttribute("content", phone);

        return "weixin";
    }


}
@@ -2287,32 +2287,67 @@
 %E6%97%A0%E6%AD%A4%E4%BA%BA%22;%0A        %7D%0A
+        System.out.println(phone);%0A
         request.

test 微信