- Python 示例(使用 Flask 框架)58one.com
这是一个简单的 Flask 应用,用于展示商品列表和购物车管理。
python
from flask import Flask, request, jsonify
app = Flask(name)
示例商品列表
products = [
{“id”: 1, “name”: “Game A”, “price”: 99.99},
{“id”: 2, “name”: “Game B”, “price”: 149.99},
{“id”: 3, “name”: “Game C”, “price”: 49.99}
]
购物车(模拟为全局变量)
cart = []
@app.route(‘/products’, methods=[‘GET’])
def get_products():
return jsonify(products)
@app.route(‘/add_to_cart’, methods=[‘POST’])
def add_to_cart():
data = request.get_json()
product_id = data.get(‘product_id’)
product = next((p for p in products if p[‘id’] == product_id), None)
if product:
cart.append(product)
return jsonify({“message”: “Added to cart”, “cart”: cart})
return jsonify({“error”: “Product not found”}), 404
@app.route(‘/cart’, methods=[‘GET’])
def get_cart():
return jsonify(cart)
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js + Express) 示例
这里是一个 Node.js 应用程序,使用 Express 框架实现类似的功能。
javascript
const express = require(‘express’);
const app = express();
const bodyParser = require(‘body-parser’);
app.use(bodyParser.json());
let products = [
{id: 1, name: ‘Game A’, price: 99.99},
{id: 2, name: ‘Game B’, price: 149.99},
{id: 3, name: ‘Game C’, price: 49.99}
];
let cart = [];
app.get(‘/products’, (req, res) => {
res.json(products);
});
app.post(‘/add_to_cart’, (req, res) => {
const { product_id } = req.body;
const product = products.find(p => p.id === product_id);
if (product) {
cart.push(product);
res.json({ message: ‘Added to cart’, cart });
} else {
res.status(404).json({ error: ‘Product not found’ });
}
});
app.get(‘/cart’, (req, res) => {
res.json(cart);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(Server running on port ${PORT}
);
});
3. Java 示例 (Spring Boot)
这里是一个简单的 Spring Boot 应用框架的启动示例,你需要创建更多的类和注解来完整实现。
java
// 仅为示意,需要更多代码来实现完整的 RESTful API
@RestController
@RequestMapping(“/api”)
public class GameController {
// 示例商品列表和购物车(实际应用中应考虑更复杂的存储方式)
private List<Product> products = Arrays.asList(
new Product(1, "Game A", 99.99),
new Product(2, "Game B", 149.99),
new Product(3, "Game C", 49.99)
);
private List<Product> cart = new ArrayList<>();
// GET /api/products
@GetMapping("/products")
public List<Product> getProducts() {
return products;
}
// POST /api/add_to_cart
@PostMapping("/add_to_cart")
public ResponseEntity<?> addToCart(@RequestBody Map<String, Integer> body) {
Integer productId = body.
由于篇幅和复杂度的限制,我无法在这里完整地编写一个全功能的游戏商城系统代码,但我可以为你提供几种不同编程语言中游戏商城核心功能的简化示例。这些示例将包括商品展示、添加到购物车和简单购物车管理的功能。
- Python 示例(使用 Flask 框架)
这是一个简单的 Flask 应用,用于展示商品列表和购物车管理。
python
from flask import Flask, request, jsonify
app = Flask(name)
示例商品列表
products = [
{“id”: 1, “name”: “Game A”, “price”: 99.99},
{“id”: 2, “name”: “Game B”, “price”: 149.99},
{“id”: 3, “name”: “Game C”, “price”: 49.99}
]
购物车(模拟为全局变量)
cart = []
@app.route(‘/products’, methods=[‘GET’])
def get_products():
return jsonify(products)
@app.route(‘/add_to_cart’, methods=[‘POST’])
def add_to_cart():
data = request.get_json()
product_id = data.get(‘product_id’)
product = next((p for p in products if p[‘id’] == product_id), None)
if product:
cart.append(product)
return jsonify({“message”: “Added to cart”, “cart”: cart})
return jsonify({“error”: “Product not found”}), 404
@app.route(‘/cart’, methods=[‘GET’])
def get_cart():
return jsonify(cart)
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js + Express) 示例
这里是一个 Node.js 应用程序,使用 Express 框架实现类似的功能。
javascript
const express = require(‘express’);
const app = express();
const bodyParser = require(‘body-parser’);
app.use(bodyParser.json());
let products = [
{id: 1, name: ‘Game A’, price: 99.99},
{id: 2, name: ‘Game B’, price: 149.99},
{id: 3, name: ‘Game C’, price: 49.99}
];
let cart = [];
app.get(‘/products’, (req, res) => {
res.json(products);
});
app.post(‘/add_to_cart’, (req, res) => {
const { product_id } = req.body;
const product = products.find(p => p.id === product_id);
if (product) {
cart.push(product);
res.json({ message: ‘Added to cart’, cart });
} else {
res.status(404).json({ error: ‘Product not found’ });
}
});
app.get(‘/cart’, (req, res) => {
res.json(cart);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(Server running on port ${PORT}
);
});
3. Java 示例 (Spring Boot)
这里是一个简单的 Spring Boot 应用框架的启动示例,你需要创建更多的类和注解来完整实现。
java
// 仅为示意,需要更多代码来实现完整的 RESTful API
@RestController
@RequestMapping(“/api”)
public class GameController {
// 示例商品列表和购物车(实际应用中应考虑更复杂的存储方式)
private List<Product> products = Arrays.asList(
new Product(1, "Game A", 99.99),
new Product(2, "Game B", 149.99),
new Product(3, "Game C", 49.99)
);
private List<Product> cart = new ArrayList<>();
// GET /api/products
@GetMapping("/products")
public List<Product> getProducts() {
return products;
}
// POST /api/add_to_cart
@PostMapping("/add_to_cart")
public ResponseEntity<?> addToCart(@RequestBody Map<String, Integer> body) {
Integer productId = body.