一、页面授权

  

// 进入页面获取权限code
function initAuthorizeCode() {
var appid = $("#appid").val();//公众号appid
var openid = $("#openId").val();//openid
var userId = $("#userId").val();
var code = getUrlParam('code');
var local = window.location.href;
var hrefurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+encodeURIComponent(local)+"&response_type=code&scope=snsapi_base&state=dcabe11a-751f-490f-9dcc-606881c6fcdb#wechat_redirect";
var localopenid = localStorage.getItem("loginStorageKey"+userId);
if((code==null || code == "")&&(openid==null||openid==""||openid=="null")){
window.location.href = hrefurl;
// window.location.replace(hrefurl);
}else{
// var localopenid = localStorage.getItem("loginStorageKey"+userId);
if(openid==undefined || openid=="" || openid==null|| openid=="null"||openid=="undefined"){
console.info(code);
$.ajax({
url: basePath+'loginController/getOpenId',
type:'post',
dataType:'json',
data:{code:code},
async:false,
success:function(result){
console.info(result);
localStorage.setItem("loginStorageKey"+userId,result.openid);
result = true;
},
error:function(msg){
console.info(msg);
},
});
} }
}

  

二、通过code获取openid

	@RequestMapping(value = "/getOpenId")
public @ResponseBody String getOpenId(HttpServletRequest request) {
Map<String, Object> maps = new HashMap<String, Object>();
maps.put("succ", true);
maps.put("msg", "success");
String code = request.getParameter("code");
String openId = (String) request.getSession().getAttribute("wx_user_openid");
try {
// 调用查询接口 如果还是失败返回失败页面
WxOauthLogin wxAuth = new WxOauthLogin();
String url = wxAuth.getAccessTokenUrl(code); log.info("获取用户openid请求参数 result param:{}", url); URL urlObject = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) urlObject.openConnection(); // 将返回的输入流转换成字符串
InputStream inputStream = urlConnection.getInputStream(); // 指定编码格式
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader in = new BufferedReader(inputStreamReader);
String jsonUserStr = in.readLine().toString(); // 释放资源
inputStream.close();
inputStream = null;
urlConnection.disconnect(); log.info("通过code换取网页授权access_token result:{}", jsonUserStr);
if (jsonUserStr != null) {
Map<String, String> resultmap = Json.toObject(jsonUserStr, Map.class);
if (StringUtil.empty(resultmap.get("openid"))) {
log.error("获取openid失败");
maps.put("succ", false);
maps.put("msg", "获取openid失败");
} else {
maps.put("openid", resultmap.get("openid"));
maps.put("unionid", resultmap.get("unionid"));
// 将openId放到缓存
if(ObjectUtils.isEmpty(openId)){
openId = resultmap.get("openid");
request.getSession().setAttribute("wx_user_openid", openId);
maps.put("openid", openId);
}
}
}
} catch (Exception e) {
log.error("获取openid失败");
maps.put("succ", false);
maps.put("msg", "获取openid失败");
}
log.info("获取用户openid执行结果:{}",Json.toJson(maps));
return Json.toJson(maps);
// return openId; }

  

三、根据code获取openid

 public String getAccessTokenUrl(String code) {
// ?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
StringBuffer url = new StringBuffer("https://api.weixin.qq.com/sns/oauth2/access_token");
url.append("?appid=" + wxResourceProp.getAppid());
url.append("&secret=" + wxResourceProp.getAppsecret());
url.append("&code=" + code);
url.append("&grant_type=authorization_code");
return url.toString();
}

  

至此未关注公众号的微信用,可以用jsapi支付方式支付公众号里的订单。

