From 883997ae875ed4beb53b71a6082f92872421a26c Mon Sep 17 00:00:00 2001 From: Shijf Date: Wed, 3 Jun 2020 14:00:00 +0800 Subject: [PATCH] Updata Version 1.0.8 --- docs/README.md | 11 ++++++ docs/echat/jssdk.md | 4 +- docs/echat/oAuth2.md | 72 +++++++++++++++++++++++++++++++++- lib/baseService/accessToken.js | 7 +++- lib/echat/jssdk/jssdk.js | 2 +- package.json | 5 ++- 6 files changed, 94 insertions(+), 7 deletions(-) diff --git a/docs/README.md b/docs/README.md index b493c44..f0e0b07 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,6 +2,17 @@ `egg-echat` 的安装非常简单,由于它是一个标准的 `egg-plugin` 包,这意味着,可以很方便的在 `eggjs` 中应用。当然在其他框架也可以用,`echat` 标准的 `npm` 包正在开发中...。 +> 当前版本: 1.0.8 + +#### 更新日志 + +- v 1.0.8 (2020年6月3日) + - 修复文档 jssdk 错误 + - 优化 oAuth2 使用说明 + - 优化 配置文件 apiUrl 强制要求,后边加不加 / 都可以了,哈P + +- v 1.0.7 + - 修复发送消息卡片推送格式问题 #### 环境要求 > - [Node.js](https://nodejs.org/zh-cn/): >= 8.0.0 diff --git a/docs/echat/jssdk.md b/docs/echat/jssdk.md index 908caff..850d913 100644 --- a/docs/echat/jssdk.md +++ b/docs/echat/jssdk.md @@ -45,7 +45,7 @@ const build = await jssdk.buildConfig(jsApiList, beat, debug); ```json { js_config: { - appId: 1000177, + appId: 'wl3584b9fc00', nonceStr: 'Xk5JFQji3e', timestamp: 1575253339, url: 'http://127.0.0.1:7001/jssdk', @@ -56,7 +56,7 @@ const build = await jssdk.buildConfig(jsApiList, beat, debug); }, js_agent_config: { agentid: 1000177, - corpid: 'wl4778b9fc00', + corpid: 'wl3584b9fc00', nonceStr: 'D8b2W3Gy4i', timestamp: 1575253339, url: 'http://127.0.0.1:7001/jssdk', diff --git a/docs/echat/oAuth2.md b/docs/echat/oAuth2.md index 058d17c..a70e0c0 100644 --- a/docs/echat/oAuth2.md +++ b/docs/echat/oAuth2.md @@ -6,6 +6,67 @@ 原理:eggjs中间件实现,中间的用法可参考 `eggjs` 文档 [中间件](eggjs.org/zh-cn/basics/middleware.html#使用中间件) 部分。 +## 一般使用 +在配置中需要配置相关的 `scope`,默认为:`snsapi_base`,此配置只能拿到基本信息,详见企业微信文档。在控制器中拿到授权信息: + +### 主要方法 + +- redirectUrl(url, scope, state) + +> - url 重定向的地址(eg: http://x.x.x.x:port),code会以查询参数的形式返回给应用(eg:http://x.x.x.x:port?code=asdfghjkl565ashhdgas ) +> - scope 共有三种可选 snsapi_base(默认) | snsapi_userinfo | snsapi_privateinfo +> - state 携带一些加密 回调回来可以以查询参数的形式(http://x.x.x.x:port?code=asdfghjkl565ashhdgas&code=test)完整携带回来 + +`{app_root}/app/controller.home.js` +```js + +async oauth2() { + + const { ctx } = this; + + // 拿到 echat 实例 + const echat = await this.ctx.echat(); + // 先判断 用户是否登录,一下代码需要根据实际情况而定 + const isEchatAuth = await ctx.service.echatAuth.check(); + if(!isEchatAuth) { + // 创建 oAuth2 实例 + const oAuth2 = echat.oauth2(); + // 获取到 code ,这个需要分情况看 如果前后端分离的话,可以前端传过来。单体应用的话,直接用下面的代码拿到code + const code = ctx.query.code; + // 此处判断一下,是否拿到了code,如果没有则需要重定向到企业微信的授权地址 + if (!code) { + // redirectUrl()方法中的参数详解 + ctx.unsafeRedirect(await oauth2.redirectUrl()); // 此处需要在系统配置里重定向白名单,并不是在此插件配置项 + return; + } + + + + // 判断用户是否需要详细信息 + function isDetail() { + let scope = (ctx.app.config.echat.Agent.oauth.hasOwnProperty('scopes') ? ctx.app.config.echat.Agent.oauth.scopes : null) ; + return scope === 'snsapi_base' ? false : true; + } + + // 拿到授权回来的 用户信息 + let userInfo = await oauth2.getUserInfo(code, isDetail()); + // 后续做登录相关业务逻辑 + //··· + // 此部分相关业务逻辑,最好写在中间中,最后将用户信息挂在 ctx 全局中 + + } + +} + + + + +``` + +## 利用 eggjs 中间件 使用 + +如果是单体应用(前端代码和后端部署在一起),就可以简单的用插件内部的中间件机制,很轻松的拿到用户信息。 + 示例讲解: 如果全局需要网页授权,则需要在配置文件中配置,如果仅仅是在某几个请求路径需要授权,则只需要在路由配置中应用即可: @@ -22,7 +83,7 @@ module.exports = app => { }; ``` -在配置中需要配置相关的 `scope`,默认为:`snsapi_base`,此配置只能拿到基本信息,详见企业微信文档。在控制器中拿到授权信息: + `{app_root}/app/controller.home.js` @@ -57,6 +118,15 @@ console.log(userInfo); +- 注意,如果使用了nginx等反向代理,则需要在插件配置文件中将 redirectUrl 改为 互联网入口地址。 +```js +//··· 省略部分代码 +AgentInfo: { + //···(修改以下部分) + // redirect_domain: '', //可信域名、不写http(https)协议 + // home_url: '', //主页链接,若没有则不写 写http(https)协议 +}, +``` diff --git a/lib/baseService/accessToken.js b/lib/baseService/accessToken.js index 14c5d34..ba2103f 100644 --- a/lib/baseService/accessToken.js +++ b/lib/baseService/accessToken.js @@ -69,8 +69,11 @@ class AccessToken { async getTokenFromServer() { const ctx = this.ctx; - - const baseUrl = this.config.Corp.ApiUrl; + let url = this.config.Corp.ApiUrl; + if (url[url.length - 1] == '/') { // 防止程序后续出错 + url = url.substr(0, url.length-1) + } + const baseUrl = url || this.config.Corp.ApiUrl; const httpClient = new HttpClient({ baseUrl, ctx diff --git a/lib/echat/jssdk/jssdk.js b/lib/echat/jssdk/jssdk.js index b6ea17d..7af56f3 100644 --- a/lib/echat/jssdk/jssdk.js +++ b/lib/echat/jssdk/jssdk.js @@ -63,7 +63,7 @@ class JsSdk { timestamp = timestamp ? timestamp : Math.round(new Date().getTime() / 1000); //时间戳 return { - appId: this.config.Agent.AgentId, + appId: this.config.Corp.CorpID, nonceStr, timestamp, url, diff --git a/package.json b/package.json index f919ee5..d3aadef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg-echat", - "version": "1.0.7", + "version": "1.0.8", "description": "基于nodejs的企业微信快速开发SDK", "eggPlugin": { "name": "echat" @@ -57,6 +57,9 @@ "bugs": { "url": "https://github.com/Shijf/egg-echat/issues" }, + "publishConfig": { + "registry": "https://npm.pkg.github.com/" + }, "homepage": "https://blog.sharef.top/egg-echat", "author": "", "license": "MIT"