微信-网页授权

参考文档

基础配置 

    代码测试说明测试公众号配置你本地的ip

完整的代码在文字末尾

 下载微信的开发者工具

公众号AppId以及AppSecret的申请,并可以获取到token

注意

  回调地址需要和js安全域名配置为同一个

  授权地址必须在微信执行(本地在微信的开发工具进行执行)

 授权地址需要进行urlEncode编码

   

步骤如下

 授权地址参考(静默授权)

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbb0f4798d5d78b80&redirect_uri=http://10.168.1.110:8080/crm/view/wechat/common_init.html?repairType=outside&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect


https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbb0f4798d5d78b80&redirect_uri=http%3A%2F%2F10.168.1.110%3A8080%2Fcrm%2Fview%2Fwechat%2Fcommon_init.html%3FrepairType%3Doutside&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect

根据code获取用户的openId

前端代码

   

var wechatCode = getUrlParam('code');
	if(null==wechatCode){
		return false;
	}
	var data="wechatCode="+wechatCode;
		$.ajax({
			url: "../../wechat/sendCode",
			type: "POST",
			data: data,
			dataType: "json",
			success: function(result) {
				console.log(result.code);
				if(result.code == "success") {
					debugger; 
					openId = result.map.openId;
					 localStorage.setItem("openId", result.map.openId);
					 location.href="repair_outside.html";
					 
				} else {
					common.comPopGo(result.msg);
					$('.container_text').fadeOut();
				}
			},
			error: function() {
				common.comPopGo('系统错误!');
				$('.container_text').fadeOut();
			}
		})

后端代码

	public static String WeChatOpenIdUrl="https://api.weixin.qq.com/sns/oauth2/access_token?grant_type=authorization_code&appid="+WeChatAppId+"&secret="+WeChatAppSecret+"&code=";
	public String getOpenId(String wechatCode) {
		String openId = null;
		if (null == Config.WeChatOpenIdUrl) {
			this.getTokenTieckt();
		}
		HttpGet get = new HttpGet(Config.WeChatOpenIdUrl + wechatCode);
		String res = HttpClientUtil.get(get, "");
		logger.info("====>getOpenId resulet 是 " + "\n" + res);
		if (StringUtils.isNotBlank(res)) {
			JSONObject json = JSONObject.fromObject(res);
			Object openIdObj = json.get("openid");
			if (null != openIdObj) {
				openId = openIdObj.toString();
			}

		}
		return openId;
	};

代码地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值