Files
smallfawn-QLScriptPublic/wxapp/casetify.js
T
2026-05-31 14:25:09 +08:00

274 lines
9.1 KiB
JavaScript

/*
------------------------------------------
@Author: sm
@Date: 2026.05.31
@Description: CASETiFY 签到
cron: 46 8 * * *
------------------------------------------
变量名:casetify
变量值:wx_server 里的 openid/账号标识,多账号用 & 或换行
------------------------------------------
*/
const { Env } = require("../tools/env.js");
const $ = new Env("CASETiFY 签到");
const axios = require("axios");
const fs = require("fs");
const path = require("path");
const WeChatServer = require("./wcs.js");
const MINI_APP_ID = "wxd0c71d6bf928a416";
const PAGE_VERSION = "160";
const API_BASE = "https://mini-app-api.casetify.cn/api/v4";
const WECHAT_ID = 260;
const POINT_MALL_TYPE = 13;
const TOKEN_CACHE_FILE = path.join(__dirname, "casetify_token_cache.json");
const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) MicroMessenger/3.9.12 MiniProgramEnv/Windows WindowsWechat/WMPF";
let ckName = "casetify";
const wechat = new WeChatServer({
url: process.env.wx_server_url || "http://192.168.31.196:8787",
appid: MINI_APP_ID,
auth: process.env.wx_auth || "",
});
function readTokenCache() {
try {
if (!fs.existsSync(TOKEN_CACHE_FILE)) return {};
return JSON.parse(fs.readFileSync(TOKEN_CACHE_FILE, "utf8")) || {};
} catch (e) {
return {};
}
}
function writeTokenCache(cache) {
try {
fs.writeFileSync(TOKEN_CACHE_FILE, JSON.stringify(cache, null, 2), "utf8");
} catch (e) {
$.log(`写入token缓存失败: ${e.message || e}`);
}
}
function normalizeDate(value) {
const parts = String(value || "").split("-");
if (parts.length !== 3) return String(value || "");
return `${parts[0]}-${String(Number(parts[1])).padStart(2, "0")}-${String(Number(parts[2])).padStart(2, "0")}`;
}
function today() {
const d = new Date();
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
}
function isTokenError(message) {
return /token|登录|授权|invalid|expire|过期|401|403/i.test(String(message || ""));
}
class Task {
constructor(openid) {
this.index = $.userIdx++;
this.openid = String(openid || "").trim();
this.token = "";
this.memberId = "";
this.customerNo = "";
this.phone = "";
this.levels = "";
this.campaignId = "";
}
async run() {
const cached = this.getCachedToken();
if (cached) {
this.applyToken(cached);
$.log(`账号[${this.index}] 使用缓存token`);
if (!(await this.checkToken())) {
this.removeCachedToken();
$.log(`账号[${this.index}] 缓存token失效,重新登录`);
}
}
if (!this.token) {
await this.loginByWxCode();
if (!this.token) return;
}
await this.getCampaignId();
await this.doSign();
this.saveCachedToken();
}
getCachedToken() {
const cache = readTokenCache();
return cache[this.openid] || null;
}
saveCachedToken() {
if (!this.token) return;
const cache = readTokenCache();
cache[this.openid] = {
token: this.token,
memberId: this.memberId,
customerNo: this.customerNo,
phone: this.phone,
levels: this.levels,
campaignId: this.campaignId,
updatedAt: new Date().toISOString(),
};
writeTokenCache(cache);
}
removeCachedToken() {
const cache = readTokenCache();
if (cache[this.openid]) {
delete cache[this.openid];
writeTokenCache(cache);
}
this.token = "";
this.memberId = "";
this.customerNo = "";
this.campaignId = "";
}
applyToken(data = {}) {
this.token = data.token || "";
this.memberId = data.memberId || data.id || "";
this.customerNo = data.customerNo || "";
this.phone = data.phone || "";
this.levels = data.levels || "";
this.campaignId = data.campaignId || "";
}
getHeaders(auth = false) {
const headers = {
"User-Agent": USER_AGENT,
"Referer": `https://servicewechat.com/${MINI_APP_ID}/${PAGE_VERSION}/page-frame.html`,
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
};
if (!auth) headers.token = this.token || "";
return headers;
}
async request(method, apiPath, data = {}, options = {}) {
const requestOptions = {
method,
url: `${API_BASE}/${apiPath}`,
headers: this.getHeaders(options.auth),
timeout: 20000,
validateStatus: () => true,
};
if (method === "GET") requestOptions.params = data;
else requestOptions.data = data;
const { status, data: result } = await axios.request(requestOptions);
if (status !== 200) throw new Error(`HTTP ${status}: ${JSON.stringify(result)}`);
if (!options.allowAnyCode && result?.resultCode !== "1") {
const err = new Error(result?.msg || JSON.stringify(result));
err.resultCode = result?.resultCode;
throw err;
}
return result;
}
async getLoginCode() {
const { data } = await wechat.getCode(this.openid);
const code = data?.code || data?.data?.code;
if (!code) throw new Error(`wx_server 未返回 code: ${JSON.stringify(data)}`);
return code;
}
async loginByWxCode() {
try {
const code = await this.getLoginCode();
const result = await this.request("GET", `estore/member/onLogin/${code}/${WECHAT_ID}`, {}, { auth: true });
const user = result.data || {};
this.applyToken({
token: user.token,
memberId: user.id,
customerNo: user.customerNo,
phone: user.phone,
levels: user.levels,
});
this.saveCachedToken();
$.log(`账号[${this.index}] 登录成功: ${this.levels || "会员"} ${this.customerNo || this.memberId || ""}`);
} catch (e) {
$.log(`账号[${this.index}] 登录失败: ${e.message || e}`);
}
}
async checkToken() {
try {
await this.getCampaignId(true);
return true;
} catch (e) {
return false;
}
}
async getCampaignId(silent = false) {
if (this.campaignId) return this.campaignId;
const result = await this.request("GET", "estore-campaign/campaign/info/get", {
campaignType: POINT_MALL_TYPE,
});
const campaignId = result.data?.detail?.campaignId || result.data?.campaignId || "";
if (!campaignId) throw new Error("未找到积分商城活动");
this.campaignId = campaignId;
this.saveCachedToken();
if (!silent) $.log(`账号[${this.index}] 积分商城活动: ${campaignId}`);
return campaignId;
}
async getSignInfo() {
if (!this.campaignId) await this.getCampaignId(true);
const result = await this.request("GET", "estore-campaign/campaign/pointsMall/assignment/sign", {
campaignId: this.campaignId,
});
return result.data || {};
}
async doSign() {
try {
const before = await this.getSignInfo();
const signDays = Array.isArray(before.signDays) ? before.signDays : [];
const todayStatus = signDays.find((item) => normalizeDate(item.signDay) === today());
const dailyTask = Array.isArray(before.assignDetail)
? before.assignDetail.find((item) => item.assignmentName && item.assignmentName.includes("单日"))
: null;
if (todayStatus?.signStatus === 1 || dailyTask?.completeStatus === 1) {
$.log(`账号[${this.index}] 今日已签到`);
return;
}
const sign = await this.request("POST", "estore-campaign/member/sign/do", {}, { allowAnyCode: true });
if (sign.resultCode !== "1") {
const message = sign.msg || JSON.stringify(sign);
if (/已签|重复/.test(message)) {
$.log(`账号[${this.index}] 今日已签到`);
return;
}
throw new Error(message);
}
const after = await this.getSignInfo();
const task = Array.isArray(after.assignDetail)
? after.assignDetail.find((item) => item.assignmentName && item.assignmentName.includes("单日"))
: null;
$.log(`账号[${this.index}] 签到成功: +${task?.awardPrice || "未知"}积分`);
} catch (e) {
const message = e.message || e;
$.log(`账号[${this.index}] 签到失败: ${message}`);
if (isTokenError(message)) this.removeCachedToken();
}
}
}
!(async () => {
$.checkEnv(ckName);
for (const openid of $.userList) {
await new Task(openid).run();
}
})()
.catch((e) => $.log(e.message || e))
.finally(() => $.done());