Skip to content

Commit

Permalink
add login-expired event
Browse files Browse the repository at this point in the history
  • Loading branch information
alexayan committed Jun 2, 2020
1 parent 02c25bf commit 7871421
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ admin.init()

### 登录 login()

调用后台功能前必须先登录,第一次登录或者登录会话过期,会在控制台中打印二维码,需要管理员通过微信进行扫码登录。除此之外,还可以通过监听 `qrcode` 事件,在回调中获取二维码,开发者可以通过自定义方式发送二维码给管理员。
调用后台功能前必须先登录,第一次登录或者登录会话过期,会在控制台中打印二维码,需要管理员通过微信进行扫码登录。

通过监听 `qrcode` 事件,在回调中获取二维码,开发者可以通过自定义方式发送二维码给管理员。

通过监听 `login-expired` 事件,处理登录过期,并添加重新登录逻辑。

```js
admin.on('qrcode', (imageBuffer) => {
Expand All @@ -71,6 +75,12 @@ admin.on('qrcode', (imageBuffer) => {
await admin.login();
```

```js
admin.on('login-expired', () => {
admin.login()
})
```

### 注册扩展指令 registerCommand()

除了使用默认小程序后台指令外,开发者还可以编写自定义指令并注册
Expand Down
3 changes: 3 additions & 0 deletions lib/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class MinaAdmin extends EventEmitter {
this.penddingCommands = [];
this.isLogin = false;
this.logger = Logger.getLogger("MinaAdmin");
this.on("login-expired", () => {
this.isLogin = false;
});
}

async init() {
Expand Down
12 changes: 11 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@ class AdminCommand extends EventEmitter {
);
options.method = options.method || "GET";
url = nodeurl.resolve("https://mp.weixin.qq.com", url);
return fetch(url, options);
const resp = await fetch(url, options);

if (resp.headers.get("content-type").indexOf("application/json") > -1) {
const cloneResp = await resp.clone();
const content = await cloneResp.json();
if (_.get(content, "ret") === 200003) {
this.admin.emit("login-expired");
}
}

return resp;
}

async getTicket(params = {}) {
Expand Down
6 changes: 4 additions & 2 deletions lib/commands/LoginCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LoginCommand extends Command {
const isLogin = await this.checkLogin();

if (isLogin) {
return;
return true;
}

this.logger.info("start login", account);
Expand Down Expand Up @@ -85,7 +85,7 @@ class LoginCommand extends Command {
return resp;
}

if (_.get(content, "base_resp.ret", -1) !== 0) {
if (_.get(content, "base_resp.ret") !== 0) {
this.setCookie("");
throw new Error("login fail");
}
Expand Down Expand Up @@ -133,6 +133,8 @@ class LoginCommand extends Command {
if (!isLogin) {
await this.exec();
}

return true;
} catch (e) {
this.logger.error(e);
this.logger.info("qrcode expired, reload....");
Expand Down
1 change: 1 addition & 0 deletions lib/commands/MinaQrcodeCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class MinaQrcodeCommand extends Command {
}
);
let content = await resp.json();
console.log(content);
if (!content.appid) {
throw new Error(
`请输入正确的小程序的名称/AppID/账号原始ID,并确保小程序允许被搜索`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mina-admin",
"version": "0.1.1",
"version": "0.1.2",
"description": "mina admin",
"homepage": "https://github.com/alexayan/mina-admin",
"author": {
Expand Down

0 comments on commit 7871421

Please sign in to comment.