H5页面获取openid,完成支付公众号(未关注公众号)支付的更多相关文章

  1. 微信网页授权-公众号支付(获取openid、用户信息等)

    名词解释: openid 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID 业务功能描述:实现H5页面可以在微信浏览器里面进行微信支付,所以需要 ...

  2. 微信公众号授权回调用户信息,获取openid

    1.--------------------用户授权登录并获取code 授权登录方式有两个,一种为静默授权登录(scope=snsapi_base),一种为非静默授权登录(scope=snsapi_u ...

  3. 微信测试号开发之九 微信网页授权:页面获取用户openid

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78981369 一:配置接口 注意:这里填写的是域名(是一个字符串),而不是URL,因此 ...

  4. Java微信公众平台开发之公众号支付(微信内H5调起支付)

    官方文档点击查看准备工作:已通过微信认证的公众号,必须通过ICP备案域名(否则会报支付失败)借鉴了很多大神的文章,在此先谢过了 整个支付流程,看懂就很好写了 一.设置支付目录 在微信公众平台设置您的公 ...

  5. 亲历H5移动端游戏微信支付接入及那些坑(二)——获取Openid和授权

    第一篇中将一些坑说明,那么这篇开始正式进入接入步骤.具体的参数说明,我不会列出,毕竟微信官方文档都有,我想大家都看的懂,而且这接口也有可能微信会变动,所以不列出来,也是不想引起大家的误解,接入步骤只起 ...

  6. 公众号H5页面接入微信登录流程

    公众号H5页面接入微信登录流程 源码地址 https://gitee.com/szxio/h5_weixin 起步 首先创建一个项目,我们采用uni-app来作为我们的前端框架 环境安装 全局安装vu ...

  7. 微信H5页面内实现一键关注公众号

    H5页面内实现关注公众号的微信JSSDK没有相关接口开放,因此就得动点脑筋来实现该功能了.下面的方法就是通过一种非常蹊跷的方式实现的. 首先,需要在公众号内发表一篇原创文章,注意是原创文章,然后由另一 ...

  8. 微信公众号之获取openId

    在小伙伴们开发微信公众号.小程序或者是在微信内置浏览器打开的项目时,会遇到的第一个问题就是如何获取openId,今天小编就给大家带来的是如何获取openId. 首先   我们要从微信开发者后台得到ap ...

  9. H5页面怎么跳转到公众号主页?看过来

    前言: 做公众号开发的小伙伴,可能会遇到这种需求: 在一个H5页面点击一个关注公众号按钮跳转到公众号主页. 听到这个需求的一瞬间,疑惑了!这不可能! 摸了摸高亮的额头!没办法,做还是要做的 开始上解决 ...

随机推荐

  1. Qt+json

    Json文件是这样: { "first fruit": { "describe":"an apple", "icon": ...

  2. CentOS7.2 安装Chrome

    /etc/yum.repos.d/目录下新建文件google-chrome.repo,向其中添加如下内容: [google-chrome] name=google-chrome baseurl=htt ...

  3. [BZOJ1257][CQOI2007]余数之和

    题目大意 给你 \(n, k\),计算 $ \sum_{i=1}^n k \bmod i$ 解析 注意到 $ k\bmod i=k-[k/i] \times i$ 则上式等于 $ n \times k ...

  4. Homebrew/Linuxbrew 安装常有工具

    Homebrew https://brew.sh/ Install: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent ...

  5. Intel IDEA 2018破解(亲测成功)

    破解网址:https://jingyan.baidu.com/article/cb5d6105d9b1b1005d2fe074.html

  6. 深入理解javascript之typeof和instanceof

    1.https://blog.csdn.net/mevicky/article/details/50353881 (深入理解javascript之typeof和instanceof)

  7. 058——VUE中vue-router之实例操作新闻列表单页面应用与路由别名的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. bzoj1077

    题解 这道题n的范围很小,所以我们可以考虑枚举+判定 设放在天平右边的是C,D. 以A+B<C+D为例,因为差分约束必须是差的形式,所以我们将式子变形 B−C<D−A然后枚举D,A的取值, ...

  9. [批处理]自动修改本机IP地址

    前言 抱着笔记本经常到处跑的人,今天回宿舍上网,明天去机房上网,后面去办公室上网,每到一个地方,都要更换一次IP网关掩码 如果都是DHCP还好,关键是为了组织为了方便管理这些地方都是使用的静态IP,所 ...

  10. FIS 的思想和优点

    资源表 各种性能优化算法的加载框架 依赖声明有助于组件化 资源自动合并 链接 与webpack对比