|
|
<?php
|
|
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
use app\common\controller\Api;
|
|
|
use think\Db;
|
|
|
use think\Validate;
|
|
|
|
|
|
/**
|
|
|
* 订单接口
|
|
|
*/
|
|
|
class Order extends Api
|
|
|
{
|
|
|
protected $orderModel;
|
|
|
protected $carModel;
|
|
|
protected $areaExtendModel;
|
|
|
protected $integralGoodsModel;
|
|
|
|
|
|
protected function _initialize()
|
|
|
{
|
|
|
parent::_initialize();
|
|
|
$this->orderModel = new \app\api\model\Order();
|
|
|
$this->carModel = new \app\api\model\Car();
|
|
|
$this->areaExtendModel = new \app\api\model\AreaExtend();
|
|
|
$this->integralGoodsModel = new \app\api\model\IntegralGoods();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (确认订单(购物车))
|
|
|
* @ApiSummary (确认订单(购物车))
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
|
|
|
* @ApiParams (name=car_id, type=string, required=true, description="购物车id 例 1,2,3")
|
|
|
* @ApiParams (name=ticket_user_id, type=string, required=false, description="用户优惠券id")
|
|
|
* @ApiParams (name=ticket_code, type=string, required=false, description="优惠码")
|
|
|
* @ApiParams (name=province_id, type=string, required=true, description="省id")
|
|
|
* @ApiParams (name=city_id, type=string, required=true, description="市")
|
|
|
* @ApiParams (name=county_id, type=string, required=true, description="县")
|
|
|
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
|
|
|
* @ApiRoute (/api/order/order)
|
|
|
* @ApiReturn({
|
|
|
|
|
|
},
|
|
|
})
|
|
|
*/
|
|
|
public function order()
|
|
|
{
|
|
|
$carModel = new \app\api\model\Car();
|
|
|
$userAddressModel = new \app\api\model\UserAddress();
|
|
|
$goodsModel = new \app\api\model\Goods();
|
|
|
$ticketCodeModel = new \app\api\model\Ticketcode();
|
|
|
$userTicketModel = new \app\api\model\UserTicket();
|
|
|
$userId = $this->getUserId();
|
|
|
$carId = $this->request->param('car_id');
|
|
|
$ticketId = $this->request->param('ticket_user_id');
|
|
|
$ticketCode = $this->request->param('ticket_code');
|
|
|
$provinceId = $this->request->param('province_id');
|
|
|
$cityId = $this->request->param('city_id');
|
|
|
$countyId = $this->request->param('county_id');
|
|
|
$postageType = $this->request->param('postage_type');
|
|
|
if (empty($carId)) $this->error('缺少参数 car_id!');
|
|
|
//判断优惠券和优惠码不能同时使用
|
|
|
if (!empty($ticketCode) && !empty($ticketId)) $this->error('优惠券与优惠码不能同时使用!');
|
|
|
if (empty($provinceId)) {
|
|
|
//获取用户默认地址
|
|
|
$address = $userAddressModel->getDefaultData(['user_id' => $userId]);
|
|
|
$provinceId = $address['province_id'];
|
|
|
}
|
|
|
//获取普通商品id
|
|
|
$goodsIds = $carModel->where(['id' => ['in', $carId], 'type' => 1])->column('goods_id');
|
|
|
//获取积分商品id
|
|
|
$goodsIntegralIds = $carModel->where(['id' => ['in', $carId], 'type' => 2])->column('goods_id');
|
|
|
//获取普通商品状态
|
|
|
if ($goodsIds) {
|
|
|
$goodsList = $goodsModel->selectGoodsStatus(['id' => ['in', $goodsIds]]);
|
|
|
|
|
|
//判断商品是否有库存和状态
|
|
|
foreach ($goodsList as $k => $v) {
|
|
|
if ($v['status'] == 2) $this->error($v['ch_name'] . '商品已下架');
|
|
|
if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
|
|
|
//判断用户收货区域是否有库存
|
|
|
$areaStock = $goodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]);
|
|
|
if (!$areaStock) $data['is_lack_stock'] = 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//获取积分商品状态
|
|
|
if ($goodsIntegralIds) {
|
|
|
$goodsIntegralList = $this->integralGoodsModel->selectGoodsStatus(['id' => ['in', $goodsIntegralIds]]);
|
|
|
|
|
|
//判断商品是否有库存和状态
|
|
|
foreach ($goodsIntegralList as $k => $v) {
|
|
|
if ($v['status'] == 2) $this->error($v['ch_name'] . '商品已下架');
|
|
|
if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
|
|
|
//判断用户收货区域是否有库存
|
|
|
$areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]);
|
|
|
if (!$areaStock) $data['is_lack_stock'] = 1;
|
|
|
}
|
|
|
}
|
|
|
//判断优惠码是否可用
|
|
|
if (!empty($ticketCode)) {
|
|
|
$ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->field('user_id,validtime')->find();
|
|
|
if (!$ticketCodeInfo) $this->error('优惠码不存在!');
|
|
|
if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
|
|
|
if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
|
|
|
}
|
|
|
|
|
|
//判断优惠券是否可用
|
|
|
if (!empty($ticketId)) {
|
|
|
$ticketInfo = $userTicketModel->where(['user_id' => $userId, 'id' => $ticketId, 'status' => 1])->find();
|
|
|
if (!$ticketInfo) $this->error('优惠码不存在!');
|
|
|
}
|
|
|
|
|
|
//获取原价,现价,折扣
|
|
|
$priceArr = $this->carModel->getCarMoney(['id' => ['in', $carId]]);
|
|
|
$data['price'] = 0; //折扣后价格
|
|
|
$data['discount_price'] = 0; //折扣金额
|
|
|
$data['original_price'] = 0; //原价
|
|
|
$data['integral'] = 0; //积分
|
|
|
foreach ($priceArr as $k => $v) {
|
|
|
$priceArr[$k]['user_type'] = $this->user['type'];
|
|
|
$data['price'] += round(get_price($v), 2);
|
|
|
$data['discount_price'] += round(get_discount_price($v), 2);
|
|
|
$data['original_price'] += $v['goods_price'];
|
|
|
if (!empty($v['integral'])) $data['integral'] += $v['integral'];
|
|
|
|
|
|
}
|
|
|
//获取运费
|
|
|
$is_special = $this->areaExtendModel->where(['province_id' => $provinceId])->value('is_special');
|
|
|
|
|
|
if ($is_special == 1 && $data['price'] >= 200) {
|
|
|
$postage = 0;
|
|
|
} elseif ($data['price'] >= 300) {
|
|
|
$postage = 0;
|
|
|
} else {
|
|
|
//配送为闪送
|
|
|
$postageWhere = ['province_id' => $provinceId, 'city_id' => $cityId, 'county_id' => $countyId];
|
|
|
if ($postageType == 2) {
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
|
|
|
} else {
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
|
|
|
}
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage1');
|
|
|
}
|
|
|
}
|
|
|
$data['postage'] = $postage;
|
|
|
$data['price'] += $postage;
|
|
|
|
|
|
//使用优惠码
|
|
|
if (!empty($ticketCodeInfo)) {
|
|
|
$data['price'] = $data['price'] - $ticketCodeInfo['price'];
|
|
|
$data['ticketcode_discount_price'] = $ticketCodeInfo['price'];
|
|
|
}
|
|
|
|
|
|
//使用优惠券
|
|
|
if (!empty($ticketInfo)) {
|
|
|
$data['price'] = $data['price'] - $ticketInfo['price'];
|
|
|
$data['ticket_discount_price'] = $ticketInfo['price'];
|
|
|
}
|
|
|
|
|
|
$this->success('success', $data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (创建订单(购物车))
|
|
|
* @ApiSummary (创建订单(购物车))
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
|
|
|
* @ApiParams (name=car_id, type=string, required=true, description="购物车id 例 1,2,3")
|
|
|
* @ApiParams (name=ticket_user_id, type=string, required=false, description="用户优惠券id")
|
|
|
* @ApiParams (name=ticket_code, type=string, required=false, description="优惠码")
|
|
|
* @ApiParams (name=address, type=string, required=true, description="商品收货地址")
|
|
|
* @ApiParams (name=present_id, type=string, required=false, description="赠品id")
|
|
|
* @ApiParams (name=delivery, type=string, required=false, description="配送时间")
|
|
|
* @ApiParams (name=invoice_type, type=string, required=false, description="发票类型:1=个人或事业单位,2=企业")
|
|
|
* @ApiParams (name=email, type=string, required=false, description="电子发票邮箱")
|
|
|
* @ApiParams (name=taitou, type=string, required=false, description="发票抬头")
|
|
|
* @ApiParams (name=duty_num, type=string, required=false, description="发票税号")
|
|
|
* @ApiParams (name=invoice_address, type=string, required=false, description="发票收货地址")
|
|
|
* @ApiParams (name=order_note, type=string, required=false, description="订单备注")
|
|
|
* @ApiRoute (/api/order/createOrder)
|
|
|
* @ApiReturn({
|
|
|
|
|
|
},
|
|
|
})
|
|
|
*/
|
|
|
public function createOrder()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|