1. 修改微信菜单栏url连接和项目里对应
"{\"button\":"
+ "["
+ "{\"name\":\"纳税服务\","
+ "\"sub_button\":"
+ "["
+ "{\"type\":\"view\",\"name\":\"政策解读\",\"url\":\"http://www.chinatax.gov.cn/n810341/n810760/index.html\"},"
+ "{\"type\":\"view\",\"name\":\"最新法规\",\"url\":\"http://www.chinatax.gov.cn/n810341/n810755/index.html\"},"
+ "{\"type\":\"click\",\"name\":\"我要咨询\",\"key\":\"m_imgmsg\"},"
+ "{\"type\":\"view\",\"name\":\"微信公示\",\"url\":\"http://kbrhwf.natappfree.cc/YA2017_1/index.html\"},"
+ "{\"type\":\"view\",\"name\":\"微信取号\",\"url\":\"http://kbrhwf.natappfree.cc/项目名称/项目地址\"}"
+ "]"
+ "},"
+ "{\"name\":\"税务查询\","
+ "\"sub_button\":"
+ "["
+ "{\"type\":\"view\",\"name\":\"法规查询\",\"url\":\"http://hd.chinatax.gov.cn/guoshui/main.jsp\"},"
+ "{\"type\":\"view\",\"name\":\"发票查询\",\"url\":\"http://121.28.0.35:7008/fpxx/fpxxcx.jsp\"}"
+ "]"
+ "}"
+ "]"
+ "}";
2. 获取用户的code,拿到code已经成功一半
/**
*
* @Title: IndexPage
* @Description: TODO首页
* @return: void
*/
@RequestMapping("/IndexPage")
// ---------------------设置
public String IndexPage()
{
System.out.println("欢迎来到ya2017_1================");
String uri = myHostName + "YA2017_1/robot/getAllOrganInfo.do";
logger.info("IndexPage() ready to redirect:"+uri);
return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid
+ "&redirect_uri="
+ uri
+ "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
}
3通过code获取Opendid
@RequestMapping("/getAllOrganInfo.do")
public String getAllOrganInfo(String code, ModelMap map,HttpServletRequest request)
{
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
+ appid
+ "&secret="
+ appsecret
+ "&code="
+ code
+ "&grant_type=authorization_code";
String backString = WeixinHttpRequestUtil.httpRequest(requestUrl,"GET", null);
WeChatUserInfo weChatUserInfo = JSON.parseObject(backString,WeChatUserInfo.class);
String weChatId = weChatUserInfo.getOpenid();
HttpSession session = request.getSession();
session.setAttribute("openId", weChatId);
//异常信息
map.addAttribute("errormsg", errormsg);
map.addAttribute("list", list);
return "/robot/AllOrgan";
}
4.通过Openid然后向用户发送推送消息
public String randomNumber(OrganInfo organInfo, ModelMap map,HttpServletRequest request, HttpServletResponse response) throws IOException
{
String openId = null;
System.out.println("---------------------------------------------------------");
// logger.info("Enter getAllOrganService(),organInfo="+organInfo.toString());
if (request.getSession().getAttribute("openId") != null)
{
openId = (String) request.getSession().getAttribute("openId");
// //
String ACCESS_TOKEN=GetToken.getAccessToken();//
String backStringTwo =null;
String messageContent="你好,你取到的随机号码是9527777,请凭随机码去大厅取号,可不用排队呦!";
if( ACCESS_TOKEN!=null){
String data="{"
+ "\"touser\": \""+openId+"\","
+ " \"msgtype\": \"text\","
+ " \"text\": {"
+ " \"content\": \""+ messageContent +"\""
+ "}"
+ "}";
String requestUrlTwo="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+ACCESS_TOKEN;
System.out.println( requestUrlTwo);
backStringTwo=WeixinHttpRequestUtil.httpRequest(requestUrlTwo,"POST",data);
}
System.out.println("backString-----"+backStringTwo);
System.out.println("跳转到成功页面之前获取到的openid===="+openId);
logger.info("get openId(weixin userid)="+openId);
}
return "/robot/success";
}
注意配置网页授权