项目作者: pengng

项目描述 :
用于微信网页授权,获取用户信息
高级语言: JavaScript
项目地址: git://github.com/pengng/wechat-oauth-middleware.git
创建时间: 2017-05-12T12:54:06Z
项目社区:https://github.com/pengng/wechat-oauth-middleware

开源协议:

下载


wechat-oauth-middleware

用于微信网页授权,获取用户信息

  • 两种模式

    • 如果是服务器需要保存用户的授权信息,则使用中间件,在中间件下游通过请求对象的wx属性拿到用户信息。
    • 如果是浏览器端要展示用户授权信息,则使用转发模式。服务器配置好一个端点,当前端页面需要获取用户信息时,利用 location.href 重定向到配置好的端点,重定向时带上 referer 参数,值为当前页面的链接,经过 页面A => 服务器B => 微信授权页C => 服务器B => 页面A,最后在页面A 的 location.href 中获取用户信息。
  • 搭配三种常用的 web 框架

    • express
    • koa
    • 原生 http 模块

Usage

  1. npm i wechat-oauth-middleware -S

OAuth(opt)

  • opt \
    • appId \ 必填,微信公众号appId
    • appSecret \ 必填,微信公众号appSecret
    • scope \ 微信授权类型,可选snsapi_basesnsapi_userinfo。默认为snsapi_base

搭配 Koa 框架

  1. const Koa = require('koa')
  2. const OAuth = require('wechat-oauth-middleware')
  3. const app = new Koa()
  4. const oauth = OAuth({
  5. appId: 'xxx',
  6. appSecret: 'xxx',
  7. scope: OAuth.SCOPE_USER_INFO
  8. })
  9. // 使用中间件
  10. app.use(oauth.koa)
  11. // 获取到的用户信息放在 ctx.wx 属性上
  12. app.use(async (ctx) => {
  13. console.log(ctx.wx)
  14. ctx.body = 'ok'
  15. })
  16. app.listen(3000)

搭配 express 框架

  1. const express = require('express')
  2. const OAuth = require('wechat-oauth-middleware')
  3. const app = express()
  4. // 配置中间件
  5. const oauth = OAuth({
  6. appId: 'xxx',
  7. appSecret: 'xxx',
  8. scope: OAuth.SCOPE_USER_INFO
  9. })
  10. // 配置错误处理
  11. app.use((err, req, res, next) => {
  12. console.warn(err)
  13. res.status(500).end('fail')
  14. })
  15. // 配置路由,使用中间件,获取到的用户信息会放到请求对象的wx属性上
  16. app.get('/wechat/login', oauth.express, (req, res) => {
  17. console.log(req.wx)
  18. res.end('ok')
  19. })
  20. app.listen(3000)

搭配原生 http 模块

  1. const http = require('http')
  2. const OAuth = require('wechat-oauth-middleware')
  3. // 配置中间件
  4. const oauth = OAuth({
  5. appId: 'xxx',
  6. appSecret: 'xxx',
  7. scope: OAuth.SCOPE_USER_INFO
  8. })
  9. const server = http.createServer((req, res) => {
  10. // 将请求对象和响应对象传入中间件,在回调中可以拿到错误对象和用户信息
  11. oauth.native(req, res, (err, ret) => {
  12. if (err) {
  13. console.error(err)
  14. res.status(500).end('fail')
  15. return
  16. }
  17. console.log(ret)
  18. res.end('ok')
  19. })
  20. })
  21. server.listen(3000)

转发模式

用于前端展示用户的信息

在服务器A.com配置授权端点,以express框架为例
  1. const express = require('express')
  2. const OAuth = require('wechat-oauth-middleware')
  3. const app = express()
  4. const oauth = OAuth({
  5. appId: 'xxx',
  6. appSecret: 'xxx',
  7. scope: OAuth.SCOPE_USER_INFO
  8. })
  9. // 将 express 属性传入 forward 方法获得转发功能
  10. // 此时授权端点为 http://A.com/auth
  11. app.get('/auth', oauth.forward(oauth.express))
  12. app.listen(3000)
在前端页面需要用户信息时,重定向到授权端点
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <script>
  10. let query = {}
  11. // 解析地址栏的参数
  12. if (location.search) {
  13. location.search.slice(1).split('&').forEach(raw => {
  14. let map = raw.split('=')
  15. query[map[0]] = decodeURIComponent(map[1])
  16. })
  17. }
  18. // 判断是否已经拿到数据
  19. if (query.openid) {
  20. // 如果地址栏已经有用户信息,则输出到页面
  21. document.write('<pre>' + JSON.stringify(query, null, 4) + '</pre>')
  22. } else {
  23. // 重定向到配置好的端点,要带上 referer 参数,获取到用户信息后才能正确跳转回当前页面
  24. location.href = 'http://A.com/auth?referer=' + encodeURIComponent(location.href)
  25. }
  26. </script>
  27. </body>
  28. </html>
Koa 配置转发端点示例
  1. const Koa = require('koa')
  2. const router = require('koa-router')()
  3. const OAuth = require('wechat-oauth-middleware')
  4. const app = new Koa()
  5. const oauth = OAuth({
  6. appId: 'xxx',
  7. appSecret: 'xxx',
  8. scope: OAuth.SCOPE_USER_INFO
  9. })
  10. router.get('/auth', oauth.forward(oauth.koa))
  11. app.use(router.routes())
  12. app.listen(3000)
原生 http 包配置转发端点示例
  1. const http = require('http')
  2. const OAuth = require('wechat-oauth-middleware')
  3. const oauth = OAuth({
  4. appId: 'xxx',
  5. appSecret: 'xxx',
  6. scope: OAuth.SCOPE_USER_INFO
  7. })
  8. http.createServer(oauth.forward(oauth.native)).listen(3000)