You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
4.2 KiB
135 lines
4.2 KiB
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\api\model\Order as OrderModel;
|
|
use app\api\model\Cart as CartModel;
|
|
use app\common\enum\DeliveryType as DeliveryTypeEnum;
|
|
use app\common\enum\order\PayType as PayTypeEnum;
|
|
|
|
/**
|
|
* 订单控制器
|
|
* Class Order
|
|
* @package app\api\controller
|
|
*/
|
|
class Order extends Controller
|
|
{
|
|
/* @var \app\api\model\User $user */
|
|
private $user;
|
|
|
|
/**
|
|
* 构造方法
|
|
* @throws \app\common\exception\BaseException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->user = $this->getUser(); // 用户信息
|
|
}
|
|
|
|
/**
|
|
* 订单确认-立即购买
|
|
* @param int $goods_id 商品id
|
|
* @param int $goods_num 购买数量
|
|
* @param int $goods_sku_id 商品sku_id
|
|
* @param int $delivery 配送方式
|
|
* @param int $pay_type 支付方式
|
|
* @param int $coupon_id 优惠券id
|
|
* @param int $shop_id 自提门店id
|
|
* @param string $remark 买家留言
|
|
* @return array
|
|
* @throws \app\common\exception\BaseException
|
|
* @throws \think\exception\DbException
|
|
* @throws \Exception
|
|
*/
|
|
public function buyNow(
|
|
$goods_id,
|
|
$goods_num,
|
|
$goods_sku_id,
|
|
$delivery = DeliveryTypeEnum::EXPRESS,
|
|
$pay_type = PayTypeEnum::WECHAT,
|
|
$shop_id = 0,
|
|
$coupon_id = null,
|
|
$remark = ''
|
|
)
|
|
{
|
|
// 商品结算信息
|
|
$model = new OrderModel;
|
|
$order = $model->getBuyNow(
|
|
$this->user,
|
|
$goods_id,
|
|
$goods_num,
|
|
$goods_sku_id,
|
|
$delivery,
|
|
$pay_type,
|
|
$shop_id
|
|
);
|
|
if (!$this->request->isPost()) {
|
|
return $this->renderSuccess($order);
|
|
}
|
|
if ($model->hasError()) {
|
|
return $this->renderError($model->getError());
|
|
}
|
|
// 创建订单
|
|
if (!$model->createOrder($this->user, $order, $coupon_id, $remark)) {
|
|
return $this->renderError($model->getError() ?: '订单创建失败');
|
|
}
|
|
// 构建微信支付请求
|
|
$payment = ($pay_type == PayTypeEnum::WECHAT) ? $model->paymentByWechat($this->user) : [];
|
|
return $this->renderSuccess([
|
|
'order_id' => $model['order_id'], // 订单id
|
|
'pay_type' => $pay_type, // 支付方式
|
|
'payment' => $payment // 微信支付参数
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 订单确认-购物车结算
|
|
* @param string $cart_ids (支持字符串ID集)
|
|
* @param int $delivery 配送方式
|
|
* @param int $pay_type 支付方式
|
|
* @param int $shop_id 自提门店id
|
|
* @param int $coupon_id 优惠券id
|
|
* @param string $remark 买家留言
|
|
* @return array
|
|
* @throws \app\common\exception\BaseException
|
|
* @throws \think\Exception
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
* @throws \Exception
|
|
*/
|
|
public function cart(
|
|
$cart_ids,
|
|
$delivery = DeliveryTypeEnum::EXPRESS,
|
|
$pay_type = PayTypeEnum::WECHAT,
|
|
$shop_id = 0,
|
|
$coupon_id = null,
|
|
$remark = ''
|
|
)
|
|
{
|
|
// 商品结算信息
|
|
$Cart = new CartModel($this->user);
|
|
$order = $Cart->getList($cart_ids, $delivery, $pay_type, $shop_id);
|
|
if (!$this->request->isPost()) {
|
|
return $this->renderSuccess($order);
|
|
}
|
|
// 创建订单
|
|
$model = new OrderModel;
|
|
if (!$model->createOrder($this->user, $order, $coupon_id, $remark)) {
|
|
return $this->renderError($model->getError() ?: '订单创建失败');
|
|
}
|
|
// 移出购物车中已下单的商品
|
|
$Cart->clearAll($cart_ids);
|
|
// 构建微信支付请求
|
|
$payment = ($pay_type == PayTypeEnum::WECHAT) ? $model->paymentByWechat($this->user) : [];
|
|
// 返回状态
|
|
return $this->renderSuccess([
|
|
'order_id' => $model['order_id'], // 订单id
|
|
'pay_type' => $pay_type, // 支付方式
|
|
'payment' => $payment // 微信支付参数
|
|
]);
|
|
}
|
|
|
|
}
|
|
|