159 lines
3.9 KiB
JavaScript
159 lines
3.9 KiB
JavaScript
const { sleep } = require('../helpers/automator');
|
|
|
|
class CourseListPage {
|
|
/**
|
|
* @param {object} miniProgram - miniprogram-automator 实例
|
|
*/
|
|
constructor(miniProgram) {
|
|
this.mp = miniProgram;
|
|
}
|
|
|
|
/**
|
|
* 等待课程列表页加载完成
|
|
*/
|
|
async waitForLoad() {
|
|
const page = await this.mp.currentPage();
|
|
await page.waitFor('.course-list-page', { timeout: 10000 });
|
|
await sleep(1500); // 等待数据加载
|
|
return page;
|
|
}
|
|
|
|
/**
|
|
* 获取课程列表
|
|
* @returns {Promise<Array<{name: string, status: string, location: string}>>}
|
|
*/
|
|
async getCourseList() {
|
|
const page = await this.mp.currentPage();
|
|
const cards = await page.$$('.course-card');
|
|
const courses = [];
|
|
|
|
for (const card of cards) {
|
|
try {
|
|
const nameEl = await card.$('.course-name');
|
|
const statusEl = await card.$('.status-tag text');
|
|
const name = nameEl ? (await nameEl.text()).trim() : '';
|
|
const status = statusEl ? (await statusEl.text()).trim() : '';
|
|
courses.push({ name, status });
|
|
} catch (e) {
|
|
// 跳过无法解析的卡片
|
|
}
|
|
}
|
|
|
|
return courses;
|
|
}
|
|
|
|
/**
|
|
* 获取课程卡片数量
|
|
*/
|
|
async getCourseCount() {
|
|
const page = await this.mp.currentPage();
|
|
try {
|
|
const cards = await page.$$('.course-card');
|
|
return cards.length;
|
|
} catch (e) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 按状态筛选课程
|
|
* @param {string} status - 'all' | 'pending' | 'in_progress' | 'ended'
|
|
*/
|
|
async filterByStatus(status) {
|
|
const page = await this.mp.currentPage();
|
|
|
|
const tabValueMap = {
|
|
all: 0,
|
|
pending: 1,
|
|
in_progress: 2,
|
|
ended: 3
|
|
};
|
|
|
|
const tabIndex = tabValueMap[status];
|
|
if (tabIndex === undefined) {
|
|
throw new Error(`未知的筛选状态: ${status}`);
|
|
}
|
|
|
|
const tabs = await page.$$('.tab-item');
|
|
if (tabs.length > tabIndex) {
|
|
await tabs[tabIndex].tap();
|
|
await sleep(1500); // 等待筛选结果渲染
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 点击指定索引的课程卡片
|
|
* @param {number} index - 课程卡片索引(从0开始)
|
|
*/
|
|
async clickCourse(index = 0) {
|
|
const page = await this.mp.currentPage();
|
|
const cards = await page.$$('.course-card');
|
|
if (cards.length <= index) {
|
|
throw new Error(`课程索引 ${index} 超出范围,当前共 ${cards.length} 个课程`);
|
|
}
|
|
await cards[index].tap();
|
|
await sleep(2000); // 等待课程详情页加载
|
|
return this.mp.currentPage();
|
|
}
|
|
|
|
/**
|
|
* 验证某个课程是否有学员(人数 > 0)
|
|
* @param {number} index - 课程卡片索引
|
|
*/
|
|
async verifyCourseHasStudents(index = 0) {
|
|
const page = await this.mp.currentPage();
|
|
const cards = await page.$$('.course-card');
|
|
if (cards.length <= index) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const infoRows = await cards[index].$$('.info-row');
|
|
// 最后一行为 "人数 X / Y" 信息
|
|
if (infoRows.length > 0) {
|
|
const lastRow = infoRows[infoRows.length - 1];
|
|
const valueEl = await lastRow.$('.info-value');
|
|
if (valueEl) {
|
|
const text = (await valueEl.text()).trim();
|
|
const match = text.match(/^(\d+)\s*\/\s*\d+$/);
|
|
if (match) {
|
|
return parseInt(match[1], 10) > 0;
|
|
}
|
|
}
|
|
}
|
|
} catch (e) {
|
|
// 忽略
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 检查是否显示"暂无课程"的空状态
|
|
*/
|
|
async isEmpty() {
|
|
const page = await this.mp.currentPage();
|
|
try {
|
|
const emptyEl = await page.$('.empty-wrap');
|
|
return !!emptyEl;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查是否仍在加载中
|
|
*/
|
|
async isLoading() {
|
|
const page = await this.mp.currentPage();
|
|
try {
|
|
const loadingEl = await page.$('.loading-wrap');
|
|
return !!loadingEl;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = CourseListPage;
|