api/app/controller/Shop.php
2021-01-04 13:45:02 +08:00

158 lines
4.8 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: yancheng<cheng@love.xiaoyan.me>
* Date: 2020/12/10
* Time: 20:35
*/
namespace app\controller;
use app\BaseController;
use app\middleware\LoginCheck;
use app\model\Carts;
use app\model\Goods;
use app\model\Order;
use app\model\OrderGoods;
use think\response\Json;
use think\facade\Db;
class Shop extends BaseController
{
protected $middleware = [
LoginCheck::class
];
public function addToCart(int $goodsId = null,int $count = null){
if(empty($goodsId) || empty($count)){
return $this->fail('参数goodsId,count都必须填写');
}
$goods = (new Goods())->find($goodsId);
if(empty($goods) || $goods->status != 1){
return $this->fail('商品不存在或者已被下架');
}
$cart = new Carts();
$uid = $this->getUserId();
$cart = $cart->where('uid',$uid)
->where('goods_id',$goodsId)
->findOrEmpty();
if($cart->isEmpty()){
$cart->uid = $uid;
$cart->goods_id = $goodsId;
$cart->count = $count;
}else{
$cart->count += $count;
}
if($cart->count > $goods->stock){
return $this->fail('库存不足');
}
$cart->add_price = $goods->sell_price;
if($cart->save()){
return $this->success();
}
return $this->fail('添加购物车失败');
}
/**
* @return Json
*/
public function queryCart(){
$goods = (new Carts())->queryByUid($this->getUserId());
$goods = $this->buildGoodsData($goods->toArray());
return json($goods);
}
public function removeCart($id = null){
if(empty($id)){
return $this->fail('参数 id 必须填写');
}
Carts::destroy($id);
return $this->success();
}
public function updateCart($id = null,$count = null){
if(empty($id) || empty($count)){
return $this->fail('参数 id ,count 都必须填写');
}
if($count < 0){
return $this->fail('你是想我给你钱吗?count必须大于0');
}
$cart = new Carts();
$cart = $cart->find($id);
if(empty($cart)){
return $this->fail('购物车不存在这个商品');
}
if($cart->uid != $this->getUserId()){
return $this->fail('非法操作(不是你的数据)');
}
$goods = (new Goods())->find($cart->goods_id);
if($count > $goods->stock){
return $this->fail('库存不足');
}
$cart->count = $count;
if($cart->save()){
return $this->success($cart->toArray());
}
return $this->fail('添加购物车失败');
}
public function checkout($ids)
{
$cart = new Carts();
$carts = $cart->where('uid',$this->getUserId())->where('id','in',$ids)->select();
if($carts->count() == 0){
return $this->fail('结算失败:没有找到待结算商品');
}
Db::startTrans();
try{
$order = new Order();
$order->uid = $this->getUserId();
$order->amount = 0;
$order->paid = 0;
if($order->save()){
$totalAmount = 0;
foreach ($carts as $c){
$g = (new Goods())->find($c->goods_id);
if(empty($g)){
continue;
}
if($g->stock < $c->count){
throw new \Exception('库存不足');
}
$g->stock -= $c->count;
$g->sell_count += $c->count;
$totalAmount += $g->sell_price * $c->count; // 累加商品价格
OrderGoods::create([
'order_id' => $order->id,
'goods_id' => $c->goods_id,
'amount' => $g->sell_price,
'count' => $c->count,
]);
$c->delete();
$g->save();
}
$order->amount = $totalAmount;
if($order->save()){
Db::commit();
return \json($order);
}
}
}catch (\Exception $e){
Db::rollback();
return $this->fail('结算失败:'.$e->getMessage());
}
return $this->fail('结算失败');
}
public function queryOrder(){
$or = new Order();
$orders = $or->queryByUid($this->getUserId());
foreach ($orders as $o){
$o->goods = $this->buildGoodsData($or->getOrderGoods($o->id)->toArray());
}
return json($orders);
}
}