电商平台上面,用户不喜欢或者不感兴趣某个商品的时候,可以长按这个商品会弹出一个框来反馈给平台。这种技术实现方案是怎样的?
在电商平台上实现用户长按商品弹出反馈框的功能,涉及前端和后端的多种技术。以下是一个可能的实现方案:
HTML/CSS:
JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>商品列表</title>
<style>
.product {
width: 200px;
height: 300px;
border: 1px solid #ccc;
margin: 10px;
display: inline-block;
position: relative;
}
.feedback-popup {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
border: 1px solid #ccc;
padding: 20px;
z-index: 1000;
}
</style>
</head>
<body>
<div class="product" data-product-id="1">
<img src="product1.jpg" alt="Product 1">
<div class="feedback-popup">
<p>为什么不喜欢这个商品?</p>
<textarea id="feedback"></textarea>
<button onclick="submitFeedback(1)">提交</button>
</div>
</div>
<div class="product" data-product-id="2">
<img src="product2.jpg" alt="Product 2">
<div class="feedback-popup">
<p>为什么不喜欢这个商品?</p>
<textarea id="feedback"></textarea>
<button onclick="submitFeedback(2)">提交</button>
</div>
</div>
<script>
document.querySelectorAll('.product').forEach(product => {
let timer;
product.addEventListener('mousedown', () => {
timer = setTimeout(() => {
product.querySelector('.feedback-popup').style.display = 'block';
}, 1000); // 长按1秒
});
product.addEventListener('mouseup', () => {
clearTimeout(timer);
});
product.addEventListener('mouseleave', () => {
clearTimeout(timer);
});
});
function submitFeedback(productId) {
const feedback = document.querySelector(`.product[data-product-id="${productId}"] #feedback`).value;
fetch('/submit-feedback', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ productId, feedback })
}).then(response => {
if (response.ok) {
alert('反馈提交成功');
document.querySelector(`.product[data-product-id="${productId}"] .feedback-popup`).style.display = 'none';
} else {
alert('反馈提交失败');
}
});
}
</script>
</body>
</html>
API 端点:
数据存储:
# 假设使用 Flask 框架
from flask import Flask, request, jsonify
import sqlite3
app = Flask(__name__)
def init_db():
conn = sqlite3.connect('feedback.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS feedback
(id INTEGER PRIMARY KEY, product_id INTEGER, feedback TEXT)''')
conn.commit()
conn.close()
@app.route('/submit-feedback', methods=['POST'])
def submit_feedback():
data = request.get_json()
product_id = data['productId']
feedback = data['feedback']
conn = sqlite3.connect('feedback.db')
c = conn.cursor()
c.execute("INSERT INTO feedback (product_id, feedback) VALUES (?, ?)", (product_id, feedback))
conn.commit()
conn.close()
return jsonify({"message": "反馈提交成功"}), 200
if __name__ == '__main__':
init_db()
app.run(debug=True)
前端:
后端:
这个方案可以根据具体需求进行调整和优化,例如增加用户身份验证、反馈分类