博客
关于我
微信公众号网页获取用户授权取得openid,和使用openid进行协助登录逻辑(Vue样例)
阅读量:780 次
发布时间:2019-03-22

本文共 3058 字,大约阅读时间需要 10 分钟。

微信网页开发 - 网页授权

一、获取openid的步骤

第一步:用户同意授权,获取code

在确保微信公众账号具有授权作用域(scope参数)的权限的前提下(默认权限包含snsapi_base和snsapi_userinfo),引导用户打开以下链接:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

注意事项:

  • scope参数有两种选择:
    • snsapi_base:不弹出授权页面,直接跳转。
    • snsapi_userinfo:弹出授权页面,可获取openid、昵称、性别、所在地等信息。

用户同意授权后,页面跳转至redirect_uri/?code=CODE&state=STATE

第二步:通过code换取网页授权access_token

这里获取的是特殊的网页授权access_token,与基础支持中的access_token不同。公众号可通过以下接口获取网页授权access_token:

https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID

特别注意:

  • access_token和secret的安全级别高,必须只存储在服务器,不允许传递给客户端。
  • 后续刷新access_token及获取用户信息的步骤必须从服务器执行。

说明:

  • 若使用snsapi_base,只需获取openid,流程到此结束。
  • 若使用snsapi_userinfo,需进行后续步骤。

第三步:刷新access_token(可选)

由于access_token有效期较短,可以使用refresh_token刷新。refresh_token有效期为30天,如失效需用户重新授权。

第四步:拉取用户信息(需scope为snsapi_userinfo)

在获取了access_token和openid后,可以通过以下接口拉取用户信息:

https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID

二、前端操作

前端功能简要说明:

  • 引导用户打开链接:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
  • 获取code,传递给后端。
  • 后端用code换取openid,返回给前端。

三、openid协助登录逻辑

业务需求:用户有自己的登录系统,若已在微信上登录,则直接跳转至内部页面,否则跳转至登录页。

登录逻辑:

  • 前端获取code,传递给后端。
  • 后端用code换取openid,调用登录接口。
    • 如果用户未绑定账户,跳转至第三方登录页。
    • 若已绑定,直接登录成功,存储token。
  • 第三方登录成功后,获取code,结合openid,传递给后端绑定账户。
  • 再次调用登录接口,存储token。
  • 四、获取openid的Vue代码示例

    export default {
    data() {
    return {
    appid: process.env.VUE_APP_WECHAT_APP_ID,
    openId: null
    };
    },
    mounted() {
    var that = this;
    const openIdStorage = storageDB.getItem('openId');
    if (openIdStorage) {
    this.openId = openIdStorage;
    } else {
    this.getCode();
    }
    },
    methods: {
    getCode() {
    const scope = 'snsapi_base'; // 静默授权
    const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.appid}&redirect_uri=${encodeURIComponent(window.location.href)}&response_type=code&scope=${scope}#wechat_redirect`;
    if (window.location.href.includes('?')) {
    window.location.href = url;
    } else {
    const theRequest = this.getUrlCode();
    const state = theRequest['state'];
    if (!state) {
    window.location.href = url;
    }
    }
    },
    getUrlCode() {
    const fullUrl = window.location.href;
    const theRequest = {};
    if (fullUrl.includes('#')) {
    const hash = fullUrl.split('#')[1];
    const param = hash.split('&')[0];
    const key = param.split('=')[0];
    const value = param.split('=')[1] || '';
    theRequest[key] = value;
    } else if (fullUrl.includes('?')) {
    const query = fullUrl.split('?')[1];
    const params = query.split('&');
    for (let param of params) {
    const [key, value] = param.split('=');
    if (key) {
    theRequest[key] = value;
    }
    }
    }
    return theRequest;
    }
    }
    };

    五、注意事项

    • 代码示例中变量如storageDB需根据实际情况配置。
    • API接口参数如ACCESS_TOKENOPENID需妥善处理。

    转载地址:http://thqwk.baihongyu.com/

    你可能感兴趣的文章
    nginx 后端获取真实ip
    查看>>
    Nginx 多端口配置和访问异常问题的排查与优化
    查看>>
    Nginx 如何代理转发传递真实 ip 地址?
    查看>>
    Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
    查看>>
    Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
    查看>>
    Nginx 学习(一):Nginx 下载和启动
    查看>>
    nginx 常用指令配置总结
    查看>>
    Nginx 常用配置清单
    查看>>
    nginx 常用配置记录
    查看>>
    nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
    查看>>
    Nginx 我们必须知道的那些事
    查看>>
    Nginx 的 proxy_pass 使用简介
    查看>>
    Nginx 的配置文件中的 keepalive 介绍
    查看>>
    Nginx 结合 consul 实现动态负载均衡
    查看>>
    Nginx 负载均衡与权重配置解析
    查看>>
    Nginx 负载均衡详解
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置https(一)—— 自签名证书
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx 配置清单(一篇够用)
    查看>>