Skip to content

Commit

Permalink
Updata Version 1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Shijf committed Jun 3, 2020
1 parent 4aed045 commit 883997a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
11 changes: 11 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/echat/jssdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
72 changes: 71 additions & 1 deletion docs/echat/oAuth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 中间件 使用

如果是单体应用(前端代码和后端部署在一起),就可以简单的用插件内部的中间件机制,很轻松的拿到用户信息。

示例讲解:

如果全局需要网页授权,则需要在配置文件中配置,如果仅仅是在某几个请求路径需要授权,则只需要在路由配置中应用即可:
Expand All @@ -22,7 +83,7 @@ module.exports = app => {
};

```
在配置中需要配置相关的 `scope`,默认为:`snsapi_base`,此配置只能拿到基本信息,详见企业微信文档。在控制器中拿到授权信息:


`{app_root}/app/controller.home.js`

Expand Down Expand Up @@ -57,6 +118,15 @@ console.log(userInfo);



- 注意,如果使用了nginx等反向代理,则需要在插件配置文件中将 redirectUrl 改为 互联网入口地址。

```js
//··· 省略部分代码
AgentInfo: {
//···(修改以下部分)
// redirect_domain: '', //可信域名、不写http(https)协议
// home_url: '', //主页链接,若没有则不写 写http(https)协议
},
```


7 changes: 5 additions & 2 deletions lib/baseService/accessToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/echat/jssdk/jssdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-echat",
"version": "1.0.7",
"version": "1.0.8",
"description": "基于nodejs的企业微信快速开发SDK",
"eggPlugin": {
"name": "echat"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 883997a

Please sign in to comment.