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"
});
example
环境变量
STORAGE_FILE: 定义运行过程中用户数据(cookie...)存储的路径, 默认为 lib/.storage
CRYPTO_KEY: 用户数据加密秘钥
Admin Api
获取 Admin 对象
const admin = new Admin({
account: "",
password: ""
})
初始化 init()
使用前必须先调用初始化方法
admin.init()
登录 login()
调用后台功能前必须先登录,第一次登录或者登录会话过期,会在控制台中打印二维码,需要管理员通过微信进行扫码登录。除此之外,还可以通过监听 qrcode 事件,在回调中获取二维码,开发者可以通过自定义方式发送二维码给管理员。
admin.on('qrcode', (imageBuffer) => {
})
await admin.login();
注册扩展指令 registerCommand()
除了使用默认小程序后台指令外,开发者还可以编写自定义指令并注册
admin.registerCommand(Command)
执行功能指令 exec(), command()
执行指令有两种不同的方式
admin.exec('cmd_name', args)
admin.command('cmd_name')(args)
获取用户信息 getUser()
admin.getUser()
内置指令
提供小程序版本管理功能(获取所有版本列表,版本设为体验版,版本提审,撤回提审,版本发布)
const codes = await admin.exec("mina_code", {
type: 'list'
});
console.log(codes.dev)
console.log(codes.exper)
console.log(codes.online)
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
});
提供小程序体验成员管理(查询,添加,删除)
const users = await admin.exec("mina_expr_users", {
type: 'list'
});
console.log(users)
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]
});
生成任意小程序的小程序码
const base64ImageStr = await admin.exec("mina_qrcode", {
type: 'gen',
appId: '抽奖助手',
appPath: 'pages/index'
});
console.log(base64ImageStr)
const appId = await admin.exec("mina_qrcode", {
type: 'appid',
appName: '抽奖助手'
});
Command Api
开发者通过继承 Command 来编写自定义指令, 具体例子可以查看 lib/commands 目录中的内置指令
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