0.1.4 • Published 4 years ago
mina-admin v0.1.4
mina-admin
微信小程序公众平台后台功能非官方 SDK
帮助构建小程序自动化系统,小程序管理后台
已支持功能
- 小程序后台登录
- 小程序体验成员管理(查询,添加,删除)
- 小程序版本管理(获取所有版本列表,版本设为体验版,版本提审,撤回提审,版本发布)
- 生成任意小程序的小程序码
- 客户端开启复制小程序路径
- 小程序获取公众号文章渠道来源数据
安装
$ npm install --save mina-admin
使用
const {Admin} = require('mina-admin');
const admin = new Admin({
account: "",
password: ""
});
await admin.init();
await admin.login();
const users = await admin.command("mina_expr_users")({
type: "list"
});
环境变量
STORAGE_FILE
: 定义运行过程中用户数据(cookie...)存储的路径, 默认为 lib/.storage
CRYPTO_KEY
: 用户数据加密秘钥
Admin Api
获取 Admin 对象
const admin = new Admin({
account: "", // 微信小程序后台登录账号
password: "" // 微信小程序后台登录密码
})
初始化 init()
使用前必须先调用初始化方法
admin.init()
登录 login()
调用后台功能前必须先登录,第一次登录或者登录会话过期,会在控制台中打印二维码,需要管理员通过微信进行扫码登录。
通过监听 qrcode
事件,在回调中获取二维码,开发者可以通过自定义方式发送二维码给管理员。
通过监听 login-expired
事件,处理登录过期,并添加重新登录逻辑。
admin.on('qrcode', (imageBuffer) => {
})
await admin.login();
admin.on('login-expired', () => {
admin.login()
})
注册扩展指令 registerCommand()
除了使用默认小程序后台指令外,开发者还可以编写自定义指令并注册
admin.registerCommand(Command)
执行功能指令 exec(), command()
执行指令有两种不同的方式
admin.exec('cmd_name', args)
admin.command('cmd_name')(args)
获取用户信息 getUser()
admin.getUser()
内置指令
MinaCodeCommand
提供小程序版本管理功能(获取所有版本列表,版本设为体验版,版本提审,撤回提审,版本发布)
// 获取当前的小程序版本(线上版本,审核版本,开发版本)
const codes = await admin.exec("mina_code", {
type: 'list'
});
console.log(codes.dev) // 开发版本 [Code, Code]
console.log(codes.exper) // 体验版本 Code
console.log(codes.online) // 线上版本 Code
// 版本设为体验版
const codes = await admin.exec("mina_code", {
type: 'expr',
code: Code
});
// 版本提审
const codes = await admin.exec("mina_code", {
type: 'review',
code: Code
});
// 撤回提审
const codes = await admin.exec("mina_code", {
type: 'cancel_review'
});
// 版本发布到线上
const codes = await admin.exec("mina_code", {
type: 'publish',
code: Code
});
MinaExprUsersCommand
提供小程序体验成员管理(查询,添加,删除)
// 获取所有有体验版权限的成员
const users = await admin.exec("mina_expr_users", {
type: 'list'
});
console.log(users) // [User, User]
// 添加指定用户为体验版成员
const users = await admin.exec("mina_expr_users", {
type: 'add',
users: [User, User]
});
// 删除指定体验版成员
const users = await admin.exec("mina_expr_users", {
type: 'remove',
users: [User, User]
});
MinaQrcodeCommand
生成任意小程序的小程序码
// 生成小程序码
const base64ImageStr = await admin.exec("mina_qrcode", {
type: 'gen',
appId: '抽奖助手',
appPath: 'pages/index'
});
console.log(base64ImageStr) // Base64 encode image string
// 获取 appId
const appId = await admin.exec("mina_qrcode", {
type: 'appid',
appName: '抽奖助手'
});
MinaToolsCommand
小程序工具指令,支持复制小程序路径
// 开启客户端复制小程序路径功能
// 该微信用户可打开小程序右上角菜单,点击“复制页面路径”并粘贴至左侧“小程序页面路径”中
const isSuccess = await admin.exec("mina_tools", {
type: 'copy-path',
appId: '抽奖助手',
userName: 'yanhaibiao1991'
});
// 获取 appId
const appId = await admin.exec("mina_tools", {
type: 'appid',
appName: '抽奖助手'
});
MinaQrcodeCommand
生成任意小程序的小程序码
// 生成小程序码
const base64ImageStr = await admin.exec("mina_qrcode", {
type: 'gen',
appId: '抽奖助手',
appPath: 'pages/index'
});
console.log(base64ImageStr) // Base64 encode image string
// 获取 appId
const appId = await admin.exec("mina_qrcode", {
type: 'appid',
appName: '抽奖助手'
});
MinaVisitOfficialSourceCommand
小程序获取公众号文章渠道来源数据
const resp = await admin.exec("mina_visit_official_source", {
page: 1,
pageCount: 10
});
Command Api
开发者通过继承 Command 来编写自定义指令, 具体例子可以查看 lib/commands
目录中的内置指令
内置指令类名和所在文件名必须符合 AxxBxxCommand
的格式, 指令名解析为 axx_bxx
const {Command} = require("mina-admin");
class CustomCommand extends Command {
async exec() {
// 指令功能入口
}
clean() {
// 指令执行完成的清理函数
}
}
command.fetch()
封装了 node-fetch
, 会设置额外的 url 参数和 cookie 绕过微信接口身份认证
let resp = await command.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?`
);
command.dom()
封装了 jsdom
, 简化处理 html
const dom = command.don("html text");
const document = dom.window.document;
document.querySelector("a")
常量
MINA_CODE_IN_DEV
: 开发版本
MINA_CODE_IN_REVIEW
: 审核中
MINA_CODE_REVIEW_PASS
: 已过审待发布
License
BSD-3-Clause © alexayan