微信公众号开发-微信公众号网页H5静默授权!!!


静默授权 也就是   默默的授权哈哈

           只能拿到已关注用户的信息

简单流程:

1.换取code.  

我用的uniapp写的

在create的时候就调用 getWechatCode()

页面:

getWechatCode() { // 非静默授权,第一次有弹框
    const code = this.getUrlParam('code') // 截取路径中的code,如果没有就去微信授权,如果已经获取到了就直接传code给后台获取openId
    console.log(" code"+code)
    alert("code"+code)
    const local = window.location.href
  if (code == null || code === '') {
    window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + 'appID' + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
  } else {
  this.getOpenId(code); // 把code传给后台获取用户信息这里根据官方文档拿到code就能获取到用户信息了然后做你想要的操作
  console.log(" code2"+code)
  alert(" code2"+code);
  }
},

 

getUrlParam(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
    let url = window.location.href.split('#')[0]
    let search = url.split('?')[1]
    console.log(search)
   if (search) {
    var r = search.substr(0).match(reg)
    if (r !== null)
      return unescape(r[2])
      return null
    } else
  return null
  },

 

 

后台就不写的吧!

这个方法说就是一个ajax    getOpenId  将code穿到后台换取openid 然后再拿到用户信息就行了

微信公众号开发推荐可以看下这个:fastweixin      https://gitee.com/pyinjava/fastweixin

还有这个

关于微信公众号开发欢迎大家一起交流!!!

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM