2 Star 3 Fork 1

luck-ying01/Action_jd_scripts

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
jd_priceProtect.js 28.03 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
/*
update 2021/1/8
京东价格保护:脚本更新地址 https://raw.githubusercontent.com/ZCY01/daily_scripts/main/jd/jd_priceProtect.js
脚本兼容: QuantumultX, Node.js
==========================Quantumultx=========================
[task_local]
# 京东价格保护
5 0 * * * https://raw.githubusercontent.com/ZCY01/daily_scripts/main/jd/jd_priceProtect.js, tag=京东价格保护, img-url=https://raw.githubusercontent.com/ZCY01/img/master/pricev1.png, enabled=true
*/
const $ = new Env('京东价格保护');
//Node.js用户请在jdCookie.js处填写京东ck;
const jdCookieNode = $.isNode() ? require('./jdCookie.js') : '';
const selfdomain = 'https://msitepp-fm.jd.com/';
const unifiedGatewayName = 'https://api.m.jd.com/';
//IOS等用户直接用NobyDa的jd cookie
let cookiesArr = [],
cookie = ''
const jdNotifyControl = $.getdata('jdPriceProtectNotify') || false //是否关闭通知,false打开通知推送,true关闭通知推送
const jdDebug = $.getdata('jdPriceProtectDebug') || false
if ($.isNode()) {
Object.keys(jdCookieNode).forEach((item) => {
cookiesArr.push(jdCookieNode[item])
})
if (process.env.JD_DEBUG && process.env.JD_DEBUG === 'false') console.log = () => {};
} else {
let cookiesData = $.getdata('CookiesJD') || "[]"
cookiesData = jsonParse(cookiesData)
cookiesArr = cookiesData.map(item => item.cookie)
cookiesArr.reverse()
cookiesArr.push(...[$.getdata('CookieJD2'), $.getdata('CookieJD')])
cookiesArr.reverse()
cookiesArr = cookiesArr.filter(item => item !== "" && item !== null && item !== undefined)
}
!(async () => {
if (!cookiesArr[0]) {
$.msg($.name, '【提示】请先获取京东账号一cookie\n直接使用NobyDa的京东签到获取', 'https://bean.m.jd.com/', {
"open-url": "https://bean.m.jd.com/"
})
return
}
for (let i = 0; i < cookiesArr.length; i++) {
if (cookiesArr[i]) {
cookie = cookiesArr[i]
$.UserName = decodeURIComponent(cookie.match(/pt_pin=(.+?);/) && cookie.match(/pt_pin=(.+?);/)[1])
$.index = i + 1
$.isLogin = false
$.nickName = ''
await TotalBean();
if (!$.isLogin) {
$.msg($.name, `【提示】cookie已失效`, `京东账号${$.index} ${$.nickName || $.UserName}\n请重新登录获取\nhttps://bean.m.jd.com/`, {
"open-url": "https://bean.m.jd.com/"
})
continue
}
console.log(`\n***********开始【京东账号${$.index}${$.nickName || $.UserName}********\n`);
$.hasNext = true
$.refundtotalamount = 0
$.orderList = new Array()
$.applyMap = {}
// TODO
$.token = ''
$.feSt = 'f'
console.log(`💥 获得首页面,解析超参数`)
await getHyperParams()
console.log($.HyperParam)
console.log(`💥 获取所有价格保护列表,排除附件商品`)
for (let page = 1; $.hasNext; page++) {
await getApplyData(page)
}
console.log(`💥 删除不符合订单`)
let taskList = []
for (let order of $.orderList) {
taskList.push(HistoryResultQuery(order))
}
await Promise.all(taskList)
console.log(`💥 ${$.orderList.length}个商品即将申请价格保护!`)
for (let order of $.orderList) {
await skuApply(order)
await $.wait(200)
}
for (let i = 1; i <= 30 && Object.keys($.applyMap).length > 0; i++) {
console.log(`⏳ 获取申请价格保护结果,${30-i}s...`)
await $.wait(1000)
if (i % 5 == 0) {
await getApplyResult()
}
}
showMsg()
}
}
})()
.catch((e) => {
console.log(`❗️ ${$.name} 运行错误!\n${e}`)
if (eval(jdDebug)) $.msg($.name, ``, `${e}`)
}).finally(() => $.done())
const getValueById = function (text, id) {
try {
const reg = new RegExp(`id="${id}".*value="(.*?)"`)
const res = text.match(reg)
return res[1]
} catch (e) {
throw new Error(`getValueById:${id} err`)
}
}
function getHyperParams() {
return new Promise((resolve, reject) => {
const options = {
"url": 'https://msitepp-fm.jd.com/rest/priceprophone/priceProPhoneMenu',
"headers": {
'Host': 'msitepp-fm.jd.com',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Connection': 'keep-alive',
'Cookie': cookie,
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
'Accept-Language': 'zh-cn',
'Referer': 'https://ihelp.jd.com/',
'Accept-Encoding': 'gzip, deflate, br',
},
}
$.get(options, (err, resp, data) => {
try {
if (err) throw new Error(JSON.stringify(err))
$.HyperParam = {
sid_hid: getValueById(data, 'sid_hid'),
type_hid: getValueById(data, 'type_hid'),
isLoadLastPropriceRecord: getValueById(data, 'isLoadLastPropriceRecord'),
isLoadSkuPrice: getValueById(data, 'isLoadSkuPrice'),
RefundType_Orderid_Repeater_hid: getValueById(data, 'RefundType_Orderid_Repeater_hid'),
isAlertSuccessTip: getValueById(data, 'isAlertSuccessTip'),
forcebot: getValueById(data, 'forcebot'),
useColorApi: getValueById(data, 'useColorApi'),
}
} catch (e) {
reject(`⚠️ ${arguments.callee.name.toString()} API返回结果解析出错\n${e}\n${JSON.stringify(data)}`)
} finally {
resolve();
}
})
})
}
function getApplyData(page) {
return new Promise((resolve, reject) => {
$.hasNext = false
const pageSize = 5
let paramObj = {};
paramObj.page = page
paramObj.pageSize = pageSize
paramObj.keyWords = ""
paramObj.sid = $.HyperParam.sid_hid
paramObj.type = $.HyperParam.type_hid
paramObj.forcebot = $.HyperParam.forcebot
paramObj.token = $.token
paramObj.feSt = $.feSt
$.post(taskurl('siteppM_priceskusPull', paramObj), (err, resp, data) => {
try {
if (err) {
console.log(`🚫 ${arguments.callee.name.toString()} API请求失败,请检查网路\n${JSON.stringify(err)}`)
} else {
let pageErrorVal = data.match(/id="pageError_\d+" name="pageError_\d+" value="(.*?)"/)[1]
if (pageErrorVal == 'noexception') {
let pageDatasSize = eval(data.match(/id="pageSize_\d+" name="pageSize_\d+" value="(.*?)"/)[1])
$.hasNext = pageDatasSize >= pageSize
let orders = [...data.matchAll(/skuApply\((.*?)\)/g)]
let titles = [...data.matchAll(/<p class="name">(.*?)<\/p>/g)]
for (let i = 0; i < orders.length; i++) {
let info = orders[i][1].split(',')
if (info.length != 4) {
throw new Error(`价格保护 ${order[1]}.length != 4`)
}
const item = {
orderId: eval(info[0]),
skuId: eval(info[1]),
sequence: eval(info[2]),
orderCategory: eval(info[3]),
title: `🛒${titles[i][1].substr(0,15)}🛒`,
}
let id = `skuprice_${item.orderId}_${item.skuId}_${item.sequence}`
let reg = new RegExp(`${id}.*?isfujian="(.*?)"`)
isfujian = data.match(reg)[1]
if (isfujian == "false") {
let skuRefundTypeDiv_orderId = `skuRefundTypeDiv_${item.orderId}`
item['refundtype'] = getValueById(data, skuRefundTypeDiv_orderId)
$.orderList.push(item)
}
//else...尊敬的顾客您好,您选择的商品本身为赠品,是不支持价保的呦,请您理解。
}
}
}
} catch (e) {
reject(`⚠️ ${arguments.callee.name.toString()} API返回结果解析出错\n${e}\n${JSON.stringify(data)}`)
} finally {
resolve();
}
})
})
}
// 申请按钮
// function skuApply(orderId, skuId, sequence, orderCategory, refundtype) {
function skuApply(order) {
return new Promise((resolve, reject) => {
let paramObj = {};
paramObj.orderId = order.orderId;
paramObj.orderCategory = order.orderCategory;
paramObj.skuId = order.skuId;
paramObj.sid = $.HyperParam.sid_hid
paramObj.type = $.HyperParam.type_hid
paramObj.refundtype = order.refundtype
paramObj.forcebot = $.HyperParam.forcebot
paramObj.token = $.token
paramObj.feSt = $.feSt
console.log(`🚀 ${order.title} 正在价格保护...`)
$.post(taskurl('siteppM_proApply', paramObj), (err, resp, data) => {
try {
if (err) {
console.log(`🚫 ${arguments.callee.name.toString()} API请求失败,请检查网路\n${JSON.stringify(err)}`)
} else {
data = JSON.parse(data)
if (data.flag) {
if (data.proSkuApplyId != null) {
$.applyMap[data.proSkuApplyId[0]] = order
}
} else {
console.log(`🚫 ${order.title} 申请失败:${data.errorMessage}`)
}
}
} catch (e) {
reject(`⚠️ ${arguments.callee.name.toString()} API返回结果解析出错\n${e}\n${JSON.stringify(data)}`)
} finally {
resolve();
}
})
})
}
function HistoryResultQuery(order) {
return new Promise((resolve, reject) => {
let paramObj = {};
paramObj.orderId = order.orderId;
paramObj.skuId = order.skuId;
paramObj.sequence = order.sequence;
paramObj.sid = $.HyperParam.sid_hid
paramObj.type = $.HyperParam.type_hid
paramObj.pin = undefined
paramObj.forcebot = $.HyperParam.forcebot
const reg = new RegExp("overTime|[^库]不支持价保|无法申请价保|请用原订单申请")
let deleted = true
$.post(taskurl('siteppM_skuProResultPin', paramObj), (err, resp, data) => {
try {
if (err) {
console.log(`🚫 ${arguments.callee.name.toString()} API请求失败,请检查网路\n${JSON.stringify(err)}`)
} else {
deleted = reg.test(data)
}
} catch (e) {
reject(`⚠️ ${arguments.callee.name.toString()} API返回结果解析出错\n${e}\n${JSON.stringify(data)}`)
} finally {
if (deleted) {
console.log(`⏰ 删除商品:${order.title}`)
$.orderList = $.orderList.filter(item => {
return item.orderId != order.orderId || item.skuId != order.skuId
})
}
resolve()
}
})
})
}
function getApplyResult() {
function handleApplyResult(ajaxResultObj) {
if (ajaxResultObj.hasResult != "undefined" && ajaxResultObj.hasResult == true) { //有结果了
let proSkuApplyId = ajaxResultObj.applyResultVo.proSkuApplyId; //申请id
let order = $.applyMap[proSkuApplyId]
delete $.applyMap[proSkuApplyId]
if (ajaxResultObj.applyResultVo.proApplyStatus == 'ApplySuccess') { //价保成功
$.refundtotalamount += ajaxResultObj.applyResultVo.refundtotalamount
} else {
console.log(`💢 ${order.title} 申请失败:${ajaxResultObj.applyResultVo.failTypeStr} 失败类型:${ajaxResultObj.applyResultVo.failType}`)
}
}
}
return new Promise((resolve, reject) => {
let proSkuApplyIds = Object.keys($.applyMap).join(",");
let paramObj = {};
paramObj.proSkuApplyIds = proSkuApplyIds;
paramObj.pin = $.HyperParam.pin
paramObj.type = $.HyperParam.type_hid
$.post(taskurl('siteppM_moreApplyResult', paramObj), (err, resp, data) => {
try {
if (err) {
console.log(`🚫 ${arguments.callee.name.toString()} API请求失败,请检查网路\n${JSON.stringify(err)}`)
} else if (data) {
data = JSON.parse(data)
let resultArray = data.applyResults;
for (let i = 0; i < resultArray.length; i++) {
let ajaxResultObj = resultArray[i];
handleApplyResult(ajaxResultObj);
}
}
} catch (e) {
reject(`⚠️ ${arguments.callee.name.toString()} API返回结果解析出错\n${e}\n${JSON.stringify(data)}`)
} finally {
resolve()
}
})
})
}
function taskurl(functionid, body) {
let urlStr = selfdomain + "rest/priceprophone/priceskusPull"
if ($.HyperParam.useColorApi == "true") {
urlStr = unifiedGatewayName + "api?appid=siteppM&functionId=" + functionid + "&forcebot=" + $.HyperParam.forcebot + "&t=" + new Date().getTime()
}
return {
"url": urlStr,
"headers": {
'Host': $.HyperParam.useColorApi == 'true' ? 'api.m.jd.com' : 'msitepp-fm.jd.com',
'Accept': '*/*',
'Accept-Language': 'zh-cn',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://msitepp-fm.jd.com',
'Connection': 'keep-alive',
'Referer': 'https://msitepp-fm.jd.com/rest/priceprophone/priceProPhoneMenu',
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1",
"Cookie": cookie
},
"body": body ? `body=${JSON.stringify(body)}` : undefined
}
}
function showMsg() {
console.log(`🎉 本次价格保护金额:${$.refundtotalamount}💰`)
if ($.refundtotalamount && !eval(jdNotifyControl)) {
$.msg($.name, ``, `京东账号${$.index} ${$.nickName || $.UserName}\n🎉 本次价格保护金额:${$.refundtotalamount}💰`, {
"open-url": "https://msitepp-fm.jd.com/rest/priceprophone/priceProPhoneMenu"
});
}
}
function TotalBean() {
return new Promise(resolve => {
const options = {
"url": `https://wq.jd.com/user/info/QueryJDUserInfo?sceneval=2`,
"headers": {
"Accept": "application/json,text/plain, */*",
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-cn",
"Connection": "keep-alive",
"Cookie": cookie,
"Referer": "https://wqs.jd.com/my/jingdou/my.shtml?sceneval=2",
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1"
}
}
$.post(options, (err, resp, data) => {
try {
if (err) {
console.log(`${JSON.stringify(err)}`)
console.log(`${$.name} API请求失败,请检查网路重试`)
} else {
if (data) {
data = JSON.parse(data);
if (data['retcode'] === 13) {
return
}
$.isLogin = true
$.nickName = data['base'].nickname;
} else {
console.log(`京东服务器返回空数据`)
}
}
} catch (e) {
$.logErr(e, resp)
} finally {
resolve();
}
})
})
}
function jsonParse(str) {
if (typeof str == "string") {
try {
return JSON.parse(str);
} catch (e) {
console.log(e);
$.msg($.name, '', '请勿随意在BoxJs输入框修改内容\n建议通过脚本去获取cookie')
return [];
}
}
}
// 来自 @chavyleung 大佬
// https://raw.githubusercontent.com/chavyleung/scripts/master/Env.js
function Env(name, opts) {
class Http {
constructor(env) {
this.env = env
}
send(opts, method = 'GET') {
opts = typeof opts === 'string' ? {
url: opts
} : opts
let sender = this.get
if (method === 'POST') {
sender = this.post
}
return new Promise((resolve, reject) => {
sender.call(this, opts, (err, resp, body) => {
if (err) reject(err)
else resolve(resp)
})
})
}
get(opts) {
return this.send.call(this.env, opts)
}
post(opts) {
return this.send.call(this.env, opts, 'POST')
}
}
return new(class {
constructor(name, opts) {
this.name = name
this.http = new Http(this)
this.data = null
this.dataFile = 'box.dat'
this.logs = []
this.isMute = false
this.isNeedRewrite = false
this.logSeparator = '\n'
this.startTime = new Date().getTime()
Object.assign(this, opts)
this.log('', `🔔${this.name}, 开始!`)
}
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(str, defaultValue = null) {
try {
return JSON.parse(str)
} catch {
return defaultValue
}
}
toStr(obj, defaultValue = null) {
try {
return JSON.stringify(obj)
} catch {
return defaultValue
}
}
getjson(key, defaultValue) {
let json = defaultValue
const val = this.getdata(key)
if (val) {
try {
json = JSON.parse(this.getdata(key))
} catch {}
}
return json
}
setjson(val, key) {
try {
return this.setdata(JSON.stringify(val), key)
} catch {
return false
}
}
getScript(url) {
return new Promise((resolve) => {
this.get({
url
}, (err, resp, body) => resolve(body))
})
}
runScript(script, runOpts) {
return new Promise((resolve) => {
let httpapi = this.getdata('@chavy_boxjs_userCfgs.httpapi')
httpapi = httpapi ? httpapi.replace(/\n/g, '').trim() : httpapi
let httpapi_timeout = this.getdata('@chavy_boxjs_userCfgs.httpapi_timeout')
httpapi_timeout = httpapi_timeout ? httpapi_timeout * 1 : 20
httpapi_timeout = runOpts && runOpts.timeout ? runOpts.timeout : httpapi_timeout
const [key, addr] = httpapi.split('@')
const opts = {
url: `http://${addr}/v1/scripting/evaluate`,
body: {
script_text: script,
mock_type: 'cron',
timeout: httpapi_timeout
},
headers: {
'X-Key': key,
'Accept': '*/*'
}
}
this.post(opts, (err, resp, body) => resolve(body))
}).catch((e) => this.logErr(e))
}
loaddata() {
if (this.isNode()) {
this.fs = this.fs ? this.fs : require('fs')
this.path = this.path ? this.path : require('path')
const curDirDataFilePath = this.path.resolve(this.dataFile)
const rootDirDataFilePath = this.path.resolve(process.cwd(), this.dataFile)
const isCurDirDataFile = this.fs.existsSync(curDirDataFilePath)
const isRootDirDataFile = !isCurDirDataFile && this.fs.existsSync(rootDirDataFilePath)
if (isCurDirDataFile || isRootDirDataFile) {
const datPath = isCurDirDataFile ? curDirDataFilePath : rootDirDataFilePath
try {
return JSON.parse(this.fs.readFileSync(datPath))
} catch (e) {
return {}
}
} else return {}
} else return {}
}
writedata() {
if (this.isNode()) {
this.fs = this.fs ? this.fs : require('fs')
this.path = this.path ? this.path : require('path')
const curDirDataFilePath = this.path.resolve(this.dataFile)
const rootDirDataFilePath = this.path.resolve(process.cwd(), this.dataFile)
const isCurDirDataFile = this.fs.existsSync(curDirDataFilePath)
const isRootDirDataFile = !isCurDirDataFile && this.fs.existsSync(rootDirDataFilePath)
const jsondata = JSON.stringify(this.data)
if (isCurDirDataFile) {
this.fs.writeFileSync(curDirDataFilePath, jsondata)
} else if (isRootDirDataFile) {
this.fs.writeFileSync(rootDirDataFilePath, jsondata)
} else {
this.fs.writeFileSync(curDirDataFilePath, jsondata)
}
}
}
lodash_get(source, path, defaultValue = undefined) {
const paths = path.replace(/\[(\d+)\]/g, '.$1').split('.')
let result = source
for (const p of paths) {
result = Object(result)[p]
if (result === undefined) {
return defaultValue
}
}
return result
}
lodash_set(obj, path, value) {
if (Object(obj) !== obj) return obj
if (!Array.isArray(path)) path = path.toString().match(/[^.[\]]+/g) || []
path
.slice(0, -1)
.reduce((a, c, i) => (Object(a[c]) === a[c] ? a[c] : (a[c] = Math.abs(path[i + 1]) >> 0 === +path[i + 1] ? [] : {})), obj)[
path[path.length - 1]
] = value
return obj
}
getdata(key) {
let val = this.getval(key)
// 如果以 @
if (/^@/.test(key)) {
const [, objkey, paths] = /^@(.*?)\.(.*?)$/.exec(key)
const objval = objkey ? this.getval(objkey) : ''
if (objval) {
try {
const objedval = JSON.parse(objval)
val = objedval ? this.lodash_get(objedval, paths, '') : val
} catch (e) {
val = ''
}
}
}
return val
}
setdata(val, key) {
let issuc = false
if (/^@/.test(key)) {
const [, objkey, paths] = /^@(.*?)\.(.*?)$/.exec(key)
const objdat = this.getval(objkey)
const objval = objkey ? (objdat === 'null' ? null : objdat || '{}') : '{}'
try {
const objedval = JSON.parse(objval)
this.lodash_set(objedval, paths, val)
issuc = this.setval(JSON.stringify(objedval), objkey)
} catch (e) {
const objedval = {}
this.lodash_set(objedval, paths, val)
issuc = this.setval(JSON.stringify(objedval), objkey)
}
} else {
issuc = this.setval(val, key)
}
return issuc
}
getval(key) {
if (this.isSurge() || this.isLoon()) {
return $persistentStore.read(key)
} else if (this.isQuanX()) {
return $prefs.valueForKey(key)
} else if (this.isNode()) {
this.data = this.loaddata()
return this.data[key]
} else {
return (this.data && this.data[key]) || null
}
}
setval(val, key) {
if (this.isSurge() || this.isLoon()) {
return $persistentStore.write(val, key)
} else if (this.isQuanX()) {
return $prefs.setValueForKey(val, key)
} else if (this.isNode()) {
this.data = this.loaddata()
this.data[key] = val
this.writedata()
return true
} else {
return (this.data && this.data[key]) || null
}
}
initGotEnv(opts) {
this.got = this.got ? this.got : require('got')
this.cktough = this.cktough ? this.cktough : require('tough-cookie')
this.ckjar = this.ckjar ? this.ckjar : new this.cktough.CookieJar()
if (opts) {
opts.headers = opts.headers ? opts.headers : {}
if (undefined === opts.headers.Cookie && undefined === opts.cookieJar) {
opts.cookieJar = this.ckjar
}
}
}
get(opts, callback = () => {}) {
if (opts.headers) {
delete opts.headers['Content-Type']
delete opts.headers['Content-Length']
}
if (this.isSurge() || this.isLoon()) {
if (this.isSurge() && this.isNeedRewrite) {
opts.headers = opts.headers || {}
Object.assign(opts.headers, {
'X-Surge-Skip-Scripting': false
})
}
$httpClient.get(opts, (err, resp, body) => {
if (!err && resp) {
resp.body = body
resp.statusCode = resp.status
}
callback(err, resp, body)
})
} else if (this.isQuanX()) {
if (this.isNeedRewrite) {
opts.opts = opts.opts || {}
Object.assign(opts.opts, {
hints: false
})
}
$task.fetch(opts).then(
(resp) => {
const {
statusCode: status,
statusCode,
headers,
body
} = resp
callback(null, {
status,
statusCode,
headers,
body
}, body)
},
(err) => callback(err)
)
} else if (this.isNode()) {
this.initGotEnv(opts)
this.got(opts)
.on('redirect', (resp, nextOpts) => {
try {
if (resp.headers['set-cookie']) {
const ck = resp.headers['set-cookie'].map(this.cktough.Cookie.parse).toString()
if (ck) {
this.ckjar.setCookieSync(ck, null)
}
nextOpts.cookieJar = this.ckjar
}
} catch (e) {
this.logErr(e)
}
// this.ckjar.setCookieSync(resp.headers['set-cookie'].map(Cookie.parse).toString())
})
.then(
(resp) => {
const {
statusCode: status,
statusCode,
headers,
body
} = resp
callback(null, {
status,
statusCode,
headers,
body
}, body)
},
(err) => {
const {
message: error,
response: resp
} = err
callback(error, resp, resp && resp.body)
}
)
}
}
post(opts, callback = () => {}) {
// 如果指定了请求体, 但没指定`Content-Type`, 则自动生成
if (opts.body && opts.headers && !opts.headers['Content-Type']) {
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded'
}
if (opts.headers) delete opts.headers['Content-Length']
if (this.isSurge() || this.isLoon()) {
if (this.isSurge() && this.isNeedRewrite) {
opts.headers = opts.headers || {}
Object.assign(opts.headers, {
'X-Surge-Skip-Scripting': false
})
}
$httpClient.post(opts, (err, resp, body) => {
if (!err && resp) {
resp.body = body
resp.statusCode = resp.status
}
callback(err, resp, body)
})
} else if (this.isQuanX()) {
opts.method = 'POST'
if (this.isNeedRewrite) {
opts.opts = opts.opts || {}
Object.assign(opts.opts, {
hints: false
})
}
$task.fetch(opts).then(
(resp) => {
const {
statusCode: status,
statusCode,
headers,
body
} = resp
callback(null, {
status,
statusCode,
headers,
body
}, body)
},
(err) => callback(err)
)
} else if (this.isNode()) {
this.initGotEnv(opts)
const {
url,
..._opts
} = opts
this.got.post(url, _opts).then(
(resp) => {
const {
statusCode: status,
statusCode,
headers,
body
} = resp
callback(null, {
status,
statusCode,
headers,
body
}, body)
},
(err) => {
const {
message: error,
response: resp
} = err
callback(error, resp, resp && resp.body)
}
)
}
}
/**
*
* 示例:$.time('yyyy-MM-dd qq HH:mm:ss.S')
* :$.time('yyyyMMddHHmmssS')
* y:年 M:月 d:日 q:季 H:时 m:分 s:秒 S:毫秒
* 其中y可选0-4位占位符、S可选0-1位占位符,其余可选0-2位占位符
* @param {*} fmt 格式化参数
*
*/
time(fmt) {
let o = {
'M+': new Date().getMonth() + 1,
'd+': new Date().getDate(),
'H+': new Date().getHours(),
'm+': new Date().getMinutes(),
's+': new Date().getSeconds(),
'q+': Math.floor((new Date().getMonth() + 3) / 3),
'S': new Date().getMilliseconds()
}
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (new Date().getFullYear() + '').substr(4 - RegExp.$1.length))
for (let k in o)
if (new RegExp('(' + k + ')').test(fmt))
fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
return fmt
}
/**
* 系统通知
*
* > 通知参数: 同时支持 QuanX 和 Loon 两种格式, EnvJs根据运行环境自动转换, Surge 环境不支持多媒体通知
*
* 示例:
* $.msg(title, subt, desc, 'twitter://')
* $.msg(title, subt, desc, { 'open-url': 'twitter://', 'media-url': 'https://github.githubassets.com/images/modules/open_graph/github-mark.png' })
* $.msg(title, subt, desc, { 'open-url': 'https://bing.com', 'media-url': 'https://github.githubassets.com/images/modules/open_graph/github-mark.png' })
*
* @param {*} title 标题
* @param {*} subt 副标题
* @param {*} desc 通知详情
* @param {*} opts 通知参数
*
*/
msg(title = name, subt = '', desc = '', opts) {
const toEnvOpts = (rawopts) => {
if (!rawopts) return rawopts
if (typeof rawopts === 'string') {
if (this.isLoon()) return rawopts
else if (this.isQuanX()) return {
'open-url': rawopts
}
else if (this.isSurge()) return {
url: rawopts
}
else return undefined
} else if (typeof rawopts === 'object') {
if (this.isLoon()) {
let openUrl = rawopts.openUrl || rawopts.url || rawopts['open-url']
let mediaUrl = rawopts.mediaUrl || rawopts['media-url']
return {
openUrl,
mediaUrl
}
} else if (this.isQuanX()) {
let openUrl = rawopts['open-url'] || rawopts.url || rawopts.openUrl
let mediaUrl = rawopts['media-url'] || rawopts.mediaUrl
return {
'open-url': openUrl,
'media-url': mediaUrl
}
} else if (this.isSurge()) {
let openUrl = rawopts.url || rawopts.openUrl || rawopts['open-url']
return {
url: openUrl
}
}
} else {
return undefined
}
}
if (!this.isMute) {
if (this.isSurge() || this.isLoon()) {
$notification.post(title, subt, desc, toEnvOpts(opts))
} else if (this.isQuanX()) {
$notify(title, subt, desc, toEnvOpts(opts))
}
}
if (!this.isMuteLog) {
let logs = ['', '==============📣系统通知📣==============']
logs.push(title)
subt ? logs.push(subt) : ''
desc ? logs.push(desc) : ''
console.log(logs.join('\n'))
this.logs = this.logs.concat(logs)
}
}
log(...logs) {
if (logs.length > 0) {
this.logs = [...this.logs, ...logs]
}
console.log(logs.join(this.logSeparator))
}
logErr(err, msg) {
const isPrintSack = !this.isSurge() && !this.isQuanX() && !this.isLoon()
if (!isPrintSack) {
this.log('', `❗️${this.name}, 错误!`, err)
} else {
this.log('', `❗️${this.name}, 错误!`, err.stack)
}
}
wait(time) {
return new Promise((resolve) => setTimeout(resolve, time))
}
done(val = {}) {
const endTime = new Date().getTime()
const costTime = (endTime - this.startTime) / 1000
this.log('', `🔔${this.name}, 结束! 🕛 ${costTime} 秒`)
this.log()
if (this.isSurge() || this.isQuanX() || this.isLoon()) {
$done(val)
}
}
})(name, opts)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/luck-ying01/Action_jd_scripts.git
git@gitee.com:luck-ying01/Action_jd_scripts.git
luck-ying01
Action_jd_scripts
Action_jd_scripts
main

搜索帮助