StoreIntegralOrderController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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\api\v1\activity;
  12. use app\Request;
  13. use app\services\activity\integral\StoreIntegralOrderServices;
  14. use app\services\activity\integral\StoreIntegralServices;
  15. use app\services\product\sku\StoreProductAttrValueServices;
  16. use app\services\other\ExpressServices;
  17. use crmeb\services\CacheService;
  18. use app\services\pay\PayServices;
  19. use app\services\pay\OrderPayServices;
  20. use app\services\pay\YuePayServices;
  21. /**
  22. * 积分订单
  23. * Class StoreIntegralController
  24. * @package app\controller\api\activity
  25. */
  26. class StoreIntegralOrderController
  27. {
  28. protected $services;
  29. public function __construct(StoreIntegralOrderServices $services)
  30. {
  31. $this->services = $services;
  32. }
  33. /**
  34. * 订单确认
  35. * @param Request $request
  36. * @return mixed
  37. */
  38. public function confirm(Request $request)
  39. {
  40. [$unique, $num] = $request->postMore([
  41. 'unique',
  42. 'num'
  43. ], true);
  44. if (!$unique) {
  45. return app('json')->fail('请提交购买的商品');
  46. }
  47. $user = $request->user()->toArray();
  48. return app('json')->successful($this->services->getOrderConfirmData($user, $unique, $num));
  49. }
  50. /**
  51. * 订单创建
  52. * @param Request $request
  53. * @return mixed
  54. * @throws \Psr\SimpleCache\InvalidArgumentException
  55. * @throws \think\Exception
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function create(Request $request, StoreProductAttrValueServices $storeProductAttrValueServices, StoreIntegralServices $storeIntegralServices)
  61. {
  62. $uid = (int)$request->uid();
  63. [$addressId, $mark, $payType, $from, $quitUrl, $unique, $num, $customForm] = $request->postMore([
  64. [['addressId', 'd'], 0],
  65. ['mark', ''],
  66. ['payType', ''],
  67. ['from', 'weixin'],
  68. ['quitUrl', ''],
  69. ['unique', ''],
  70. [['num', 'd'], 0],
  71. ['custom_form', []],
  72. ], true);
  73. $attrValue = $storeProductAttrValueServices->uniqueByField($unique, 4);
  74. if (!$attrValue) {
  75. return app('json')->fail('商品不存在,请重新选择商品下单!');
  76. }
  77. $productInfo = $storeIntegralServices->getIntegralOne((int)$attrValue['product_id']);
  78. if (!$productInfo) {
  79. return app('json')->fail('商品不存在,请重新选择商品下单!');
  80. }
  81. $attrValue = is_object($attrValue) ? $attrValue->toArray() : $attrValue;
  82. $productInfo = is_object($productInfo) ? $productInfo->toArray() : $productInfo;
  83. $productInfo['attrInfo'] = $attrValue;
  84. $num = (int)$num;
  85. //判断积分商品限量
  86. [$attrValue, $unique, $storeIntegralProductInfo] = $storeIntegralServices->checkoutProductStock($uid, $productInfo['id'], $num, $unique);
  87. try {
  88. //弹出队列
  89. if (!CacheService::popStock($unique, $num, 4)) {
  90. return app('json')->fail('该商品库存不足');
  91. }
  92. $order = $this->services->createOrder($uid, $addressId, $payType, $mark, $request->user()->toArray(), $num, $productInfo, $customForm);
  93. } catch (\Throwable $e) {
  94. //生成失败归还库存
  95. CacheService::setStock($unique, $num, 4, false);
  96. return app('json')->fail($e->getMessage());
  97. }
  98. $orderId = $order['order_id'];
  99. if ($orderId && !$order['paid']) {
  100. $orderInfo = $order->toArray();
  101. if (!$orderInfo || !isset($orderInfo['paid'])) {
  102. return app('json')->fail('支付订单不存在!');
  103. }
  104. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  105. $info = compact('orderId');
  106. switch ($payType) {
  107. case PayServices::WEIXIN_PAY:
  108. /** @var OrderPayServices $payServices */
  109. $payServices = app()->make(OrderPayServices::class);
  110. if (!$from && $request->isApp()) {
  111. $from = 'weixin';
  112. }
  113. $info['jsConfig'] = $payServices->orderIntegralPay($orderInfo, $from);
  114. if ($from == 'weixinh5') {
  115. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  116. } else {
  117. return app('json')->status('wechat_pay', '订单创建成功', $info);
  118. }
  119. break;
  120. case PayServices::YUE_PAY:
  121. /** @var YuePayServices $yueServices */
  122. $yueServices = app()->make(YuePayServices::class);
  123. $pay = $yueServices->yueIntegralOrderPay($orderInfo, $uid);
  124. if ($pay['status'] === true)
  125. return app('json')->status('success', '余额支付成功', $info);
  126. else {
  127. if (is_array($pay))
  128. return app('json')->status($pay['status'], $pay['msg'], $info);
  129. else
  130. return app('json')->status('pay_error', $pay);
  131. }
  132. break;
  133. case PayServices::ALIAPY_PAY:
  134. if (!$quitUrl && ($request->isH5() || $request->isWechat())) {
  135. return app('json')->status('pay_error', '请传入支付宝支付回调URL', $info);
  136. }
  137. $quitUrl = $quitUrl . '?order_id=' . $orderInfo['order_id'];
  138. /** @var OrderPayServices $payServices */
  139. $payServices = app()->make(OrderPayServices::class);
  140. $info['jsConfig'] = $payServices->alipayIntegralOrder($orderInfo, $quitUrl, $from == 'routine');
  141. $payKey = md5($orderInfo['order_id']);
  142. CacheService::set($payKey, ['order_id' => $orderInfo['order_id'], 'other_pay_type' => false], 300);
  143. $info['pay_key'] = $payKey;
  144. return app('json')->status(PayServices::ALIAPY_PAY . '_pay', '订单创建成功', $info);
  145. break;
  146. default:
  147. return app('json')->status('success', '订单创建成功', $info);
  148. break;
  149. }
  150. } elseif ($orderId && $order['paid']) {
  151. return app('json')->status('success', '订单创建成功', ['orderId' => $order['order_id']]);
  152. } else return app('json')->fail('订单生成失败!');
  153. }
  154. /**
  155. * 订单详情
  156. * @param Request $request
  157. * @param $uni
  158. * @return mixed
  159. */
  160. public function detail(Request $request, $uni)
  161. {
  162. if (!strlen(trim($uni))) return app('json')->fail('参数错误');
  163. $order = $this->services->getOne(['order_id' => $uni, 'is_del' => 0]);
  164. if (!$order) return app('json')->fail('订单不存在');
  165. $order = $order->toArray();
  166. if (!$order['paid']) return app('json')->fail('订单未支付,无法查看');
  167. $orderData = $this->services->tidyOrder($order);
  168. return app('json')->successful('ok', $orderData);
  169. }
  170. /**
  171. * 订单列表
  172. * @param Request $request
  173. * @return mixed
  174. */
  175. public function lst(Request $request)
  176. {
  177. $where['uid'] = $request->uid();
  178. $where['paid'] = 1;
  179. $where['is_del'] = 0;
  180. $where['is_system_del'] = 0;
  181. $list = $this->services->getOrderApiList($where);
  182. return app('json')->successful($list);
  183. }
  184. /**
  185. * 订单收货
  186. * @param Request $request
  187. * @return mixed
  188. */
  189. public function take(Request $request)
  190. {
  191. [$order_id] = $request->postMore([
  192. ['order_id', ''],
  193. ], true);
  194. if (!$order_id) return app('json')->fail('参数错误!');
  195. $order = $this->services->takeOrder($order_id, (int)$request->uid());
  196. if ($order) {
  197. return app('json')->successful('收货成功');
  198. } else
  199. return app('json')->fail('收货失败');
  200. }
  201. /**
  202. * 订单 查看物流
  203. * @param Request $request
  204. * @param $uni
  205. * @return mixed
  206. */
  207. public function express(Request $request, ExpressServices $expressServices, $uni)
  208. {
  209. if (!$uni || !($order = $this->services->getUserOrderDetail($uni, $request->uid()))) return app('json')->fail('查询订单不存在!');
  210. if ($order['delivery_type'] != 'express') return app('json')->fail('该订单不是快递发货,无法查询物流信息');
  211. if (!$order['delivery_id']) return app('json')->fail('该订单不存在快递单号!');
  212. $cacheName = 'integral' . $order['order_id'] . $order['delivery_id'];
  213. return app('json')->successful([
  214. 'order' => $order,
  215. 'express' => [
  216. 'result' => ['list' => $expressServices->query($cacheName, $order['delivery_id'], $order['delivery_code'])
  217. ]
  218. ]
  219. ]);
  220. }
  221. /**
  222. * 订单删除
  223. * @param Request $request
  224. * @return mixed
  225. */
  226. public function del(Request $request)
  227. {
  228. [$order_id] = $request->postMore([
  229. ['order_id', ''],
  230. ], true);
  231. if (!$order_id) return app('json')->fail('参数错误!');
  232. $res = $this->services->removeOrder($order_id, (int)$request->uid());
  233. if ($res) {
  234. return app('json')->successful();
  235. } else {
  236. return app('json')->fail('删除失败');
  237. }
  238. }
  239. }