微信开发获取openid并向用户推送消息

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

注意配置网页授权
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值