微信登錄


流程圖: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