dataGenerator.ts
8.49 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import * as fs from 'fs';
import * as path from 'path';
//商品管理-随机商品名
export function generateUniqueProductName(baseName: string = '测试商品'): string {
// 使用时间戳,精确到毫秒,保证每次都不一样
// const timestamp = Date.now();
// 再加一个随机数,防止同一毫秒内多次调用
const random = Math.floor(Math.random() * 100);
return `${baseName}_${random}`;
}
//代销入库-随机批次号
export function generateOtherName(baseName: string = '批次别名'): string {
// 使用时间戳,精确到毫秒,保证每次都不一样
const timestamp = Date.now();
// 再加一个随机数,防止同一毫秒内多次调用
// const random = Math.floor(Math.random() * 100);
return `${baseName}${timestamp}`;
}
// 随机生成中文客户名称
export function generateCustomerName(): string {
const familyNames = ['张', '王', '李', '刘', '陈', '杨', '黄', '赵', '周', '吴', '徐', '孙', '马', '朱', '胡', '郭', '何', '林', '罗', '高'];
const givenNames = ['伟', '芳', '娜', '敏', '静', '丽', '强', '磊', '军', '洋', '勇', '艳', '杰', '涛', '明', '超', '秀英', '华', '平', '刚', '桂英', '文', '志强', '建华', '建国', '建军', '晓明', '小红', '建军', '婷婷', '秀兰', '伟', '芳', '军', '勇', '艳', '杰', '娟', '涛', '明', '超', '秀英', '华'];
const familyName = familyNames[Math.floor(Math.random() * familyNames.length)];
const givenName = givenNames[Math.floor(Math.random() * givenNames.length)];
return familyName + givenName;
}
// 随机生成身份证号码(18位)
export function generateIdCard(): string {
// 省份代码
const provinceCodes = ['11', '12', '13', '14', '15', '21', '22', '23', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '50', '51', '52', '53', '54', '61', '62', '63', '64', '65'];
// 随机选择省份
const provinceCode = provinceCodes[Math.floor(Math.random() * provinceCodes.length)];
// 随机生成城市和区县代码(各2位)
const cityCode = String(Math.floor(Math.random() * 99) + 1).padStart(2, '0');
const districtCode = String(Math.floor(Math.random() * 99) + 1).padStart(2, '0');
// 随机生成出生日期(1950-01-01 到 2005-12-31)
const startYear = 1950;
const endYear = 2005;
const year = Math.floor(Math.random() * (endYear - startYear + 1)) + startYear;
const month = String(Math.floor(Math.random() * 12) + 1).padStart(2, '0');
const day = String(Math.floor(Math.random() * 28) + 1).padStart(2, '0');
const birthDate = `${year}${month}${day}`;
// 随机生成顺序码(3位)
const sequenceCode = String(Math.floor(Math.random() * 999)).padStart(3, '0');
// 前17位
const id17 = provinceCode + cityCode + districtCode + birthDate + sequenceCode;
// 计算校验码
const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
const checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
let sum = 0;
for (let i = 0; i < 17; i++) {
sum += parseInt(id17[i]) * weights[i];
}
const checkCode = checkCodes[sum % 11];
return id17 + checkCode;
}
// 隨机生成手机号码
export function generatePhoneNumber(): string {
// 中国移动号段
const mobilePrefixes = ['134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '178', '182', '183', '184', '187', '188'];
// 中国联通号段
const unicomPrefixes = ['130', '131', '132', '145', '155', '156', '175', '176', '185', '186'];
// 中国电信号段
const telecomPrefixes = ['133', '149', '153', '177', '180', '181', '189'];
const allPrefixes = [...mobilePrefixes, ...unicomPrefixes, ...telecomPrefixes];
const prefix = allPrefixes[Math.floor(Math.random() * allPrefixes.length)];
const suffix = String(Math.floor(Math.random() * 100000000)).padStart(8, '0');
return prefix + suffix;
}
// 随机生成详细地址(街道、门牌号等,不含省市区)
export function generateDetailedAddress(): string {
const streets = ['中山路', '人民路', '建设路', '解放路', '和平路', '幸福路', '文化路', '科技路', '商业街', '繁华大道', '朝阳路', '胜利路', '长江路', '黄河路', '振兴路', '发展大道', '创业路', '创新路', '东风路', '西山路'];
const communities = ['阳光小区', '幸福花园', '和谐家园', '金色年华', '锦绣华庭', '碧水蓝天', '盛世华城', '锦绣江南', '紫荆花园', '翠竹苑', '莲花小区', '牡丹园', '玫瑰园', '百合苑', '茉莉花园'];
const buildings = ['1栋', '2栋', '3栋', '5栋', '6栋', '8栋', '9栋', '10栋', 'A座', 'B座', 'C座', 'D座'];
const units = ['1单元', '2单元', '3单元', '一单元', '二单元', '三单元'];
const floors = ['1楼', '2楼', '3楼', '5楼', '6楼', '8楼', '10楼', '12楼', '15楼', '18楼', '20楼'];
const rooms = ['101室', '201室', '301室', '401室', '501室', '601室', '701室', '801室', '1001室', '1101室', '1201室'];
const street = streets[Math.floor(Math.random() * streets.length)];
const community = communities[Math.floor(Math.random() * communities.length)];
const building = buildings[Math.floor(Math.random() * buildings.length)];
const unit = units[Math.floor(Math.random() * units.length)];
const floor = floors[Math.floor(Math.random() * floors.length)];
const room = rooms[Math.floor(Math.random() * rooms.length)];
// 随机组合地址格式
const formats = [
`${street}${Math.floor(Math.random() * 999) + 1}号`,
`${street}${Math.floor(Math.random() * 999) + 1}号${community}`,
`${community}${building}${unit}${room}`,
`${street}${Math.floor(Math.random() * 999) + 1}号${community}${building}${room}`,
`${street}${Math.floor(Math.random() * 999) + 1}号${floor}${room}`,
`${community}${building}${unit}${floor}${room}`
];
return formats[Math.floor(Math.random() * formats.length)];
}
// 生成完整的客户信息对象
export function generateCustomerInfo(): {
name: string;
idCard: string;
phone: string;
detailedAddress: string;
} {
return {
name: generateCustomerName(),
idCard: generateIdCard(),
phone: generatePhoneNumber(),
detailedAddress: generateDetailedAddress()
};
}
/**
* 从指定目录随机选择一张图片
* @param imageDir 图片目录路径,默认为 'test-data/img'
* @returns 随机选择的图片完整路径,如果目录不存在或没有图片则返回 null
*/
export function getRandomImage(imageDir: string = 'test-data/img'): string | null {
try {
// 获取项目根目录
const projectRoot = process.cwd();
const fullImagePath = path.join(projectRoot, imageDir);
// 检查目录是否存在
if (!fs.existsSync(fullImagePath)) {
console.warn(`图片目录不存在: ${fullImagePath}`);
return null;
}
// 读取目录中的所有文件
const files = fs.readdirSync(fullImagePath);
// 过滤出图片文件(支持常见图片格式)
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'];
const imageFiles = files.filter(file => {
const ext = path.extname(file).toLowerCase();
return imageExtensions.includes(ext);
});
if (imageFiles.length === 0) {
console.warn(`图片目录中没有图片文件: ${fullImagePath}`);
return null;
}
// 随机选择一张图片
const randomIndex = Math.floor(Math.random() * imageFiles.length);
const selectedImage = imageFiles[randomIndex];
// 返回相对于项目根目录的路径
return path.join(imageDir, selectedImage);
} catch (error) {
console.error('获取随机图片失败:', error);
return null;
}
}
// 随机生成备注(50字以内)
export function generateRemark(maxLength: number = 50): string {
const phrases = [
'自动化测试数据',
'测试备注信息',
'系统自动生成',
'功能验证数据',
'接口测试记录',
'数据录入测试',
'业务流程验证',
'质量保证测试',
'回归测试数据',
'冒烟测试记录'
];
const randomPhrase = phrases[Math.floor(Math.random() * phrases.length)];
const timestamp = Date.now().toString().slice(-6);
const remark = `${randomPhrase}_${timestamp}`;
// 确保不超过最大长度
return remark.slice(0, maxLength);
}
// 随机生成金额(1-9999)
export function generateAmount(min: number = 1, max: number = 9999): string {
const amount = Math.floor(Math.random() * (max - min + 1)) + min;
return amount.toString();
}