介绍
这个JavaScript工具可以帮助你检测微信小程序是否被封禁。通过调用微信官方API接口,我们可以获取小程序的当前状态。返回结果包含状态码和详细封禁信息,其中code字段为1表示小程序正常,为0表示小程序被封禁。
完整代码
/**
* 微信小程序封禁状态检测工具
* 版本: 2025年8月最新
* 状态码说明:
* - 0: 小程序被封禁
* - 1: 小程序正常
*/
class WeChatMiniProgramChecker {
// API基础URL
static API_BASE_URL = 'https://api.wxapi.work/xcx/checkxcx.php';
/**
* 检测小程序状态
* @param {string} appid - 小程序AppID
* @param {Object} [options] - 可选参数
* @param {number} [options.timeout=10000] - 请求超时时间(毫秒)
* @returns {Promise<Object>} 返回检测结果
*/
static async checkMiniProgramStatus(appid, options = {}) {
try {
// 参数验证
if (!this.isValidAppID(appid)) {
throw new Error('无效的小程序AppID格式');
}
// 准备请求参数
const timeout = options.timeout || 10000;
const apiUrl = `${this.API_BASE_URL}?appid=${encodeURIComponent(appid)}`;
// 使用AbortController实现超时控制
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
// 发送API请求
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'User-Agent': 'WeChatMiniProgramChecker/1.0',
'Accept': 'application/json'
},
signal: controller.signal
});
clearTimeout(timeoutId);
// 检查响应状态
if (!response.ok) {
throw new Error(`HTTP错误: ${response.status}`);
}
// 解析JSON响应
const result = await response.json();
// 验证返回数据格式
if (!result || typeof result.code === 'undefined' || !result.appid) {
throw new Error('API返回数据格式不正确');
}
// 标准化状态码
const normalizedResult = this.normalizeResult(result);
return normalizedResult;
} catch (error) {
// 返回错误信息
return {
code: 0,
appid: appid || '',
status: `检测失败: ${error.name === 'AbortError' ? '请求超时' : error.message}`,
error: true
};
}
}
/**
* 标准化API返回结果
* @param {Object} result - API原始结果
* @returns {Object}
*/
static normalizeResult(result) {
// 确保code是数字类型
const code = Number(result.code);
// 根据不同的状态码返回对应的消息
let status = result.status;
if (!status) {
status = code === 1 ? '小程序正常' : '小程序已被封禁';
}
return {
code,
appid: result.appid,
status,
original: result
};
}
/**
* 验证小程序AppID格式
* @param {string} appid - 小程序AppID
* @returns {boolean}
*/
static isValidAppID(appid) {
// 微信小程序AppID格式验证
const appidRegex = /^wx[0-9a-f]{16}$/;
return appidRegex.test(appid);
}
/**
* 批量检测多个小程序
* @param {string[]} appids - 小程序AppID数组
* @param {Object} [options] - 可选参数
* @returns {Promise<Object[]>}
*/
static async checkMultipleMiniPrograms(appids, options = {}) {
const results = [];
// 使用Promise.all并行请求
const promises = appids.map(appid =>
this.checkMiniProgramStatus(appid, options)
.then(result => ({ ...result }))
);
// 等待所有请求完成
return Promise.all(promises);
}
}
// 使用示例
async function demo() {
console.log('微信小程序封禁状态检测 - 2025年8月最新');
console.log('====================================');
// 单个小程序检测示例
const appid = 'wx81894c6dbb81c2e2';
console.log(`\n检测小程序: ${appid}`);
const result = await WeChatMiniProgramChecker.checkMiniProgramStatus(appid);
console.log('检测结果:', JSON.stringify(result, null, 2));
// 批量检测示例
const appids = ['wx81894c6dbb81c2e2', 'wx1234567890abcdef'];
console.log(`\n批量检测小程序: ${appids.join(', ')}`);
const batchResults = await WeChatMiniProgramChecker.checkMultipleMiniPrograms(appids);
console.log('批量检测结果:');
batchResults.forEach(item => {
const statusText = item.code === 1 ? '正常' : '被封禁';
console.log(`${item.appid}: ${statusText} - ${item.status}`);
});
}
// 浏览器端UI组件
function setupBrowserUI() {
const app = document.createElement('div');
app.innerHTML = `
<style>
.wxmp-checker {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 25px;
background: #FFFFFF;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.wxmp-checker h2 {
color: #07C160;
text-align: center;
margin-bottom: 25px;
font-size: 22px;
}
.input-group {
display: flex;
margin-bottom: 20px;
}
.wxmp-checker input {
flex: 1;
padding: 14px;
border: 1px solid #E6E6E6;
border-radius: 8px;
margin-right: 10px;
font-size: 16px;
transition: border-color 0.3s;
}
.wxmp-checker input:focus {
border-color: #07C160;
outline: none;
}
.wxmp-checker button {
padding: 14px 28px;
background: #07C160;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
font-weight: 500;
transition: all 0.3s;
}
.wxmp-checker button:hover {
background: #05A84E;
transform: translateY(-1px);
}
.wxmp-checker button:active {
transform: translateY(0);
}
.result {
margin-top: 25px;
padding: 20px;
border-radius: 8px;
transition: all 0.3s;
border-left: 5px solid transparent;
}
.code-0 {
border-left-color: #FA5151;
background-color: #FFF0F0;
}
.code-1 {
border-left-color: #07C160;
background-color: #F0FFF4;
}
.error {
border-left-color: #FFC300;
background-color: #FFF9E6;
}
.result h3 {
margin-top: 0;
margin-bottom: 15px;
color: #333;
font-size: 18px;
}
.result p {
margin: 8px 0;
color: #333;
}
.result strong {
color: #555;
}
.result pre {
margin-top: 15px;
padding: 12px;
background: rgba(0,0,0,0.03);
border-radius: 6px;
overflow-x: auto;
font-size: 14px;
}
.appid-example {
font-size: 14px;
color: #888;
margin-top: 5px;
}
</style>
<div class="wxmp-checker">
<h2>微信小程序封禁状态检测</h2>
<div class="input-group">
<input type="text" id="appidInput" placeholder="输入小程序AppID" />
<button onclick="checkMiniProgram()">检测</button>
</div>
<div class="appid-example">示例: wx81894c6dbb81c2e2</div>
<div id="resultContainer"></div>
</div>
`;
document.body.appendChild(app);
window.checkMiniProgram = async function() {
const appid = document.getElementById('appidInput').value.trim();
const resultContainer = document.getElementById('resultContainer');
if (!appid) {
resultContainer.innerHTML = '<div class="result error"><h3>错误</h3><p>请输入小程序AppID</p></div>';
return;
}
resultContainer.innerHTML = '<div class="result"><p>检测中...</p></div>';
try {
const result = await WeChatMiniProgramChecker.checkMiniProgramStatus(appid);
let statusClass = 'error';
let statusText = '未知状态';
if (!result.error) {
statusClass = `code-${result.code}`;
statusText = result.code === 1 ? '正常' : '被封禁';
}
resultContainer.innerHTML = `
<div class="result ${statusClass}">
<h3>检测结果</h3>
<p><strong>小程序AppID:</strong> ${result.appid}</p>
<p><strong>状态:</strong> ${statusText}</p>
<p><strong>详情:</strong> ${result.status}</p>
${result.original ? `<pre>${JSON.stringify(result.original, null, 2)}</pre>` : ''}
</div>
`;
} catch (error) {
resultContainer.innerHTML = `
<div class="result error">
<h3>检测失败</h3>
<p>${error.message}</p>
</div>
`;
}
};
}
// 自动检测运行环境
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
// 浏览器环境
document.addEventListener('DOMContentLoaded', setupBrowserUI);
} else {
// Node.js环境
demo().catch(console.error);
}
// 导出模块
if (typeof module !== 'undefined' && module.exports) {
module.exports = WeChatMiniProgramChecker;
}
使用方法
浏览器端使用
-
在HTML文件中引入脚本:
<script src="wechat-miniprogram-checker.js"></script>
-
直接在页面中使用:
// 检测单个小程序
const result = await WeChatMiniProgramChecker.checkMiniProgramStatus('wx81894c6dbb81c2e2');
console.log(result);
// 批量检测
const results = await WeChatMiniProgramChecker.checkMultipleMiniPrograms(['wx81894c6dbb81c2e2', 'wx1234567890abcdef']);
Node.js环境使用
-
安装依赖(如果需要):
npm install node-fetch
-
使用示例:
const WeChatMiniProgramChecker = require('./wechat-miniprogram-checker');
async function main() {
const result = await WeChatMiniProgramChecker.checkMiniProgramStatus('wx81894c6dbb81c2e2');
console.log('检测结果:', result);
}
main();
直接运行
// 直接在浏览器控制台或Node.js中运行
WeChatMiniProgramChecker.checkMiniProgramStatus('wx81894c6dbb81c2e2')
.then(result => console.log(result))
.catch(error => console.error(error));
返回结果格式
{
"code": 0,
"appid": "wx81894c6dbb81c2e2",
"status": "已被封禁,封禁原因:存在绕开、规避或对抗平台审核监管的行为",
"original": {
"code": 0,
"appid": "wx81894c6dbb81c2e2",
"status": "已被封禁,封禁原因:存在绕开、规避或对抗平台审核监管的行为"
}
}
返回结果说明
-
code
:-
0: 小程序被封禁
-
1: 小程序正常
-
-
appid
: 被检测的小程序ID -
status
: 封禁状态的详细描述(当code为0时包含封禁原因) -
original
: API原始返回数据(包含更多详细信息)
功能特点
-
精确的状态检测:清晰区分正常和封禁状态
-
AppID格式验证:自动验证输入的小程序AppID格式
-
详细的封禁原因:展示微信官方提供的封禁原因
-
批量检测支持:高效同时检测多个小程序
-
微信风格UI:浏览器端提供符合微信风格的检测界面
-
完善的错误处理:详细的错误捕获和提示
-
跨平台兼容:支持浏览器和Node.js环境
注意事项
-
本代码适用于2025年8月最新版本的API接口
-
需要网络连接才能调用API
-
微信的封禁策略可能随时变化,建议定期更新检测逻辑
-
批量检测时请注意频率限制,避免被封禁IP
-
如果遇到CORS问题,建议通过后端服务调用API