ListController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\frontend\modules\order\controllers;
  3. use app\backend\modules\goods\models\GoodsTradeSet;
  4. use app\common\components\ApiController;
  5. use app\common\facades\Setting;
  6. use app\common\models\order\GoodsTradeLog;
  7. use app\frontend\models\Order;
  8. use app\frontend\modules\order\services\OrderService;
  9. use Illuminate\Support\Carbon;
  10. use Illuminate\Support\Facades\DB;
  11. class ListController extends ApiController
  12. {
  13. /**
  14. * @var Order
  15. */
  16. protected $order;
  17. /**
  18. * @return Order
  19. */
  20. protected function getOrder()
  21. {
  22. if(!isset($this->order)){
  23. return $this->_getOrder();
  24. }
  25. return $this->order;
  26. }
  27. /**
  28. * @return Order
  29. */
  30. protected function _getOrder()
  31. {
  32. $another_where = [];
  33. $other_plugin_ids = [];
  34. $allow_uids = [];
  35. if (!is_null($event_arr = \app\common\modules\shop\ShopConfig::current()->get('order_list_search_where'))) {
  36. foreach ($event_arr as $v){
  37. $class = array_get($v, 'class');
  38. $function = array_get($v, 'function');
  39. $res = $class::$function($another_where);
  40. if ($res['result'] == 1) {
  41. if ($res['change_where']) $another_where = $res['another_where'];
  42. $another_where = array_merge($another_where, $res['data']);
  43. $other_plugin_ids = array_merge($other_plugin_ids, $res['other_plugin_ids'] ?: []);
  44. if ($res['allow_uids']) $allow_uids = array_merge($allow_uids,$res['allow_uids']);
  45. }
  46. }
  47. }
  48. $query = app('OrderManager')->make('Order')->newQueryWithoutScopes()
  49. ->select(app('OrderManager')->make('Order')->getTable().'.*')
  50. // ->uid()
  51. ->orders()
  52. ->hidePluginIds([],$other_plugin_ids)
  53. // ->hidePluginIds([96])
  54. ->where($another_where)
  55. ->keywordSearch(request()->input('keyword', ''));
  56. if ($allow_uids) {
  57. if (!in_array(-1, $allow_uids)) {
  58. $allow_uids[] = \YunShop::app()->getMemberId();
  59. $query->uid($allow_uids);
  60. }
  61. } else {
  62. $query->uid();
  63. }
  64. return $this->order = $query;
  65. }
  66. /**
  67. * 在订单中心设置自定义按钮
  68. */
  69. private function setMenuGroup(){
  70. $menuGroup = [];
  71. $menuClass = app()->tagged("orderCenterMenuGroup");
  72. foreach ($menuClass as $menus) {
  73. $menu = new $menus;
  74. if($menu->enable()){
  75. $menuGroup=
  76. [
  77. "name"=>$menu->getName(),
  78. "enable"=>$menu->enable(),
  79. "id"=>$menu->getId(),
  80. "api"=>$menu->getApi(),
  81. "value"=>$menu->getValue(),
  82. ];
  83. }
  84. }
  85. return $menuGroup;
  86. }
  87. protected function getData()
  88. {
  89. $pageSize = request()->input('pagesize',20);
  90. $model = $this->getOrder()->where(app('OrderManager')->make('Order')->getTable().'.is_member_deleted',0)->paginate($pageSize);
  91. //慈善基金-订单金额
  92. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('charity_fund_charity_money')) ) {
  93. $is_open = \Yunshop\CharityFund\services\SetConfigService::getSetConfig('is_open');
  94. if($is_open){
  95. $orderIds = $model->pluck('id');
  96. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('charity_fund_charity_money'), 'class');
  97. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('charity_fund_charity_money'), 'function');
  98. $ret = $class::$function($orderIds);
  99. $model->map(function($item) use ($ret){
  100. return $item->charity_fund_money = $ret[$item->id] ?: 0;
  101. });
  102. }
  103. }
  104. //门店预约
  105. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_order'))) {
  106. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_order'), 'class');
  107. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('store_reserve_order'), 'function');
  108. $model->map(function($item) use ($class,$function){
  109. return $item->store_reserve = $class::$function($item->id)['time'];
  110. });
  111. }
  112. //商品交易设置
  113. $model->map(function($item) {
  114. $item->hasManyOrderGoods->map(function ($g) {
  115. $goods_trade_log = GoodsTradeLog::where(['order_id'=>$g->order_id,'goods_id'=>$g->goods_id])->first();
  116. if (!$goods_trade_log || !app('plugins')->isEnabled('address-code')) {
  117. $g->show_time_word = '';
  118. } else {
  119. $g->show_time_word = $goods_trade_log->show_time_word;
  120. }
  121. });
  122. });
  123. $orderData = $model->toArray();
  124. $orderData["menu_group"]=$this->setMenuGroup();
  125. $orderData['data'] = $this->setOtherData($orderData['data']);
  126. $orderData['receipt_goods_notice'] = OrderService::getReceiptGoodsNotice();
  127. return $orderData;
  128. }
  129. public function setOtherData($order)
  130. {
  131. $config = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.order-list-other-data');
  132. foreach ($config as $key => $item) {
  133. $class = array_get($item,'class');
  134. $function = array_get($item,'function');
  135. if(class_exists($class) && method_exists($class,$function) && is_callable([$class,$function])){
  136. $order = $class::$function($order);
  137. }
  138. }
  139. return $order;
  140. }
  141. /**
  142. * 所有订单(不包括"已删除"订单)
  143. * @return \Illuminate\Http\JsonResponse
  144. */
  145. public function index()
  146. {
  147. return $this->successJson($msg = 'ok', $data = $this->getData());
  148. }
  149. /**
  150. * 待付款订单
  151. * @return \Illuminate\Http\JsonResponse
  152. */
  153. public function waitPay()
  154. {
  155. $this->getOrder()->waitPay();
  156. return $this->successJson($msg = 'ok', $data = $this->getData());
  157. }
  158. /**
  159. * 待发货订单
  160. * @return \Illuminate\Http\JsonResponse
  161. */
  162. public function waitSend()
  163. {
  164. $this->getOrder()->waitSend();
  165. return $this->successJson($msg = 'ok', $data = $this->getData());
  166. }
  167. /**
  168. * 待收货订单
  169. * @return \Illuminate\Http\JsonResponse
  170. */
  171. public function waitReceive()
  172. {
  173. $this->getOrder()->waitReceive();
  174. return $this->successJson($msg = 'ok', $data = $this->getData());
  175. }
  176. /**
  177. * 已完成订单
  178. * @return \Illuminate\Http\JsonResponse
  179. */
  180. public function completed()
  181. {
  182. $this->getOrder()->completed();
  183. return $this->successJson($msg = 'ok', $data = $this->getData());
  184. }
  185. }