- Python (使用Flask框架)58one.com
python
from flask import Flask, render_template, request
app = Flask(name)
products = [
{“id”: 1, “name”: “Product 1”, “price”: 100},
{“id”: 2, “name”: “Product 2”, “price”: 200},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
@app.route(‘/buy/int:product_id’, methods=[‘POST’])
def buy(product_id):
# 这里应该包含购买逻辑,比如与数据库交互
product = next((item for item in products if item[“id”] == product_id), None)
if product:
# 假设购买成功,返回成功消息
return "Purchased: " + product[“name”]
else:
return “Product not found”, 404
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Node.js和Express框架)
javascript
const express = require(‘express’);
const app = express();
const products = [
// … 产品数据
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
app.post(‘/buy/:product_id’, (req, res) => {
const productId = parseInt(req.params.product_id);
const product = products.find(p => p.id === productId);
if (product) {
// 购买逻辑
res.send(Purchased: ${product.name}
);
} else {
res.status(404).send(‘Product not found’);
}
});
// … 其他中间件和配置
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (使用Spring Boot)
由于Java和Spring Boot的代码相对较长且复杂,这里只提供一个简化的Controller类示例。
java
@RestController
@RequestMapping(“/”)
public class ShopController {
private List<Product> products = // ... 初始化产品列表
@GetMapping
public String index(Model model) {
model.addAttribute("products", products);
return "index"; // 指向Thymeleaf模板
}
@PostMapping("/buy/{productId}")
public ResponseEntity<?> buy(@PathVariable("productId") int productId) {
// 购买逻辑
// ...
// 返回响应
return ResponseEntity.ok("Purchased: Product X"); // 假设X是产品名称
}
// ... 其他方法和类(如Product类、服务层等)
}
请注意,这些示例仅用于演示目的,并不包含完整的购物商城功能,如用户认证、库存管理、支付集成、数据库操作等。在实际开发中,你需要考虑这些因素,并可能需要使用额外的库和框架来实现这些功能。由于生成完整的购物商城代码超出了简短的回答范围,我将为你提供用几种不同编程语言编写的简化示例,以展示购物商城的基本结构和功能。
- Python (使用Flask框架)
python
from flask import Flask, render_template, request
app = Flask(name)
products = [
{“id”: 1, “name”: “Product 1”, “price”: 100},
{“id”: 2, “name”: “Product 2”, “price”: 200},
# …
]
@app.route(‘/’)
def index():
return render_template(‘index.html’, products=products)
@app.route(‘/buy/int:product_id’, methods=[‘POST’])
def buy(product_id):
# 这里应该包含购买逻辑,比如与数据库交互
product = next((item for item in products if item[“id”] == product_id), None)
if product:
# 假设购买成功,返回成功消息
return "Purchased: " + product[“name”]
else:
return “Product not found”, 404
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Node.js和Express框架)
javascript
const express = require(‘express’);
const app = express();
const products = [
// … 产品数据
];
app.get(‘/’, (req, res) => {
res.render(‘index’, { products });
});
app.post(‘/buy/:product_id’, (req, res) => {
const productId = parseInt(req.params.product_id);
const product = products.find(p => p.id === productId);
if (product) {
// 购买逻辑
res.send(Purchased: ${product.name}
);
} else {
res.status(404).send(‘Product not found’);
}
});
// … 其他中间件和配置
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (使用Spring Boot)
由于Java和Spring Boot的代码相对较长且复杂,这里只提供一个简化的Controller类示例。
java
@RestController
@RequestMapping(“/”)
public class ShopController {
private List<Product> products = // ... 初始化产品列表
@GetMapping
public String index(Model model) {
model.addAttribute("products", products);
return "index"; // 指向Thymeleaf模板
}
@PostMapping("/buy/{productId}")
public ResponseEntity<?> buy(@PathVariable("productId") int productId) {
// 购买逻辑
// ...
// 返回响应
return ResponseEntity.ok("Purchased: Product X"); // 假设X是产品名称
}
// ... 其他方法和类(如Product类、服务层等)
}
请注意,这些示例仅用于演示目的,并不包含完整的购物商城功能,如用户认证、库存管理、支付集成、数据库操作等。在实际开发中,你需要考虑这些因素,并可能需要使用额外的库和框架来实现这些功能。