You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
596 lines
28 KiB
596 lines
28 KiB
4 years ago
|
/*
|
||
|
原作者:Sunert
|
||
|
|
||
|
*****************************
|
||
|
中青任务安卓版 包含浏览赚&看看赚&&搜索赚
|
||
|
json版,请把配置文件放到同目录下,默认zqrw1.json
|
||
|
在环境变量export zqjson="zqrw1.json&zqrw2.json",指定脚本执行的json文件,多账户请用&分割,不指定默认zqrw1.json
|
||
|
json格式如下: //用户名自己写,searchdatas:搜索赚body,startdatas:浏览赚body,lookdatas:看看赚body,每条一行
|
||
|
{
|
||
|
"user":"夏天",
|
||
|
"kkzmodel":"json",
|
||
|
"searchdatas":[
|
||
|
"",
|
||
|
"",
|
||
|
""
|
||
|
],
|
||
|
"startdatas":[
|
||
|
"",
|
||
|
"",
|
||
|
""
|
||
|
],
|
||
|
"lookdatas": [
|
||
|
"",
|
||
|
"",
|
||
|
""
|
||
|
]
|
||
|
}
|
||
|
改写:jammy
|
||
|
改写主程序,改写支持安卓版,整合少林功夫猴搜索赚
|
||
|
增加宝箱检测,未开启则自动开启
|
||
|
增加未完成任务检测通知
|
||
|
20211121 全部改写
|
||
|
2021-12-1 增加看看赚模式选择,如果有xxx_kkz.txt文件,优先选择读取文本模式,如用json模式请定义kkzmodel为json
|
||
|
*/
|
||
|
|
||
|
const jsname = '中青看点-看看赚任务ALL'
|
||
|
const $ = Env(jsname)
|
||
|
const notifyFlag = 1; //0为关闭通知,1为打开通知,默认为1
|
||
|
const logDebug = 0
|
||
|
|
||
|
const notify = $.isNode() ? require('./sendNotify') : '';
|
||
|
|
||
|
let notifyStr = ''
|
||
|
let rndtime = "" //毫秒
|
||
|
let httpResult //global buffer
|
||
|
|
||
|
var fs = require("fs");
|
||
|
|
||
|
//使用参数 青龙task不支持参数
|
||
|
//const args = process.argv.slice(2);
|
||
|
//let filename = (args.length>0?args[0]:undefined)|| "./zq_yilia.json";
|
||
|
|
||
|
// .task desi # 指定账号执行 task zqkkzrw.js desi zqjson 1 使用'zqjson'的第一个参数 多个可以写1-3
|
||
|
let filename =`./${process.env.zqjson || "./zq_yilia"}.json`;
|
||
|
console.log(filename);
|
||
|
let kkzname = filename.replace(/.json/, "_kkz.txt");
|
||
|
|
||
|
let zqbody
|
||
|
|
||
|
let startArr = [], lookArr = [], searchArr = [], jsonArr = [];
|
||
|
let gainscore = 0, lookscore = 0, searchscore = 0, boxscore = 0;
|
||
|
let StartBody = [], LookBody = [], searchBody = [];
|
||
|
|
||
|
let usernmae = "", zqjsons = "", gainbody = "", lookbody = "", zqcookie = "", kkzmodel = "";
|
||
|
|
||
|
///////////////////////////////////////////////////////////////////
|
||
|
|
||
|
!(async () => {
|
||
|
|
||
|
if (typeof $request !== "undefined") {
|
||
|
$.msg(jsname + ': 此脚本不做重写,请检查重写设置')
|
||
|
}
|
||
|
else {
|
||
|
if (!(await checkEnvJson())) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
notifyStr += username + "本次任务记录:\n";
|
||
|
await taskall()
|
||
|
if (zqcookie) {
|
||
|
console.log(`--------开始检测宝箱奖励状态`);
|
||
|
await $.wait(5000);
|
||
|
$.index = 0;
|
||
|
await getBoxRewardConf()
|
||
|
console.log(`收取${$.index}次宝箱,获得${boxscore}金币\n`)
|
||
|
if (boxscore > 0) {
|
||
|
notifyStr += `收取${$.index}次宝箱,获得${boxscore}金币\n`;
|
||
|
}
|
||
|
console.log(`--------开始检测未完成任务`);
|
||
|
await $.wait(1000);
|
||
|
await checktasklist(0, '看看赚');
|
||
|
await $.wait(1000);
|
||
|
await checktasklist(2, '浏览赚');
|
||
|
}
|
||
|
await showmsg()
|
||
|
}
|
||
|
|
||
|
})()
|
||
|
.catch((e) => $.logErr(e))
|
||
|
.finally(() => $.done())
|
||
|
|
||
|
//通知
|
||
|
async function showmsg () {
|
||
|
|
||
|
notifyBody = "///////////////////////\n" + jsname + "运行通知\n\n" + notifyStr
|
||
|
|
||
|
if (notifyFlag != 1) {
|
||
|
console.log(notifyBody);
|
||
|
}
|
||
|
|
||
|
if (notifyFlag == 1) {
|
||
|
console.log(notifyBody);
|
||
|
if ($.isNode()) { await notify.sendNotify($.name, notifyBody); }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
//json读取
|
||
|
async function checkEnvJson () {
|
||
|
|
||
|
console.log(`您使用的是json文件模式\n`)
|
||
|
if (!check_file_exist(filename)) {
|
||
|
return false
|
||
|
}
|
||
|
zqbody = require(filename);
|
||
|
username = zqbody.user;
|
||
|
zqcookie = zqbody.zqcookie;
|
||
|
searchArr = zqbody.searchdatas;
|
||
|
//看看赚默认模式json格式,文件模式请设置为file
|
||
|
kkzmodel = zqbody.kkzmodel ? zqbody.kkzmodel : 'file'
|
||
|
if (check_file_exist(kkzname) && kkzmodel != 'json') {
|
||
|
console.log(`找到看看赚body文本,选择读取文件模式\n`)
|
||
|
let kkzbodys = ''
|
||
|
kkzbody = fs.readFileSync(kkzname, "utf8");
|
||
|
if (kkzbody !== `undefined`) {
|
||
|
kkzbodys = kkzbody.split("\n");
|
||
|
}
|
||
|
Object.keys(kkzbodys).forEach((item) => {
|
||
|
if (kkzbodys[item] && !kkzbodys[item].startsWith("#")) {
|
||
|
lookArr.push(kkzbodys[item])
|
||
|
}
|
||
|
})
|
||
|
} else {
|
||
|
lookArr = zqbody.lookdatas;
|
||
|
startArr = zqbody.startdatas;
|
||
|
}
|
||
|
if (!lookArr) {
|
||
|
console.log('没有找到看看赚body,请先抓取')
|
||
|
//return false
|
||
|
}
|
||
|
if (!startArr) {
|
||
|
console.log('没有找到浏览赚body,请先抓取')
|
||
|
}
|
||
|
if (!searchArr) {
|
||
|
console.log('没有找到搜素赚body,请先抓取')
|
||
|
}
|
||
|
if (!zqcookie) {
|
||
|
console.log('没有找到中青ck,不能自动开宝箱')
|
||
|
}
|
||
|
zqcookie = replaceCookie(zqcookie)
|
||
|
|
||
|
return true
|
||
|
}
|
||
|
//处理ck格式
|
||
|
function replaceCookie (zqcookieItem) {
|
||
|
if (zqcookieItem.indexOf('cookie=') == -1 && zqcookieItem.indexOf('zqkey=') > -1) {
|
||
|
zqcookieItem = zqcookieItem.replace(/zqkey=/, "cookie=")
|
||
|
}
|
||
|
if (zqcookieItem.indexOf('cookie_id=') == -1 && zqcookieItem.indexOf('zqkey_id=') > -1) {
|
||
|
zqcookieItem = zqcookieItem.replace(/zqkey_id=/, "cookie_id=")
|
||
|
}
|
||
|
return zqcookieItem
|
||
|
}
|
||
|
|
||
|
async function taskall () {
|
||
|
|
||
|
console.log(`${username} 共提供了${startArr.length}次浏览赚任务`)
|
||
|
if (startArr.length !== 0) {
|
||
|
$.index = 0;
|
||
|
notifyStr += `浏览赚:共${startArr.length}次任务,`;
|
||
|
for (let i = 0; i < startArr.length; i++) {
|
||
|
if (startArr[i]) {
|
||
|
gainbody = startArr[i];
|
||
|
$.log(`----------\n🏠开始浏览赚第 ${k} ${$.index + 1}次任务`)
|
||
|
}
|
||
|
await GainStart();
|
||
|
}
|
||
|
console.log(`----------\n\n中青看点共完成${$.index}次任务,共计获得${gainscore}个青豆,浏览赚任务全部结束`);
|
||
|
notifyStr += `浏览赚完成${$.index}次任务` + ` 获得${gainscore}个青豆\n`;
|
||
|
}
|
||
|
|
||
|
console.log(`---休息一下,开始下一个任务。`);
|
||
|
await $.wait(8000);
|
||
|
|
||
|
|
||
|
console.log(`\n${username}共提供${lookArr.length}次看看赚任务\n`)
|
||
|
if (lookArr.length !== 0) {
|
||
|
$.index = 0;
|
||
|
notifyStr += `看看赚:共${lookArr.length}次任务,`;
|
||
|
for (let k = 0; k < lookArr.length; k++) {
|
||
|
if (lookArr[k]) {
|
||
|
lookbody = lookArr[k];
|
||
|
$.log(`----------\n🏠开始中青看点看看赚第 ${k} ${$.index + 1}次任务`)
|
||
|
}
|
||
|
await lookStart();
|
||
|
}
|
||
|
console.log(`\n中青看点共完成${$.index}次看看赚任务,共计获得${lookscore}个青豆,看看赚任务全部结束`);
|
||
|
notifyStr += `看看赚完成${$.index}次任务` + ` 获得${lookscore}个青豆\n`;
|
||
|
}
|
||
|
|
||
|
console.log(`---休息一下,开始下一个任务。`);
|
||
|
await $.wait(10000);
|
||
|
|
||
|
console.log(`\n\n${username}共提供了${searchArr.length}次搜索赚任务`);
|
||
|
if (searchArr.length !== 0) {
|
||
|
$.index = 0;
|
||
|
notifyStr += `搜索赚:共${searchArr.length}次任务,`;
|
||
|
for (let k = 0; k < searchArr.length; k++) {
|
||
|
zqsszbody1 = searchArr[k];
|
||
|
console.log(`----------\n🏠开始搜索赚第 ${k} ${$.index + 1} 次任务`)
|
||
|
await sszStart();
|
||
|
await $.wait(1000);
|
||
|
}
|
||
|
console.log(`-------------------------\n\n中青看点共完成${$.index}次搜索赚任务,共计获得${searchscore}个青豆,搜素赚任务全部结束`);
|
||
|
notifyStr += `搜素赚完成${$.index}次任务` + ` 获得${searchscore}个青豆\n`;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
//看看赚宝箱状态
|
||
|
async function getBoxRewardConf () {
|
||
|
let caller = printCaller()
|
||
|
let reqCk = 'device_type=android&' + zqcookie
|
||
|
let url = 'https://kd.youth.cn/WebApi/Nameless/getBoxRewardConf?' + reqCk
|
||
|
let urlObject = populateGetUrl(url)
|
||
|
urlObject.headers.Referer = 'https://kd.youth.cn/h5/20190527watchMoney/?' + reqCk
|
||
|
await httpGet(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
|
||
|
if (result.status == 1) {
|
||
|
if (result.data && result.data.list && Array.isArray(result.data.list)) {
|
||
|
for (let i = 0; i < result.data.list.length; i++) {
|
||
|
let boxItem = result.data.list[i]
|
||
|
if (boxItem.status == 1) {
|
||
|
console.log(`🏠宝箱${i + 1}:${boxItem.score}青豆未领取,尝试领取宝箱${i + 1}`);
|
||
|
await $.wait(Math.floor(Math.random() * 2000) + 32000)
|
||
|
await getBoxReward(boxItem.id)
|
||
|
}
|
||
|
else if (boxItem.status == 0) console.log(`开启宝箱需完成任务:${boxItem.num} 次未达成,跳过。`)
|
||
|
else if (boxItem.status == 2) console.log(`🏠宝箱${i + 1}:${boxItem.score}青豆已领取,跳过。`)
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
console.log(`获取看看赚宝箱状态失败:${result.message}`)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//看看赚宝箱领取
|
||
|
async function getBoxReward (id) {
|
||
|
let caller = printCaller()
|
||
|
let reqCk = 'device_type=android&' + zqcookie
|
||
|
let url = `https://kandian.wkandian.com/WebApi/Nameless/getBoxReward?id=${id}&${reqCk}`
|
||
|
let urlObject = populateGetUrl(url)
|
||
|
urlObject.headers.Referer = 'https://kandian.wkandian.com/h5/20190527watchMoney/?' + reqCk
|
||
|
await httpGet(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
|
||
|
if (result.status == 1) {
|
||
|
boxscore += parseInt(result.data)
|
||
|
$.index++
|
||
|
console.log(`打开第${id + 1}个看看赚宝箱获得:${result.data}金币`)
|
||
|
} else {
|
||
|
console.log(`领取看看赚宝箱失败:${result.message}`)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//检测新增浏览赚2和看看赚0任务
|
||
|
async function checktasklist (id, desc) {
|
||
|
let list1 = 0, list2 = 0;
|
||
|
let listArr = [], lists = '';
|
||
|
let rndtime = Math.floor(new Date().getTime())
|
||
|
let caller = printCaller()
|
||
|
let reqCk = 'device_type=android&' + zqcookie
|
||
|
let url = `https://kd.youth.cn/WebApi/Nameless/getTaskBrowse?type=${id}&${reqCk}`
|
||
|
let urlObject = populateGetUrl(url)
|
||
|
urlObject.headers.Referer = 'https://kd.youth.cn/h5/20190527watchMoney/?' + reqCk
|
||
|
urlObject.headers.Host = 'kd.youth.cn'
|
||
|
await httpGet(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
|
||
|
if (result.status == 1) {
|
||
|
listArr = result.data.list;
|
||
|
for (let i = 0; i < listArr.length; i++) {
|
||
|
if (listArr[i].status != '2') {
|
||
|
console.log(listArr[i].title + '未完成');
|
||
|
lists += listArr[i].title + ' ';
|
||
|
list1 += 1;
|
||
|
} else {
|
||
|
list2 += 1;
|
||
|
}
|
||
|
}
|
||
|
console.log(`${desc}已完成任务数${list2}`);
|
||
|
console.log(`${desc}未完成任务数${list1}`);
|
||
|
if (list1 > 0) {
|
||
|
notifyStr += `${username},发现 ${desc} 更新了 ${list1} 个新的任务:\n`;
|
||
|
notifyStr += `${lists}\n`;
|
||
|
}
|
||
|
} else {
|
||
|
console.log(`${result}`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//浏览赚
|
||
|
async function GainStart () {
|
||
|
let caller = printCaller()
|
||
|
let url = 'http://kandian.wkandian.com/v5/task/browse_start.json'
|
||
|
let urlObject = populatePostUrl(url, gainbody)
|
||
|
await httpPost(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
|
||
|
if (result.success == false) {
|
||
|
console.log(result.message + "已自动跳过")
|
||
|
} else {
|
||
|
comstate = result.items.comtele_state;
|
||
|
if (comstate == 0) {
|
||
|
console.log("任务开始," + result.items.banner_id + result.message);
|
||
|
await $.wait(10000);
|
||
|
await GainEnd()
|
||
|
} else if (comstate == 1) {
|
||
|
console.log("任务:" + result.items.banner_id + "已完成,本次跳过");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
//看看赚
|
||
|
async function lookStart () {
|
||
|
let caller = printCaller()
|
||
|
let url = 'http://kandian.wkandian.com/v5/Nameless/adlickstart.json'
|
||
|
let urlObject = populatePostUrl(url, lookbody)
|
||
|
await httpPost(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
|
||
|
if (result.success == false) {
|
||
|
console.log(result.message + "已自动跳过")
|
||
|
} else {
|
||
|
comstate = result.items.comtele_state;
|
||
|
if (comstate == 0) {
|
||
|
console.log("任务开始," + result.items.banner_id + result.message);
|
||
|
for (let j = 0; j < result.items.see_num - result.items.read_num; j++) {
|
||
|
console.log("任务执行第" + parseInt(j + 1) + "次")
|
||
|
await $.wait(8000);
|
||
|
await lookstatus()
|
||
|
}
|
||
|
await $.wait(10000);
|
||
|
await lookEnd()
|
||
|
} else if (comstate == 1) {
|
||
|
console.log("任务:" + result.items.banner_id + "已完成,本次跳过");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function GainEnd () {
|
||
|
let caller = printCaller()
|
||
|
let url = 'http://kandian.wkandian.com/v5/task/browse_end.json'
|
||
|
let urlObject = populatePostUrl(url, gainbody)
|
||
|
await httpPost(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
|
||
|
if (result.success == true) {
|
||
|
console.log("任务" + result.items.banner_id + result.message + ",恭喜获得" + result.items.score + "个青豆");
|
||
|
gainscore += parseInt(result.items.score);
|
||
|
$.index += 1;//获得奖励,次数加1
|
||
|
} else {
|
||
|
console.log(result.message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function lookstatus () {
|
||
|
let caller = printCaller()
|
||
|
let url = 'http://kandian.wkandian.com/v5/Nameless/bannerstatus.json'
|
||
|
let urlObject = populatePostUrl(url, lookbody)
|
||
|
await httpPost(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
|
||
|
if (result.success == true) {
|
||
|
console.log("任务" + result.items.banner_id + result.message);
|
||
|
} else {
|
||
|
console.log(result.message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function lookEnd () {
|
||
|
let caller = printCaller()
|
||
|
let url = 'http://kandian.wkandian.com/v5/Nameless/adlickend.json'
|
||
|
let urlObject = populatePostUrl(url, lookbody)
|
||
|
await httpPost(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
|
||
|
if (result.success == true) {
|
||
|
console.log("任务" + result.items.banner_id + result.message + "," + result.items.desc)
|
||
|
lookscore += parseInt(result.items.score);
|
||
|
$.index += 1;//获得奖励,次数加1
|
||
|
} else {
|
||
|
console.log(result.message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//搜索赚激活
|
||
|
async function sszStart (timeout = 0) {
|
||
|
let caller = printCaller()
|
||
|
let url = 'http://kandian.wkandian.com/v5/Sousuo/playStart.json'
|
||
|
let urlObject = populatePostUrl(url, zqsszbody1)
|
||
|
await httpPost(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
|
||
|
if (result.success === true) {
|
||
|
console.log('激活搜索赚任务成功')
|
||
|
comstate = result.items.comtele_state
|
||
|
if (comstate === 1) {
|
||
|
console.log('任务: ' + result.items.task_id + '已完成,跳过')
|
||
|
} else {
|
||
|
console.log("任务开始," + result.items.task_id + result.message);
|
||
|
for (let i = 0; i < 4; i++) {
|
||
|
await $.wait(2000);
|
||
|
await look()
|
||
|
await $.wait(10000);
|
||
|
await look()
|
||
|
await $.wait(10000);
|
||
|
await end()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
console.log('激活搜索赚任务失败')
|
||
|
console.log("该搜索赚任务已自动跳过")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function look (timeout = 0) {
|
||
|
let caller = printCaller()
|
||
|
let url = 'http://kandian.wkandian.com/v5/Sousuo/playStatus.json'
|
||
|
let urlObject = populatePostUrl(url, zqsszbody1)
|
||
|
await httpPost(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
console.log(result)
|
||
|
}
|
||
|
//搜索赚奖励
|
||
|
async function end (timeout = 0) {
|
||
|
let caller = printCaller()
|
||
|
let url = 'http://kandian.wkandian.com/v5/Sousuo/playEnd.json'
|
||
|
let urlObject = populatePostUrl(url, zqsszbody1)
|
||
|
await httpPost(urlObject, caller)
|
||
|
let result = httpResult;
|
||
|
if (!result) return
|
||
|
if (result.items.score !== "undefined") {
|
||
|
console.log('搜索赚获得:' + result.items.score + '金币');
|
||
|
searchscore += parseInt(result.items.score);
|
||
|
$.index += 1;//获得奖励,次数加1
|
||
|
} else {
|
||
|
console.log('领取奖励失败')
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
////////////////////////////////////////////////////////////////////
|
||
|
function populatePostUrl (url, reqBody) {
|
||
|
let rndtime = Math.floor(new Date().getTime() / 1000)
|
||
|
let urlObject = {
|
||
|
url: url,
|
||
|
headers: {
|
||
|
'User-Agent': 'okhttp/3.12.2',
|
||
|
'Host': 'kandian.wkandian.com',
|
||
|
'device-platform': 'android',
|
||
|
'Connection': 'keep-alive',
|
||
|
},
|
||
|
body: reqBody
|
||
|
}
|
||
|
return urlObject;
|
||
|
}
|
||
|
|
||
|
function populateGetUrl (url) {
|
||
|
let rndtime = Math.floor(new Date().getTime() / 1000)
|
||
|
let urlObject = {
|
||
|
url: url,
|
||
|
headers: {
|
||
|
'User-Agent': 'okhttp/3.12.2',
|
||
|
'Host': 'kandian.wkandian.com',
|
||
|
'device-platform': 'android',
|
||
|
'Connection': 'keep-alive',
|
||
|
}
|
||
|
}
|
||
|
return urlObject;
|
||
|
}
|
||
|
|
||
|
|
||
|
async function httpPost (url, caller) {
|
||
|
httpResult = null
|
||
|
return new Promise((resolve) => {
|
||
|
$.post(url, async (err, resp, data) => {
|
||
|
try {
|
||
|
if (err) {
|
||
|
console.log(caller + ": post请求失败");
|
||
|
console.log(JSON.stringify(err));
|
||
|
$.logErr(err);
|
||
|
} else {
|
||
|
if (safeGet(data)) {
|
||
|
httpResult = JSON.parse(data, caller);
|
||
|
if (logDebug) console.log(httpResult);
|
||
|
}
|
||
|
}
|
||
|
} catch (e) {
|
||
|
$.logErr(e, resp);
|
||
|
} finally {
|
||
|
resolve();
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
async function httpGet (url, caller) {
|
||
|
httpResult = null
|
||
|
return new Promise((resolve) => {
|
||
|
$.get(url, async (err, resp, data) => {
|
||
|
try {
|
||
|
if (err) {
|
||
|
console.log(caller + ": get请求失败");
|
||
|
console.log(JSON.stringify(err));
|
||
|
$.logErr(err);
|
||
|
} else {
|
||
|
if (safeGet(data, caller)) {
|
||
|
httpResult = JSON.parse(data);
|
||
|
if (logDebug) console.log(httpResult);
|
||
|
}
|
||
|
}
|
||
|
} catch (e) {
|
||
|
$.logErr(e, resp);
|
||
|
} finally {
|
||
|
resolve();
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function safeGet (data, caller) {
|
||
|
try {
|
||
|
if (typeof JSON.parse(data) == "object") {
|
||
|
return true;
|
||
|
} else {
|
||
|
console.log(`Function ${caller}: 未知错误`);
|
||
|
console.log(data)
|
||
|
}
|
||
|
} catch (e) {
|
||
|
console.log(e);
|
||
|
console.log(`Function ${caller}: 服务器访问数据为空,请检查自身设备网络情况`);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function printCaller () {
|
||
|
return (new Error()).stack.split("\n")[2].trim().split(" ")[1]
|
||
|
}
|
||
|
function randomNum (minNum, maxNum) {
|
||
|
switch (arguments.length) {
|
||
|
case 1:
|
||
|
return parseInt(Math.random() * minNum + 1, 10);
|
||
|
break;
|
||
|
case 2:
|
||
|
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
|
||
|
break;
|
||
|
default:
|
||
|
return 0;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
//检查文件是否存在
|
||
|
function check_file_exist (filename) {
|
||
|
try {
|
||
|
fs.accessSync(filename, fs.constants.R_OK);
|
||
|
return true;
|
||
|
} catch (err) {
|
||
|
console.error(`检测文件${filename}不存在`);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function Env (t, e) { class s { constructor(t) { this.env = t } send (t, e = "GET") { t = "string" == typeof t ? { url: t } : t; let s = this.get; return "POST" === e && (s = this.post), new Promise((e, i) => { s.call(this, t, (t, s, r) => { t ? i(t) : e(s) }) }) } get (t) { return this.send.call(this.env, t) } post (t) { return this.send.call(this.env, t, "POST") } } return new class { constructor(t, e) { this.name = t, this.http = new s(this), this.data = null, this.dataFile = "box.dat", this.logs = [], this.isMute = !1, this.isNeedRewrite = !1, this.logSeparator = "\n", this.startTime = (new Date).getTime(), Object.assign(this, e), this.log("", `\ud83d\udd14${this.name}, \u5f00\u59cb!`) } isNode () { return "undefined" != typeof module && !!module.exports } isQuanX () { return "undefined" != typeof $task } isSurge () { return "undefined" != typeof $httpClient && "undefined" == typeof $loon } isLoon () { return "undefined" != typeof $loon } toObj (t, e = null) { try { return JSON.parse(t) } catch { return e } } toStr (t, e = null) { try { return JSON.stringify(t) } catch { return e } } getjson (t, e) { let s = e; const i = this.getdata(t); if (i) try { s = JSON.parse(this.getdata(t)) } catch { } return s } setjson (t, e) { try { return this.setdata(JSON.stringify(t), e) } catch { return !1 } } getScript (t) { return new Promise(e => { this.get({ url: t }, (t, s, i) => e(i)) }) } runScript (t, e) { return new Promise(s => { let i = this.getdata("@chavy_boxjs_userCfgs.httpapi"); i = i ? i.replace(/\n/g, "").trim() : i; let r = this.getdata("@chavy_boxjs_userCfgs.httpapi_timeout"); r = r ? 1 * r : 20, r = e && e.timeout ? e.timeout : r; const [o, h] = i.split("@"), a = { url: `http://${h}/v1/scripting/evaluate`, body: { script_text: t, mock_type: "cron", timeout: r }, headers: { "X-Key": o, Accept: "*/*" } }; this.post(a, (t, e, i) => s(i)) }).catch(t => this.logErr(t)) } loaddata () { if (!this.isNode()) return {}; { this.fs = this.fs ? this.fs : require("fs"), this.path = this.path ? this.path : require("path"); const t = this.path.resolve(this.dataFile), e = this.path.resolve(process.cwd(), this.dataFile), s = this.fs.existsSync(t), i = !s && this.fs.existsSync(e); if (!s && !i) return {}; { const i = s ? t : e; try { return JSON.parse(this.fs.readFileSync(i)) } catch (t) { return {} } } } } writedata () { if (this.isNode()) { this.fs = this.fs ? this.fs : require("fs"), this.path = this.path ? this.path : require("path"); const t = this.path.resolve(this.dataFile), e = this.path.resolve(process.cwd(), this.dataFile), s = this.fs.existsSync(t), i = !s && this.fs.existsSync(e), r = JSON.stringify(this.data); s ? this.fs.writeFileSync(t, r) : i ? this.fs.writeFileSync(e, r) : this.fs.writeFileSync(t, r) } } lodash_get (t, e, s) { const i = e.replace(/\[(\d+)\]/g, ".$1").split("."); let r = t; for (const t of i) if (r = Object(r)[t], void 0 === r) return s; return r } lodash_set (t, e, s) { return Object(t) !== t ? t : (Array.isArray(e) || (e = e.toString().match(/[^.[\]]+/g) || []), e.slice(0, -1).reduce((t, s, i) => Object(t[s]) === t[s] ? t[s] : t[s] = Math.abs(e[i + 1]) >> 0 == +e[i + 1] ? [] : {}, t)[e[e.length - 1]] = s, t) } getdata (t) { let e = this.getval(t); if (/^@/.test(t)) { const [, s, i] = /^@(.*?)\.(.*?)$/.exec(t), r = s ? this.getval(s) : ""; if (r) try { const t = JSON.parse(r); e = t ? this.lodash_get(t, i, "") : e } catch (t) { e = "" } } return e } setdata (t, e) { let s = !1; if (/^@/.test(e)) { const [, i, r] = /^@(.*?)\.(.*?)$/.exec(e), o = this.getval(i), h = i ? "null" === o ? null : o || "{}" : "{}"; try { const e = JSON.parse(h); this.lodash_set(e, r, t), s = this.setval(JSON.stringify(e), i) } catch (e) { const o = {}; this.lodash_set(o, r, t), s = this.setval(JSON.stringify(o), i) } } else s = this.setval(t, e); return s } getval (t) { return this.isSurge() || this.isLoon() ? $persistentStore.read(t) : this.isQuanX() ? $prefs.valueForKey(t) : this.isNode() ? (this.data = this.loaddata(), this.data[t]) : this.data && this.data[t] || null } setval (t, e) { return this.isSurge() || this.isLoon
|