-- 通用模块 require("TSLib") local ts = require("ts") local ios_config = require("config") local json = ts.json -- server_path = 'http://192.168.31.217:9999/' server_path = "http://35.220.167.14:9999/" function api_log(content) -- 获取当前时间(精确到秒的 Unix 时间戳) local current_seconds = os.time() -- 如需格式化显示(年-月-日 时:分:秒),可配合 os.date() local formatted_time = os.date("%Y-%m-%d %H:%M:%S", current_seconds) nLog('[' .. formatted_time .. ']' .. content) end function api_post(action_name, url, body) -- 通用post请求 api_log('开始' .. action_name .. '请求: ' .. ts.json.encode(body)) local default_header = {["Content-Type"] = "application/json"} while true do status_resp, header_resp, body_resp = ts.httpPost( server_path .. url, default_header, body, true ) toast(action_name .. '结果' .. body_resp, 2) api_log(action_name .. '状态码' .. status_resp) api_log(action_name .. '结果' .. body_resp) func_success, ress = pcall(json.decode, body_resp) if not func_success then wolf_log('api 请求json解析 '.. body_resp ..' 执行异常,进行重试 err:' .. ress) toast('api 请求json解析 '.. body_resp ..' 执行异常,进行重试 err:' .. ress) -- toast("api 接口异常,重试中...", 2) else if json.decode(body_resp).code == 200 then break else wolf_log('api 请求异常'.. body_resp ..' 进行重试') toast('api 请求异常'.. body_resp ..' 进行重试') end end mSleep(1000) end return ress end function api_get(action_name,url,param,retry_times,check_func) -- 通用get请求 param_str = '?' for param_key, param_value in pairs(param) do if param_value and param_value ~= "" then param_str = param_str .. param_key .. '=' .. param_value .. '&' end end param_str = string.sub(param_str, 1, -2) api_log('开始' .. action_name .. '参数' .. param_str) if retry_times <= 0 then retry_times = 99999 end index = 0 while index <= retry_times do index = index + 1 toast("第" .. index .. '/' .. retry_times .. '次' ..action_name, 2) api_log("第" .. index .. '/' .. retry_times .. '次' ..action_name) status_resp, header_resp, body_resp = ts.httpGet( server_path .. url .. param_str, {}, {} ) toast(action_name .. '结果' .. body_resp, 2) api_log(action_name .. '状态码' .. status_resp) api_log(action_name .. '结果' .. body_resp) if status_resp == 200 then json_response = json.decode(body_resp) if json_response.code == 200 then if check_func == nil then api_log('无业务校验,直接返回结果' ) return json_response else if check_func(json_response) then return json_response else api_log('未通过业务校验' .. '1s后重试请求' ) end end else api_log(action_name .. '业务返回' .. json_response.code .. '1s后重试请求' ) end else api_log('接口请求异常,1s后重试') end mSleep(1000) end return nil end function get_task(device_id) return api_get( '获取任务', 'api/v1/main/run', { device_name=device_id, wan_ip=getNetIP() }, -1, nil ) end function get_mobile_sms(task_id) -- 获取手机号验证码 -- function check(res) -- return os.time() - res.data.update_time < 300 -- end return api_get( '获取手机验证码', 'api/v1/account_task/phoneCode', {task_id=task_id}, 8, nil ) end function get_email_sms(task_id) -- 获取邮箱验证码 function check(res) return os.time() - res.data.update_time < 300 end return api_get( '获取邮箱验证码', 'api/v1/account_task/emailCode', {task_id=task_id}, 10, check ) end function return_task_success(task_id, back_name, password, email) -- 上报任务成功 -- device_clear(email) register_timestamp = os.time() post_body = { task_id = task_id, password = password, backups_key = back_name, register_time = os.date("%Y-%m-%d %H:%M:%S", register_timestamp), active_time = os.date("%Y-%m-%d %H:%M:%S", register_timestamp), register_ip = getNetIP(), register_data = {} } wolf_log('上报任务成功参数:'.. json.encode(post_body)) return api_post( '上报任务成功', 'api/v1/account_task/success', post_body ) end function return_download_task_success(task_id, keyword) -- 上报订阅任务成功 post_body = { task_id = task_id, keyword = keyword, } -- device_clear(ios_config.ios_任务信息.task_data.email) return api_post( '上报订阅 [' .. keyword .. '] 任务成功', 'api/v1/app_task/success', post_body ) end function return_download_task_error(task_id) -- 上报订阅任务失败 post_body = { task_id = task_id } -- device_clear(ios_config.ios_任务信息.task_data.email) return api_post( '上报订阅 [' .. keyword .. '] 任务失败...', 'api/v1/app_task/fail', post_body ) end function return_device_error(device_name) -- 上报设备异常 post_body = { device_name = device_name } return api_post( '上报设备异常', 'api/v1/main/deviceError', post_body ) end function return_task_fail(url, body, email) -- 上报任务失败 -- nLog(url) -- nLog(ts.json.encode(body)) -- device_clear(email) return api_post( '上报任务失败', url, body ) end function return_upload_task(app_id, task_info) -- 上报抓包结果 post_body = { app_id = app_id, data = task_info } return api_post( '上报任务抓包结果', 'api/v1/app/upload', post_body ) end function return_deactivate_task(backups_key) -- 上报需要重新激活账号 post_body = { backup_key = backups_key, device_name = ios_config.ios_任务信息.device_id } return api_post( '上报需要重新激活账号', '/api/v1/main/accountError', post_body ) end function return_setRank_task(device_name) -- 上报任务配置排行结果 post_body = { device_name = device_name, } return api_post( '上报任务配置排行结果', 'api/v1/app/setRank', post_body ) end function return_deviceExtUpdate_task(device_name, versions) -- 上报命令执行结果 post_body = { device_name = device_name, version = versions } return api_post( '上报命令执行结果', 'api/v1/main/deviceExtUpdate', post_body ) end function get_page_config() -- 获取配置文件 w,h = getScreenSize(); api_log('屏幕宽度' .. w) api_log('屏幕高度' .. h) resolution_str = w .. '_' .. h api_log('屏幕分辨率:' .. resolution_str) -- 系统版本 sysver = getOSVer() api_log('系统版本' .. sysver) res = api_get( '获取配置文件', 'api/v1/main/config', {resolution = resolution_str,os = sysver}, 1, nil ) if res then return res.data else return res end end function push_phone_code_num(task_id) -- 提交手机号发送验证码次数记录 return api_post( '提交手机号发送验证码次数记录', 'api/v1/account_task/pushPhoneCode', {task_id=task_id} ) end