您的当前位置:首页>全部文章>文章详情

thinkphp写一个商城小程序的API接口

发表于:2022-11-24 10:23:32浏览:28次TAG:
商品列表接口
public function goodsList(){
 $page = I('page',1,'intval');
 $limit = I('limit',10,'intval');
 $category_id = I('category_id',0,'intval');
 $condition = [];
 if($category_id){
     $condition['category_id'] = $category_id;
 }
 $goods = M('goods')->where($condition)
     ->limit(($page-1)*$limit,$limit)
     ->select();
 $data = [
     'code' => 0,
     'msg' => 'success',
     'data' => $goods
 ];
 $this->ajaxReturn($data);
}

商品详情接口
public function goodsDetail(){
 $goods_id = I('goods_id',0,'intval');
 $goods = M('goods')->where(['id'=>$goods_id])->find();
 if(!$goods){
     $this->ajaxReturn([
         'code' => 1,
         'msg' => '商品不存在'
     ]);
 }
 // 获取商品图片
 $goods['images'] = M('goods_images')->where(['goods_id'=>$goods_id])->select();
 // 获取商品属性
 $attrs = M('goods_attr')->where(['goods_id'=>$goods_id])->select();
 $goods_attr = [];
 foreach ($attrs as $attr){
     $goods_attr[$attr['attr_id']] = $attr['attr_value'];
 }
 $goods['attrs'] = $goods_attr;
 // 获取商品评价
 $goods['comments'] = M('goods_comment')->where(['goods_id'=>$goods_id])->select();
 $data = [
     'code' => 0,
     'msg' => 'success',
     'data' => $goods
 ];
 $this->ajaxReturn($data);
}

添加购物车接口
public function addCart(){
 $user_id = I('user_id',0,'intval');
 $goods_id = I('goods_id',0,'intval');
 $attr_id = I('attr_id',0,'intval');
 $quantity = I('quantity',1,'intval');
 $goods = M('goods')->where(['id'=>$goods_id])->find();
 if(!$goods){
     $this->ajaxReturn([
         'code' => 1,
         'msg' => '商品不存在'
     ]);
 }
 $cart = M('user_cart')->where(['user_id'=>$user_id,'goods_id'=>$goods_id,'attr_id'=>$attr_id])->find();
 if($cart){
     M('user_cart')->where(['id'=>$cart['id']])->setInc('quantity',$quantity);
 }else{
     M('user_cart')->add([
         'user_id' => $user_id,
         'goods_id' => $goods_id,
         'attr_id' => $attr_id,
         'price' => $goods['price'],
         'quantity' => $quantity,
     ]);
 }
 $this->ajaxReturn([
     'code' => 0,
     'msg' => 'success'
 ]);
}

查看购物车接口
public function cartList(){
 $user_id = I('user_id',0,'intval');
 $carts = M('user_cart')->where(['user_id'=>$user_id])->select();
 foreach ($carts as &$cart){
     $goods = M('goods')->where(['id'=>$cart['goods_id']])->find();
     $cart['goods'] = $goods;
     $cart['total_price'] = $cart['price'] * $cart['quantity'];
     unset($cart['price']);
 }
 $data = [
     'code' => 0,
     'msg' => 'success',
     'data' => $carts
 ];
 $this->ajaxReturn($data);
}

下单接口
public function order(){
 $user_id = I('user_id',0,'intval');
 $carts = M('user_cart')->where(['user_id'=>$user_id])->select();
 if(!$carts){
     $this->ajaxReturn([
         'code' => 1,
         'msg' => '购物车为空'
     ]);
 }
 $total_price = 0;
 $order_goods = [];
 foreach ($carts as $cart){
     $goods = M('goods')->where(['id'=>$cart['goods_id']])->find();
     $total_price += $goods['price'] * $cart['quantity'];
     $order_goods[] = [
         'goods_id' => $cart['goods_id'],
         'attr_id' => $cart['attr_id'],
         'price' => $cart['price'],
         'quantity' => $cart['quantity'],
     ];
 }
 $order_sn = 'order_' . date('YmdHis') . rand(1000,9999);
 M()->startTrans();
 try{
     $order_id = M('goods_order')->add([
         'order_sn' => $order_sn,
         'user_id' => $user_id,
         'total_price' => $total_price,
         'pay_price' => $total_price,
         'add_time' => time(),
     ]);
     foreach ($order_goods as &$goods){
         $goods['order_id'] = $order_id;
     }
     M('goods_order_goods')->addAll($order_goods);
     M('user_cart')->where(['user_id'=>$user_id])->delete();
     M()->commit();
     $this->ajaxReturn([
         'code' => 0,
         'msg' => 'success',
         'data' => [
             'order_sn' => $order_sn,
             'total_price' => $total_price,
         ]
     ]);
 }catch(Exception $e){
     M()->rollback();
     $this->ajaxReturn([
         'code' => 1,
         'msg' => '下单失败'
     ]);
 }
}
0.093496s