-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters