📅  最后修改于: 2023-12-03 14:53:41.005000             🧑  作者: Mango
本程序主要功能是帮助用户寻找朋友。用户可以在该程序中创建账户、编辑个人信息,并浏览其他用户的信息。用户可以通过匹配自己的兴趣、爱好等信息寻找到与自己相似的朋友,并通过私信等方式与TA们联系。
用户信息:
{
"username": "lisa",
"password": "123456",
"age": 25,
"gender": "female",
"hobby": ["travel", "reading"],
"location": "Shanghai"
}
私信信息:
{
"from_user": "lisa",
"to_user": "john",
"message": "Hi John, how are you?"
}
@app.route('/register', methods=['POST'])
def register():
# 从请求参数中获取用户名和密码
username = request.json.get('username')
password = request.json.get('password')
# 将用户信息存入数据库
user = {"username": username, "password": password}
db.users.insert_one(user)
return {"msg": "success"}
@app.route('/edit_profile', methods=['POST'])
def edit_profile():
# 从请求参数中获取用户信息
username = request.json.get('username')
age = request.json.get('age')
gender = request.json.get('gender')
hobby = request.json.get('hobby')
location = request.json.get('location')
# 更新用户信息
db.users.update_one({"username": username}, {"$set": {"age": age, "gender": gender, "hobby": hobby, "location": location}})
return {"msg": "success"}
@app.route('/search_user', methods=['POST'])
def search_user():
# 从请求参数中获取用户名和兴趣爱好
username = request.json.get('username')
hobby = request.json.get('hobby')
# 查询符合条件的用户信息
users = db.users.find({"username": {"$ne": username}, "hobby": hobby})
# 构造返回值
res = []
for user in users:
res.append({"username": user["username"], "age": user["age"], "gender": user["gender"], "hobby": user["hobby"], "location": user["location"]})
return {"users": res}
@app.route('/view_user_profile', methods=['POST'])
def view_user_profile():
# 从请求参数中获取用户名和目标用户的用户名
username = request.json.get('username')
view_username = request.json.get('view_username')
# 查询目标用户的信息
user = db.users.find_one({"username": view_username})
# 构造返回值
res = {"username": user["username"], "age": user["age"], "gender": user["gender"], "hobby": user["hobby"], "location": user["location"]}
return res
@app.route('/send_message', methods=['POST'])
def send_message():
# 从请求参数中获取发送者、接收者和私信内容
from_user = request.json.get('from_user')
to_user = request.json.get('to_user')
message = request.json.get('message')
# 将私信信息存入数据库
msg = {"from_user": from_user, "to_user": to_user, "message": message}
db.messages.insert_one(msg)
return {"msg": "success"}
from flask import Flask, request
from pymongo import MongoClient
# 连接 MongoDB 数据库
client = MongoClient('mongodb://localhost:27017/')
db = client.friend_finder
app = Flask(__name__)
# 启动服务
if __name__ == '__main__':
app.run()
以上是本程序的基本框架和实现方法。如果你想要进一步完善和扩展该程序,可以考虑以下几个方面: