最近闲着没事,想着搞个商城玩玩。毕竟现在电商这么火,咱也得跟上时代的步伐不是?于是乎,我决定用PHP搞一个开源的多用户商城系统。为啥选PHP?因为简单,随便找个服务器就能跑,不像某些语言,配置个环境都能让人怀疑人生。
需求分析
咱们得明确一下这个商城系统的基本需求。既然是“多用户”商城,那肯定得支持多个商家入驻,每个商家都能有自己的店铺。用户可以在这些店铺里浏览商品、下单、支付等等。听起来挺简单的,但实际做起来,细节多得让人头大。
技术选型
既然是用PHP,那肯定得选个框架。Laravel?太臃肿了。ThinkPHP?太老了。最后我选了个轻量级的框架——CodeIgniter。为啥选它?因为简单,文档也全,而且社区活跃,遇到问题搜一下基本都能解决。
数据库,MySQL是标配,毕竟PHP和MySQL是黄金搭档。前端的话,Bootstrap搞定,反正咱也不是专业搞前端的,能用就行。
项目结构
项目结构得规划好,不然以后扩展起来会很头疼。我大致分了这么几个模块:
用户模块:包括买家和卖家
商品模块:商品的CRUD操作
订单模块:订单的生成、支付、发货等
支付模块:集成支付宝、微信支付
后台管理模块:管理员对商家、商品、订单的管理
用户模块
用户模块是最基础的,得先搞定。注册、登录、忘记密码这些功能都得有。注册的时候得区分买家和卖家,卖家得提交一些额外的信息,比如店铺名称、营业执照啥的。
代码大致如下:
// 注册接口
public function register() {
$username = $this->input->post('username');
$password = $this->input->post('password');
$role = $this->input->post('role'); // 1:买家 2:卖家
// 验证用户名是否已存在
if ($this->user_model->check_username($username)) {
$this->output->set_output(json_encode(['code' => 0, 'msg' => '用户名已存在']));
return;
}
// 密码加密
$password_hash = password_hash($password, PASSWORD_DEFAULT);
// 插入用户表
$user_id = $this->user_model->insert_user($username, $password_hash, $role);
if ($user_id) {
// 如果是卖家,插入卖家表
if ($role == 2) {
$shop_name = $this->input->post('shop_name');
$license = $this->input->post('license');
$this->seller_model->insert_seller($user_id, $shop_name, $license);
}
$this->output->set_output(json_encode(['code' => 1, 'msg' => '注册成功']));
} else {
}
}
商品模块
商品模块相对复杂一些,因为涉及到商品的分类、属性、库存等。商品分类得支持无限级分类,属性也得支持自定义。库存管理得精确到SKU级别,不然容易超卖。
// 添加商品接口
public function add_product() {
$seller_id = $this->session->userdata('seller_id');
$category_id = $this->input->post('category_id');
$product_name = $this->input->post('product_name');
$stock = $this->input->post('stock');
// 插入商品表
$product_id = $this->product_model->insert_product($seller_id, $category_id, $product_name, $price, $stock);
if ($product_id) {
// 插入商品属性
foreach ($attributes as $attr) {
$this->product_model->insert_product_attribute($product_id, $attr['name'], $attr['value']);
$this->output->set_output(json_encode(['code' => 1, 'msg' => '商品添加成功']));
}
}
订单模块
订单模块是整个商城的核心,涉及到订单的生成、支付、发货、退款等一系列操作。订单的生成得考虑并发问题,不然容易超卖。支付得集成支付宝和微信支付,退款也得支持。
// 生成订单接口
public function create_order() {
$user_id = $this->session->userdata('user_id');
$cart_items = $this->input->post('cart_items');
// 检查库存
foreach ($cart_items as $item) {
$product_id = $item['product_id'];
$quantity = $item['quantity'];
if (!$this->product_model->check_stock($product_id, $quantity)) {
$this->output->set_output(json_encode(['code' => 0, 'msg' => '库存不足']));
return;
}
// 生成订单号
$order_sn = date('YmdHis') . mt_rand(1000, 9999);
// 插入订单表
$order_id = $this->order_model->insert_order($user_id, $order_sn, $cart_items);
if ($order_id) {
// 减少库存
foreach ($cart_items as $item) {
$this->product_model->decrease_stock($item['product_id'], $item['quantity']);
$this->output->set_output(json_encode(['code' => 1, 'msg' => '订单生成成功', 'order_id' => $order_id]));
}
}
支付模块
支付模块得集成支付宝和微信支付,这里以支付宝为例。支付宝的SDK比较完善,集成起来相对容易。
// 支付宝支付接口
public function alipay_pay() {
$order_id = $this->input->post('order_id');
$order_info = $this->order_model->get_order_info($order_id);
if (!$order_info) {
$this->output->set_output(json_encode(['code' => 0, 'msg' => '订单不存在']));
}
require_once APPPATH . 'libraries/alipay/AopClient.php';
require_once APPPATH . 'libraries/alipay/request/AlipayTradePagePayRequest.php';
$aop = new AopClient();
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = "your_app_id";
$aop->rsaPrivateKey = 'your_private_key';
$aop->alipayrsaPublicKey = 'your_public_key';
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset = 'UTF-8';
$aop->format = 'json';
$request = new AlipayTradePagePayRequest();
$request->setReturnUrl(base_url('order/pay_success'));
$request->setBizContent(json_encode([
'out_trade_no' => $order_info['order_sn'],
'product_code' => 'FAST_INSTANT_TRADE_PAY',
'total_amount' => $order_info['total_amount'],
'subject' => '订单支付',
]));
$response = $aop->pageExecute($request);
echo $response;
}
后台管理模块
后台管理模块主要是管理员对商家、商品、订单的管理。管理员可以审核商家的入驻申请,下架违规商品,处理退款申请等。
// 审核商家入驻申请
public function audit_seller() {
$seller_id = $this->input->post('seller_id');
$status = $this->input->post('status'); // 1:通过 2:拒绝
if ($this->seller_model->update_seller_status($seller_id, $status)) {
$this->output->set_output(json_encode(['code' => 1, 'msg' => '审核成功']));
}
}
遇到的问题
1. 并发问题:在生成订单时,如果多个用户同时下单,可能会导致库存超卖。解决这个问题的方法是使用数据库的悲观锁或乐观锁。我选择了乐观锁,因为性能更好。
2. 支付回调:支付宝和微信支付的回调处理得特别注意,因为回调可能会重复调用。处理方法是记录回调日志,确保每个回调只处理一次。
3. 性能问题:随着商品和订单数量的增加,查询性能会下降。