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:
1 parent
f41fc72fe2
commit
18c5e5a3be
15 files changed
+1042
-141
No files matched your search
+52
-19
@@ -2,7 +2,9 @@
|
||||
import { CFG, TAG } from './config.js';
|
||||
import { R, S, state, points, saveRunState, PHASE_LEARNING } from './state.js';
|
||||
import { detectLessonType, pointLabel } from './points.js';
|
||||
import { pointsAfterLesson } from './list.js';
|
||||
import {
|
||||
platformMode, rememberCourseAward, noteCourseCompleted, refreshPlatformPoints,
|
||||
} from './points-platform.js';
|
||||
import { goToList, mediaPage } from './route.js';
|
||||
import { courseIdFromUrl, norm, textOf, truncate, findVisibleClickable } from './utils.js';
|
||||
import { later } from './lifecycle.js';
|
||||
@@ -17,7 +19,9 @@ import {
|
||||
import { examTick, examPhase } from './exam.js';
|
||||
import { completeCurrent, failCurrent } from './complete.js';
|
||||
|
||||
// ---- 每日积分:把「课时完成」折算成一次任务 ----
|
||||
// ---- 每日积分:整门课完成才计 1 次 ----
|
||||
// 多课时课程按「整门课」算一次任务,不按节数累计:
|
||||
// 逐节只记住今天学过的类型,等整门课(含考试)完成时才计分。
|
||||
// 类型在播放期间就按 lessonId 缓存下来:平台记账或自动切节后页面可能已经
|
||||
// 跳到下一节,事后再读目录就不知道刚完成的是哪一类了。
|
||||
function pointLessonId(key) {
|
||||
@@ -34,19 +38,44 @@ function rememberLessonType(key) {
|
||||
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;
|
||||
}
|
||||
|
||||
// 整门课完成 → 计 1 次。同一门课当天只计一次(去重键 courseId|course)。
|
||||
// 平台台账可用时以平台为准:先记下课程类型,等平台真的入账后再计入。
|
||||
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 }); }, 8000);
|
||||
later(function () { refreshPlatformPoints(points, { force: true }); }, 25000);
|
||||
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;
|
||||
}
|
||||
@@ -86,6 +115,13 @@ export function courseTick() {
|
||||
return;
|
||||
}
|
||||
|
||||
// 换了一门课就清掉「今天学过什么类型」,整门课完成时只按本课的类型计分
|
||||
const pointCourseKey = curId || urlId || '';
|
||||
if (R.coursePointCourseId !== pointCourseKey) {
|
||||
R.coursePointCourseId = pointCourseKey;
|
||||
R.coursePointType = '';
|
||||
}
|
||||
|
||||
if (S.coursePhase !== PHASE_LEARNING) {
|
||||
S.coursePhase = PHASE_LEARNING;
|
||||
saveRunState();
|
||||
@@ -133,7 +169,7 @@ export function courseTick() {
|
||||
const confirmed = !!(R.lastSummary && R.lastSummary.complete);
|
||||
R.lastSummary = summary;
|
||||
if (confirmed) {
|
||||
recordLessonPoint(R.waitLessonKey || R.lastLessonKey);
|
||||
recordCoursePoint();
|
||||
completeCurrent();
|
||||
return;
|
||||
}
|
||||
@@ -147,10 +183,8 @@ export function courseTick() {
|
||||
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)) {
|
||||
@@ -253,7 +287,7 @@ export function courseTick() {
|
||||
}
|
||||
R.waitStart = 0;
|
||||
R.lastProgressAt = Date.now();
|
||||
recordLessonPoint(R.waitLessonKey || R.lastLessonKey);
|
||||
recordCoursePoint();
|
||||
completeCurrent();
|
||||
return;
|
||||
}
|
||||
@@ -264,8 +298,7 @@ export function courseTick() {
|
||||
R.lastLessonKey = newKey;
|
||||
R.doneLatchKey = '';
|
||||
R.lastProgressAt = Date.now();
|
||||
const pointType = recordLessonPoint(R.waitLessonKey);
|
||||
if (pointType && pointsAfterLesson(pointType)) return;
|
||||
markLessonStudied(R.waitLessonKey);
|
||||
setStatus('已自动进入下一节');
|
||||
return;
|
||||
}
|
||||
@@ -274,10 +307,10 @@ export function courseTick() {
|
||||
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;
|
||||
@@ -352,7 +385,7 @@ export function advanceLesson() {
|
||||
|
||||
const s = readSummary();
|
||||
if (s && s.complete) {
|
||||
recordLessonPoint(R.waitLessonKey || R.lastLessonKey);
|
||||
recordCoursePoint();
|
||||
completeCurrent();
|
||||
} else {
|
||||
setStatus('没有找到下一节,等待课程完成…');
|
||||
|
||||
+5
-2
@@ -12,7 +12,7 @@ import {
|
||||
chooseShortCourse, sortCardsByDuration, formatDuration,
|
||||
pendingShortItems, scanCards, currentPage, maxPageNumber,
|
||||
startCourse, jumpToPage, nextPage,
|
||||
ensurePointsInventory, pointsAvailability, pointsDone, pointsTarget, pointsAfterLesson, startStandalone,
|
||||
ensurePointsInventory, pointsAvailability, pointsDone, pointsTarget, startStandalone,
|
||||
} from './list.js';
|
||||
import { courseTick, manualCourseTick } from './course.js';
|
||||
import {
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
completeCurrent, failCurrent, skipCurrent, courseSkipped,
|
||||
} from './complete.js';
|
||||
import { examTick, examPhase } from './exam.js';
|
||||
import { refreshPlatformPoints, cachedPlatformPoints } from './points-platform.js';
|
||||
import {
|
||||
hookAllVideos, readSummary, readCountdown, lessonItems,
|
||||
findNextLessonElement, clickLesson, alignToFirstIncomplete,
|
||||
@@ -78,6 +79,8 @@ try {
|
||||
Object.defineProperty(window, '__yunXueTangAuto', {
|
||||
value: {
|
||||
S, R, state, points, tick, save: saveRunState, flush: flushRunState, route,
|
||||
// 平台积分台账:调试时可手动刷新(见 dev/tests/points-api-probe.mjs)
|
||||
refreshPlatformPoints, cachedPlatformPoints,
|
||||
// CFG 暴露给无头运行器:脚本内可调参数(重试次数、冷却等)
|
||||
CFG,
|
||||
mediaPage, courseIdFromUrl,
|
||||
@@ -100,7 +103,7 @@ try {
|
||||
// 以下供开发/真机测试驱动内部路径使用:
|
||||
scanCards, currentPage, maxPageNumber, startCourse, jumpToPage, nextPage,
|
||||
ensurePointsInventory, pointsAvailability, pointsDone, pointsTarget,
|
||||
pointsAfterLesson, startStandalone,
|
||||
startStandalone,
|
||||
listTick, courseTick, manualCourseTick,
|
||||
loginFields, findLoginVm, checkLoginAgreement, setNativeValue,
|
||||
saveCreds, loadCreds, clearSavedCreds,
|
||||
|
||||
+11
-33
@@ -7,7 +7,7 @@ import {
|
||||
invalidateShortCatalog, PHASE_ENTERING, PHASE_LEARNING, PHASE_IDLE,
|
||||
} from './state.js';
|
||||
import { apiTypeKey, detectLessonType, nextTarget, pointLabel, POINT_RULES } from './points.js';
|
||||
import { goToList } from './route.js';
|
||||
import { cachedPlatformPoints, platformNoAward, refreshPlatformPoints } from './points-platform.js';
|
||||
import { norm, textOf, truncate } from './utils.js';
|
||||
import { clickNode } from './player.js';
|
||||
import { setStatus, updatePanel } from './panel.js';
|
||||
@@ -532,6 +532,9 @@ export function pointsAvailability(videoAvailable) {
|
||||
|
||||
export 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());
|
||||
}
|
||||
|
||||
@@ -542,37 +545,6 @@ export function pointsTarget() {
|
||||
});
|
||||
}
|
||||
|
||||
// 从当前课程页回列表,交给 listTick 停止或换下一类。
|
||||
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);
|
||||
}
|
||||
|
||||
// 本类刚满额 / 今日任务已无内容时离开课程页。
|
||||
// 返回 true = 已经发起跳转,调用方不要再切下一节。
|
||||
export 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;
|
||||
}
|
||||
|
||||
// 进入单条文档/音频/微课:直接按 kngId 打开对应播放页
|
||||
export function startStandalone(item, type) {
|
||||
@@ -853,11 +825,17 @@ export function listTick() {
|
||||
// 每日积分模式:先补文档/音频/微课的单条内容;视频额度靠课程里的视频课时。
|
||||
// 四类都满额(或剩余类型确认没内容)就收尾,不再无意义地刷课。
|
||||
if (state.pointsEnabled) {
|
||||
// 每次到列表页都顺带刷新平台台账(内部有 20 秒缓存,不会刷爆接口)
|
||||
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;
|
||||
}
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@
|
||||
* - 进入课程(列表刷课或自己点开)都会先对齐到「第一个未学完的课时」,即从第一章第一节起
|
||||
* 顺序学习,不沿用平台上次停留的位置(否则前面的课时会被跳过,整门课永远学不完)。
|
||||
* 切换成功与否以目录高亮为准:没切过去会重试并如实提示「未能切换」,不会假装已切换。
|
||||
* - 不按门数设上限:每日积分模式下按课时类型累计,积分目标完成或没有可学内容时收尾。
|
||||
* - 不按门数设上限:每日积分模式下整门课完成计 1 次,积分目标完成或没有可学内容时收尾。
|
||||
* - 脚本不跳过普通课程:没有「跳过本课」,也不会把课程拉黑。某门课没学完(例如遇到
|
||||
* 需要人工完成的滑块/拼图验证、答题/测验章节、长时间无计时进展)就退避重试:
|
||||
* 30 秒起每次翻倍、最长 5 分钟,同一门课最多重试 CFG.maxCourseRetries 次,
|
||||
|
||||
+9
-2
@@ -2,6 +2,7 @@
|
||||
import { VERSION, LS_ENABLED, LS_PREFER_SHORT, SS_SHORT_CATALOG, PANEL_ID, TAG } from './config.js';
|
||||
import { R, S, state, points, saveRunState } from './state.js';
|
||||
import { LS_POINTS_MODE } from './points.js';
|
||||
import { cachedPlatformPoints } from './points-platform.js';
|
||||
import { route } from './route.js';
|
||||
import { isLoginPage, startBatch, doLoginAndStart, clearSavedCreds } from './login.js';
|
||||
|
||||
@@ -44,6 +45,12 @@ export function setStatus(text) {
|
||||
if (statusEl) statusEl.textContent = text;
|
||||
}
|
||||
|
||||
// 面板上再带一份平台台账的真实今日积分,避免本地未归类的记录让数字偏小
|
||||
function platformSuffix() {
|
||||
const p = cachedPlatformPoints();
|
||||
return p && p.ok ? ' · 平台 ' + p.todayPoints + '/' + p.maxPoints : '';
|
||||
}
|
||||
|
||||
export function updatePanel() {
|
||||
if (cbBatch) cbBatch.checked = S.batchRunning;
|
||||
if (statsEl) {
|
||||
@@ -64,7 +71,7 @@ export function updatePanel() {
|
||||
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();
|
||||
@@ -271,7 +278,7 @@ export function buildPanel() {
|
||||
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);
|
||||
|
||||
@@ -0,0 +1,255 @@
|
||||
// ======== 平台积分台账 ========
|
||||
// 「我的积分」页 /ssp/#/my/point 背后的接口才是最直接的证据:
|
||||
// GET /ssp/rule/org 各积分规则的每日上限(文档/视频/音频/微课)
|
||||
// POST /ssp/point/history/self 今日积分明细
|
||||
// 脚本不再假设「学完一门就该 +3」,只把平台真实入账的记录折算成积分。
|
||||
//
|
||||
// 鉴权:接口认 localStorage.token(登录后由平台写入),不用 cookie。
|
||||
// 接口不可用(未登录 / 跨域 / 网络)时返回 ok:false,调用方退回本地计数。
|
||||
import { norm } from './utils.js';
|
||||
import { pointLabel } from './points.js';
|
||||
|
||||
const API = 'https://api-phx-tc.yunxuetang.cn/ssp/';
|
||||
const RULE_CODES = { doc: 'SB1031', video: 'SB1032', audio: 'SB1033', micro: 'SB1034' };
|
||||
// 台账里的 nameType 就是平台自己的课件类型:1006 文档 / 1007 视频 / 1008 音频 / 1009 微课
|
||||
const NAME_TYPES = { 1006: 'doc', 1007: 'video', 1008: 'audio', 1009: 'micro' };
|
||||
const CACHE_TTL = 20000;
|
||||
const TAGS_KEY = 'yun-xue-tang-point-awards-v1';
|
||||
const PENDING_TTL = 6 * 60 * 60 * 1000;
|
||||
const NO_AWARD_LIMIT = 3;
|
||||
|
||||
let cache = null;
|
||||
let inflight = null;
|
||||
let mode = ''; // '' | 'platform' | 'local':首次刷新成功与否决定本次会话的计数方式
|
||||
let completionsSinceAward = 0;
|
||||
let lastLedgerCount = -1;
|
||||
|
||||
function pad2(n) {
|
||||
return n < 10 ? '0' + n : '' + n;
|
||||
}
|
||||
|
||||
function dayKey(ms) {
|
||||
const d = new Date(ms);
|
||||
return d.getFullYear() + '-' + pad2(d.getMonth() + 1) + '-' + pad2(d.getDate());
|
||||
}
|
||||
|
||||
function token() {
|
||||
try { return localStorage.getItem('token') || ''; } catch (e) { return ''; }
|
||||
}
|
||||
|
||||
export function platformAvailable() {
|
||||
return !!token();
|
||||
}
|
||||
|
||||
/** 首次刷新是否已经确定计数方式;未确定前调用方按本地模式处理 */
|
||||
export function platformMode() {
|
||||
return mode;
|
||||
}
|
||||
|
||||
export function cachedPlatformPoints() {
|
||||
return cache;
|
||||
}
|
||||
|
||||
/** 平台连续多少门课没有新增入账记录(用于判断「积分其实已满」) */
|
||||
export 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: 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) { /* ignore */ }
|
||||
return { day: day, titles: {}, pending: [], unknown: {} };
|
||||
}
|
||||
|
||||
function saveTags(t) {
|
||||
try { localStorage.setItem(TAGS_KEY, JSON.stringify(t)); } catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
/** 整门课完成时记下「这门课算哪一类」,等平台台账入账后据此归类 */
|
||||
export function rememberCourseAward(title, type) {
|
||||
if (!title || !type) return;
|
||||
const t = loadTags(dayKey(Date.now()));
|
||||
t.titles[norm(title)] = type;
|
||||
t.pending.push({ type: type, at: Date.now(), title: norm(title) });
|
||||
if (t.pending.length > 50) t.pending = t.pending.slice(-50);
|
||||
saveTags(t);
|
||||
}
|
||||
|
||||
/** 一门课完成了(平台模式下用来判断「连续 N 门没有入账」) */
|
||||
export function noteCourseCompleted() {
|
||||
if (mode === 'platform') completionsSinceAward++;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把平台台账折算进本地计数器。纯逻辑,方便离线测试。
|
||||
* @returns {number} 本次新增计入的条数
|
||||
*/
|
||||
export 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 || '');
|
||||
// 优先用平台台账自带的 nameType,它比标题/待对账记录都可靠
|
||||
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();
|
||||
}
|
||||
|
||||
export async function fetchPlatformPoints(now) {
|
||||
const tk = token();
|
||||
const at = now || Date.now();
|
||||
if (!tk) return { ok: false, at: at, reason: 'no-token' };
|
||||
const day = dayKey(at);
|
||||
// 平台网关要求 source 头(501 = PC 端);缺了会返回 400「请求头 source 为空」
|
||||
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: headers }),
|
||||
getJson(API + 'point/history/self?limit=200&offset=0', {
|
||||
method: 'POST', headers: headers, body: 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),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sourceType 1002 = 在线学习;登录奖励等其它来源不算每日学习积分
|
||||
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: at,
|
||||
day: day,
|
||||
rules: ruleMap,
|
||||
records: records,
|
||||
todayPoints: todayPoints,
|
||||
maxPoints: maxPoints,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新平台台账并折算进计数器。并发调用共用一个请求;失败不抛错。
|
||||
* @param {object} tracker points.js 的 tracker(需要 record / resetCounts)
|
||||
* @param {{force?:boolean}} [opts]
|
||||
*/
|
||||
export 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);
|
||||
}
|
||||
|
||||
/** 某个类型在平台规则里是否启用/上限多少;没有平台数据时返回 null */
|
||||
export function platformRule(key) {
|
||||
if (!cache || !cache.ok) return null;
|
||||
return cache.rules[key] || null;
|
||||
}
|
||||
|
||||
/** 全部启用规则加起来的每日积分上限;没有平台数据时返回 0 */
|
||||
export function platformMaxPoints() {
|
||||
if (!cache || !cache.ok) return 0;
|
||||
return cache.maxPoints;
|
||||
}
|
||||
+20
-10
@@ -2,11 +2,12 @@
|
||||
// 平台按「学习完成」发积分,四类任务各有每日上限:
|
||||
// 文档 1 次 + 视频 3 次 + 音频 2 次 + 微课 2 次,每次 3 分,单日最多 24 分。
|
||||
//
|
||||
// 这里的口径是「完成一个课时算一次」,不是「完成一整门课」:
|
||||
// · 多课时课程里每学完一节视频/文档/音频/微课,各计一次;
|
||||
// 这里的口径是「整门课完成算一次」,不是「完成一节课时就算一次」:
|
||||
// · 多课时课程要所有课时(含考试)都完成后才计一次,按今天实际学过的课时类型归类;
|
||||
// · 单视频/音频/课件播放页学完即计一次;
|
||||
// · 只剩考试、今天没学内容的课程不计分;
|
||||
// · 类型识别不出来的课时不猜、不计分;
|
||||
// · 同一天同一课时重复上报(刷新、重进、平台重发)只计一次。
|
||||
// · 同一天同一门课重复上报(刷新、重进、平台重发)只计一次。
|
||||
//
|
||||
// 计数落 localStorage 并带日期,跨刷新/运行器重启不丢,跨天自动清零。
|
||||
// 纯数据逻辑,不碰 DOM,方便脱离浏览器直接测。
|
||||
@@ -183,24 +184,32 @@ export function createPointsTracker(opts = {}) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 记一次课时完成。
|
||||
* @param {string} key detectLessonType 的返回值
|
||||
* @param {string} [lessonId] 当天去重键(建议 courseId|lessonKey);空串表示不去重
|
||||
* 记一次任务完成(整门课或单条内容)。
|
||||
* @param {string} key detectLessonType 的返回值
|
||||
* @param {string} [taskId] 当天去重键(建议 courseId|course / kngId|media);空串表示不去重
|
||||
* @returns {boolean} 是否真的计入了
|
||||
*/
|
||||
function record(key, lessonId) {
|
||||
function record(key, taskId) {
|
||||
rollover();
|
||||
if (!enabled) return false;
|
||||
if (!ruleOf(key) || isFull(key)) return false;
|
||||
if (lessonId) {
|
||||
if (state.seen[lessonId]) return false;
|
||||
state.seen[lessonId] = 1;
|
||||
if (taskId) {
|
||||
if (state.seen[taskId]) return false;
|
||||
state.seen[taskId] = 1;
|
||||
}
|
||||
state.counts[key] = count(key) + 1;
|
||||
save();
|
||||
return true;
|
||||
}
|
||||
|
||||
/** 切换到平台台账口径时清零并重建 */
|
||||
function resetCounts() {
|
||||
rollover();
|
||||
for (const rule of POINT_RULES) state.counts[rule.key] = 0;
|
||||
state.seen = {};
|
||||
save();
|
||||
}
|
||||
|
||||
function totalPoints() {
|
||||
let total = 0;
|
||||
for (const rule of POINT_RULES) {
|
||||
@@ -263,5 +272,6 @@ export function createPointsTracker(opts = {}) {
|
||||
return {
|
||||
record, count, cap, isFull, remaining, totalPoints, maxPoints,
|
||||
allFull, done, progress, summary, snapshot, setEnabled, isEnabled, rollover,
|
||||
resetCounts,
|
||||
};
|
||||
}
|
||||
+5
-1
@@ -19,7 +19,7 @@ import { createPointsTracker, LS_POINTS_MODE } from './points.js';
|
||||
export const state = {
|
||||
autoPlayEnabled: true, // 当前课程自动连播(自己点开的课)
|
||||
preferShortCourses: CFG.preferShortCourses, // 列表刷课时优先后时长更短的课程(正序)
|
||||
pointsEnabled: true, // 每日积分模式:按课时类型计分并封顶
|
||||
pointsEnabled: true, // 每日积分模式:整门课完成计 1 次,按课时类型封顶
|
||||
};
|
||||
try {
|
||||
state.autoPlayEnabled = localStorage.getItem(LS_ENABLED) !== '0';
|
||||
@@ -259,6 +259,8 @@ export const R = {
|
||||
// 每日积分模式:当前课时的类型缓存(课时 id → doc/video/audio/micro),
|
||||
// 以及文档/音频/微课的单条库存(视频靠课程里的课时,不单独存)
|
||||
lessonTypes: {},
|
||||
coursePointCourseId: '', // 「今天学过的类型」所属课程 id
|
||||
coursePointType: '', // 这门课今天学过的类型,整门课完成时计一次
|
||||
pointsInventory: null,
|
||||
pointsInventoryLoading: false,
|
||||
pointsInventoryFailedAt: 0,
|
||||
@@ -282,6 +284,8 @@ export function resetCourseRuntime() {
|
||||
R.lastLessonKey = '';
|
||||
R.doneLatchKey = '';
|
||||
R.lessonTypes = {};
|
||||
R.coursePointCourseId = '';
|
||||
R.coursePointType = '';
|
||||
R.lastSummary = null;
|
||||
R.lastCountdown = null;
|
||||
R.lastProgressAt = Date.now();
|
||||
|
||||
Reference in new issue
Block a user