首先附上微信官方文档:
授权登陆大致流程
1.前端重定向到授权地址:
var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + "你的appid" + "&redirect_uri=" + "你当前的页面地址,这个地址跟后台的域名回调地址的域名相同" + "&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect";# 重定向到微信授权地址window.location.href=url复制代码
注:redirect_uri:的地址所在域名需要在公众平台后台配置: 如图:
2.获取微信返回到code,给后台传过去
var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + "你的appid" + "&redirect_uri=" + "你当前的页面地址,这个地址跟后台的域名回调地址的域名相同" + "&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect";var codeUrl = location.search();if(codeUrl!=""&& codeUrl!=null&&code.indexOf("code")!=-1){//此处就获得了code,具体代码自己写吧,就是截取字符串获取code参数}else{// 重定向到微信授权地址window.location.href=url}复制代码