feat: 每日积分改以平台积分台账为准

- 新增 points-platform:读取 /ssp/#/my/point 背后的 rule/org 与 point/history/self
- 整门课完成不再直接 +3,等平台真的入账后按 nameType 归类计数
- 平台今日学习积分达到上限,或连续 3 门确有学习内容却未入账时收尾
- 接口不可用(未登录/跨域/网络)时退回本地计数,面板额外显示平台台账
- 新增离线回归测试与只读探测脚本

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
voidandClaude Sonnet 4.6 committed 2026-11-02 20:43:40 +08:00
1 parent f41fc72fe2
commit 18c5e5a3be
15 files changed
+1042 -141

No files matched your search

+276 -60
View File
@@ -29,7 +29,7 @@
* - 进入课程(列表刷课或自己点开)都会先对齐到「第一个未学完的课时」,即从第一章第一节起
* 顺序学习,不沿用平台上次停留的位置(否则前面的课时会被跳过,整门课永远学不完)。
* 切换成功与否以目录高亮为准:没切过去会重试并如实提示「未能切换」,不会假装已切换。
* - 不按门数设上限:每日积分模式下按课时类型累计,积分目标完成或没有可学内容时收尾。
* - 不按门数设上限:每日积分模式下整门课完成计 1 次,积分目标完成或没有可学内容时收尾。
* - 脚本不跳过普通课程:没有「跳过本课」,也不会把课程拉黑。某门课没学完(例如遇到
* 需要人工完成的滑块/拼图验证、答题/测验章节、长时间无计时进展)就退避重试:
* 30 秒起每次翻倍、最长 5 分钟,同一门课最多重试 CFG.maxCourseRetries 次,
@@ -231,18 +231,24 @@
function remaining(key) {
return Math.max(0, cap(key) - count(key));
}
function record(key, lessonId) {
function record(key, taskId) {
rollover();
if (!enabled) return false;
if (!ruleOf(key) || isFull(key)) return false;
if (lessonId) {
if (state2.seen[lessonId]) return false;
state2.seen[lessonId] = 1;
if (taskId) {
if (state2.seen[taskId]) return false;
state2.seen[taskId] = 1;
}
state2.counts[key] = count(key) + 1;
save();
return true;
}
function resetCounts() {
rollover();
for (const rule of POINT_RULES) state2.counts[rule.key] = 0;
state2.seen = {};
save();
}
function totalPoints() {
let total = 0;
for (const rule of POINT_RULES) {
@@ -303,7 +309,8 @@
snapshot,
setEnabled,
isEnabled,
rollover
rollover,
resetCounts
};
}
@@ -314,7 +321,7 @@
preferShortCourses: CFG.preferShortCourses,
// 列表刷课时优先后时长更短的课程(正序)
pointsEnabled: true
// 每日积分模式:按课时类型计分并封顶
// 每日积分模式:整门课完成计 1 次,按课时类型封顶
};
try {
state.autoPlayEnabled = localStorage.getItem(LS_ENABLED) !== "0";
@@ -560,6 +567,10 @@
// 每日积分模式:当前课时的类型缓存(课时 id → doc/video/audio/micro),
// 以及文档/音频/微课的单条库存(视频靠课程里的课时,不单独存)
lessonTypes: {},
coursePointCourseId: "",
// 「今天学过的类型」所属课程 id
coursePointType: "",
// 这门课今天学过的类型,整门课完成时计一次
pointsInventory: null,
pointsInventoryLoading: false,
pointsInventoryFailedAt: 0,
@@ -581,6 +592,8 @@
R.lastLessonKey = "";
R.doneLatchKey = "";
R.lessonTypes = {};
R.coursePointCourseId = "";
R.coursePointType = "";
R.lastSummary = null;
R.lastCountdown = null;
R.lastProgressAt = Date.now();
@@ -763,8 +776,8 @@
function mediaPage() {
return /^#\/(?:video|audio|ppt|document|article|micro|microcourse)\/play(?:[?/]|$)/i.test(location.hash || "");
}
function goToList(force) {
if (!force && Date.now() - R.lastGoListAt < 5e3) return;
function goToList(force2) {
if (!force2 && Date.now() - R.lastGoListAt < 5e3) return;
R.lastGoListAt = Date.now();
const url = S.catalogUrl || location.origin + "/kng/#/list";
if (route() === "list") {
@@ -779,6 +792,202 @@
}
}
// src/points-platform.js
var API = "https://api-phx-tc.yunxuetang.cn/ssp/";
var RULE_CODES = { doc: "SB1031", video: "SB1032", audio: "SB1033", micro: "SB1034" };
var NAME_TYPES = { 1006: "doc", 1007: "video", 1008: "audio", 1009: "micro" };
var CACHE_TTL = 2e4;
var TAGS_KEY = "yun-xue-tang-point-awards-v1";
var PENDING_TTL = 6 * 60 * 60 * 1e3;
var NO_AWARD_LIMIT = 3;
var cache = null;
var inflight = null;
var mode = "";
var completionsSinceAward = 0;
var lastLedgerCount = -1;
function pad22(n) {
return n < 10 ? "0" + n : "" + n;
}
function dayKey2(ms) {
const d = new Date(ms);
return d.getFullYear() + "-" + pad22(d.getMonth() + 1) + "-" + pad22(d.getDate());
}
function token() {
try {
return localStorage.getItem("token") || "";
} catch (e) {
return "";
}
}
function platformMode() {
return mode;
}
function cachedPlatformPoints() {
return cache;
}
function platformNoAward() {
return mode === "platform" && completionsSinceAward >= NO_AWARD_LIMIT;
}
function loadTags(day) {
try {
const o = JSON.parse(localStorage.getItem(TAGS_KEY) || "{}");
if (o && o.day === day) {
return {
day,
titles: o.titles && typeof o.titles === "object" ? o.titles : {},
pending: Array.isArray(o.pending) ? o.pending : [],
unknown: o.unknown && typeof o.unknown === "object" ? o.unknown : {}
};
}
} catch (e) {
}
return { day, titles: {}, pending: [], unknown: {} };
}
function saveTags(t) {
try {
localStorage.setItem(TAGS_KEY, JSON.stringify(t));
} catch (e) {
}
}
function rememberCourseAward(title, type) {
if (!title || !type) return;
const t = loadTags(dayKey2(Date.now()));
t.titles[norm(title)] = type;
t.pending.push({ type, at: Date.now(), title: norm(title) });
if (t.pending.length > 50) t.pending = t.pending.slice(-50);
saveTags(t);
}
function noteCourseCompleted() {
if (mode === "platform") completionsSinceAward++;
}
function applyLedger(data, tracker, opts = {}) {
if (!data || !data.ok) return 0;
const t = loadTags(data.day);
if (opts.reset) {
if (typeof tracker.resetCounts === "function") tracker.resetCounts();
t.unknown = {};
}
let added = 0;
for (const r of data.records || []) {
const id = "platform:" + r.id;
const key = norm(r.remark || "");
let type = NAME_TYPES[Number(r.nameType)] || "";
if (!type) type = key && t.titles[key] ? t.titles[key] : "";
if (type) {
const j = t.pending.findIndex((p) => p.title === key && p.type === type);
if (j >= 0) t.pending.splice(j, 1);
} else {
const i = t.pending.findIndex((p) => Date.now() - (p.at || 0) < PENDING_TTL);
if (i >= 0) {
type = t.pending[i].type;
t.pending.splice(i, 1);
}
}
if (type) {
if (tracker.record(type, id)) {
added++;
console.log("[云学堂助手] 积分 +" + r.point + "(平台入账):" + pointLabel(type) + " · " + tracker.summary());
}
} else if (!t.unknown[id]) {
t.unknown[id] = 1;
console.warn("[云学堂助手] 平台积分 +" + r.point + " 未能归类:" + (r.remark || r.id));
}
}
saveTags(t);
return added;
}
async function getJson(url, opts) {
const res = await fetch(url, opts);
if (!res.ok) throw new Error("HTTP " + res.status);
return res.json();
}
async function fetchPlatformPoints(now) {
const tk = token();
const at = now || Date.now();
if (!tk) return { ok: false, at, reason: "no-token" };
const day = dayKey2(at);
const headers = {
"Content-Type": "application/json;charset=UTF-8",
source: "501",
"x-yxt-product": "xxv2",
token: tk
};
const body = JSON.stringify({
startDate: day + " 00:00:00",
endDate: day + " 23:59:59",
sourceType: -1,
optionType: 0,
type: 1,
userId: ""
});
const results = await Promise.all([
getJson(API + "rule/org", { headers }),
getJson(API + "point/history/self?limit=200&offset=0", {
method: "POST",
headers,
body
})
]);
const rules = results[0];
const ledger = results[1];
const ruleMap = {};
for (const r of rules && rules.datas || []) {
if (!r || Number(r.enable) !== 1) continue;
for (const key of Object.keys(RULE_CODES)) {
if (r.code === RULE_CODES[key]) {
ruleMap[key] = {
cap: Math.max(0, Number(r.times) || 0),
point: Math.max(0, Number(r.point) || 0)
};
}
}
}
const records = (ledger && ledger.datas || []).filter((r) => r && r.id && Number(r.point) > 0 && Number(r.sourceType) === 1002);
const todayPoints = records.reduce((n, r) => n + (Number(r.point) || 0), 0);
const maxPoints = Object.keys(ruleMap).reduce((n, k) => n + ruleMap[k].cap * ruleMap[k].point, 0);
return {
ok: true,
at,
day,
rules: ruleMap,
records,
todayPoints,
maxPoints
};
}
function refreshPlatformPoints(tracker, opts = {}) {
if (!force(opts) && cache && Date.now() - cache.at < CACHE_TTL) {
return Promise.resolve(cache);
}
if (inflight) return inflight;
inflight = fetchPlatformPoints().then((data) => {
cache = data;
if (data.ok) {
if (mode !== "platform") {
mode = "platform";
applyLedger(data, tracker, { reset: true });
} else {
applyLedger(data, tracker);
}
if (data.records.length > lastLedgerCount) completionsSinceAward = 0;
lastLedgerCount = data.records.length;
} else if (!mode) {
mode = "local";
}
return data;
}).catch((e) => {
cache = { ok: false, at: Date.now(), reason: e && e.message ? e.message : String(e) };
if (!mode) mode = "local";
return cache;
}).finally(() => {
inflight = null;
});
return inflight;
}
function force(opts) {
return !!(opts && opts.force);
}
// src/login.js
var tick = function() {
};
@@ -1119,6 +1328,10 @@
console.log(TAG, text);
if (statusEl) statusEl.textContent = text;
}
function platformSuffix() {
const p = cachedPlatformPoints();
return p && p.ok ? " · 平台 " + p.todayPoints + "/" + p.maxPoints : "";
}
function updatePanel() {
if (cbBatch) cbBatch.checked = S.batchRunning;
if (statsEl) {
@@ -1139,7 +1352,7 @@
if (cbPoints) cbPoints.checked = state.pointsEnabled;
if (pointsEl) {
pointsEl.style.display = state.pointsEnabled ? "block" : "none";
pointsEl.textContent = state.pointsEnabled ? points.summary() : "";
pointsEl.textContent = state.pointsEnabled ? points.summary() + platformSuffix() : "";
}
const onLogin = isLoginPage();
if (loginBox) loginBox.style.display = onLogin ? "block" : "none";
@@ -1359,7 +1572,7 @@
cbPoints.type = "checkbox";
cbPoints.id = "yun-xue-tang-points-enabled";
cbPoints.checked = state.pointsEnabled;
cbPoints.title = "按课时类型计分:文档1 / 视频3 / 音频2 / 微课2,每天最多 24 分";
cbPoints.title = "整门课完成计 1 次:文档1 / 视频3 / 音频2 / 微课2,每天最多 24 分";
cbPoints.addEventListener("change", function() {
state.pointsEnabled = cbPoints.checked;
points.setEnabled(state.pointsEnabled);
@@ -2200,8 +2413,8 @@
}
function shortCatalogRequest(vm) {
const orgId = vm && vm.collegeId || localStorage.getItem("orgId") || "";
const token = localStorage.getItem("token") || "";
if (!token) throw new Error("未找到登录 token,可能尚未登录或登录已过期");
const token2 = localStorage.getItem("token") || "";
if (!token2) throw new Error("未找到登录 token,可能尚未登录或登录已过期");
const body = JSON.stringify({
collegeId: orgId,
catalogId: vm && vm.currentCatalogId || "",
@@ -2213,7 +2426,7 @@
const headers = {
"content-type": "application/json;charset=UTF-8",
accept: "application/json, text/plain, */*",
token,
token: token2,
"yxt-orgdomain": location.hostname,
source: "501"
};
@@ -2518,6 +2731,8 @@
}
function pointsDone() {
if (!state.pointsEnabled) return false;
const p = cachedPlatformPoints();
if (p && p.ok && p.maxPoints > 0 && p.todayPoints >= p.maxPoints) return true;
return points.done(pointsAvailability());
}
function pointsTarget() {
@@ -2528,36 +2743,6 @@
available: pointsAvailability()
});
}
function leaveForPoints(reason) {
if (S.activeCourse) {
S.activeCourse = null;
S.coursePhase = PHASE_IDLE;
}
S.awaitingListReturn = true;
saveRunState();
setStatus(reason);
console.log(TAG, reason);
goToList(true);
}
function pointsAfterLesson(type) {
if (!state.pointsEnabled) return false;
if (!R.pointsInventory) {
ensurePointsInventory();
return false;
}
if (pointsDone()) {
leaveForPoints("今日积分:可用内容已学完(" + points.summary() + ")");
return true;
}
if (type && points.isFull(type)) {
const target = pointsTarget();
if (target && target.type !== type) {
leaveForPoints("“" + pointLabel(type) + "”已满额,切换学习“" + pointLabel(target.type) + "”");
return true;
}
}
return false;
}
function startStandalone(item, type) {
if (!item) return false;
R.lastCardClickAt = Date.now();
@@ -2792,11 +2977,16 @@
}
R.retryStatusSec = -1;
if (state.pointsEnabled) {
refreshPlatformPoints(points);
const invStatus = ensurePointsInventory();
if (invStatus === "loading") return;
if (platformNoAward()) {
stopBatch("今日积分:平台连续 3 门课未入账,判定今日积分已满或没有可计分内容");
return;
}
if (pointsDone()) {
const text = points.summary();
console.log(TAG, "今日积分:可用内容已学完", text);
console.log(TAG, "今日积分:平台台账已达上限或可用内容已学完", text);
stopBatch("今日积分:可用内容已学完(" + text + ")");
return;
}
@@ -3186,17 +3376,38 @@
hash: location.hash
});
if (!type) return;
R.lessonType = type;
R.lessonTypes[pointLessonId(key)] = type;
}
function recordLessonPoint(key) {
function markLessonStudied(key) {
if (!state.pointsEnabled) return "";
const id = pointLessonId(key);
const type = R.lessonTypes[id] || R.lessonType || detectLessonType({ hash: location.hash });
const type = R.lessonTypes[id] || detectLessonType({ hash: location.hash });
if (!type) return "";
if (!points.record(type, id)) return "";
R.coursePointType = type;
return type;
}
function recordCoursePoint() {
if (!state.pointsEnabled) return "";
const key = R.waitLessonKey || R.lastLessonKey || "";
const type = R.coursePointType || R.lessonTypes[pointLessonId(key)] || "";
if (!type) return "";
if (platformMode() === "platform") {
const title = S.activeCourse && S.activeCourse.title || courseTitle() || "";
rememberCourseAward(title, type);
noteCourseCompleted();
refreshPlatformPoints(points, { force: true });
later(function() {
refreshPlatformPoints(points, { force: true });
}, 8e3);
later(function() {
refreshPlatformPoints(points, { force: true });
}, 25e3);
setStatus("整门课完成,等待平台记账:" + pointLabel(type));
return type;
}
if (!points.record(type, pointLessonId("course"))) return "";
const text = points.snapshot().text;
console.log(TAG, "积分 +3:" + pointLabel(type) + "(" + text + ")");
console.log(TAG, "积分 +3:" + pointLabel(type) + "(整门课完成 · " + text + ")");
setStatus("积分 +3:" + pointLabel(type) + "(" + text + ")");
return type;
}
@@ -3235,6 +3446,11 @@
}
return;
}
const pointCourseKey = curId || urlId || "";
if (R.coursePointCourseId !== pointCourseKey) {
R.coursePointCourseId = pointCourseKey;
R.coursePointType = "";
}
if (S.coursePhase !== PHASE_LEARNING) {
S.coursePhase = PHASE_LEARNING;
saveRunState();
@@ -3262,7 +3478,7 @@
const confirmed = !!(R.lastSummary && R.lastSummary.complete);
R.lastSummary = summary;
if (confirmed) {
recordLessonPoint(R.waitLessonKey || R.lastLessonKey);
recordCoursePoint();
completeCurrent();
return;
}
@@ -3274,8 +3490,7 @@
const prevKey = R.lastLessonKey;
const curKey = readActiveLessonKey();
R.lastSummary = summary;
const pointType = !isMedia && summary.kind !== "percent" ? recordLessonPoint(prevKey) : "";
if (pointType && pointsAfterLesson(pointType)) return;
if (!isMedia && summary.kind !== "percent") markLessonStudied(prevKey);
if (!isMedia && summary.kind !== "percent" && (!prevKey || curKey === prevKey)) {
setStatus("本节已记录,切换下一节…");
advanceLesson();
@@ -3353,7 +3568,7 @@
}
R.waitStart = 0;
R.lastProgressAt = Date.now();
recordLessonPoint(R.waitLessonKey || R.lastLessonKey);
recordCoursePoint();
completeCurrent();
return;
}
@@ -3362,18 +3577,17 @@
R.lastLessonKey = newKey;
R.doneLatchKey = "";
R.lastProgressAt = Date.now();
const pointType = recordLessonPoint(R.waitLessonKey);
if (pointType && pointsAfterLesson(pointType)) return;
markLessonStudied(R.waitLessonKey);
setStatus("已自动进入下一节");
return;
}
if (!isMedia && s2 && s2.x > R.waitSummaryX) {
R.waitStart = 0;
R.lastProgressAt = Date.now();
const pointType = recordLessonPoint(R.waitLessonKey || R.lastLessonKey);
markLessonStudied(R.waitLessonKey || R.lastLessonKey);
if (s2.complete) {
completeCurrent();
} else if (!pointType || !pointsAfterLesson(pointType)) {
} else {
advanceLesson();
}
return;
@@ -3445,7 +3659,7 @@
}
const s = readSummary();
if (s && s.complete) {
recordLessonPoint(R.waitLessonKey || R.lastLessonKey);
recordCoursePoint();
completeCurrent();
} else {
setStatus("没有找到下一节,等待课程完成…");
@@ -3593,6 +3807,9 @@
save: saveRunState,
flush: flushRunState,
route,
// 平台积分台账:调试时可手动刷新(见 dev/tests/points-api-probe.mjs)
refreshPlatformPoints,
cachedPlatformPoints,
// CFG 暴露给无头运行器:脚本内可调参数(重试次数、冷却等)
CFG,
mediaPage,
@@ -3639,7 +3856,6 @@
pointsAvailability,
pointsDone,
pointsTarget,
pointsAfterLesson,
startStandalone,
listTick,
courseTick,