OrderController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\work;
  12. use app\Request;
  13. use app\services\activity\combination\StorePinkServices;
  14. use app\services\activity\coupon\StoreCouponIssueServices;
  15. use app\services\message\service\StoreServiceRecordServices;
  16. use app\services\order\StoreOrderEconomizeServices;
  17. use app\services\order\StoreOrderPromotionsServices;
  18. use app\services\order\StoreOrderRefundServices;
  19. use app\services\order\StoreOrderServices;
  20. use app\services\order\StoreOrderStatusServices;
  21. use app\services\product\product\StoreProductServices;
  22. use app\services\store\SystemStoreServices;
  23. use app\services\user\UserInvoiceServices;
  24. use app\services\user\UserServices;
  25. /**
  26. * 订单
  27. * Class OrderController
  28. * @package app\controller\api\v1\work
  29. */
  30. class OrderController extends BaseWorkController
  31. {
  32. /**
  33. * OrderController constructor.
  34. * @param StoreOrderServices $services
  35. */
  36. public function __construct(StoreOrderServices $services)
  37. {
  38. parent::__construct();
  39. $this->service = $services;
  40. }
  41. /**
  42. * 获取订单列表
  43. * @param Request $request
  44. * @param StoreServiceRecordServices $services
  45. * @param StoreOrderRefundServices $storeOrderRefundServices
  46. * @return mixed
  47. */
  48. public function getUserOrderList(Request $request, StoreServiceRecordServices $services, StoreOrderRefundServices $storeOrderRefundServices)
  49. {
  50. $where = $request->getMore([
  51. ['type', '', '', 'status'],
  52. ['search', '', '', 'real_name'],
  53. ]);
  54. $uid = $this->clientInfo['uid'] ?? 0;
  55. if (!$uid) {
  56. return $this->success(['list' => [], 'count' => 0]);
  57. }
  58. $where['uid'] = $uid;
  59. $where['is_del'] = 0;
  60. $where['is_system_del'] = 0;
  61. $where['refund_type'] = [0, 1, 3, 6];
  62. if ($where['status'] == -1) {
  63. $list = $storeOrderRefundServices->refundList(['uid' => $where['uid'], 'real_name' => $where['real_name'], 'refund_type' => [1, 2, 4, 5]])['list'] ?? [];
  64. } else {
  65. $list = $this->service->getOrderApiList($where + ['pid' => 0], ['*'], ['pink', 'invoice']);
  66. }
  67. return $this->success($list);
  68. }
  69. /**
  70. * 订单详情
  71. * @param StoreOrderEconomizeServices $services
  72. * @param StoreOrderPromotionsServices $storeOrderPromotiosServices
  73. * @param $id 订单id
  74. * @return mixed
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function orderInfo(StoreOrderEconomizeServices $services, StoreOrderPromotionsServices $storeOrderPromotiosServices, $id)
  80. {
  81. if (!$id || !($orderInfo = $this->service->get($id))) {
  82. return $this->fail('订单不存在');
  83. }
  84. $order = $orderInfo->toArray();
  85. $order['split'] = [];
  86. //门店是否开启 || 门店自提是否开启
  87. if (!sys_config('store_func_status', 1) || !sys_config('store_self_mention')) {
  88. //关闭门店自提后 订单隐藏门店信息
  89. $order['shipping_type'] = 1;
  90. }
  91. if ($order['verify_code']) {
  92. $verify_code = $order['verify_code'];
  93. $verify[] = substr($verify_code, 0, 4);
  94. $verify[] = substr($verify_code, 4, 4);
  95. $verify[] = substr($verify_code, 8);
  96. $order['_verify_code'] = implode(' ', $verify);
  97. }
  98. //收银台订单 用户无信息 手机号
  99. if ($order['shipping_type'] == 4 && $order['uid'] && !$order['real_name']) {
  100. /** @var UserServices $userServices */
  101. $userServices = app()->make(UserServices::class);
  102. $userInfo = $userServices->getUserCacheInfo((int)$order['uid']);
  103. $order['real_name'] = $userInfo['nickname'];
  104. $order['user_phone'] = $userInfo['phone'];
  105. }
  106. $order['add_time_y'] = date('Y-m-d', $order['add_time']);
  107. $order['add_time_h'] = date('H:i:s', $order['add_time']);
  108. $order['system_store'] = false;
  109. if (!$order['store_id'] && $order['shipping_type'] == 2) {
  110. $order['store_id'] = $this->service->value(['pid' => $order['id']], 'store_id');
  111. }
  112. if ($order['store_id']) {
  113. /** @var SystemStoreServices $storeServices */
  114. $storeServices = app()->make(SystemStoreServices::class);
  115. $order['system_store'] = $storeServices->getStoreDispose($order['store_id']);
  116. }
  117. $order['mapKey'] = sys_config('tengxun_map_key');
  118. $order['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  119. $order['pay_weixin_open'] = (int)sys_config('pay_weixin_open') ?? 0;//微信支付 1 开启 0 关闭
  120. $order['ali_pay_status'] = (bool)sys_config('ali_pay_status');//支付包支付 1 开启 0 关闭
  121. $orderData = $this->service->tidyOrder($order, true, true);
  122. //核算优惠金额
  123. $vipTruePrice = 0;
  124. $refund_num = 0;
  125. foreach ($orderData['cartInfo'] ?? [] as $key => &$cart) {
  126. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  127. $refund_num = bcadd((string)$refund_num, (string)$cart['refund_num'], 0);
  128. }
  129. $orderData['vip_true_price'] = $vipTruePrice;
  130. //优惠活动优惠详情
  131. $orderData['promotions_detail'] = $storeOrderPromotiosServices->getOrderPromotionsDetail((int)$order['id']);
  132. //同步查询订单商品为查询到 查询缓存信息
  133. if (!$orderData['cartInfo']) {
  134. $cartGroup = $this->service->getCacheOrderInfo((int)$order['uid'], $order['unique']);
  135. $orderData['cartInfo'] = $cartGroup['cartInfo'] ?? [];
  136. }
  137. $economize = $services->get(['order_id' => $order['order_id']], ['postage_price', 'member_price']);
  138. if ($economize) {
  139. $orderData['postage_price'] = $economize['postage_price'];
  140. $orderData['member_price'] = $economize['member_price'];
  141. } else {
  142. $orderData['postage_price'] = 0;
  143. $orderData['member_price'] = 0;
  144. }
  145. $orderData['routine_contact_type'] = sys_config('routine_contact_type', 0);
  146. /** @var UserInvoiceServices $userInvoice */
  147. $userInvoice = app()->make(UserInvoiceServices::class);
  148. $invoice_func = $userInvoice->invoiceFuncStatus();
  149. $orderData['invoice_func'] = $invoice_func['invoice_func'];
  150. $orderData['special_invoice'] = $invoice_func['special_invoice'];
  151. $orderData['refund_cartInfo'] = [];
  152. $orderData['refund_total_num'] = $orderData['total_num'];
  153. $orderData['refund_pay_price'] = $orderData['pay_price'];
  154. $orderData['is_apply_refund'] = $orderData['total_num'] > $refund_num;
  155. $orderData['is_batch_refund'] = count($orderData['cartInfo']) > 1;
  156. $orderData['pinkStatus'] = null;
  157. if ($orderData['type'] == 3) {
  158. /** @var StorePinkServices $pinkService */
  159. $pinkService = app()->make(StorePinkServices::class);
  160. $orderData['pinkStatus'] = $pinkService->value(['order_id' => $orderData['order_id']], 'status');
  161. }
  162. /** @var StoreOrderStatusServices $statusServices */
  163. $statusServices = app()->make(StoreOrderStatusServices::class);
  164. $log = $statusServices->getColumn(['oid' => $order['id']], 'change_time', 'change_type');
  165. if (isset($log['delivery'])) {
  166. $delivery = date('Y-m-d', $log['delivery']);
  167. } elseif (isset($log['delivery_goods'])) {
  168. $delivery = date('Y-m-d', $log['delivery_goods']);
  169. } elseif (isset($log['delivery_fictitious'])) {
  170. $delivery = date('Y-m-d', $log['delivery_fictitious']);
  171. } else {
  172. $delivery = '';
  173. }
  174. $orderData['order_log'] = [
  175. 'create' => isset($log['cache_key_create_order']) ? date('Y-m-d', $log['cache_key_create_order']) : '',
  176. 'pay' => isset($log['pay_success']) ? date('Y-m-d', $log['pay_success']) : '',
  177. 'delivery' => $delivery,
  178. 'take' => isset($log['take_delivery']) ? date('Y-m-d', $log['take_delivery']) : '',
  179. 'complete' => isset($log['check_order_over']) ? date('Y-m-d', $log['check_order_over']) : '',
  180. ];
  181. if ($orderData['give_coupon']) {
  182. $couponIds = is_string($orderData['give_coupon']) ? explode(',', $orderData['give_coupon']) : $orderData['give_coupon'];
  183. /** @var StoreCouponIssueServices $couponIssueService */
  184. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  185. $orderData['give_coupon'] = $couponIssueService->getColumn([['id', 'IN', $couponIds]], 'id,coupon_title');
  186. }
  187. $orderInfo = $orderData;
  188. /** @var UserServices $services */
  189. $userServices = app()->make(UserServices::class);
  190. $userInfo = $userServices->getUserWithTrashedInfo($orderInfo['uid']);
  191. if (!$userInfo) {
  192. return $this->fail('用户信息不存在');
  193. }
  194. $userInfo = $userInfo->hidden(['pwd', 'add_ip', 'last_ip', 'login_type']);
  195. $userInfo['spread_name'] = '';
  196. if ($userInfo['spread_uid']) {
  197. $userInfo['spread_name'] = $userServices->value(['uid' => $userInfo['spread_uid']], 'nickname');
  198. }
  199. return $this->success(compact('orderInfo', 'userInfo'));
  200. }
  201. }