1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// 模拟数据存储
class MockStorage {
constructor() {
this.storageKey = "oj_mock_data";
this.initData();
}
initData() {
if (!localStorage.getItem(this.storageKey)) {
const initialData = {
problems: [
{
id: 1,
title: "两数之和",
label: "数组,哈希表,简单",
testPointNum: 3,
description:
"给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回它们的数组下标。\n\n你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。",
testPointList: [
{ input: "3\n2 7 11 15\n9", output: "0 1", isSample: 1 },
{ input: "2\n3 2 4\n6", output: "1 2", isSample: 0 },
{ input: "2\n3 3\n6", output: "0 1", isSample: 0 },
],
createTime: "2024-01-15 10:00:00",
},
{
id: 2,
title: "验证回文串",
label: "字符串,双指针,简单",
testPointNum: 6,
description:
"给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。\n\n说明:本题中,我们将空字符串定义为有效的回文串。",
testPointList: [
{
input: '"A man, a plan, a canal: Panama"',
output: "true",
isSample: 1,
},
{ input: '"race a car"', output: "false", isSample: 1 },
{ input: '""', output: "true", isSample: 0 },
],
createTime: "2024-01-16 14:30:00",
},
],
nextProblemId: 3,
};
this.saveData(initialData);
}
}
getData() {
return JSON.parse(localStorage.getItem(this.storageKey)) || {};
}
saveData(data) {
localStorage.setItem(this.storageKey, JSON.stringify(data));
}
// 获取所有题目
getProblems() {
return this.getData().problems || [];
}
// 添加题目
addProblem(problem) {
const data = this.getData();
const newProblem = {
...problem,
id: data.nextProblemId++,
createTime: new Date().toLocaleString("zh-CN"),
};
data.problems.push(newProblem);
this.saveData(data);
return newProblem;
}
// 更新题目
updateProblem(updatedProblem) {
const data = this.getData();
const index = data.problems.findIndex((p) => p.id === updatedProblem.id);
if (index !== -1) {
data.problems[index] = {
...data.problems[index],
...updatedProblem,
// 保留创建时间
createTime: data.problems[index].createTime,
};
this.saveData(data);
return data.problems[index];
}
return null;
}
// 删除题目
deleteProblem(id) {
const data = this.getData();
data.problems = data.problems.filter((p) => p.id !== id);
this.saveData(data);
return true;
}
// 分页查询
getProblemsByPage(pageNum = 1, size = 10) {
const problems = this.getProblems();
const start = (pageNum - 1) * size;
const end = start + size;
const pageData = problems.slice(start, end);
return {
records: pageData,
total: problems.length,
pageNum,
size,
};
}
// 根据ID查询
getProblemById(id) {
const problems = this.getProblems();
return problems.find((p) => p.id === Number(id));
}
}
// 创建单例实例
export const mockStorage = new MockStorage();
|