题库导入功能完成,考试计划功能完成。
This commit is contained in:
@@ -40,9 +40,27 @@ export interface TaskReport {
|
||||
}>;
|
||||
}
|
||||
|
||||
export interface ActiveTaskStat {
|
||||
taskId: string;
|
||||
taskName: string;
|
||||
subjectName: string;
|
||||
totalUsers: number;
|
||||
completedUsers: number;
|
||||
completionRate: number;
|
||||
passRate: number;
|
||||
excellentRate: number;
|
||||
startAt: string;
|
||||
endAt: string;
|
||||
}
|
||||
|
||||
export class ExamTaskModel {
|
||||
static async findAll(): Promise<TaskWithSubject[]> {
|
||||
const sql = `
|
||||
static async findAll(): Promise<(TaskWithSubject & {
|
||||
completedUsers: number;
|
||||
passRate: number;
|
||||
excellentRate: number;
|
||||
})[]> {
|
||||
// 1. 先获取所有任务的基本信息
|
||||
const baseTasks = await all(`
|
||||
SELECT
|
||||
t.id,
|
||||
t.name,
|
||||
@@ -51,14 +69,112 @@ export class ExamTaskModel {
|
||||
t.end_at as endAt,
|
||||
t.created_at as createdAt,
|
||||
s.name as subjectName,
|
||||
COUNT(DISTINCT etu.user_id) as userCount
|
||||
COUNT(DISTINCT etu.user_id) as userCount,
|
||||
s.total_score as totalScore
|
||||
FROM exam_tasks t
|
||||
JOIN exam_subjects s ON t.subject_id = s.id
|
||||
LEFT JOIN exam_task_users etu ON t.id = etu.task_id
|
||||
GROUP BY t.id
|
||||
ORDER BY t.created_at DESC
|
||||
`;
|
||||
return query(sql);
|
||||
`);
|
||||
|
||||
// 2. 为每个任务计算完成人数、合格率和优秀率
|
||||
const tasksWithStats: any[] = [];
|
||||
for (const task of baseTasks) {
|
||||
// 获取该任务的详细报表数据
|
||||
const report = await this.getReport(task.id);
|
||||
|
||||
// 计算合格率(得分率60%以上)
|
||||
const passingUsers = report.details.filter((d: any) => {
|
||||
if (d.score === null) return false;
|
||||
return (d.score / task.totalScore) >= 0.6;
|
||||
}).length;
|
||||
|
||||
const passRate = report.totalUsers > 0
|
||||
? Math.round((passingUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
// 计算优秀率(得分率80%以上)
|
||||
const excellentUsers = report.details.filter((d: any) => {
|
||||
if (d.score === null) return false;
|
||||
return (d.score / task.totalScore) >= 0.8;
|
||||
}).length;
|
||||
|
||||
const excellentRate = report.totalUsers > 0
|
||||
? Math.round((excellentUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
tasksWithStats.push({
|
||||
...task,
|
||||
completedUsers: report.completedUsers,
|
||||
passRate,
|
||||
excellentRate
|
||||
});
|
||||
}
|
||||
|
||||
return tasksWithStats;
|
||||
}
|
||||
|
||||
static async getActiveTasksWithStats(): Promise<ActiveTaskStat[]> {
|
||||
const now = new Date().toISOString();
|
||||
|
||||
// 1. 获取当前时间有效的任务,包括开始和结束时间
|
||||
const activeTasks = await all(`
|
||||
SELECT
|
||||
t.id, t.name as taskName, s.name as subjectName, s.total_score as totalScore,
|
||||
t.start_at as startAt, t.end_at as endAt
|
||||
FROM exam_tasks t
|
||||
JOIN exam_subjects s ON t.subject_id = s.id
|
||||
WHERE t.start_at <= ? AND t.end_at >= ?
|
||||
ORDER BY t.created_at DESC
|
||||
`, [now, now]);
|
||||
|
||||
const stats: ActiveTaskStat[] = [];
|
||||
|
||||
for (const task of activeTasks) {
|
||||
// 2. 获取每个任务的详细报告数据
|
||||
const report = await this.getReport(task.id);
|
||||
|
||||
// 3. 计算完成率
|
||||
const completionRate = report.totalUsers > 0
|
||||
? Math.round((report.completedUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
// 4. 计算合格率(得分率60%以上)
|
||||
const passingUsers = report.details.filter(d => {
|
||||
if (d.score === null) return false;
|
||||
return (d.score / task.totalScore) >= 0.6;
|
||||
}).length;
|
||||
|
||||
const passRate = report.totalUsers > 0
|
||||
? Math.round((passingUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
// 5. 计算优秀率(得分率80%以上)
|
||||
const excellentUsers = report.details.filter(d => {
|
||||
if (d.score === null) return false;
|
||||
return (d.score / task.totalScore) >= 0.8;
|
||||
}).length;
|
||||
|
||||
const excellentRate = report.totalUsers > 0
|
||||
? Math.round((excellentUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
stats.push({
|
||||
taskId: task.id,
|
||||
taskName: task.taskName,
|
||||
subjectName: task.subjectName,
|
||||
totalUsers: report.totalUsers,
|
||||
completedUsers: report.completedUsers,
|
||||
completionRate,
|
||||
passRate,
|
||||
excellentRate,
|
||||
startAt: task.startAt,
|
||||
endAt: task.endAt
|
||||
});
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
static async findById(id: string): Promise<ExamTask | null> {
|
||||
@@ -140,6 +256,16 @@ export class ExamTaskModel {
|
||||
await run(`DELETE FROM exam_tasks WHERE id = ?`, [id]);
|
||||
}
|
||||
|
||||
static async getTaskUsers(taskId: string): Promise<string[]> {
|
||||
const rows = await all(`
|
||||
SELECT user_id as userId
|
||||
FROM exam_task_users
|
||||
WHERE task_id = ?
|
||||
`, [taskId]);
|
||||
|
||||
return rows.map(row => row.userId);
|
||||
}
|
||||
|
||||
static async getReport(taskId: string): Promise<TaskReport> {
|
||||
const task = await this.findById(taskId);
|
||||
if (!task) throw new Error('任务不存在');
|
||||
@@ -209,23 +335,141 @@ export class ExamTaskModel {
|
||||
if (!subject) throw new Error('科目不存在');
|
||||
|
||||
const { QuestionModel } = await import('./question');
|
||||
const questions: Awaited<ReturnType<typeof QuestionModel.getRandomQuestions>> = [];
|
||||
|
||||
for (const [type, ratio] of Object.entries(subject.typeRatios)) {
|
||||
if (ratio <= 0) continue;
|
||||
const typeScore = Math.floor((ratio / 100) * subject.totalScore);
|
||||
const avgScore = 10;
|
||||
const count = Math.max(1, Math.round(typeScore / avgScore));
|
||||
|
||||
const categories = Object.entries(subject.categoryRatios)
|
||||
.filter(([, r]) => r > 0)
|
||||
.map(([c]) => c);
|
||||
|
||||
const qs = await QuestionModel.getRandomQuestions(type as any, count, categories);
|
||||
questions.push(...qs);
|
||||
let questions: Awaited<ReturnType<typeof QuestionModel.getRandomQuestions>> = [];
|
||||
|
||||
// 构建包含所有类别的数组,根据比重重复对应次数
|
||||
const allCategories: string[] = [];
|
||||
for (const [category, catRatio] of Object.entries(subject.categoryRatios)) {
|
||||
if (catRatio > 0) {
|
||||
// 根据比重计算该类别应占的总题目数比例
|
||||
const count = Math.round((catRatio / 100) * 100); // 放大100倍避免小数问题
|
||||
for (let i = 0; i < count; i++) {
|
||||
allCategories.push(category);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 确保总题目数至少为1
|
||||
if (allCategories.length === 0) {
|
||||
allCategories.push('通用');
|
||||
}
|
||||
|
||||
// 按题型分配题目
|
||||
for (const [type, typeRatio] of Object.entries(subject.typeRatios)) {
|
||||
if (typeRatio <= 0) continue;
|
||||
|
||||
// 计算该题型应占的总分
|
||||
const targetTypeScore = Math.round((typeRatio / 100) * subject.totalScore);
|
||||
let currentTypeScore = 0;
|
||||
let typeQuestions: Awaited<ReturnType<typeof QuestionModel.getRandomQuestions>> = [];
|
||||
|
||||
// 尝试获取足够分数的题目
|
||||
while (currentTypeScore < targetTypeScore) {
|
||||
// 随机选择一个类别
|
||||
const randomCategory = allCategories[Math.floor(Math.random() * allCategories.length)];
|
||||
|
||||
// 获取该类型和类别的随机题目
|
||||
const availableQuestions = await QuestionModel.getRandomQuestions(
|
||||
type as any,
|
||||
10, // 一次获取多个,提高效率
|
||||
[randomCategory]
|
||||
);
|
||||
|
||||
if (availableQuestions.length === 0) {
|
||||
break; // 该类型/类别没有题目,跳过
|
||||
}
|
||||
|
||||
// 过滤掉已选题目
|
||||
const availableUnselected = availableQuestions.filter(q =>
|
||||
!questions.some(selected => selected.id === q.id)
|
||||
);
|
||||
|
||||
if (availableUnselected.length === 0) {
|
||||
break; // 没有可用的新题目了
|
||||
}
|
||||
|
||||
// 选择分数最接近剩余需求的题目
|
||||
const remainingForType = targetTypeScore - currentTypeScore;
|
||||
const selectedQuestion = availableUnselected.reduce((prev, curr) => {
|
||||
const prevDiff = Math.abs(remainingForType - prev.score);
|
||||
const currDiff = Math.abs(remainingForType - curr.score);
|
||||
return currDiff < prevDiff ? curr : prev;
|
||||
});
|
||||
|
||||
// 添加到题型题目列表
|
||||
typeQuestions.push(selectedQuestion);
|
||||
currentTypeScore += selectedQuestion.score;
|
||||
|
||||
// 防止无限循环
|
||||
if (typeQuestions.length > 100) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
questions.push(...typeQuestions);
|
||||
}
|
||||
|
||||
// 如果总分不足,尝试补充题目
|
||||
let totalScore = questions.reduce((sum, q) => sum + q.score, 0);
|
||||
while (totalScore < subject.totalScore) {
|
||||
// 获取所有类型的随机题目
|
||||
const allTypes = Object.keys(subject.typeRatios).filter(type => subject.typeRatios[type] > 0);
|
||||
if (allTypes.length === 0) break;
|
||||
|
||||
const randomType = allTypes[Math.floor(Math.random() * allTypes.length)];
|
||||
const availableQuestions = await QuestionModel.getRandomQuestions(
|
||||
randomType as any,
|
||||
10,
|
||||
allCategories
|
||||
);
|
||||
|
||||
if (availableQuestions.length === 0) break;
|
||||
|
||||
// 过滤掉已选题目
|
||||
const availableUnselected = availableQuestions.filter(q =>
|
||||
!questions.some(selected => selected.id === q.id)
|
||||
);
|
||||
|
||||
if (availableUnselected.length === 0) break;
|
||||
|
||||
// 选择分数最接近剩余需求的题目
|
||||
const remainingScore = subject.totalScore - totalScore;
|
||||
const selectedQuestion = availableUnselected.reduce((prev, curr) => {
|
||||
const prevDiff = Math.abs(remainingScore - prev.score);
|
||||
const currDiff = Math.abs(remainingScore - curr.score);
|
||||
return currDiff < prevDiff ? curr : prev;
|
||||
});
|
||||
|
||||
questions.push(selectedQuestion);
|
||||
totalScore += selectedQuestion.score;
|
||||
|
||||
// 防止无限循环
|
||||
if (questions.length > 200) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果总分超过,尝试移除一些题目
|
||||
while (totalScore > subject.totalScore) {
|
||||
// 找到最接近剩余差值的题目
|
||||
const excessScore = totalScore - subject.totalScore;
|
||||
let closestIndex = -1;
|
||||
let closestDiff = Infinity;
|
||||
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
const diff = Math.abs(questions[i].score - excessScore);
|
||||
if (diff < closestDiff) {
|
||||
closestDiff = diff;
|
||||
closestIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (closestIndex === -1) break;
|
||||
|
||||
// 移除该题目
|
||||
totalScore -= questions[closestIndex].score;
|
||||
questions.splice(closestIndex, 1);
|
||||
}
|
||||
|
||||
const totalScore = questions.reduce((sum, q) => sum + q.score, 0);
|
||||
|
||||
return {
|
||||
questions,
|
||||
@@ -241,9 +485,9 @@ export class ExamTaskModel {
|
||||
FROM exam_tasks t
|
||||
INNER JOIN exam_task_users tu ON t.id = tu.task_id
|
||||
INNER JOIN exam_subjects s ON t.subject_id = s.id
|
||||
WHERE tu.user_id = ? AND t.start_at <= ? AND t.end_at >= ?
|
||||
WHERE tu.user_id = ? AND t.start_at <= ?
|
||||
ORDER BY t.start_at DESC
|
||||
`, [userId, now, now]);
|
||||
`, [userId, now]);
|
||||
|
||||
return rows.map(row => ({
|
||||
id: row.id,
|
||||
|
||||
@@ -47,18 +47,43 @@ export class QuestionModel {
|
||||
return this.findById(id) as Promise<Question>;
|
||||
}
|
||||
|
||||
// 批量创建题目
|
||||
// 批量创建题目 - 优化为使用事务批量插入
|
||||
static async createMany(questions: CreateQuestionData[]): Promise<{ success: number; errors: string[] }> {
|
||||
const errors: string[] = [];
|
||||
let success = 0;
|
||||
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
try {
|
||||
await this.create(questions[i]);
|
||||
success++;
|
||||
} catch (error: any) {
|
||||
errors.push(`第${i + 1}题: ${error.message}`);
|
||||
// 使用事务提高性能
|
||||
try {
|
||||
// 开始事务
|
||||
await run('BEGIN TRANSACTION');
|
||||
|
||||
const sql = `
|
||||
INSERT INTO questions (id, content, type, options, answer, score, category)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
try {
|
||||
const question = questions[i];
|
||||
const id = uuidv4();
|
||||
const optionsStr = question.options ? JSON.stringify(question.options) : null;
|
||||
const answerStr = Array.isArray(question.answer) ? JSON.stringify(question.answer) : question.answer;
|
||||
const category = question.category && question.category.trim() ? question.category.trim() : '通用';
|
||||
|
||||
// 直接执行插入,不调用单个create方法
|
||||
await run(sql, [id, question.content, question.type, optionsStr, answerStr, question.score, category]);
|
||||
success++;
|
||||
} catch (error: any) {
|
||||
errors.push(`第${i + 1}题: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
await run('COMMIT');
|
||||
} catch (error: any) {
|
||||
// 回滚事务
|
||||
await run('ROLLBACK');
|
||||
errors.push(`事务错误: ${error.message}`);
|
||||
}
|
||||
|
||||
return { success, errors };
|
||||
|
||||
@@ -8,9 +8,56 @@ export interface QuestionCategory {
|
||||
}
|
||||
|
||||
export class QuestionCategoryModel {
|
||||
// 获取所有题目类别,包括从题目表中聚合的新类别
|
||||
static async findAll(): Promise<QuestionCategory[]> {
|
||||
const sql = `SELECT id, name, created_at as createdAt FROM question_categories ORDER BY created_at DESC`;
|
||||
return query(sql);
|
||||
try {
|
||||
// 1. 首先从题目表中聚合所有唯一类别
|
||||
const questionCategoriesSql = `
|
||||
SELECT DISTINCT category as name
|
||||
FROM questions
|
||||
WHERE category IS NOT NULL AND category != ''
|
||||
`;
|
||||
const questionCategories = await query(questionCategoriesSql);
|
||||
|
||||
// 2. 获取现有类别表中的类别
|
||||
const existingCategoriesSql = `
|
||||
SELECT id, name, created_at as createdAt
|
||||
FROM question_categories
|
||||
ORDER BY created_at DESC
|
||||
`;
|
||||
const existingCategories = await query(existingCategoriesSql);
|
||||
|
||||
// 3. 创建现有类别名称的映射,用于快速查找
|
||||
const existingCategoryNames = new Set(existingCategories.map(cat => cat.name));
|
||||
|
||||
// 4. 找出题目表中存在但类别表中不存在的新类别
|
||||
const newCategories = questionCategories.filter(qCat => !existingCategoryNames.has(qCat.name));
|
||||
|
||||
// 5. 批量创建新类别
|
||||
if (newCategories.length > 0) {
|
||||
await run('BEGIN TRANSACTION');
|
||||
|
||||
const createSql = `INSERT INTO question_categories (id, name) VALUES (?, ?)`;
|
||||
for (const newCat of newCategories) {
|
||||
await run(createSql, [uuidv4(), newCat.name]);
|
||||
}
|
||||
|
||||
await run('COMMIT');
|
||||
|
||||
// 6. 重新获取所有类别,包括新创建的
|
||||
return this.findAll();
|
||||
}
|
||||
|
||||
// 如果没有新类别,直接返回现有类别
|
||||
return existingCategories;
|
||||
} catch (error: any) {
|
||||
// 如果事务失败,回滚
|
||||
await run('ROLLBACK');
|
||||
console.error('获取题目类别失败:', error);
|
||||
// 回退到原始逻辑
|
||||
const sql = `SELECT id, name, created_at as createdAt FROM question_categories ORDER BY created_at DESC`;
|
||||
return query(sql);
|
||||
}
|
||||
}
|
||||
|
||||
static async findById(id: string): Promise<QuestionCategory | null> {
|
||||
|
||||
@@ -142,13 +142,15 @@ export class QuizModel {
|
||||
}
|
||||
|
||||
// 获取所有答题记录(管理员用)
|
||||
static async findAllRecords(limit = 10, offset = 0): Promise<{ records: QuizRecord[]; total: number }> {
|
||||
static async findAllRecords(limit = 10, offset = 0): Promise<{ records: any[]; total: number }> {
|
||||
const recordsSql = `
|
||||
SELECT r.id, r.user_id as userId, u.name as userName, u.phone as userPhone,
|
||||
r.total_score as totalScore, r.correct_count as correctCount, r.total_count as totalCount,
|
||||
r.created_at as createdAt
|
||||
r.created_at as createdAt, r.subject_id as subjectId, s.name as subjectName,
|
||||
r.task_id as taskId
|
||||
FROM quiz_records r
|
||||
JOIN users u ON r.user_id = u.id
|
||||
LEFT JOIN exam_subjects s ON r.subject_id = s.id
|
||||
ORDER BY r.created_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`;
|
||||
@@ -160,8 +162,29 @@ export class QuizModel {
|
||||
get(countSql)
|
||||
]);
|
||||
|
||||
// 对于每条记录,计算该考试任务的参与人数
|
||||
const processedRecords = await Promise.all(records.map(async (record) => {
|
||||
let examCount = 0;
|
||||
if (record.taskId) {
|
||||
// 统计该任务的参与人数
|
||||
const taskCountSql = `SELECT COUNT(DISTINCT user_id) as count FROM quiz_records WHERE task_id = ?`;
|
||||
const taskCountResult = await get(taskCountSql, [record.taskId]);
|
||||
examCount = taskCountResult.count || 0;
|
||||
} else if (record.subjectId) {
|
||||
// 统计该科目的参与人数
|
||||
const subjectCountSql = `SELECT COUNT(DISTINCT user_id) as count FROM quiz_records WHERE subject_id = ?`;
|
||||
const subjectCountResult = await get(subjectCountSql, [record.subjectId]);
|
||||
examCount = subjectCountResult.count || 0;
|
||||
}
|
||||
|
||||
return {
|
||||
...record,
|
||||
examCount
|
||||
};
|
||||
}));
|
||||
|
||||
return {
|
||||
records,
|
||||
records: processedRecords,
|
||||
total: countResult.total
|
||||
};
|
||||
}
|
||||
@@ -215,7 +238,7 @@ export class QuizModel {
|
||||
averageScore: number;
|
||||
typeStats: Array<{ type: string; total: number; correct: number; correctRate: number }>;
|
||||
}> {
|
||||
const totalUsersSql = `SELECT COUNT(DISTINCT user_id) as total FROM quiz_records`;
|
||||
const totalUsersSql = `SELECT COUNT(*) as total FROM users`;
|
||||
const totalRecordsSql = `SELECT COUNT(*) as total FROM quiz_records`;
|
||||
const averageScoreSql = `SELECT AVG(total_score) as average FROM quiz_records`;
|
||||
|
||||
|
||||
@@ -43,6 +43,58 @@ export class UserModel {
|
||||
await run(`DELETE FROM users WHERE id = ?`, [id]);
|
||||
}
|
||||
|
||||
// 更新用户信息
|
||||
static async update(id: string, data: Partial<CreateUserData>): Promise<User> {
|
||||
// 验证数据
|
||||
const errors = this.validateUserData(data as CreateUserData);
|
||||
if (errors.length > 0) {
|
||||
throw new Error(errors.join(', '));
|
||||
}
|
||||
|
||||
// 检查手机号唯一性
|
||||
if (data.phone !== undefined) {
|
||||
const existingUser = await this.findByPhone(data.phone);
|
||||
if (existingUser && existingUser.id !== id) {
|
||||
throw new Error('手机号已存在');
|
||||
}
|
||||
}
|
||||
|
||||
// 构建更新字段
|
||||
const fields: string[] = [];
|
||||
const values: any[] = [];
|
||||
|
||||
if (data.name !== undefined) {
|
||||
fields.push('name = ?');
|
||||
values.push(data.name);
|
||||
}
|
||||
|
||||
if (data.phone !== undefined) {
|
||||
fields.push('phone = ?');
|
||||
values.push(data.phone);
|
||||
}
|
||||
|
||||
if (data.password !== undefined) {
|
||||
fields.push('password = ?');
|
||||
values.push(data.password);
|
||||
}
|
||||
|
||||
if (fields.length === 0) {
|
||||
return this.findById(id) as Promise<User>;
|
||||
}
|
||||
|
||||
values.push(id);
|
||||
const sql = `UPDATE users SET ${fields.join(', ')} WHERE id = ?`;
|
||||
|
||||
await run(sql, values);
|
||||
const updatedUser = await this.findById(id);
|
||||
|
||||
if (!updatedUser) {
|
||||
throw new Error('用户不存在');
|
||||
}
|
||||
|
||||
return updatedUser;
|
||||
}
|
||||
|
||||
static async findById(id: string): Promise<User | null> {
|
||||
const sql = `SELECT id, name, phone, password, created_at as createdAt FROM users WHERE id = ?`;
|
||||
const user = await get(sql, [id]);
|
||||
@@ -55,6 +107,12 @@ export class UserModel {
|
||||
return user || null;
|
||||
}
|
||||
|
||||
static async findByName(name: string): Promise<User[]> {
|
||||
const sql = `SELECT id, name, phone, password, created_at as createdAt FROM users WHERE name = ?`;
|
||||
const users = await query(sql, [name]);
|
||||
return users || [];
|
||||
}
|
||||
|
||||
static async findAll(limit = 10, offset = 0): Promise<{ users: User[]; total: number }> {
|
||||
const usersSql = `
|
||||
SELECT id, name, phone, password, created_at as createdAt
|
||||
@@ -75,15 +133,21 @@ export class UserModel {
|
||||
};
|
||||
}
|
||||
|
||||
static validateUserData(data: CreateUserData): string[] {
|
||||
// 验证用户数据,支持部分数据验证
|
||||
static validateUserData(data: Partial<CreateUserData>): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (!data.name || data.name.length < 2 || data.name.length > 20) {
|
||||
errors.push('姓名长度必须在2-20个字符之间');
|
||||
// 只验证提供了的数据
|
||||
if (data.name !== undefined) {
|
||||
if (data.name.length < 2 || data.name.length > 20) {
|
||||
errors.push('姓名长度必须在2-20个字符之间');
|
||||
}
|
||||
}
|
||||
|
||||
if (!data.phone || !/^1[3-9]\d{9}$/.test(data.phone)) {
|
||||
errors.push('手机号格式不正确,请输入11位中国手机号');
|
||||
if (data.phone !== undefined) {
|
||||
if (!/^1[3-9]\d{9}$/.test(data.phone)) {
|
||||
errors.push('手机号格式不正确,请输入11位中国手机号');
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
|
||||
Reference in New Issue
Block a user