其他分享
首页 > 其他分享> > 微信登录

微信登录

作者:互联网

流程图: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=>{
    ...
  })

标签:openid,code,登录,微信,access,href,let,unionid
来源: https://www.cnblogs.com/jiorence/p/15951603.html