parent
241a6f95c6
commit
86ab82ec9e
Binary file not shown.
@ -0,0 +1,9 @@
|
||||
# models.py
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
class User(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||
role = db.Column(db.String(20), nullable=False) # 角色字段,例如 '指挥者' 或 '成员'
|
@ -0,0 +1,14 @@
|
||||
<!-- templates/upload.html -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>上传照片</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>上传照片</h1>
|
||||
<form method="POST" action="{{ url_for('upload') }}" enctype="multipart/form-data">
|
||||
<input type="file" name="file">
|
||||
<input type="submit" value="上传">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,6 @@
|
||||
# test.py
|
||||
from models import db, User
|
||||
|
||||
user = User(username='commander', role='指挥者')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
@ -0,0 +1,52 @@
|
||||
from flask import Flask, request, jsonify
|
||||
from flask_mysqldb import MySQL
|
||||
import MySQLdb.cursors
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# MySQL 配置
|
||||
app.config['MYSQL_HOST'] = 'localhost'
|
||||
app.config['MYSQL_USER'] = 'root' # 替换为你的 MySQL 用户名
|
||||
app.config['MYSQL_PASSWORD'] = 'lin556688' # 替换为你的 MySQL 密码
|
||||
app.config['MYSQL_DB'] = 'mydatabase'
|
||||
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
|
||||
|
||||
mysql = MySQL(app)
|
||||
|
||||
# 添加消息和照片
|
||||
@app.route('/message', methods=['POST'])
|
||||
def add_message():
|
||||
message = request.form['message']
|
||||
photo = request.files['photo']
|
||||
|
||||
# 读取照片内容
|
||||
photo_data = photo.read()
|
||||
|
||||
# 插入数据到 MySQL
|
||||
cursor = mysql.connection.cursor()
|
||||
cursor.execute("INSERT INTO messages (message, photo) VALUES (%s, %s)", (message, photo_data))
|
||||
mysql.connection.commit()
|
||||
cursor.close()
|
||||
|
||||
return jsonify({'status': 'success'}), 201
|
||||
|
||||
# 获取所有消息
|
||||
@app.route('/messages', methods=['GET'])
|
||||
def get_messages():
|
||||
cursor = mysql.connection.cursor()
|
||||
cursor.execute("SELECT id, message, created_at FROM messages") # 不直接查询照片以提高性能
|
||||
result = cursor.fetchall()
|
||||
cursor.close()
|
||||
|
||||
messages = []
|
||||
for row in result:
|
||||
messages.append({
|
||||
'id': row['id'],
|
||||
'message': row['message'],
|
||||
'created_at': row['created_at']
|
||||
})
|
||||
|
||||
return jsonify(messages)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000)
|
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 78 KiB |
Loading…
Reference in new issue