要通过单击朋友的个人资料将消息状态更新为只读,通常涉及到前端和后端的协同工作。以下是一个基本的实现思路:
<!-- 示例HTML -->
<button id="readOnlyButton">设为只读</button>
// 示例JavaScript
document.getElementById('readOnlyButton').addEventListener('click', function() {
fetch('/api/updateMessageStatus', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ friendId: 'friend123', status: 'readOnly' })
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('消息状态已更新为只读');
} else {
alert('更新失败,请重试');
}
})
.catch(error => console.error('Error:', error));
});
# 示例Python Flask代码
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/api/updateMessageStatus', methods=['POST'])
def update_message_status():
data = request.get_json()
friend_id = data.get('friendId')
status = data.get('status')
# 这里应该有数据库操作的代码
# 例如:update_message_in_db(friend_id, status)
return jsonify({'success': True})
if __name__ == '__main__':
app.run(debug=True)
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
import jsonschema
message_schema = {
"type": "object",
"properties": {
"friendId": {"type": "string"},
"status": {"type": "string"}
},
"required": ["friendId", "status"]
}
@app.route('/api/updateMessageStatus', methods=['POST'])
def update_message_status():
data = request.get_json()
try:
jsonschema.validate(data, message_schema)
except jsonschema.ValidationError as e:
return jsonify({'success': False, 'error': str(e)}), 400
# 继续处理逻辑
通过上述步骤和方法,可以实现通过单击朋友的个人资料将消息状态更新为只读的功能。
领取专属 10元无门槛券
手把手带您无忧上云