448 lines
15 KiB
PHP
448 lines
15 KiB
PHP
<?php
|
||
/**
|
||
* Created by PhpStorm.
|
||
* User: sunny
|
||
* Date: 2019/2/11
|
||
* Time: 4:13 PM
|
||
*/
|
||
|
||
namespace App\Api\Controllers;
|
||
|
||
use App\Api\Resources\AddressResource;
|
||
use App\Api\Resources\BuyNowGoodsResource;
|
||
use App\Api\Resources\CartSellerResource;
|
||
use App\Api\Resources\OrdersListResource;
|
||
use App\Logistics\Logistic;
|
||
use App\Models\Cart;
|
||
use App\Models\Freight;
|
||
use App\Models\GoodsParams;
|
||
use App\Models\Seller;
|
||
use App\Models\VipPament;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
use RuLong\Area\Models\UserAddress;
|
||
use RuLong\Order\Exceptions\OrderException;
|
||
use RuLong\Order\Models\Order;
|
||
use RuLong\Order\Models\OrderDetail;
|
||
|
||
class OrdersController extends Controller
|
||
{
|
||
public function __construct()
|
||
{
|
||
$this->middleware('auth.api');
|
||
$this->user = \Auth::guard('api')->user();
|
||
$this->uid = \Auth::guard('api')->id();
|
||
}
|
||
|
||
/**
|
||
* 小程序我的订单列表请求该接口。
|
||
* @param
|
||
* @return array
|
||
*/
|
||
public function index()
|
||
{
|
||
$orders = Order::where('user_id', $this->uid)
|
||
->where('state', '<>', Order::ORDER_CLOSED)
|
||
->orderBy('id', 'desc')
|
||
->get();
|
||
return OrdersListResource::collection($orders)->additional([
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 小程序查看订单详情请求该接口。
|
||
* @param $orderid
|
||
* @return array
|
||
*/
|
||
public function show($id)
|
||
{
|
||
$order = Order::find($id);
|
||
return [
|
||
'data' => new OrdersListResource($order),
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 小程序取消订单该接口。
|
||
* @param $orderid
|
||
* @return array
|
||
*/
|
||
public function delete($id)
|
||
{
|
||
$order = Order::find($id);
|
||
try {
|
||
$order->close();
|
||
return $this->success(['msg' => '签收成功']);
|
||
} catch (\Exception $e) {
|
||
return $this->failed('签收失败' . $e->getmessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 小程序签收订单请求该接口。
|
||
* @param $orderid
|
||
* @return array
|
||
*/
|
||
public function sign($id)
|
||
{
|
||
$order = Order::find($id);
|
||
try {
|
||
$order->signin();
|
||
return $this->success(['msg' => '签收成功']);
|
||
} catch (\Exception $e) {
|
||
return $this->failed('签收失败' . $e->getmessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 小程序物流跟踪请求该接口。
|
||
* @param $orderid
|
||
* @return array
|
||
*/
|
||
public function logistic($id)
|
||
{
|
||
$order = Order::find($id);
|
||
$message = Logistic::getMessage($order->express->company ?? '', $order->express->number ?? ''); //类型csn,编号
|
||
|
||
return [
|
||
'data' => [
|
||
'order_info' => new OrdersListResource($order),
|
||
'logistic_info' => [
|
||
'code' => $message['code'],
|
||
'name' => $message['code'] == 'OK' ? $message['name'] : '',
|
||
'no' => $message['code'] == 'OK' ? $message['no'] : '',
|
||
'logisticLists' => $message['code'] == 'OK' ? $message['list'] : [],
|
||
],
|
||
],
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 小程序购物车下单确认订单数据接口
|
||
* 购物车下单,每个店铺产生一个订单,order表中的订单类型为商品订单GOODS,order表中的订单商品id为0;
|
||
* 商品订单GOODS,商家结算运费=用户支付运费。
|
||
* @param \Illuminate\Http\Request $request
|
||
* @return array
|
||
*/
|
||
public function cartSure(Request $request)
|
||
{
|
||
try {
|
||
DB::transaction(function () use ($request) {
|
||
|
||
$otherParams = $request->otherParams;
|
||
$carts = array();
|
||
$address = UserAddress::find($request->addressId);
|
||
foreach (explode(',', $request->cart_ids) as $key => $value) {
|
||
$cart = Cart::find($value);
|
||
if ($cart) {
|
||
if ($cart->params->stock < 1) {
|
||
$cart->delete();
|
||
} else {
|
||
if ($cart->number < $cart->params->stock) {
|
||
if (!isset($carts[$cart->params->goods->seller_id])) {
|
||
$carts[$cart->params->goods->seller_id] = [];
|
||
}
|
||
array_push($carts[$cart->params->goods->seller_id], $cart);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
$items = array();
|
||
|
||
foreach ($carts as $key => $seller_item) {
|
||
//获取店铺运费、金额、积分小计
|
||
$seller_total[$key] = self::sellerTotal($key, $seller_item, $address);
|
||
|
||
$score_order[$key] = $seller_total[$key]['score'];
|
||
//账户积分
|
||
if ($this->user->account->score < $seller_total[$key]['score']) {
|
||
throw new OrderException('积分不足');
|
||
}
|
||
|
||
$express_type[$key] = 1;
|
||
$remark[$key] = '';
|
||
$items[$key] = array();
|
||
//店铺配送方式及店铺留言
|
||
foreach ($otherParams as $other) {
|
||
if ($other['seller_id'] === $key) {
|
||
$express_type[$key] = $other['express'];
|
||
$remark[$key] = $other['remark'];
|
||
break;
|
||
}
|
||
}
|
||
|
||
foreach ($seller_item as $cart_item) {
|
||
$params = GoodsParams::find($cart_item->params_id);
|
||
array_push($items[$key], new OrderDetail(['goods' => $params, 'number' => $cart_item->number]));
|
||
$cart_item->delete();
|
||
}
|
||
|
||
//Orders::create(用户id,店铺id,订单类型及赠品id,订单商品,收货地址,买家留言,商品总价,可兑换积分,店铺总运费,配送方式,结算总运费)
|
||
\Orders::create($this->uid, $key, ['type' => 'GOODS', 'id' => 0], $items[$key], $address, $remark[$key], null, $score_order[$key], $seller_total[$key]['freightPrice'], $express_type[$key], $seller_total[$key]['freightPrice']);
|
||
}
|
||
});
|
||
$order = Order::where('user_id', $this->uid)->where('state', 'UNPAY')->orderBy('created_at', 'desc')->first();
|
||
return [
|
||
'data' => ['orderid' => $order->id, 'actualPrice' => $order->total - $order->score],
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
];
|
||
} catch (\Exception $e) {
|
||
return [
|
||
'msg' => $e->getmessage(),
|
||
'status' => 'ERROR',
|
||
'status_code' => 400,
|
||
];
|
||
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 购物车多店铺生成多订单,计算店铺商品金额、运费金额、积分金额总和
|
||
* @param
|
||
* @return array
|
||
*/
|
||
private function sellerTotal($seller_id, $seller_item, $address = null)
|
||
{
|
||
$amount = 0;
|
||
$freightPrice = \Params::get('default_freight');
|
||
$heavy = 0;
|
||
$score = 0;
|
||
foreach ($seller_item as $cart_item) {
|
||
$params = GoodsParams::find($cart_item->params_id);
|
||
$amount += $cart_item->number * $params->price;
|
||
if ($params->is_free_freight == 0) {
|
||
$heavy += $cart_item->number * $params->heavy;
|
||
}
|
||
$score += $cart_item->number * $params->score;
|
||
}
|
||
|
||
if ($heavy === 0) {
|
||
$freightPrice = 0; //包邮
|
||
} else {
|
||
//计算每个商户订单的运费
|
||
if (!empty($address)) {
|
||
$freight = Freight::where(['seller_id' => $seller_id, 'area_id' => $address->province->id])->first();
|
||
if ($freight) {
|
||
$freightPrice = $freight->basic;
|
||
if ($freight->heavy < $heavy) {
|
||
$freightPrice += ceil($heavy - $freight->heavy) * $freight->added;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return [
|
||
'freightPrice' => $freightPrice,
|
||
'score' => $score,
|
||
'amount' => $amount - $score,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 单品立即购买,包括普通商品,vip赠品,满仓赠品
|
||
* @param
|
||
* @return array
|
||
*/
|
||
public function buyNow(Request $request)
|
||
{
|
||
$address = $request->addressId ? UserAddress::find($request->addressId) : null;
|
||
$params_id = $request->params_id;
|
||
$number = $request->number;
|
||
$item_type = $request->item_type;
|
||
$params = GoodsParams::find($params_id);
|
||
|
||
$seller = Seller::find($params->goods->seller_id);
|
||
|
||
$amount = $params->price * $number;
|
||
$score = 0;
|
||
|
||
$freightPrice = \Params::get('default_freight');
|
||
|
||
if ($item_type == 'VIP_GIFT') {
|
||
//确认是否有领取权限
|
||
if (self::canTakeVipGift() !== true) {
|
||
return $this->failed('没有权限领取');
|
||
}
|
||
$amount = 0;
|
||
$freightPrice = 9; //赠品固定运费9元
|
||
|
||
} elseif ($item_type == 'FULL_GIFT') {
|
||
//确认是否有领取权限
|
||
if (self::canTakeFullGift() !== true) {
|
||
return $this->failed('没有权限领取');
|
||
}
|
||
$amount = 0;
|
||
//计算运费
|
||
$heavy = $number * $params->heavy;
|
||
$freightPrice = self::freightPrice($seller->id, $heavy, $address);
|
||
|
||
} elseif ($item_type == 'GOODS') {
|
||
$score = $params->score * $number;
|
||
$amount -= $score;
|
||
//计算运费
|
||
$heavy = $number * $params->heavy;
|
||
$freightPrice = self::freightPrice($seller->id, $heavy, $address);
|
||
}
|
||
return [
|
||
'data' => [
|
||
'seller' => new CartSellerResource($seller),
|
||
'goods' => new BuyNowGoodsResource($params),
|
||
'number' => $number,
|
||
'item_type' => $item_type,
|
||
'total' => [
|
||
'amount' => number_format($amount, 2),
|
||
'score' => number_format($score, 2),
|
||
'freight' => $freightPrice,
|
||
'freightOriginal' => $freightPrice,
|
||
'orderPrice' => $amount + $freightPrice,
|
||
|
||
],
|
||
'express' => [
|
||
'type1' => 1,
|
||
'type2' => 0,
|
||
],
|
||
'address' => $address ? new AddressResource($address) : [],
|
||
],
|
||
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
];
|
||
|
||
}
|
||
|
||
/**
|
||
* 单品生成订单,,包括商品订单、vip赠品订单、满仓赠品订单
|
||
* @param
|
||
* @return array
|
||
*/
|
||
public function save(Request $request)
|
||
{
|
||
try {
|
||
DB::transaction(function () use ($request) {
|
||
//收货地址
|
||
$address = UserAddress::find($request->addressId);
|
||
|
||
//请求参数
|
||
$params_id = $request->params_id;
|
||
$number = $request->number;
|
||
$express_type = $request->express_type;
|
||
$remark = $request->remark;
|
||
$item_type = $request->item_type;
|
||
|
||
//立即购买的商品规格
|
||
$params = GoodsParams::find($params_id);
|
||
|
||
//商户id
|
||
$seller_id = $params->goods->seller_id;
|
||
|
||
//积分计算
|
||
$score = $params->score * $number;
|
||
|
||
$score_order = 0;
|
||
|
||
if ($item_type == 'GOODS') {
|
||
$score_order = $score;
|
||
}
|
||
|
||
if ($this->user->account->score < $score_order) {
|
||
throw new OrderException('积分不足');
|
||
}
|
||
|
||
//运费计算
|
||
$heavy = $number * $params->heavy;
|
||
$freightPrice = self::freightPrice($seller_id, $heavy, $address);
|
||
|
||
//商品详情
|
||
$items = array();
|
||
array_push($items, new OrderDetail(['goods' => $params, 'number' => $number]));
|
||
|
||
//订单类型 GOODS:商品订单 VIP_GIFT:赠品订单 FULL_GIFT:满仓订单
|
||
$order_item = [
|
||
'type' => $item_type,
|
||
'id' => $item_type == 'GOODS' ? 0 : $params->id,
|
||
];
|
||
|
||
//生成订单
|
||
\Orders::create($this->uid, $seller_id, $order_item, $items, $address, $remark, null, $score_order, $freightPrice, $express_type, $freightPrice);
|
||
|
||
});
|
||
$order = Order::where('user_id', $this->uid)->where('state', 'UNPAY')->orderBy('created_at', 'desc')->first();
|
||
return [
|
||
'data' => ['orderid' => $order->id, 'actualPrice' => $order->total - $order->score],
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
];
|
||
} catch (\Exception $e) {
|
||
return [
|
||
'msg' => $e->getmessage(),
|
||
'status' => 'ERROR',
|
||
'status_code' => 400,
|
||
];
|
||
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 单品订单运费计算
|
||
* @param
|
||
* @return array
|
||
*/
|
||
private function freightPrice($seller_id, $heavy, $address = null)
|
||
{
|
||
//运费计算
|
||
$freightPrice = \Params::get('default_freight');
|
||
|
||
if ($heavy === 0) {
|
||
$freightPrice = 0; //包邮
|
||
} else {
|
||
//计算每个商户订单的运费
|
||
if (!empty($address)) {
|
||
$freight = Freight::where(['seller_id' => $seller_id, 'area_id' => $address->province->id])->first();
|
||
if ($freight) {
|
||
$freightPrice = $freight->basic;
|
||
if ($freight->heavy < $heavy) {
|
||
$freightPrice = ceil($heavy - $freight->heavy) * $freight->added;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return $freightPrice;
|
||
}
|
||
|
||
/**
|
||
* 验证是否可以领取vip赠品
|
||
* @param
|
||
* @return boolean
|
||
*/
|
||
private function canTakeVipGift()
|
||
{
|
||
$vipPay = VipPament::where(['user_id' => $this->uid, 'state' => 'SUCCESS'])->first();
|
||
$isVipUser = $vipPay ? true : false;
|
||
$vipOrders = Order::where(['user_id' => $this->uid, 'item_type' => 'VIP_GIFT'])->whereRaw('substring(cast(status as char),1,1) = 1')->first();
|
||
$canTakeVipGift = !$vipOrders && $isVipUser ? true : false;
|
||
return $canTakeVipGift;
|
||
}
|
||
|
||
/**
|
||
* 验证是否可以领取满仓赠品
|
||
* @param
|
||
* @return boolean
|
||
*/
|
||
private function canTakeFullGift()
|
||
{
|
||
$isfull = $this->user->account->act_a > 0 ? true : false;
|
||
$fullOrder = Order::where(['user_id' => $this->uid, 'item_type' => 'FULL_GIFT'])->whereRaw('substring(cast(status as char),1,1) = 1')->first();
|
||
$canTakeFullGift = !$fullOrder && $isfull ? true : false;
|
||
return $canTakeFullGift;
|
||
}
|
||
}
|