Skip to content

Commit

Permalink
✨支持客户端开启复制小程序路径
Browse files Browse the repository at this point in the history
  • Loading branch information
alexayan committed Jun 3, 2020
1 parent 7871421 commit 9f31584
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [x] 小程序体验成员管理(查询,添加,删除)
- [x] 小程序版本管理(获取所有版本列表,版本设为体验版,版本提审,撤回提审,版本发布)
- [x] 生成任意小程序的小程序码
- [x] 客户端开启复制小程序路径

## 安装

Expand Down Expand Up @@ -204,10 +205,34 @@ const appId = await admin.exec("mina_qrcode", {
});
```

### [MinaToolsCommand](lib/commands/MinaToolsCommand.js)

小程序工具指令,支持复制小程序路径

```js
// 开启客户端复制小程序路径功能
// 该微信用户可打开小程序右上角菜单,点击“复制页面路径”并粘贴至左侧“小程序页面路径”中
const isSuccess = await admin.exec("mina_tools", {
type: 'copy-path',
appId: '抽奖助手',
userName: 'yanhaibiao1991'
});
```

```js
// 获取 appId
const appId = await admin.exec("mina_tools", {
type: 'appid',
appName: '抽奖助手'
});
```

## Command Api

开发者通过继承 Command 来编写自定义指令, 具体例子可以查看 `lib/commands` 目录中的内置指令

内置指令类名和所在文件名必须符合 `AxxBxxCommand` 的格式, 指令名解析为 axx_bxx

```js
const {Command} = require("mina-admin");

Expand Down
1 change: 0 additions & 1 deletion lib/commands/MinaQrcodeCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class MinaQrcodeCommand extends Command {
}
);
let content = await resp.json();
console.log(content);
if (!content.appid) {
throw new Error(
`请输入正确的小程序的名称/AppID/账号原始ID,并确保小程序允许被搜索`
Expand Down
110 changes: 110 additions & 0 deletions lib/commands/MinaToolsCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"use strict";

/**
* @description  小程序码生成服务
*/

const querystring = require("../utils/querystring");
const Command = require("../command");

class MinaToolsCommand extends Command {
async exec() {
const opt = this.args.type;
switch (opt) {
case "copy-path":
return this.enableCopyMinaPath();
case "appid":
return this.queryAppId();
default:
throw new Error(`${opt} not support`);
}
}

/**
* 开启小程序路径复制
*/
async enableCopyMinaPath() {
this.logger.info("enable copy mina path...");
let appId = this.args.appId;
const userName = this.args.userName;
if (!appId) {
throw new Error(
"请输入正确的小程序的名称/AppID/账号原始ID,并确保小程序允许被搜索"
);
}

if (!userName) {
throw new Error("请输入项目成员的微信号");
}

if (!/^wx.{16}$/.test(appId)) {
appId = await this.getAppidByAppName(appId);
}

const userInfo = this.admin.getUser();
const params = {
path: `/wxopen/wxaqrcode?action=copy_wxa_path`,
token: userInfo.token,
random: String(Math.random())
};
let resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?${querystring.encode(params)}`,
{
body: `appid=${appId}&username=${userName}`,
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
}
}
);
let content = await resp.json();
return !content.ret;
}

async queryAppId() {
this.logger.info("query appid...");
let appName = this.args.appName;
if (!appName) {
throw new Error(
"请输入正确的小程序的名称/AppID/账号原始ID,并确保小程序允许被搜索"
);
}

const appId = await this.getAppidByAppName(appName);
return appId;
}

/**
* 根据名称/AppID/账号原始ID获取 appId
* @param {string} appName 名称/AppID/账号原始ID
*/
async getAppidByAppName(appName) {
this.logger.info("getAppidByAppName...", appName);
const userInfo = this.admin.getUser();
const params = {
path: `/wxopen/wxaqrcode?action=search`,
token: userInfo.token,
random: String(Math.random())
};
let resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?${querystring.encode(params)}`,
{
body: `appid=${encodeURIComponent(appName)}`,
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
}
}
);
let content = await resp.json();
if (!content.appid) {
throw new Error(
`请输入正确的小程序的名称/AppID/账号原始ID,并确保小程序允许被搜索`
);
}

return content.appid;
}
}

module.exports = MinaToolsCommand;
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.2",
"version": "0.1.3",
"description": "mina admin",
"homepage": "https://github.com/alexayan/mina-admin",
"author": {
Expand Down

0 comments on commit 9f31584

Please sign in to comment.