📜  寻找朋友 (1)

📅  最后修改于: 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?"
}
API 接口
创建账户
  • 请求方式:POST
  • 请求参数:{"username": "XXX", "password": "XXX"}
  • 返回值:{"msg": "success"}
@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"}
编辑个人信息
  • 请求方式:POST
  • 请求参数:{"username": "XXX", "age": 25, "gender": "female", "hobby": ["travel", "reading"], "location": "Shanghai"}
  • 返回值:{"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"}
搜索其他用户
  • 请求方式:POST
  • 请求参数:{"username": "XXX", "hobby": "travel"}
  • 返回值:{"users": [{"username": "lisa", "age": 25, "gender": "female", "hobby": ["travel", "reading"], "location": "Shanghai"}]}
@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}
查看其他用户的个人信息
  • 请求方式:POST
  • 请求参数:{"username": "lisa", "view_username": "john"}
  • 返回值:{"username": "john", "age": 28, "gender": "male", "hobby": ["reading"], "location": "Beijing"}
@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
发送私信给其他用户
  • 请求方式:POST
  • 请求参数:{"from_user": "lisa", "to_user": "john", "message": "Hi John, how are you?"}
  • 返回值:{"msg": "success"}
@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()

以上是本程序的基本框架和实现方法。如果你想要进一步完善和扩展该程序,可以考虑以下几个方面:

  • 实现用户之间的实时聊天功能
  • 添加好友和分组管理功能
  • 改进匹配算法,提高匹配准确度
  • 提供更加友好的图形化界面