微信登录


流程图:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Development_Guide.html

 

 

 

 

 

1.点击登陆按钮

  let link = location.href;

  let uri = encodeURIComponent(link);

  window.location.href =
  `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;

2.重定向后的页面
  http://sw.juzhonglw.cn/testwxh5/?code=001X7G1w36oG3Y2i111w3wYlPe0X7G1T&state=STATE#/packageBuild/common/projectList
  截取出code的值

3.拿取code参数

let href = location.href.split('code=')[1];
  if (href) {
    let code = href.split('&')[0];
  if (code) {
    // 通过code 拿取openid、unionid、access_token 等信息
    this.aotuLogin(code)
  }
}

4.通过刚刚拿到的code,调用接口获取 openid、unionid、access_token 等信息(后端完成,然后提供接口给前端调用)

  https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

5.调用微信登录接口,提供 openid、unionid 给后端

  request('/user/login',{
    openid,unionid
  }).then(d=>{
    ...
  })


免责声明!

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



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