JavaScript检测微信域名封禁状态(2025年8月最新)

介绍

这个JavaScript工具可以帮助你检测微信内打开的域名是否被封禁。通过调用微信官方API接口,我们可以获取域名的当前状态。返回结果格式为JSON,其中status字段为"1"表示域名被拦截,"0"表示域名被封禁,"2"表示域名正常。

完整代码

/**
 * 微信域名封禁状态检测工具
 * 版本: 2025年8月最新
 * 状态码说明:
 *   - 0: 域名被封禁
 *   - 1: 域名被拦截
 *   - 2: 域名正常
 */

class WeChatDomainChecker {
    // API基础URL
    static API_BASE_URL = 'https://api.wxapi.work/wx/';
    
    /**
     * 检测微信域名状态
     * @param {string} domain - 要检测的域名
     * @param {Object} [options] - 可选参数
     * @param {number} [options.timeout=10000] - 请求超时时间(毫秒)
     * @returns {Promise<Object>} 返回检测结果
     */
    static async checkDomainStatus(domain, options = {}) {
        try {
            // 参数验证
            if (!this.isValidDomain(domain)) {
                throw new Error('无效的域名格式');
            }
            
            // 准备请求参数
            const timeout = options.timeout || 10000;
            const apiUrl = `${this.API_BASE_URL}?url=${encodeURIComponent(domain)}`;
            
            // 使用AbortController实现超时控制
            const controller = new AbortController();
            const timeoutId = setTimeout(() => controller.abort(), timeout);
            
            // 发送API请求
            const response = await fetch(apiUrl, {
                method: 'GET',
                headers: {
                    'User-Agent': 'WeChatDomainChecker/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.status === 'undefined' || typeof result.message === 'undefined') {
                throw new Error('API返回数据格式不正确');
            }
            
            // 标准化状态码
            const normalizedResult = this.normalizeResult(result);
            
            return normalizedResult;
            
        } catch (error) {
            // 返回错误信息
            return {
                status: "0",
                message: `检测失败: ${error.name === 'AbortError' ? '请求超时' : error.message}`,
                error: true
            };
        }
    }
    
    /**
     * 标准化API返回结果
     * @param {Object} result - API原始结果
     * @returns {Object}
     */
    static normalizeResult(result) {
        // 确保status是字符串类型
        const status = String(result.status);
        
        // 根据不同的状态码返回对应的消息
        let message = result.message;
        if (!message) {
            switch (status) {
                case '0': message = '域名被封禁'; break;
                case '1': message = '域名被拦截'; break;
                case '2': message = '域名正常'; break;
                default: message = '未知状态';
            }
        }
        
        return {
            status,
            message,
            original: result
        };
    }
    
    /**
     * 验证域名格式
     * @param {string} domain - 域名
     * @returns {boolean}
     */
    static isValidDomain(domain) {
        // 简单的域名格式验证
        const domainRegex = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
        return domainRegex.test(domain);
    }
    
    /**
     * 批量检测多个域名
     * @param {string[]} domains - 域名数组
     * @param {Object} [options] - 可选参数
     * @returns {Promise<Object[]>}
     */
    static async checkMultipleDomains(domains, options = {}) {
        const results = [];
        
        // 使用Promise.all并行请求
        const promises = domains.map(domain => 
            this.checkDomainStatus(domain, options)
                .then(result => ({ domain, ...result }))
        );
        
        // 等待所有请求完成
        return Promise.all(promises);
    }
}

// 使用示例
async function demo() {
    console.log('微信域名封禁状态检测 - 2025年8月最新');
    console.log('====================================');
    
    // 单个域名检测示例
    const domain = 'baidu.com';
    console.log(`\n检测单个域名: ${domain}`);
    
    const result = await WeChatDomainChecker.checkDomainStatus(domain);
    console.log('检测结果:', JSON.stringify(result, null, 2));
    
    // 批量检测示例
    const domains = ['baidu.com', 'google.com', 'qq.com'];
    console.log(`\n批量检测域名: ${domains.join(', ')}`);
    
    const batchResults = await WeChatDomainChecker.checkMultipleDomains(domains);
    console.log('批量检测结果:');
    batchResults.forEach(item => {
        const statusText = 
            item.status === '2' ? '正常' :
            item.status === '1' ? '被拦截' : '被封禁';
        console.log(`${item.domain}: ${statusText} - ${item.message}`);
    });
}

// 浏览器端UI组件
function setupBrowserUI() {
    const app = document.createElement('div');
    app.innerHTML = `
        <style>
            .wechat-checker {
                font-family: Arial, sans-serif;
                max-width: 600px;
                margin: 0 auto;
                padding: 20px;
                box-shadow: 0 0 10px rgba(0,0,0,0.1);
            }
            .wechat-checker h2 {
                color: #07C160;
                text-align: center;
            }
            .wechat-checker input {
                width: 70%;
                padding: 10px;
                margin-right: 10px;
            }
            .wechat-checker button {
                padding: 10px 15px;
                background: #07C160;
                color: white;
                border: none;
                cursor: pointer;
            }
            .wechat-checker button:hover {
                background: #05A84E;
            }
            .result {
                margin-top: 20px;
                padding: 15px;
                border-radius: 5px;
            }
            .status-0 { background: #FFEBEE; color: #C62828; }
            .status-1 { background: #FFF8E1; color: #FF8F00; }
            .status-2 { background: #E8F5E9; color: #2E7D32; }
            .error { background: #F5F5F5; color: #616161; }
        </style>
        
        <div class="wechat-checker">
            <h2>微信域名状态检测</h2>
            <div>
                <input type="text" id="domainInput" placeholder="输入域名(如: baidu.com)" />
                <button onclick="checkWeChatDomain()">检测</button>
            </div>
            <div id="resultContainer"></div>
        </div>
    `;
    
    document.body.appendChild(app);
    
    window.checkWeChatDomain = async function() {
        const domain = document.getElementById('domainInput').value.trim();
        const resultContainer = document.getElementById('resultContainer');
        
        if (!domain) {
            resultContainer.innerHTML = '<div class="error">请输入要检测的域名</div>';
            return;
        }
        
        resultContainer.innerHTML = '<div>检测中...</div>';
        
        try {
            const result = await WeChatDomainChecker.checkDomainStatus(domain);
            
            let statusClass = 'error';
            let statusText = '未知状态';
            
            if (!result.error) {
                switch (result.status) {
                    case '0': 
                        statusClass = 'status-0';
                        statusText = '被封禁';
                        break;
                    case '1': 
                        statusClass = 'status-1';
                        statusText = '被拦截';
                        break;
                    case '2': 
                        statusClass = 'status-2';
                        statusText = '正常';
                        break;
                }
            }
            
            resultContainer.innerHTML = `
                <div class="result ${statusClass}">
                    <h3>检测结果</h3>
                    <p><strong>域名:</strong> ${domain}</p>
                    <p><strong>状态:</strong> ${statusText}</p>
                    <p><strong>详情:</strong> ${result.message}</p>
                    ${result.original ? `<pre>${JSON.stringify(result.original, null, 2)}</pre>` : ''}
                </div>
            `;
        } catch (error) {
            resultContainer.innerHTML = `
                <div class="error">
                    <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 = WeChatDomainChecker;
}

使用方法

浏览器端使用

  1. 在HTML文件中引入脚本:

<script src="wechat-domain-checker.js"></script>
  1. 直接在页面中使用:

// 检测单个域名
const result = await WeChatDomainChecker.checkDomainStatus('baidu.com');
console.log(result);

// 批量检测
const results = await WeChatDomainChecker.checkMultipleDomains(['baidu.com', 'qq.com']);

Node.js环境使用

  1. 安装依赖(如果需要):

npm install node-fetch
  1. 使用示例:

const WeChatDomainChecker = require('./wechat-domain-checker');

async function main() {
    const result = await WeChatDomainChecker.checkDomainStatus('baidu.com');
    console.log('检测结果:', result);
}

main();

直接运行

// 直接在浏览器控制台或Node.js中运行
WeChatDomainChecker.checkDomainStatus('baidu.com')
    .then(result => console.log(result))
    .catch(error => console.error(error));

返回结果格式

{
  "status": "2",
  "message": "域名正常",
  "original": {
    "status": "2",
    "message": "域名正常"
  }
}

返回结果说明

  • status:

    • "0": 域名被封禁

    • "1": 域名被拦截

    • "2": 域名正常

  • message: 对状态的文字描述

  • original: API原始返回数据(包含更多详细信息)

功能特点

  1. ​三种状态检测​​:精确区分正常、拦截和封禁状态

  2. ​域名格式验证​​:自动验证输入域名的格式

  3. ​超时控制​​:可配置的请求超时机制

  4. ​批量检测​​:支持同时检测多个域名

  5. ​跨平台​​:支持浏览器和Node.js环境

  6. ​友好的UI​​:浏览器端提供美观的检测界面

  7. ​错误处理​​:完善的错误处理和异常捕获

注意事项

  1. 本代码适用于2025年8月最新版本的API接口

  2. 需要网络连接才能调用API

  3. 支持HTTPS协议

  4. 在生产环境中使用时,建议添加重试机制

  5. 如果遇到CORS问题,可能需要配置代理服务器

  6. 微信的封禁策略可能随时变化,请定期更新检测逻辑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值