文件已上传:https://www.pan38.com/share.php?code=XHUZM 提取码:8888
整的微信朋友圈自动发布功能,包含环境检查、界面元素定位、图片识别、异常处理等模块。使用时需要提前准备要发布的图片并修改配置参数。注意不同微信版本可能需要调整控件ID。
/**
- Auto.js 微信朋友圈自动发布脚本
- 功能:自动打开微信->进入朋友圈->发布图文内容
- 需要:Auto.js 4.1.0+、安卓7.0+、已root或开启无障碍服务
*/
// ============== 常量配置区域 ==============
const CONFIG = {
WECHAT_PACKAGE: "com.tencent.mm", // 微信包名
DELAY: {
SHORT: 800,
MEDIUM: 1500,
LONG: 3000
},
RETRY_TIMES: 3,
SCREEN_WIDTH: device.width,
SCREEN_HEIGHT: device.height
};
// ============== 主功能函数 ==============
function main() {
// 前置检查
if (!prepareEnvironment()) {
toast("环境准备失败");
return;
}
// 朋友圈内容配置
const momentContent = {
text: "这是通过AutoJS自动发布的朋友圈内容\\n测试时间:" + new Date().toLocaleString(),
images: [
"/sdcard/Pictures/test1.jpg",
"/sdcard/Pictures/test2.png"
]
};
// 执行发布流程
publishMoment(momentContent);
}
// ============== 功能模块 ==============
function prepareEnvironment() {
// 检查无障碍服务
if (!auto.service) {
toast("请先开启无障碍服务");
return false;
}
// 检查微信是否安装
if (!app.getPackageName(CONFIG.WECHAT_PACKAGE)) {
toast("未安装微信");
return false;
}
// 检查图片是否存在
for (let img of momentContent.images) {
if (!files.exists(img)) {
toast("图片不存在: " + img);
return false;
}
}
return true;
}
function publishMoment(content) {
try {
// 启动微信
launchApp("微信");
sleep(CONFIG.DELAY.LONG);
// 进入朋友圈
enterMomentPage();
// 点击发布按钮
clickCameraButton();
// 输入文本内容
inputTextContent(content.text);
// 添加图片
if (content.images && content.images.length > 0) {
addImages(content.images);
}
// 点击发布
confirmPublish();
toast("朋友圈发布成功");
} catch (e) {
console.error("发布失败:", e);
toast("朋友圈发布失败: " + e.message);
}
}
// ============== 具体操作函数 ==============
function enterMomentPage() {
// 查找发现tab
let discoveryTab = id("com.tencent.mm:id/f4g").findOne(CONFIG.DELAY.MEDIUM);
if (!discoveryTab) {
throw new Error("未找到发现tab");
}
discoveryTab.click();
sleep(CONFIG.DELAY.MEDIUM);
// 查找朋友圈入口
let momentEntry = text("朋友圈").findOne(CONFIG.DELAY.MEDIUM);
if (!momentEntry) {
throw new Error("未找到朋友圈入口");
}
momentEntry.click();
sleep(CONFIG.DELAY.LONG);
}
function clickCameraButton() {
// 查找相机按钮(右上角)
let cameraBtn = id("com.tencent.mm:id/l3").findOne(CONFIG.DELAY.MEDIUM);
if (!cameraBtn) {
throw new Error("未找到发布按钮");
}
cameraBtn.click();
sleep(CONFIG.DELAY.MEDIUM);
}
function inputTextContent(text) {
// 定位输入框
let inputBox = className("EditText").findOne(CONFIG.DELAY.MEDIUM);
if (!inputBox) {
throw new Error("未找到文本输入框");
}
// 输入内容
inputBox.setText(text);
sleep(CONFIG.DELAY.SHORT);
}
function addImages(imagePaths) {
// 点击添加图片按钮
let addImageBtn = id("com.tencent.mm:id/ey").findOne(CONFIG.DELAY.MEDIUM);
if (!addImageBtn) {
throw new Error("未找到添加图片按钮");
}
addImageBtn.click();
sleep(CONFIG.DELAY.MEDIUM);
// 选择相册
let albumEntry = text("从相册选择").findOne(CONFIG.DELAY.MEDIUM);
if (!albumEntry) {
throw new Error("未找到相册入口");
}
albumEntry.click();
sleep(CONFIG.DELAY.LONG);
// 选择图片
for (let i = 0; i < Math.min(9, imagePaths.length); i++) {
let img = images.read(imagePaths[i]);
let target = findImage(img);
if (!target) {
console.warn("未找到匹配图片:", imagePaths[i]);
continue;
}
target.click();
sleep(CONFIG.DELAY.SHORT);
}
// 确认选择
let confirmBtn = id("com.tencent.mm:id/gv").findOne(CONFIG.DELAY.MEDIUM);
if (confirmBtn) {
confirmBtn.click();
sleep(CONFIG.DELAY.LONG);
}
}
function confirmPublish() {
// 点击发布按钮
let publishBtn = text("发表").findOne(CONFIG.DELAY.MEDIUM);
if (!publishBtn) {
throw new Error("未找到发布按钮");
}
publishBtn.click();
sleep(CONFIG.DELAY.LONG);
}
// ============== 工具函数 ==============
function findImage(targetImg) {
let screenshot = captureScreen();
let result = findImage(screenshot, targetImg, {
region: [0, 0, CONFIG.SCREEN_WIDTH, CONFIG.SCREEN_HEIGHT],
threshold: 0.7
});
return result ? result : null;
}
function retryOperation(operation, maxRetry = CONFIG.RETRY_TIMES) {
for (let i = 0; i < maxRetry; i++) {
try {
return operation();
} catch (e) {
if (i === maxRetry - 1) throw e;
sleep(CONFIG.DELAY.MEDIUM);
}
}
}
// ============== 脚本入口 ==============
module.exports = main;
// 立即执行
main();