Order.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\out;
  12. use app\Request;
  13. use app\services\order\StoreCartServices;
  14. use app\services\order\StoreOrderCreateServices;
  15. use app\services\order\StoreOrderInvoiceServices;
  16. use app\services\order\StoreOrderRefundServices;
  17. use app\services\order\StoreOrderSuccessServices;
  18. use app\services\order\StoreOrderTakeServices;
  19. use app\services\user\UserAddressServices;
  20. use think\facade\App;
  21. use app\services\product\product\StoreProductServices;
  22. use app\services\user\UserServices;
  23. use app\services\order\StoreOrderServices;
  24. /**
  25. * Class Order
  26. * @package app\kefuapi\controller
  27. */
  28. class Order
  29. {
  30. /**
  31. * Order constructor.
  32. * @param App $app
  33. * @param StoreOrderServices $services
  34. */
  35. public function __construct(StoreOrderServices $services)
  36. {
  37. $this->services = $services;
  38. }
  39. /**
  40. * 获取订单列表
  41. * @return mixed
  42. */
  43. public function lst(Request $request)
  44. {
  45. $where = $request->getMore([
  46. ['status', ''],
  47. ['real_name', ''],
  48. ['is_del', ''],
  49. ['data', '', '', 'time'],
  50. ['type', ''],
  51. ['pay_type', ''],
  52. ['order', ''],
  53. ['field_key', ''],
  54. ]);
  55. $where['shipping_type'] = 1;
  56. $where['is_system_del'] = 0;
  57. if (!$where['real_name'] && !in_array($where['status'], [-1, -2, -3])) {
  58. $where['pid'] = 0;
  59. }
  60. return app('json')->success($this->services->getOrderList($where, ['*'], ['split' => function ($query) {
  61. $query->field('id,pid');
  62. }, 'pink', 'invoice']));
  63. }
  64. /**
  65. * 根据订单id获取订单状态
  66. * @param $oid
  67. * @return mixed
  68. */
  69. public function get_status($oid)
  70. {
  71. if (!$oid) return app('json')->fail('参数错误');
  72. return app('json')->success($this->services->outGetStatus($oid));
  73. }
  74. /**
  75. * 根据订单id查询收货方式
  76. * @param $oid
  77. * @return mixed
  78. */
  79. public function get_shipping_type($oid)
  80. {
  81. if (!$oid) return app('json')->fail('参数错误');
  82. return app('json')->success($this->services->outGetShippingType($oid));
  83. }
  84. /**
  85. * 根据订单id查询配送信息
  86. * @param $oid
  87. * @return mixed
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function delivery_type($oid)
  93. {
  94. if (!$oid) return app('json')->fail('参数错误');
  95. return app('json')->success($this->services->OutDeliveryType($oid));
  96. }
  97. /**
  98. * 收货
  99. * @param $oid
  100. * @param StoreOrderTakeServices $services
  101. * @return mixed
  102. */
  103. public function take_delivery($oid, StoreOrderTakeServices $services)
  104. {
  105. if (!$oid) return app('json')->fail('缺少参数');
  106. $order = $this->services->get(['order_id' => $oid]);
  107. if (!$order)
  108. return app('json')->fail('Data does not exist!');
  109. if ($order['status'] == 2)
  110. return app('json')->fail('不能重复收货!');
  111. if ($order['paid'] == 1 && $order['status'] == 1)
  112. $data['status'] = 2;
  113. else if ($order['pay_type'] == 'offline')
  114. $data['status'] = 2;
  115. else
  116. return app('json')->fail('请先发货或者送货!');
  117. if (!$this->services->update($order['id'], $data)) {
  118. return app('json')->fail('收货失败,请稍候再试!');
  119. } else {
  120. $services->storeProductOrderUserTakeDelivery($order);
  121. return app('json')->success('收货成功');
  122. }
  123. }
  124. /**
  125. * 根据订单id获取发票信息
  126. * @param $oid
  127. * @param StoreOrderInvoiceServices $services
  128. * @return mixed
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\DbException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. */
  133. public function invoice($oid, StoreOrderInvoiceServices $services)
  134. {
  135. if (!$oid) return app('json')->fail('缺少参数');
  136. $id = $this->services->value(['order_id' => $oid], 'id');
  137. return app('json')->success($services->getOneInvoice((int)$id));
  138. }
  139. /**
  140. * 获取订单详情
  141. * @param $oid
  142. * @return mixed
  143. */
  144. public function detail($oid)
  145. {
  146. if (!$oid || !($orderInfo = $this->services->get(['order_id' => $oid]))) {
  147. return $this->app('json')('订单不存在');
  148. }
  149. $orderInfo = $this->services->tidyOrder($orderInfo->toArray(), true, true);
  150. //核算优惠金额
  151. $vipTruePrice = 0;
  152. foreach ($orderInfo['cartInfo'] ?? [] as $cart) {
  153. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  154. }
  155. $orderInfo['vip_true_price'] = $vipTruePrice;
  156. $orderInfo['total_price'] = bcadd((string)$orderInfo['total_price'], (string)$orderInfo['vip_true_price'], 2);
  157. return app('json')->success(compact('orderInfo'));
  158. }
  159. /**
  160. * 修改订单
  161. * @param $id
  162. * @return mixed
  163. */
  164. public function update(Request $request, $oid)
  165. {
  166. if (!$oid) return app('json')->fail('缺少参数');
  167. $order = $this->services->get(['order_id' => $oid]);
  168. $data = $request->postMore([
  169. ['pay_price', 0],
  170. ]);
  171. // $this->validate($data, StoreOrderValidate::class);
  172. if ($data['pay_price'] < 0) return app('json')->fail('请输入实际支付金额');
  173. $data['total_price'] = $order['total_price'];
  174. $data['order_id'] = $order['order_id'];
  175. $this->services->updateOrder((int)$order['id'], $data);
  176. return app('json')->success('修改成功');
  177. }
  178. /**
  179. * 购物车 添加
  180. * @param Request $request
  181. * @return mixed
  182. * @throws \think\db\exception\DataNotFoundException
  183. * @throws \think\db\exception\ModelNotFoundException
  184. * @throws \think\exception\DbException
  185. */
  186. public function cart_add(Request $request, StoreCartServices $services, StoreProductServices $productServices)
  187. {
  188. $where = $request->postMore([
  189. ['spu', ''],//普通商品编号
  190. [['cartNum', 'd'], 1], //购物车数量
  191. ['uniqueId', ''],//属性唯一值
  192. ['uid', 0]
  193. ]);
  194. $where['productId'] = $productServices->value(['spu' => $where['spu']], 'id');
  195. if (!$where['productId']) {
  196. return app('json')->fail('参数错误');
  197. }
  198. $uid = $where['uid'];
  199. [$cartId, $cartNum] = $services->setCart($uid, (int)$where['productId'], (int)$where['cartNum'], $where['uniqueId'], 0, true);
  200. if (!$cartId) return app('json')->fail('添加失败');
  201. else return app('json')->successful('ok', ['cartId' => $cartId]);
  202. }
  203. /**
  204. * 获取商品运费
  205. * @param Request $request
  206. * @return mixed
  207. * @throws \Psr\SimpleCache\InvalidArgumentException
  208. */
  209. public function postage(Request $request)
  210. {
  211. [$cartId, $uid, $addressId] = $request->getMore([
  212. 'cartId',
  213. 'uid', //用户id
  214. ['addressId', 0],//地址id
  215. ], true);
  216. return app('json')->successful($this->services->outGetPostage($cartId, (int)$uid, (int)$addressId));
  217. }
  218. /**
  219. * 订单确认
  220. * @param Request $request
  221. * @param UserServices $userServices
  222. * @return mixed
  223. * @throws \Psr\SimpleCache\InvalidArgumentException
  224. * @throws \think\db\exception\DataNotFoundException
  225. * @throws \think\db\exception\DbException
  226. * @throws \think\db\exception\ModelNotFoundException
  227. */
  228. public function confirm(Request $request, UserServices $userServices)
  229. {
  230. [$cartId, $addressId, $uid] = $request->postMore([
  231. 'cartId',
  232. ['addressId', 0],
  233. ['uid', 0]
  234. ], true);
  235. if (!is_string($cartId) || !$cartId) {
  236. return app('json')->fail('请提交购买的商品');
  237. }
  238. $user = $userServices->get($uid);
  239. return app('json')->successful($this->services->getOrderConfirmData($user, $cartId, true, (int)$addressId));
  240. }
  241. /**
  242. * 售后列表
  243. * @param Request $request
  244. * @param StoreOrderRefundServices $storeOrderRefundServices
  245. * @return mixed
  246. * @throws \think\db\exception\DataNotFoundException
  247. * @throws \think\db\exception\DbException
  248. * @throws \think\db\exception\ModelNotFoundException
  249. */
  250. public function refund_list(Request $request, StoreOrderRefundServices $storeOrderRefundServices)
  251. {
  252. $where = $request->getMore([
  253. ['order_id', ''],
  254. ['time', '', '', 'refund_reason_time'],
  255. ['refund_type', 0],
  256. ['page', 0],
  257. ['limit', 0]
  258. ]);
  259. return app('json')->success($storeOrderRefundServices->refundList($where));
  260. }
  261. /**
  262. * 订单创建
  263. * @param Request $request
  264. * @param StoreOrderCreateServices $createServices
  265. * @param $key
  266. * @return mixed
  267. * @throws \Psr\SimpleCache\InvalidArgumentException
  268. * @throws \think\db\exception\DataNotFoundException
  269. * @throws \think\db\exception\DbException
  270. * @throws \think\db\exception\ModelNotFoundException
  271. */
  272. public function create(Request $request, StoreOrderCreateServices $createServices, $key)
  273. {
  274. [$addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, $from, $shipping_type, $real_name, $phone, $storeId, $news, $invoice_id, $quitUrl, $discountId, $uid, $paid] = $request->postMore([
  275. [['addressId', 'd'], 0],
  276. [['couponId', 'd'], 0],
  277. ['payType', ''],
  278. ['useIntegral', 0],
  279. ['mark', ''],
  280. [['combinationId', 'd'], 0],
  281. [['pinkId', 'd'], 0],
  282. [['seckill_id', 'd'], 0],
  283. [['bargainId', 'd'], ''],
  284. ['from', 'weixin'],
  285. [['shipping_type', 'd'], 1],
  286. ['real_name', ''],
  287. ['phone', ''],
  288. [['store_id', 'd'], 0],
  289. ['new', 0],
  290. [['invoice_id', 'd'], 0],
  291. ['quitUrl', ''],
  292. [['discountId', 'd'], 0],
  293. ['uid', 0],
  294. ['paid', 0]
  295. ], true);
  296. if (!$key) return app('json')->fail('参数错误!');
  297. if ($this->services->getOne(['unique' => $key, 'uid' => $uid, 'is_del' => 0]))
  298. return app('json')->status('extend_order', '订单已创建,请点击查看完成支付', ['orderId' => $key, 'key' => $key]);
  299. $cartGroup = $this->services->getCacheOrderInfo($uid, $key);
  300. if (!$cartGroup) {
  301. return app('json')->fail('请勿重复提交或订单已过期 请刷新当前页面!');
  302. }
  303. $cartInfo = $cartGroup['cartInfo'];
  304. if (!$cartInfo) {
  305. return app('json')->fail('订单已过期或提交的商品不在送达区域,请刷新当前页面或重新选择商品下单!');
  306. }
  307. if ($discountId && isset($cartGroup['invalidCartInfo']) && $cartGroup['invalidCartInfo']) {
  308. return app('json')->fail('套餐中有商品已失效或者不在送达区域');
  309. }
  310. $payType = strtolower($payType);
  311. if ($shipping_type == 1) {
  312. if (!$addressId) {
  313. return app('json')->fail('请选择收货地址!');
  314. }
  315. $addressInfo = $cartGroup['addr'] ?? [];
  316. if (!$addressInfo || $addressInfo['id'] != $addressId) {
  317. /** @var UserAddressServices $addressServices */
  318. $addressServices = app()->make(UserAddressServices::class);
  319. if (!$addressInfo = $addressServices->getOne(['uid' => $uid, 'id' => $addressId, 'is_del' => 0]))
  320. return app('json')->fail('地址选择有误!');
  321. $addressInfo = $addressInfo->toArray();
  322. }
  323. } else {
  324. if ((!$real_name || !$phone)) {
  325. return app('json')->fail('请填写姓名和电话');
  326. }
  327. $addressInfo['real_name'] = $real_name;
  328. $addressInfo['phone'] = $phone;
  329. $addressInfo['province'] = '';
  330. $addressInfo['city'] = '';
  331. $addressInfo['district'] = '';
  332. $addressInfo['detail'] = '';
  333. }
  334. if (!$this->services->checkPaytype($payType)) {
  335. return app('json')->fail('暂不支持该支付方式,请刷新页面或者联系管理员');
  336. }
  337. $isChannel = $this->getChennel[$from] ?? ($request->isApp() ? 0 : 1);
  338. try {
  339. $order = $createServices->createOrder($uid, $key, $cartGroup, (int)$addressId, $payType, $addressInfo, $request->user()->toArray(), !!$useIntegral, $couponId, $mark, $pinkId, $isChannel, $shipping_type, $storeId, !!$news, [], (int)$invoice_id);
  340. } catch (\Throwable $e) {
  341. $order = false;
  342. \think\facade\Log::error('订单生成失败,原因:' . $e->getMessage());
  343. }
  344. $orderId = $order['order_id'];
  345. if ($orderId) {
  346. $orderInfo = $this->services->getOne(['order_id' => $orderId]);
  347. if (!$orderInfo || !isset($orderInfo['paid'])) {
  348. return app('json')->fail('支付订单不存在!');
  349. }
  350. $orderInfo = $orderInfo->toArray();
  351. $info = compact('orderId', 'key');
  352. if ($paid == 1) {
  353. /** @var StoreOrderSuccessServices $orderServices */
  354. $orderServices = app()->make(StoreOrderSuccessServices::class);
  355. $orderServices->paySuccess($orderInfo, $payType);
  356. return app('json')->status('success', '微信支付成功');
  357. } else {
  358. return app('json')->status('success', '订单创建成功');
  359. }
  360. } else return app('json')->fail('订单生成失败!');
  361. }
  362. }