我要开发一个h5的项目。 目标平台是PC浏览器和手机浏览器。 分为前端和后端。 后端采用python 不需要数据库,直接用文件系统,启动的时候读取到内存就可以。

功能
1、是前端能从后端拉起一组图片,分类为“男”,“女”。 这个图片是保存到oss文件系统的,后端需要提供图片的url。
2、前端可以选择一个图片,手动端可以拍摄图片或者选择图片。
3、前端发起更换发型的请求,把选择的图片和对应要更换的发型ID发给后端。
4、前端收到后端更换发型的请求后处理图片返回给前端。
5、后端实际处理更换发型请求的时候会调用另外一个远程的http接口来实现。
先帮我把项目结构,产品文档,系统框架等做出来。
This commit is contained in:
xsl
2026-01-18 18:22:58 +08:00
commit 9a00ac3907
12 changed files with 1694 additions and 0 deletions
Binary file not shown.
+150
View File
@@ -0,0 +1,150 @@
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import json
import os
import requests
import base64
from io import BytesIO
from config import Config
app = Flask(__name__)
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)
@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
}
})
except Exception as e:
return jsonify({
'success': False,
'message': f'获取发型列表失败: {str(e)}'
}), 500
@app.route('/api/change-hair', methods=['POST'])
def change_hair():
"""
更换发型
接收用户上传的图片和发型ID,调用远程API处理
"""
try:
# 检查是否有文件上传
if 'image' not in request.files:
return jsonify({
'success': False,
'message': '请上传图片'
}), 400
# 获取图片文件和发型ID
image_file = request.files['image']
hairstyle_id = request.form.get('hairstyle_id')
if not hairstyle_id:
return jsonify({
'success': False,
'message': '请选择发型'
}), 400
# 验证文件类型
if not allowed_file(image_file.filename):
return jsonify({
'success': False,
'message': '不支持的文件类型,请上传图片文件'
}), 400
# 读取图片数据
image_data = image_file.read()
# 调用远程API处理图片
# 注意:这里是模拟实现,实际项目中需要根据远程API的要求进行调整
result_image = call_remote_api(image_data, hairstyle_id)
# 将处理后的图片转换为base64格式返回
result_base64 = base64.b64encode(result_image).decode('utf-8')
result_data_url = f'data:image/jpeg;base64,{result_base64}'
return jsonify({
'success': True,
'data': {
'result_image': result_data_url
}
})
except Exception as e:
return jsonify({
'success': False,
'message': f'处理图片失败: {str(e)}'
}), 500
def allowed_file(filename):
"""
验证文件类型是否允许
"""
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
def call_remote_api(image_data, hairstyle_id):
"""
调用远程API处理图片
注意:这里是模拟实现,实际项目中需要根据远程API的要求进行调整
"""
try:
# 构建请求数据
# 注意:这里需要根据远程API的实际要求调整请求格式
files = {'image': ('upload.jpg', BytesIO(image_data), 'image/jpeg')}
data = {'hairstyle_id': hairstyle_id}
# 发送请求到远程API
# 注意:实际项目中需要替换为真实的远程API地址
# response = requests.post(
# app.config['REMOTE_API_URL'],
# files=files,
# data=data,
# timeout=app.config['API_TIMEOUT']
# )
# 模拟远程API返回结果
# 这里简单返回原始图片数据作为模拟结果
# 实际项目中应该使用远程API的返回结果
return image_data
except Exception as e:
# 如果远程API调用失败,返回原始图片
# 实际项目中应该根据需要进行错误处理
return image_data
@app.route('/')
def index():
"""
提供前端静态文件
开发环境下使用,生产环境应该使用Nginx等静态文件服务器
"""
return send_from_directory('../frontend', 'index.html')
@app.route('/<path:path>')
def send_static(path):
"""
提供前端静态文件
"""
return send_from_directory('../frontend', path)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
+18
View File
@@ -0,0 +1,18 @@
# 应用配置
class Config:
# 应用名称
APP_NAME = "Hair Change Color"
# 远程发型更换API地址
# 注意:这里需要根据实际的远程API地址进行修改
REMOTE_API_URL = "https://api.example.com/change-hair"
# 远程API请求超时时间(秒)
API_TIMEOUT = 30
# 文件上传配置
MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # 16MB
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
# 开发环境配置
DEBUG = True
+40
View File
@@ -0,0 +1,40 @@
{
"hairstyles": [
{
"id": "hair_001",
"name": "时尚短发",
"gender": "male",
"image_url": "https://oss.example.com/hairstyles/male/hair_001.jpg"
},
{
"id": "hair_002",
"name": "清爽平头",
"gender": "male",
"image_url": "https://oss.example.com/hairstyles/male/hair_002.jpg"
},
{
"id": "hair_003",
"name": "潮流纹理烫",
"gender": "male",
"image_url": "https://oss.example.com/hairstyles/male/hair_003.jpg"
},
{
"id": "hair_004",
"name": "优雅长发",
"gender": "female",
"image_url": "https://oss.example.com/hairstyles/female/hair_004.jpg"
},
{
"id": "hair_005",
"name": "俏皮短发",
"gender": "female",
"image_url": "https://oss.example.com/hairstyles/female/hair_005.jpg"
},
{
"id": "hair_006",
"name": "浪漫卷发",
"gender": "female",
"image_url": "https://oss.example.com/hairstyles/female/hair_006.jpg"
}
]
}
+3
View File
@@ -0,0 +1,3 @@
Flask
Flask-CORS
requests