const $ = new Env('我在校园每日健康打卡'); let accountArr = [ '账号----密码', //按照左边的格式填写我在校园账号密码 ], account, password; $.cookie = ''; let location = { latitude: '23.748942', //学校定位的纬度 longitude: '113.10877', //学校定位的经度 city: '', district: '', province: '' } let notifyContext = ``, healthOptions = ``; !(async () => { if (!accountArr[0]) { console.log('请先放置我在校园帐号密码') return; } for (let i = 0; i < accountArr.length; i++) { if (accountArr[i]) { account = accountArr[i].split('----')[0]; password = accountArr[i].split('----')[1]; $.isLogin = false; healthOptions = `%5B` notifyContext += `【${account}` console.log(`【${account}】`); if ($.isLogin === false) await main(); $.cookie = ''; } await $.wait(2000) } let myDate = new Date(); notifyContext += `%0D%0A执行日期:${$.date}%0D%0A打卡时间:${myDate.getHours()}:${myDate.getMinutes()}:${myDate.getSeconds()}%0D%0A我在校园打卡 By X1a0He` await sendBark(); })().catch((e) => { $.log('', `? ${$.name}, 失败! 原因: ${e}!`, '') }).finally(() => { $.done(); }) async function main() { await login(account, password); await getTodayData(); if ($.isLogin) { await getLocation(); await submitHealth(); } } function login(username, password) { return new Promise(resolve => { console.log('正在登录中...'); $.get({ url: `https://gw.wozaixiaoyuan.com/basicinfo/mobile/login/username?username=${username}&password=${password}`, headers: { "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/87.0.4280.88", } }, async (err, resp, data) => { try { if (err) { console.log(`${JSON.stringify(err)}`) console.log(`${$.name} login function出错`) } else { let dataJSON = JSON.parse(data); if (typeof dataJSON.sessionUser !== 'undefined') { notifyContext += ` ${dataJSON.sessionUser.name}】` console.log(`${dataJSON.sessionUser.name} 登录成功`) $.isLogin = true; $.cookie = `JWSESSION=${resp.headers.jwsession}`; console.log(`Cookie:${$.cookie}`) } else if (dataJSON.code === 101) { notifyContext += `】${dataJSON.message}%0D%0A` console.log(`${dataJSON.message}`) } } } catch (e) { $.logErr(e, resp) } finally { resolve(); } }) }) } function getTodayData() { return new Promise(resolve => { $.get({ url: "https://student.wozaixiaoyuan.com/health/getToday.json", headers: { "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/87.0.4280.88", "Cookie": $.cookie } }, async (err, resp, data) => { try { if (err) { console.log(`${JSON.stringify(err)}`) console.log(`${$.name} API请求失败,请检查网路重试`) } else { if (safeGet(data)) { let result = JSON.parse(data); if (result.code === -10) { $.isLogin = false; } else { $.date = result.data.date; $.subjectNum = result.data.titles.length //题目数量 for (let i = 0; i < $.subjectNum; i++) { $.healthOptionsNum = result.data.titles[i].healthOptions.length for (let j = 0; j < $.healthOptionsNum; j++) { $.send = false; if (result.data.titles[i].healthOptions[j].option.indexOf("无下列情况,身体健康") !== -1 || result.data.titles[i].healthOptions[j].option.indexOf("清远") !== -1 || result.data.titles[i].healthOptions[j].option.indexOf("绿码") !== -1) { $.send = true; i === $.subjectNum - 1 ? healthOptions += `%22${j}%22` : healthOptions += `%22${j}%22%2C`; //上面是提交的时候的答案body组成逻辑,这个需要自己抓包修改 // console.log(`选项${i + 1}:${result.data.titles[i].healthOptions[j].option}`); // notifyContext += `选项${i + 1}:${result.data.titles[i].healthOptions[j].option}%0D%0A` break; } } } healthOptions += `%5D` } } } } catch (e) { $.logErr(e, resp) } finally { resolve(); } }) }) } function getLocation() { return new Promise(resolve => { $.get({ url: `https://restapi.amap.com/v3/geocode/regeo?key=5df7fee749f489424dd417dfcb792b45&location=${location.longitude}%2C${location.latitude}&s=rsx&appname=5df7fee749f489424dd417dfcb792b45`, headers: { "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/87.0.4280.88", } }, async (err, resp, data) => { try { if (err) { console.log(`${JSON.stringify(err)}`) console.log(`${$.name} API请求失败,请检查网路重试`) } else { if (safeGet(data)) { let dataJSON = JSON.parse(data); location.city = dataJSON.regeocode.addressComponent.city; location.district = dataJSON.regeocode.addressComponent.district; location.province = dataJSON.regeocode.addressComponent.province; } } } catch (e) { $.logErr(e, resp) } finally { resolve(); } }) }) } function sendBark() { return new Promise(resolve => { console.log('正在发送Bark通知'); $.get({ url: `https://api.day.app/barkKey/我在校园打卡执行详情/${notifyContext}`, }, async (err, resp, data) => { try { if (err) { console.log(`${JSON.stringify(err)}`) console.log(`${$.name} API请求失败,请检查网路重试`) } else { if (safeGet(data)) { let dataJSON = JSON.parse(data); if (dataJSON.code === 200) { console.log('Bark通知发送成功') } } } } catch (e) { $.logErr(e, resp) } finally { resolve(); } }) }) } function submitHealth() { return new Promise(resolve => { let options = { url: 'https://student.wozaixiaoyuan.com/health/save.json', headers: { 'Cookie': $.cookie }, body: `answers=${healthOptions}&latitude=${encodeURIComponent(location.latitude)}&longitude=${encodeURIComponent(location.longitude)}&country=%E4%B8%AD%E5%9B%BD&city=${encodeURIComponent(location.city)}&district=${encodeURIComponent(location.district)}&province=${encodeURIComponent(location.province)}&township=&street=` } console.log("正在打卡...") if ($.send) { $.post(options, (err, resp, data) => { try { if (err) { console.log(`${JSON.stringify(err)}`) console.log(`我在校园API请求失败,请检查网路重试`) } else { if (safeGet(data)) { let dataJSON = JSON.parse(data); if (dataJSON.code === 0) { console.log("打卡成功") notifyContext += `打卡成功%0D%0A` } else if (dataJSON.code === 1) { console.log(`${dataJSON.message}`); notifyContext += `${dataJSON.message}%0D%0A` } else { console.log(`${dataJSON}`) notifyContext += `${dataJSON}%0D%0A` } } } } catch (e) { $.logErr(e, resp) } finally { resolve(); } }) } else { console.log("打卡失败,\$\.send变量为false") notifyContext += `打卡失败,失败原因:\$\.send变量为false,请查看题目是否已经变动%0D%0A` } }) } function safeGet(data) { try { if (typeof JSON.parse(data) == "object") { return true; } } catch (e) { console.log(e); console.log(`我在校园服务器访问数据为空,请检查自身设备网络情况`); return false; } } 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.encoding = 'utf-8' this.startTime = new Date().getTime() Object.assign(this, opts) console.log(`??${this.name}, 开始!\n`) // this.log('', `??${this.name}, 开始!`) } isNode() { return 'undefined' !== typeof module && !!module.exports } 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.isNode()) { this.data = this.loaddata() return this.data[key] } else { return (this.data && this.data[key]) || null } } setval(val, key) { 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.isNode()) { let iconv = require('iconv-lite') 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, rawBody } = resp callback(null, { status, statusCode, headers, rawBody }, iconv.decode(rawBody, this.encoding)) }, (err) => { const { message: error, response: resp } = err callback(error, resp, resp && iconv.decode(resp.rawBody, this.encoding)) } ) } } post(opts, callback = () => {}) { const method = opts.method ? opts.method.toLocaleLowerCase() : 'post' // 如果指定了请求体, 但没指定`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.isNode()) { let iconv = require('iconv-lite') this.initGotEnv(opts) const { url, ..._opts } = opts this.got[method](url, _opts).then( (resp) => { const { statusCode: status, statusCode, headers, rawBody } = resp callback(null, { status, statusCode, headers, rawBody }, iconv.decode(rawBody, this.encoding)) }, (err) => { const { message: error, response: resp } = err callback(error, resp, resp && iconv.decode(resp.rawBody, this.encoding)) } ) } } /** * * 示例:$.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 {string} fmt 格式化参数 * @param {number} 可选: 根据指定时间戳返回格式化日期 * */ time(fmt, ts = null) { const date = ts ? new Date(ts) : new Date() let o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'H+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds(), 'q+': Math.floor((date.getMonth() + 3) / 3), 'S': date.getMilliseconds() } if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (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; return undefined; } 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) { 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() } })(name, opts) }