Retrieving User Profile Information
This endpoint queries the user database and returns all user data to the frontend.
API Documentation
URL: /users
Method: GET
Response:
user.to_dict() # Returns all user table data as a dictionary
Business Logic
- Retrieve user ID from the g variable
user_id = g.current_user_id
- Query the database using the user ID
user_record = User.query.get(user_id)
- If the user exists, convert the user object to a dictionary and return to the frontand
return jsonify(error_code=RET.SUCCESS, message="Success", data=user_record.to_dict())
Setting User Avatar
API Documentation
URL: /users/avatar
Method: POST
Response:
{
"avatar_url": "Image URL stored in Qiniu Cloud",
"code": 200 # Indicates successful update
}
Business Logic
- Retrieve user ID from the g variable
user_id = g.current_user_id
- Get and validate the uploaded avatar image (submitted as form data)
avatar_file = request.files.get('avatar')
if avatar_file is None:
return jsonify(error_code=RET.PARAM_ERROR, message='No image uploaded')
- Read the binary image data
image_data = avatar_file.read()
- Save the image to Qiniu Cloud, which returns a file name
file_name = storage_service.upload(image_data)
- Save the Qiniu Cloud URL to the database
User.query.filter_by(id=user_id).update({"avatar_url": file_name})
db.session.commit()
- Consturct the complete avatar URL and return to front end
full_avatar_url = constants.QINIU_DOMAIN + file_name
return jsonify(error_code=RET.SUCCESS, message='Avatar saved successfully', data={"avatar_url": full_avatar_url})
Updating Username
API Documentation
URL: /users/name
Method: PUT
Response:
{
"code": 200,
"name": "username"
}
Business Logic
- Retrieve user ID from the g variable
user_id = g.current_user_id
- Accept the username from the request
request_data = request.get_json()
if not request_data:
return jsonify(error_code=RET.PARAM_ERROR, message="Incomplete parameters")
username = request_data.get("name") # Desired username
if not username:
return jsonify(error_code=RET.PARAM_ERROR, message="Username cannot be empty")
- Save the username and check for duplicates using database unique constraint
try:
User.query.filter_by(id=user_id).update({"name": username})
db.session.commit()
except Exception as e:
logging.error(e)
db.session.rollback()
return jsonify(error_code=RET.DB_ERROR, message="Username update failed")
- Update the name field in the session
session["username"] = username
return jsonify(error_code=RET.SUCCESS, message="Success", data={"name": username})
Retrieving User Verification Information
API Documentation
URL: /users/auth
Method: GET
Response:
{
"code": 200,
"data": user.to_dict() # Returns user verification data as dictionary
}
Business Logic
- Retrieve user ID from the g variable
user_id = g.current_user_id
- Query the user table and return all data to the frontend
user_record = User.query.get(user_id)
- Validate the user record
if user_record is None:
return jsonify(error_code=RET.NO_DATA, message="Invalid operation")
- Convert to dictionary and return to frontend
return jsonify(error_code=RET.SUCCESS, message="Success", data=user_record.verification_to_dict())
Saving User Verification Information
API Documentation
URL: /users/auth
Method: POST
Response:
{
"error_code": RET.SUCCESS,
"message": "Success"
}
Business Logic
- Retrieve user ID from the g variable
user_id = g.current_user_id
- Accept verification information from the frontend
request_data = request.get_json()
if not request_data:
return jsonify(error_code=RET.PARAM_ERROR, message="Invalid parameters")
real_name = request_data.get("real_name") # Real name
id_card = request_data.get("id_card") # ID number
# Parameter validation
if not all([real_name, id_card]):
return jsonify(error_code=RET.PARAM_ERROR, message="Missing required parameters")
- Save real name and ID number to the database
User.query.filter_by(id=user_id, real_name=None, id_card=None).update({"real_name": real_name, "id_card": id_card})
db.session.commit()
- Return success response
return jsonify(error_code=RET.SUCCESS, message="Verification information saved")