save code

This commit is contained in:
xsl
2026-01-18 20:04:25 +08:00
parent 9a00ac3907
commit bf2d7d2bba
6 changed files with 516 additions and 98 deletions
+34 -10
View File
@@ -13,26 +13,50 @@ app.config.from_object(Config)
CORS(app) # 启用CORS,允许跨域请求
# 加载发型数据
hairstyles_data = None
with open('data/hairstyles.json', 'r', encoding='utf-8') as f:
hairstyles_data = json.load(f)
def load_hairstyles_from_file():
"""
从hair_res.txt文件加载发型数据
"""
hairstyles = []
try:
# 读取hair_res.txt文件
with open('../hair_res.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
# 解析文件内容,提取图片URL
for i, line in enumerate(lines):
line = line.strip()
# 查找以https开头的行,这些是图片URL
if line.startswith('https://'):
# 生成发型ID和名称
hairstyle_id = f'hair_{i:03d}'
hairstyle_name = f'发型{i}'
# 添加到发型列表
hairstyles.append({
'id': hairstyle_id,
'name': hairstyle_name,
'image_url': line
})
except Exception as e:
print(f'加载发型数据失败: {str(e)}')
return hairstyles
# 加载发型数据
hairstyles_data = load_hairstyles_from_file()
@app.route('/api/hairstyles', methods=['GET'])
def get_hairstyles():
"""
获取发型列表
返回按性别分类的发型数据
返回所有发型数据,不再区分男女
"""
try:
# 按性别分类发型
male_hairstyles = [h for h in hairstyles_data['hairstyles'] if h['gender'] == 'male']
female_hairstyles = [h for h in hairstyles_data['hairstyles'] if h['gender'] == 'female']
return jsonify({
'success': True,
'data': {
'male': male_hairstyles,
'female': female_hairstyles
'hairstyles': hairstyles_data
}
})
except Exception as e: