| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\controller\api\store\order;
- use app\controller\admin\user\OilLevel;
- use think\App;
- use crmeb\basic\BaseController;
- use crmeb\services\LockService;
- use think\exception\ValidateException;
- use app\validate\api\UserReceiptValidate;
- use app\common\repositories\store\order\StoreCartRepository;
- use app\common\repositories\delivery\DeliveryOrderRepository;
- use app\common\repositories\store\order\StoreOrderRepository;
- use app\common\repositories\store\order\StoreGroupOrderRepository;
- use app\common\repositories\store\order\StoreOrderCreateRepository;
- use app\common\repositories\store\order\StoreOrderReceiptRepository;
- /**
- * Class StoreOrder
- * @package app\controller\api\store\order
- * @author xaboy
- * @day 2020/6/10
- */
- class StoreOrder extends BaseController
- {
- /**
- * @var StoreOrderRepository
- */
- protected $repository;
- /**
- * StoreOrder constructor.
- * @param App $app
- * @param StoreOrderRepository $repository
- */
- public function __construct(App $app, StoreOrderRepository $repository)
- {
- parent::__construct($app);
- $this->repository = $repository;
- }
- public function v2CheckOrder(StoreCartRepository $cartRepository, StoreOrderCreateRepository $orderCreateRepository)
- {
- $cartId = (array)$this->request->param('cart_id', []);
- $addressId = (int)$this->request->param('address_id');
- $couponIds = (array)$this->request->param('use_coupon', []);
- $takes = (array)$this->request->param('takes', []);
- $useIntegral = (bool)$this->request->param('use_integral', false);
- $user = $this->request->userInfo();
- $uid = $user->uid;
- $spread_uid = $user->spread_uid;
- if ($spread_uid<=0){
- return app('json')->fail('没有推荐人无法下单');
- }
- if (!($count = count($cartId)) || $count != count($cartRepository->validIntersection($cartId, $uid)))
- return app('json')->fail('数据无效');
- $orderInfo = $orderCreateRepository->v2CartIdByOrderInfo($user, $cartId, $takes, $couponIds, $useIntegral, $addressId);
- return app('json')->success($orderInfo);
- }
- public function v2CreateOrder(StoreCartRepository $cartRepository, StoreOrderCreateRepository $orderCreateRepository)
- {
- $cartId = (array)$this->request->param('cart_id', []);
- $addressId = (int)$this->request->param('address_id');
- $couponIds = (array)$this->request->param('use_coupon', []);
- $takes = (array)$this->request->param('takes', []);
- $useIntegral = (bool)$this->request->param('use_integral', false);
- $receipt_data = (array)$this->request->param('receipt_data', []);
- $extend = (array)$this->request->param('extend', []);
- $mark = (array)$this->request->param('mark', []);
- $payType = $this->request->param('pay_type');
- $key = (string)$this->request->param('key');
- $post = (array)$this->request->param('post');
- if(!$key){
- return app('json')->fail('订单操作超时,请刷新页面');
- }
- $payType = ($payType === 'pc') ? 'balance' : $payType;
- if (!in_array($payType, StoreOrderRepository::PAY_TYPE, true))
- return app('json')->fail('请选择正确的支付方式');
- $validate = app()->make(UserReceiptValidate::class);
- foreach ($receipt_data as $receipt) {
- if (!is_array($receipt)) throw new ValidateException('发票信息有误');
- $validate->check($receipt);
- }
- $uid = $this->request->uid();
- $user = $this->request->userInfo();
- $spread_uid = $user->spread_uid;
- if ($spread_uid<=0){
- return app('json')->fail('没有推荐人无法下单');
- }
- if (!($count = count($cartId)) || $count != count($cartRepository->validIntersection($cartId, $uid)))
- return app('json')->fail('已生成订单,请勿重复提交~');
- $groupOrder = app()->make(LockService::class)->exec('order.create', function () use ($key, $orderCreateRepository, $receipt_data, $mark, $extend, $cartId, $payType, $takes, $couponIds, $useIntegral, $addressId, $post) {
- return $orderCreateRepository->v2CreateOrder($key, array_search($payType, StoreOrderRepository::PAY_TYPE), $this->request->userInfo(), $cartId, $extend, $mark, $receipt_data, $takes, $couponIds, $useIntegral, $addressId, $post);
- });
- //全部改成创建订单,下一步调用支付
- try{
- $orderList = $this->repository->getSearch([])->where('group_order_id',$groupOrder->group_order_id)->select();
- foreach ($orderList as $item ) {
- $this->repository->autoPrinter($item->order_id, $item->mer_id, 2);
- }
- }catch (\Exception $e) {
- }
- return app('json')->success(['order_id' => $groupOrder->group_order_id]);
- /**
- * 以下是立即支付,返回支付或者跳转信息
- */
- //try {
- // return $this->repository->pay($payType, $this->request->userInfo(), $groupOrder, $this->request->param('return_url'), $this->request->isApp());
- //} catch (\Exception $e) {
- // return app('json')->status('error', $e->getMessage(), ['order_id' => $groupOrder->group_order_id]);
- //}
- }
- /**
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author xaboy
- * @day 2020/6/10
- */
- public function lst()
- {
- [$page, $limit] = $this->getPage();
- $where['status'] = $this->request->param('status');
- $where['search'] = $this->request->param('store_name');
- $where['uid'] = $this->request->uid();
- $where['paid'] = 1;
- // $where['is_user'] = 1;
- $data = $this->repository->getList($where, $page, $limit);
- return app('json')->success($data);
- }
- /**
- * @param $id
- * @return mixed
- * @author xaboy
- * @day 2020/6/10
- */
- public function detail($id)
- {
- $order = $this->repository->getDetail((int)$id, $this->request->uid());
- if (!$order)
- return app('json')->fail('订单不存在');
- if ($order->order_type == 1) {
- $order->append(['take', 'refund_status', 'open_receipt']);
- }
- return app('json')->success($order->toArray());
- }
- /**
- * @return mixed
- * @author xaboy
- * @day 2020/6/10
- */
- public function number()
- {
- return app('json')->success(['orderPrice' => $this->request->userInfo()->pay_price] + $this->repository->userOrderNumber($this->request->uid()));
- }
- /**
- * @param StoreGroupOrderRepository $groupOrderRepository
- * @return mixed
- * @author xaboy
- * @day 2020/6/10
- */
- public function groupOrderList(StoreGroupOrderRepository $groupOrderRepository)
- {
- [$page, $limit] = $this->getPage();
- $list = $groupOrderRepository->getList(['uid' => $this->request->uid(), 'paid' => 0], $page, $limit);
- return app('json')->success($list);
- }
- /**
- * @param $id
- * @param StoreGroupOrderRepository $groupOrderRepository
- * @return mixed
- * @author xaboy
- * @day 2020/6/10
- */
- public function groupOrderDetail($id, StoreGroupOrderRepository $groupOrderRepository)
- {
- $groupOrder = $groupOrderRepository->detail($this->request->uid(), (int)$id);
- if (!$groupOrder)
- return app('json')->fail('订单不存在');
- else
- return app('json')->success($groupOrder->append(['cancel_time', 'cancel_unix'])->toArray());
- }
- /**
- * 订单状态查询
- * @param $id
- * @param StoreGroupOrderRepository $groupOrderRepository
- * @return \think\response\Json
- * @author Qinii
- */
- public function groupOrderStatus($id, StoreGroupOrderRepository $groupOrderRepository)
- {
- $groupOrder = $groupOrderRepository->status($this->request->uid(), intval($id));
- if (!$groupOrder)
- return app('json')->fail('订单不存在');
- if ($groupOrder->paid) $groupOrder->append(['give_coupon']);
- $activity_type = 0;
- $activity_id = 0;
- foreach ($groupOrder->orderList as $order) {
- $activity_type = max($order->activity_type, $activity_type);
- if ($order->activity_type == 4 && $groupOrder->paid) {
- $order->append(['orderProduct']);
- $activity_id = $order->orderProduct[0]['activity_id'];
- }
- }
- $groupOrder->activity_type = $activity_type;
- $groupOrder->activity_id = $activity_id;
- return app('json')->success($groupOrder->toArray());
- }
- /**
- * @param $id
- * @param StoreGroupOrderRepository $groupOrderRepository
- * @return mixed
- * @author xaboy
- * @day 2020/6/10
- */
- public function cancelGroupOrder($id, StoreGroupOrderRepository $groupOrderRepository)
- {
- $groupOrderRepository->cancel((int)$id, $this->request->uid());
- return app('json')->success('取消成功');
- }
- /**
- * 订单付款操作
- * @param $id
- * @param StoreGroupOrderRepository $groupOrderRepository
- * @return mixed|\think\response\Json
- * @author Qinii
- */
- public function groupOrderPay($id, StoreGroupOrderRepository $groupOrderRepository)
- {
- //TODO 佣金结算,佣金退回,物流查询
- $type = $this->request->param('type');
- $is_points = $this->request->param('is_points',0);
- if (!in_array($type, StoreOrderRepository::PAY_TYPE))
- return app('json')->fail('请选择正确的支付方式');
- $groupOrder = $groupOrderRepository->detail($this->request->uid(), (int)$id, false);
- if (!$groupOrder)
- return app('json')->fail('订单不存在或已支付');
- $this->repository->changePayType($groupOrder, array_search($type, StoreOrderRepository::PAY_TYPE));
- if ($groupOrder['pay_price'] == 0) {
- $this->repository->paySuccess($groupOrder);
- return app('json')->status('success', '支付成功', ['order_id' => $groupOrder['group_order_id']]);
- }
- if ($type == 'offline') {
- if (count($groupOrder['orderList']) > 1) {
- return app('json')->fail('线下支付仅支持同店铺商品');
- }
- if (!systemConfig('offline_switch')) {
- return app('json')->fail('未开启线下支付功能');
- }
- if (!(($groupOrder['orderList'][0]->merchant['offline_switch']) ?? '')) {
- return app('json')->fail('该店铺未开启线下支付');
- }
- return app('json')->status('success', '线下支付,请告知收银员', ['order_id' => $groupOrder['group_order_id']]);
- }
- @file_put_contents('quanju.txt',"-123\r\n",8);
- try {
- return $this->repository->pay($type, $this->request->userInfo(), $groupOrder, $this->request->param('return_url'), $this->request->isApp());
- } catch (\Exception $e) {
- return app('json')->status('error', $e->getMessage(), ['order_id' => $groupOrder->group_order_id]);
- }
- }
- public function take($id)
- {
- $this->repository->takeOrder($id, $this->request->userInfo());
- return app('json')->success('确认收货成功');
- }
- public function express($id)
- {
- $order = $this->repository->getWhere(['order_id' => $id, 'is_del' => 0]);
- if (!$order)
- return app('json')->fail('订单不存在');
- if (!$order->delivery_type || !$order->delivery_id)
- return app('json')->fail('订单未发货');
- $express = $this->repository->express($id,null);
- $order->append(['orderProduct']);
- return app('json')->success(compact('express', 'order'));
- }
- public function verifyCode($id)
- {
- $order = $this->repository->getWhere(['order_id' => $id, 'uid' => $this->request->uid(), 'is_del' => 0]);
- if (!$order)return app('json')->fail('订单状态有误');
- return app('json')->success(['qrcode' => $this->repository->wxQrcode($id, $order)]);
- }
- public function del($id)
- {
- $this->repository->userDel($id, $this->request->uid());
- return app('json')->success('删除成功');
- }
- public function createReceipt($id)
- {
- $data = $this->request->params(['receipt_type' , 'receipt_title' , 'duty_paragraph', 'receipt_title_type', 'bank_name', 'bank_code', 'address','tel', 'email']);
- $order = $this->repository->getWhere(['order_id' => $id, 'uid' => $this->request->uid(), 'is_del' => 0]);
- if (!$order) return app('json')->fail('订单不属于您或不存在');
- app()->make(StoreOrderReceiptRepository::class)->add($data, $order);
- return app('json')->success('操作成功');
- }
- public function getOrderDelivery($id, DeliveryOrderRepository $orderRepository)
- {
- $res = $orderRepository->show($id, $this->request->uid());
- return app('json')->success($res);
- }
- public function getCashierOrder($id)
- {
- $data = $this->repository->payConfig($id, $this->request->uid());
- return app('json')->success($data);
- }
- public function payConfig()
- {
- $id = $this->request->param('id',0);
- $type = $this->request->param('type',0);
- if ($type) {
- $data = $this->repository->payConfigPresell($id, $this->request->uid());
- } else {
- $data = $this->repository->payConfig($id, $this->request->uid());
- }
- return app('json')->success($data);
- }
- public function cancelOrder($id)
- {
- $order = $this->repository->getSearch(['uid' => $this->request->uid()])->where('order_id',$id)->find();
- if (!$order) return app('json')->fail('订单状态有误');
- // if (!$order->refund_status) return app('json')->fail('订单已过退款/退货期限');
- if ($order->status < 0) return app('json')->fail('订单已退款');
- if ($order->status == 10 || !$order->is_cancel) return app('json')->fail('订单不支持退款');
- if ($order->is_virtual !== 4) return app('json')->fail('订单不支持退款');
- $this->repository->cancelOrder($order);
- return app('json')->success('订单已取消');
- }
- /**
- * 核销
- * @day 2020/8/15
- */
- // public function validateVerify()
- // {
- // $data = $this->request->params(['data']);
- // $order_id = $this->request->params(['order_id']);
- // $user = $this->request->userInfo();
- //// $num = $this->request->params(['num']);
- //// $merId = $this->request->merId();
- // // 根据订单ID、商家ID、验证码和订单类型查询订单,并连带查询订单产品信息
- // $order = $this->repository->getWhere(['order_id' => $order_id, 'order_type' => 1], '*', ['orderProduct']);
- // // 如果订单不存在,则抛出验证异常
- // if (!$order) return app('json')->fail('订单不存在');
- // // 如果订单未支付,则抛出验证异常
- // if (!$order->paid) return app('json')->fail('订单未支付');
- // // 如果订单已全部核销,则抛出验证异常
- // if ($order['status']) return app('json')->fail('订单已全部核销,请勿重复操作');
- // if ($user['oil_level']>0){
- // $name = \app\common\model\user\OilLevel::where('id',$user['oil_level'])->value('name');
- //
- // if ($name!=='服务中心'){
- // return app('json')->fail('不是服务中心不能进行核销');
- // }
- // }else{
- //
- // return app('json')->fail('不是服务中心不能进行核销');
- // }
- // $this->repository->verifyPartOrder($order, $data,$user['uid']);
- // return app('json')->success('订单核销成功');
- // }
- /**
- * 验证核销并返回商品内容
- * @day 2020/8/15
- */
- public function validateVerify()
- {
- $data = $this->request->params(['data']);
- $order_id = $this->request->params(['order_id']);
- $user = $this->request->userInfo();
- var_dump($data);die();
- // $num = $this->request->params(['num']);
- // $merId = $this->request->merId();
- // 根据订单ID、商家ID、验证码和订单类型查询订单,并连带查询订单产品信息
- $order = $this->repository->getWhere(['order_id' => $order_id, 'order_type' => 1], '*', ['orderProduct']);
- // 如果订单不存在,则抛出验证异常
- if (!$order) return app('json')->fail('订单不存在');
- // 如果订单未支付,则抛出验证异常
- if (!$order->paid) return app('json')->fail('订单未支付');
- // 如果订单已全部核销,则抛出验证异常
- if ($order['status']) return app('json')->fail('订单已全部核销,请勿重复操作');
- if ($user['oil_level']>0){
- $name = \app\common\model\user\OilLevel::where('id',$user['oil_level'])->value('name');
- if ($name!=='服务中心'){
- return app('json')->fail('不是服务中心不能进行核销');
- }
- }else{
- return app('json')->fail('不是服务中心不能进行核销');
- }
- $this->repository->verifyPartOrder($order, $data,$user['uid']);
- return app('json')->success('订单核销成功');
- // $order_id = $this->request->params(['order_sn']);
- //
- //// $num = $this->request->params(['num']);
- //// $merId = $this->request->merId();
- // // 根据订单ID、商家ID、验证码和订单类型查询订单,并连带查询订单产品信息
- // $order = $this->repository->getWhere(['order_sn' => $order_id], '*', ['orderProduct']);
- // // 如果订单不存在,则抛出验证异常
- // if (!$order) return app('json')->fail('订单不存在');
- // // 如果订单未支付,则抛出验证异常
- // if (!$order->paid) return app('json')->fail('订单未支付');
- // // 如果订单已全部核销,则抛出验证异常
- // if ($order['status']) return app('json')->fail('订单已全部核销,请勿重复操作');
- // $user = $this->request->userInfo();
- // if ($user['oil_level']>0){
- // $name = \app\common\model\user\OilLevel::where('id',$user['oil_level'])->value('name');
- //
- // if ($name!=='服务中心'){
- // return app('json')->fail('不是服务中心不能进行核销');
- // }
- // }else{
- // var_dump($user['oil_level']);
- // return app('json')->fail('不是服务中心不能进行核销');
- // }
- // foreach ($order['orderProduct'] as $k => $v){
- // $v['verify_num'] = \app\common\model\store\order\StoreCart::where('product_id',$v['product_id'])->where('cart_id',$v['cart_id'])->value('verify_num');
- // }
- //// $this->repository->verifyPartOrder($order, $data);
- // return app('json')->success($order);
- }
- }
|