{"id":27168039,"url":"https://github.com/alexayan/mina-admin","last_synced_at":"2025-04-09T05:20:21.118Z","repository":{"id":35206599,"uuid":"215987819","full_name":"alexayan/mina-admin","owner":"alexayan","description":"微信小程序公众平台后台 SDK","archived":false,"fork":false,"pushed_at":"2023-01-04T22:56:15.000Z","size":579,"stargazers_count":14,"open_issues_count":15,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-14T01:59:08.256Z","etag":null,"topics":["miniapp","miniprogram"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexayan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-18T09:23:00.000Z","updated_at":"2023-10-17T07:02:22.000Z","dependencies_parsed_at":"2023-01-15T16:10:15.943Z","dependency_job_id":null,"html_url":"https://github.com/alexayan/mina-admin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexayan%2Fmina-admin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexayan%2Fmina-admin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexayan%2Fmina-admin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexayan%2Fmina-admin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexayan","download_url":"https://codeload.github.com/alexayan/mina-admin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247981469,"owners_count":21027928,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["miniapp","miniprogram"],"created_at":"2025-04-09T05:20:17.203Z","updated_at":"2025-04-09T05:20:21.104Z","avatar_url":"https://github.com/alexayan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mina-admin [![NPM version][npm-image]][npm-url]\n\u003e 微信小程序公众平台后台功能非官方 SDK\n\u003e \n\u003e 帮助构建小程序自动化系统，小程序管理后台\n\n## 已支持功能\n\n- [x] 小程序后台登录\n- [x] 小程序体验成员管理（查询，添加，删除）\n- [x] 小程序版本管理（获取所有版本列表，版本设为体验版，版本提审，撤回提审，版本发布）\n- [x] 生成任意小程序的小程序码\n- [x] 客户端开启复制小程序路径\n- [x] 小程序获取公众号文章渠道来源数据\n\n## 安装\n\n```sh\n$ npm install --save mina-admin\n```\n\n## 使用\n\n```js\nconst {Admin} = require('mina-admin');\n\nconst admin = new Admin({\n  account: \"\",\n  password: \"\"\n});\n\nawait admin.init();\nawait admin.login();\nconst users = await admin.command(\"mina_expr_users\")({\n  type: \"list\"\n});\n```\n\n[example](example.js)\n\n## 环境变量\n\n`STORAGE_FILE`: 定义运行过程中用户数据(cookie...)存储的路径, 默认为 `lib/.storage`\n\n`CRYPTO_KEY`: 用户数据加密秘钥\n\n## Admin Api\n\n### 获取 Admin 对象\n\n```js\nconst admin = new Admin({\n  account: \"\", // 微信小程序后台登录账号\n  password: \"\" // 微信小程序后台登录密码\n})\n```\n\n### 初始化 init()\n\n使用前必须先调用初始化方法\n\n```js\nadmin.init()\n```\n\n### 登录 login()\n\n调用后台功能前必须先登录，第一次登录或者登录会话过期，会在控制台中打印二维码，需要管理员通过微信进行扫码登录。\n\n通过监听 `qrcode` 事件，在回调中获取二维码，开发者可以通过自定义方式发送二维码给管理员。\n\n通过监听 `login-expired` 事件，处理登录过期，并添加重新登录逻辑。\n\n```js\nadmin.on('qrcode', (imageBuffer) =\u003e {\n  \n})\nawait admin.login();\n```\n\n```js\nadmin.on('login-expired', () =\u003e {\n  admin.login()\n})\n```\n\n### 注册扩展指令 registerCommand()\n\n除了使用默认小程序后台指令外，开发者还可以编写自定义指令并注册\n\n```js\nadmin.registerCommand(Command)\n```\n\n### 执行功能指令 exec(), command()\n\n执行指令有两种不同的方式\n\n```js\nadmin.exec('cmd_name', args)\nadmin.command('cmd_name')(args)\n```\n\n### 获取用户信息 getUser()\n\n```js\nadmin.getUser()\n```\n\n## 内置指令\n\n### [MinaCodeCommand](lib/commands/MinaCodeCommand.js)\n\n提供小程序版本管理功能（获取所有版本列表，版本设为体验版，版本提审，撤回提审，版本发布）\n\n```js\n// 获取当前的小程序版本（线上版本，审核版本，开发版本）\nconst codes = await admin.exec(\"mina_code\", {\n  type: 'list'\n});\n\nconsole.log(codes.dev) // 开发版本 [Code, Code]\nconsole.log(codes.exper) // 体验版本 Code\nconsole.log(codes.online) // 线上版本 Code\n```\n\n```js\n// 版本设为体验版\nconst codes = await admin.exec(\"mina_code\", {\n  type: 'expr',\n  code: Code\n});\n```\n\n```js\n// 版本提审\nconst codes = await admin.exec(\"mina_code\", {\n  type: 'review',\n  code: Code\n});\n```\n\n```js\n// 撤回提审\nconst codes = await admin.exec(\"mina_code\", {\n  type: 'cancel_review'\n});\n```\n\n```js\n// 版本发布到线上\nconst codes = await admin.exec(\"mina_code\", {\n  type: 'publish',\n  code: Code\n});\n```\n\n### [MinaExprUsersCommand](lib/commands/MinaExprUsersCommand.js)\n\n提供小程序体验成员管理（查询，添加，删除）\n\n```js\n// 获取所有有体验版权限的成员\nconst users = await admin.exec(\"mina_expr_users\", {\n  type: 'list'\n});\n\nconsole.log(users) // [User, User]\n```\n\n```js\n// 添加指定用户为体验版成员\nconst users = await admin.exec(\"mina_expr_users\", {\n  type: 'add',\n  users: [User, User]\n});\n```\n\n```js\n// 删除指定体验版成员\nconst users = await admin.exec(\"mina_expr_users\", {\n  type: 'remove',\n  users: [User, User]\n});\n```\n\n### [MinaQrcodeCommand](lib/commands/MinaQrcodeCommand.js)\n\n生成任意小程序的小程序码\n\n```js\n// 生成小程序码\nconst base64ImageStr = await admin.exec(\"mina_qrcode\", {\n  type: 'gen',\n  appId: '抽奖助手',\n  appPath: 'pages/index'\n});\n\nconsole.log(base64ImageStr) // Base64 encode image string\n```\n\n```js\n// 获取 appId\nconst appId = await admin.exec(\"mina_qrcode\", {\n  type: 'appid',\n  appName: '抽奖助手'\n});\n```\n\n### [MinaToolsCommand](lib/commands/MinaToolsCommand.js)\n\n小程序工具指令，支持复制小程序路径\n\n```js\n// 开启客户端复制小程序路径功能\n// 该微信用户可打开小程序右上角菜单，点击“复制页面路径”并粘贴至左侧“小程序页面路径”中\nconst isSuccess = await admin.exec(\"mina_tools\", {\n  type: 'copy-path',\n  appId: '抽奖助手',\n  userName: 'yanhaibiao1991'\n});\n```\n\n```js\n// 获取 appId\nconst appId = await admin.exec(\"mina_tools\", {\n  type: 'appid',\n  appName: '抽奖助手'\n});\n```\n\n### [MinaQrcodeCommand](lib/commands/MinaQrcodeCommand.js)\n\n生成任意小程序的小程序码\n\n```js\n// 生成小程序码\nconst base64ImageStr = await admin.exec(\"mina_qrcode\", {\n  type: 'gen',\n  appId: '抽奖助手',\n  appPath: 'pages/index'\n});\n\nconsole.log(base64ImageStr) // Base64 encode image string\n```\n\n```js\n// 获取 appId\nconst appId = await admin.exec(\"mina_qrcode\", {\n  type: 'appid',\n  appName: '抽奖助手'\n});\n```\n\n### [MinaVisitOfficialSourceCommand](lib/commands/MinaVisitOfficialSourceCommand.js)\n\n小程序获取公众号文章渠道来源数据\n\n```js\nconst resp = await admin.exec(\"mina_visit_official_source\", {\n  page: 1,\n  pageCount: 10\n});\n```\n\n## Command Api\n\n开发者通过继承 Command 来编写自定义指令, 具体例子可以查看 `lib/commands` 目录中的内置指令\n\n内置指令类名和所在文件名必须符合 `AxxBxxCommand` 的格式, 指令名解析为 axx_bxx\n\n```js\nconst {Command} = require(\"mina-admin\");\n\nclass CustomCommand extends Command {\n  async exec() {\n    // 指令功能入口\n  }\n\n  clean() {\n    // 指令执行完成的清理函数\n  }\n}\n```\n\n### command.fetch()\n\n封装了 [`node-fetch`](https://github.com/bitinn/node-fetch), 会设置额外的 url 参数和 cookie 绕过微信接口身份认证\n\n```js\nlet resp = await command.fetch(\n  `https://mp.weixin.qq.com/wxamp/cgi/route?`\n);\n```\n\n### command.dom()\n\n封装了 [`jsdom`](https://github.com/jsdom/jsdom), 简化处理 html\n\n```js\nconst dom = command.don(\"html text\");\nconst document = dom.window.document;\ndocument.querySelector(\"a\")\n```\n\n## 常量\n\n`MINA_CODE_IN_DEV`: 开发版本\n\n`MINA_CODE_IN_REVIEW`: 审核中\n\n`MINA_CODE_REVIEW_PASS`: 已过审待发布\n\n## License\n\nBSD-3-Clause © [alexayan](https://github.com/alexayan)\n\n\n[npm-image]: https://badge.fury.io/js/mina-admin.svg\n[npm-url]: https://npmjs.org/package/mina-admin\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexayan%2Fmina-admin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexayan%2Fmina-admin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexayan%2Fmina-admin/lists"}