/** * description 消息已读未读状态检测 */ let packageName = 'com.ss.android.ugc.aweme'; let tenantId = ""; let scriptVersion = ""; //toast扩展参数 let toastExtra = { "x":(device.getScreenWidth() - 800) / 2, "y":(device.getScreenHeight() - 200) / 2, "textColor":"#ffffff", "duration":30000, "width":800, "height":200, } /** * description 脚本入口 */ run() /** * description 跳转消息页面 */ toMessage(); /** * description 唤醒设备 */ function run() { if (!isAccMode()) { toast('非无障碍模式,无法执行脚本', toastExtra); randomSleep("非无障碍模式,无法执行脚本", "", "", "", "", ""); exit(); } //自动化服务是否正常 if (!autoServiceStart(2)) { toast('自动化服务启动失败,无法执行脚本', toastExtra); randomSleep("自动化服务启动失败,无法执行脚本", "", "", "", "", ""); exit(); } //脚本停止监听 setStopCallback(function () { device.cancelKeepingAwake(); //取消保持唤醒状态 logd("脚本停止"); deviceOperaLog("脚本停止", "脚本停止", 0, "", "", "", "", "") home(); }); //脚本异常停止监听 setExceptionCallback(function (msg) { device.cancelKeepingAwake(); //取消保持唤醒状态 logd("脚本异常停止"); deviceOperaLog("脚本异常停止", "脚本异常停止", 0, "", "", "", "", "") home(); }); daemonEnv(true); //守护自动化环境 randomSleep("脚本开始运行", "脚本运行", "", "", "", ""); //保持设备唤醒 importClass(android.os.PowerManager); device.keepAwake(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP); //保持屏幕唤醒 device.keepScreenOn(); sleep(1000); //息屏设备上滑解锁 let lock = id("com.android.systemui:id/notification_stack_scroller").getOneNodeInfo(5000); if (lock) { let screenWidth = device.getScreenWidth(); let screenHeight = device.getScreenHeight(); let startX = screenWidth / 2; let startY = screenHeight - 200; let endX = screenWidth / 2; let endY = 50; let duration = 2000; swipeToPoint(startX, startY, endX, endY, duration); } getDevice() launch(packageName, 5000, 2); //打开抖音 } /** * description 通过包名启动应用 * @param packageName {string} : 应用包名 * @param delay {number} : 启动后等待时长(毫秒) * @param startNum {number} : 重试次数 * @return {boolean} : 返回是否成功 */ function launch(packageName, delay, startNum) { let result = false; //判断应用的包名是否存在 if (utils.isAppExist(packageName)) { //判断应用是否在前台 if (getRunningPkg() == null) { toast('请开启辅助功能权限'); exit(); } else if (getRunningPkg() === packageName) { logd('应用已经在前台'); result = true; } else { logd('启动应用...'); let alert_selectors = text("允许"); let num = 0; //判断应用是否启动成功 while (num < startNum) { if (getRunningPkg() === packageName) { logd('启动成功'); randomSleep("脚本启动成功,版本:" + scriptVersion, "脚本启动", "", "", "", ""); result = true; break; } else if (has(alert_selectors)) { //初次打开应用 可能会弹出是否允许打开的弹窗 let node = alert_selectors.getOneNodeInfo(1000); if (node) { node.click(); } } else { utils.openApp(packageName); sleep(delay); num++; } } } alert(); //应用启动后处理弹窗 operaLog(1); } else { toast('应用未安装'); exit(); } return result; } /** * description 跳转消息页面 */ function toMessage() { try { //定位到首页 execSync(function () { isHome(); }, 1000); let message_icon = text("消息").getOneNodeInfo(randomSleep("准备点击消息按钮", "", "", "", "", "")); //寻找消息图标并点击 if (message_icon.click()) { operaLog(2); randomSleep("点击消息按钮,进入消息页面", "", "", "", "", "") // 获取已读 回复数量 let reply = '' let message_icon = desc("消息,按钮").getOneNodeInfo(2000) if (message_icon) { let nodeInfo = message_icon.parent().parent(); let siblings = nodeInfo.siblings(); if (siblings) { // 私信回复数量 reply = siblings[0].allChildren()[0].text; } } let read = 0 while (true) { let nodeInfo = text("已读 ·").getNodeInfo(2000); let more = text("暂时没有更多了").getOneNodeInfo(2000); if (nodeInfo) { read = nodeInfo.length + read } if (more) { break } sleep(3000); swiper() } updateStatus(read, reply) // 定位到首页 isHome(); randomSleep("脚本运行结束", "脚本运行结束", "", "", "", ""); } } catch (e) { logd(e) randomSleep("脚本异常", "脚本异常", "", "", "", ""); isHome() } } function swiper() { let screenWidth = device.getScreenWidth(); let screenHeight = device.getScreenHeight(); let startX = screenWidth / 2; let startY = screenHeight - 200; let endX = screenWidth / 2; let endY = 50; let duration = 2000; swipeToPoint(startX, startY, endX, endY, duration); } function updateStatus(read, reply) { let deviceNo = ecloud.getDeviceNo(); let query = { "pageNumber": 1, "pageSize": 2, "fields": "", "query": "", "tableNameEn": "device_user", "search": { "device_no": deviceNo } }; let queryr = ecloud.dynamicQuery(query) queryr = JSON.parse(queryr) let replyed = queryr.reply if (replyed !== null && replyed !== '' && replyed !== undefined && replyed !== 'undefined') { reply = parseInt(replyed, 10) + parseInt(reply, 10) } let update = { "tableNameEn": "device_user", "columns": { "readed": read + "", "reply": reply + "" }, "query": "", "search": { "device_no": deviceNo } } let updater = ecloud.dynamicUpdate(update) logd("=== {}", updater); } /** * 记录设备操作记录日志 */ function deviceOperaLog(operateType, logDetail, intervals, tarPostName, tarUniqueNo, tarComment, tarNickname,time) { let paltform = ''; let uniqueNo = ''; if ('com.ss.android.ugc.aweme'.equals(packageName)) { // 斗音 paltform = 'DY' uniqueNo = ecloud.getDeviceNo().substring(3) } else if ('com.xingin.xhs'.equals(packageName)) { // 小红书 paltform = 'XHS' uniqueNo = ecloud.getDeviceNo().substring(4) } else if ('com.smile.gifmaker'.equals(packageName)) { // 快手 paltform = 'KS' uniqueNo = ecloud.getDeviceNo().substring(3) } let add = { "tableNameEn": "device_log", "columns": { "unique_no": uniqueNo, "platform": paltform, "device_no": ecloud.getDeviceNo(), "task_name": ecloud.getTaskInfo().taskName, "log_detail": logDetail, "operate_type": operateType, "intervals": intervals + "ms", "operate_time": getDate(), "tar_post_name": tarPostName, "tar_unique_no": tarUniqueNo, "tar_comment": tarComment, "tar_nickname": tarNickname, "task_id": ecloud.getTaskInfo().taskId, "tenant_id": tenantId, "serial_num": time } } ecloud.dynamicAdd(add) } /** * description 获取当前时间 * @return {string} : 返回时间 */ function getDate() { let date = new Date(); let sign2 = ":"; let year = date.getFullYear() // 年 let month = date.getMonth() + 1; // 月 let day = date.getDate(); // 日 let hour = date.getHours(); // 时 let minutes = date.getMinutes(); // 分 let seconds = date.getSeconds() //秒 let weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']; let week = weekArr[date.getDay()]; // 给一位数的数据前面加 “0” if (month >= 1 && month <= 9) { month = "0" + month; } if (day >= 0 && day <= 9) { day = "0" + day; } if (hour >= 0 && hour <= 9) { hour = "0" + hour; } if (minutes >= 0 && minutes <= 9) { minutes = "0" + minutes; } if (seconds >= 0 && seconds <= 9) { seconds = "0" + seconds; } return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds; } /** * 记录埋点数据 */ function operaLog(num) { let paltform = ''; let operateType = ''; let uniqueNo = ''; if (num === 1) { operateType = '应用启动' } else if (num === 2) { operateType = '点击消息' } else if (num === 3) { operateType = '点击互动消息' } else if (num === 4) { operateType = '脚本运行结束' } if ('com.ss.android.ugc.aweme'.equals(packageName)) { // 斗音 paltform = 'DY' uniqueNo = ecloud.getDeviceNo().substring(3) } else if ('com.xingin.xhs'.equals(packageName)) { // 小红书 paltform = 'XHS' uniqueNo = ecloud.getDeviceNo().substring(4) } else if ('com.smile.gifmaker'.equals(packageName)) { // 快手 paltform = 'KS' uniqueNo = ecloud.getDeviceNo().substring(3) } let add = { "tableNameEn": "trace", "columns": { "device_no": ecloud.getDeviceNo(), "platform": paltform, "unique_no": uniqueNo, "post_name": '', "nickname": '', "keyword": '', "operate_type": operateType, "operate_time": timeFormat("yyyy-MM-dd HH:mm:ss")+'', "task_id": ecloud.getTaskInfo().taskId, "task_name": ecloud.getTaskInfo().taskName, "tenant_id": tenantId, } } ecloud.dynamicAdd(add) } /** * description 异步执行线程 处理弹窗 * @return {boolean} : 返回是否成功 */ function alert() { thread.execAsync(function () { logd('启动线程,查找弹窗'); let btnText = ["下次再说", "以后再说", "关闭", "允许", "暂不", "暂时不要", "我知道了", "知道了", "取消"]; while (true) { for (let i = 0; i < btnText.length; i++) { let node = text(btnText[i]).getOneNodeInfo(1000); if (node) { node.click(); logd('弹窗已处理'); randomSleep("弹窗处理完毕", "", "", "", "", "") } } } }); return true; } /** * description 定位到首页 * @return {boolean} : 返回是否成功 */ function isHome() { let result = false; while (true) { var home_selectors = text("首页").getOneNodeInfo(3000); //首页按钮 var message_selectors = text("消息").getOneNodeInfo(3000); //消息按钮 //判断是否在首页 if (home_selectors && message_selectors) { var follow_selectors = text("关注"); //关注按钮 var recommend_selectors = text("推荐"); //推荐按钮 if (has(follow_selectors) && has(recommend_selectors)) { toast('已定位首页'); randomSleep("定位到首页", "", "", "", "", ""); result = true; break; } else { home_selectors.click(); sleep(3000); } } else { toast('返回首页...'); //返回上一页 go_back(1); } } return result; } /** * description 自定义返回函数 * @param page {number} : 返回页数 */ function go_back(page) { let count = 0; while (count < page) { back(); count++; randomSleep("返回上一页", "", "", "", "", ""); } } /** * description 随机暂停时间 */ function randomSleep(log, opeType, tarPostName, tarUniqueNo, tarComment, tarNickname) { let min = Math.ceil(30); let max = Math.floor(60); let times = (Math.floor(Math.random() * (max - min + 1)) + min) * 100; logd(log + ",停留" + times + "ms"); // 记录设备操作日志 if (opeType === "脚本启动" || opeType === "脚本运行结束") { let serialNum = time() deviceOperaLog(opeType, log + ",停留" + times + "ms", times, tarPostName, tarUniqueNo, tarComment, tarNickname, serialNum) }else { deviceOperaLog(opeType, log + ",停留" + times + "ms", times, tarPostName, tarUniqueNo, tarComment, tarNickname,null) } sleep(times); return times; } /** * 获取设备信息 */ function getDevice() { var d = ecloud.getTaskInfo() let taskId = d.taskId taskId = parseInt(taskId, 10) scriptVersion = d.scriptVersion var url = "http://47.120.7.97:8098/openapi/getTask"; var pa = { "dataSecret": "test123" }; var x = http.postJSON(url, pa, 10 * 1000); x = JSON.parse(x) for (let i = 0; i < x.length; i++) { if (x[i].taskId === taskId) { tenantId = x[i].tenantId } } } /** * description 启动自动化 * @return {boolean} : 返回自动化服务结果 */ function autoServiceStart(time) { for (let i = 0; i < time; i++) { if (isServiceOk()) { return true; } let started = startEnv(); randomSleep("第' + (i + 1) + '次启动服务结果: " + started, "", "", "", "", ""); if (isServiceOk()) { return true; } } return isServiceOk(); }