StoreOrderController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\admin;
  12. use app\adminapi\controller\v1\agent\AgentLevel;
  13. use app\model\user\User;
  14. use app\Request;
  15. use app\services\order\DeliveryServiceServices;
  16. use app\services\order\StoreOrderCartInfoServices;
  17. use app\services\order\StoreOrderCreateServices;
  18. use app\services\order\StoreOrderDeliveryServices;
  19. use app\services\order\StoreOrderEconomizeServices;
  20. use app\services\order\StoreOrderRefundServices;
  21. use app\services\order\StoreOrderServices;
  22. use app\services\order\StoreOrderWapServices;
  23. use app\services\order\StoreOrderWriteOffServices;
  24. use app\services\pay\OrderOfflineServices;
  25. use app\services\serve\ServeServices;
  26. use app\services\system\store\SystemStoreServices;
  27. use app\services\system\store\SystemStoreStaffServices;
  28. use app\services\user\UserServices;
  29. use app\services\shipping\ExpressServices;
  30. /**
  31. * 订单类
  32. * Class StoreOrderController
  33. * @package app\api\controller\admin\order
  34. */
  35. class StoreOrderController
  36. {
  37. /**
  38. * @var StoreOrderWapServices
  39. */
  40. protected $service;
  41. /**
  42. * StoreOrderController constructor.
  43. * @param StoreOrderWapServices $services
  44. */
  45. public function __construct(StoreOrderWapServices $services)
  46. {
  47. $this->service = $services;
  48. }
  49. /**
  50. * 订单 查看物流
  51. * @param StoreOrderCartInfoServices $services
  52. * @param ExpressServices $expressServices
  53. * @param $uni
  54. * @param string $type
  55. * @return mixed
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function express(StoreOrderServices $orderServices, StoreOrderCartInfoServices $services, ExpressServices $expressServices, $uni, $type = '')
  61. {
  62. if ($type == 'refund') {
  63. /** @var StoreOrderRefundServices $refundService */
  64. $refundService = app()->make(StoreOrderRefundServices::class);
  65. $order = $refundService->refundDetail($uni);
  66. $express = $order['refund_express'];
  67. $cacheName = $uni . $express;
  68. $orderInfo = [];
  69. $info = [];
  70. $cartNew = [];
  71. foreach ($order['cart_info'] as $k => $cart) {
  72. $cartNew['cart_num'] = $cart['cart_num'];
  73. $cartNew['truePrice'] = $cart['truePrice'];
  74. $cartNew['postage_price'] = $cart['postage_price'];
  75. $cartNew['productInfo']['image'] = $cart['productInfo']['image'];
  76. $cartNew['productInfo']['store_name'] = $cart['productInfo']['store_name'];
  77. $cartNew['productInfo']['unit_name'] = $cart['productInfo']['unit_name'] ?? '';
  78. array_push($info, $cartNew);
  79. unset($cart);
  80. }
  81. $orderInfo['cartInfo'] = $info;
  82. $orderInfo['delivery_id'] = $express;
  83. $orderInfo['delivery_name'] = $order['refund_express_name'];
  84. $orderInfo['delivery_code'] = '';
  85. } else {
  86. if (!$uni || !($order = $orderServices->getUserOrderDetail($uni, 0, []))) {
  87. return app('json')->fail(410173);
  88. }
  89. if ($type != 'refund' && ($order['delivery_type'] != 'express' || !$order['delivery_id'])) {
  90. return app('json')->fail(410206);
  91. }
  92. $express = $type == 'refund' ? $order['refund_express'] : $order['delivery_id'];
  93. $cacheName = $uni . $express;
  94. $orderInfo = [];
  95. $cartInfo = $services->getCartColunm(['oid' => $order['id']], 'cart_info', 'unique');
  96. $info = [];
  97. $cartNew = [];
  98. foreach ($cartInfo as $k => $cart) {
  99. $cart = json_decode($cart, true);
  100. $cartNew['cart_num'] = $cart['cart_num'];
  101. $cartNew['truePrice'] = $cart['truePrice'];
  102. $cartNew['postage_price'] = $cart['postage_price'];
  103. $cartNew['productInfo']['image'] = $cart['productInfo']['image'];
  104. $cartNew['productInfo']['store_name'] = $cart['productInfo']['store_name'];
  105. $cartNew['productInfo']['unit_name'] = $cart['productInfo']['unit_name'] ?? '';
  106. array_push($info, $cartNew);
  107. unset($cart);
  108. }
  109. $orderInfo['delivery_id'] = $express;
  110. $orderInfo['delivery_name'] = $type == 'refund' ? '用户退回' : $order['delivery_name'];;
  111. $orderInfo['delivery_code'] = $type == 'refund' ? '' : $order['delivery_code'];
  112. $orderInfo['delivery_type'] = $order['delivery_type'];
  113. $orderInfo['user_address'] = $order['user_address'];
  114. $orderInfo['user_mark'] = $order['mark'];
  115. $orderInfo['cartInfo'] = $info;
  116. }
  117. return app('json')->success([
  118. 'order' => $orderInfo,
  119. 'express' => [
  120. 'result' => ['list' => $expressServices->query($cacheName, $orderInfo['delivery_id'], $orderInfo['delivery_code'], $order['user_phone'])
  121. ]
  122. ]
  123. ]);
  124. }
  125. /**
  126. * 订单数据统计
  127. * @param StoreOrderServices $services
  128. * @return mixed
  129. */
  130. public function statistics(StoreOrderServices $services)
  131. {
  132. $dataCount = $services->getOrderData();
  133. $dataPrice = $this->service->getOrderTimeData();
  134. $data = array_merge($dataCount, $dataPrice);
  135. return app('json')->success($data);
  136. }
  137. /**
  138. * 订单每月统计数据
  139. * @param Request $request
  140. * @return mixed
  141. */
  142. public function data(Request $request)
  143. {
  144. [$start, $stop] = $request->getMore([
  145. ['start', 0],
  146. ['stop', 0]
  147. ], true);
  148. return app('json')->success($this->service->getOrderDataPriceCount(['time' => [$start, $stop]]));
  149. }
  150. /**
  151. * 订单列表
  152. * @param Request $request
  153. * @return mixed
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\DbException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. */
  158. public function lst(Request $request)
  159. {
  160. $where = $request->getMore([
  161. ['status', ''],
  162. ['is_del', 0],
  163. ['data', '', '', 'time'],
  164. ['type', ''],
  165. ['field_key', ''],
  166. ['field_value', ''],
  167. ['keywords', '', '', 'real_name']
  168. ]);
  169. $where['is_system_del'] = 0;
  170. if (!in_array($where['status'], [-1, -2, -3])) {
  171. $where['pid'] = 0;
  172. }
  173. return app('json')->success($this->service->getWapAdminOrderList($where));
  174. }
  175. /**
  176. * 订单详情
  177. * @param Request $request
  178. * @param StoreOrderServices $services
  179. * @param UserServices $userServices
  180. * @param $orderId
  181. * @return mixed
  182. * @throws \think\db\exception\DataNotFoundException
  183. * @throws \think\db\exception\DbException
  184. * @throws \think\db\exception\ModelNotFoundException
  185. */
  186. public function detail(Request $request, StoreOrderServices $services, UserServices $userServices, $orderId)
  187. {
  188. $economizeServices = app()->make(StoreOrderEconomizeServices::class);
  189. $orderData = $services->getUserOrderByKey($economizeServices, $orderId, 0);
  190. $orderData['nickname'] = $userServices->value(['uid' => $orderData['uid']], 'nickname');
  191. return app('json')->success($orderData);
  192. }
  193. /**
  194. * 订单发货获取订单信息
  195. * @param UserServices $userServices
  196. * @param $orderId
  197. * @return mixed
  198. */
  199. public function delivery_gain(UserServices $userServices, $orderId)
  200. {
  201. $order = $this->service->getOne(['order_id' => $orderId], 'real_name,user_phone,user_address,order_id,uid,status,paid,id');
  202. if (!$order) return app('json')->fail(410173);
  203. if ($order['paid']) {
  204. $order['nickname'] = $userServices->value(['uid' => $order['uid']], 'nickname');
  205. $order['config_export_open'] = (bool)((int)sys_config('config_export_open'));
  206. $order = $order->hidden(['uid', 'status', 'paid'])->toArray();
  207. return app('json')->success($order);
  208. }
  209. return app('json')->fail(100016);
  210. }
  211. /**
  212. * 订单发货
  213. * @param Request $request
  214. * @param StoreOrderDeliveryServices $services
  215. * @param $id
  216. * @return mixed
  217. */
  218. public function delivery_keep(Request $request, StoreOrderDeliveryServices $services, $id)
  219. {
  220. $data = $request->postMore([
  221. ['type', 1],
  222. ['delivery_name', ''],//快递公司id
  223. ['delivery_id', ''],//快递单号
  224. ['delivery_code', ''],//快递公司编码
  225. ['delivery_type', ''],//快递公司名称
  226. ['express_record_type', 2],//发货记录类型
  227. ['express_temp_id', ""],//电子面单模板
  228. ['to_name', ''],//寄件人姓名
  229. ['to_tel', ''],//寄件人电话
  230. ['to_addr', ''],//寄件人地址
  231. ['sh_delivery_name', ''],//送货人姓名
  232. ['sh_delivery_id', ''],//送货人电话
  233. ['sh_delivery_uid', ''],//送货人ID
  234. ['fictitious_content', ''],//虚拟发货内容
  235. ['pickup_time', []]
  236. ]);
  237. if ($data['delivery_type']) {
  238. $data['delivery_name'] = $data['delivery_type'];
  239. unset($data['delivery_type']);
  240. }
  241. $services->delivery((int)$id, $data);
  242. return app('json')->success(410273);
  243. }
  244. /**
  245. * 订单改价
  246. * @param Request $request
  247. * @param StoreOrderServices $services
  248. * @return mixed
  249. * @throws \Exception
  250. */
  251. public function price(Request $request, StoreOrderServices $services)
  252. {
  253. [$order_id, $price] = $request->postMore([
  254. ['order_id', ''],
  255. ['price', '']
  256. ], true);
  257. $order = $this->service->getOne(['order_id' => $order_id], 'id,user_phone,id,paid,pay_price,order_id,total_price,total_postage,pay_postage,gain_integral');
  258. if (!$order) return app('json')->fail(410173);
  259. if ($order['paid']) {
  260. return app('json')->fail(410174);
  261. }
  262. if ($price === '') return app('json')->fail(410175);
  263. if ($price < 0) return app('json')->fail(410176);
  264. if ($order['pay_price'] == $price) return app('json')->success(100001, ['order_id' => $order_id]);
  265. $order_id = $services->updateOrder($order['id'], ['total_price' => $order['total_price'], 'pay_price' => $price]);
  266. return app('json')->success(100001, ['order_id' => $order_id]);
  267. }
  268. /**
  269. * 订单备注
  270. * @param Request $request
  271. * @return mixed
  272. */
  273. public function remark(Request $request)
  274. {
  275. [$order_id, $remark] = $request->postMore([
  276. ['order_id', ''],
  277. ['remark', '']
  278. ], true);
  279. $order = $this->service->getOne(['order_id' => $order_id], 'id,remark');
  280. if (!$order) return app('json')->fail(410173);
  281. if (!strlen(trim($remark))) return app('json')->fail(410177);
  282. $order->remark = $remark;
  283. if (!$order->save())
  284. return app('json')->fail(100025);
  285. return app('json')->success(100024);
  286. }
  287. /**
  288. * 订单交易额/订单数量时间统计
  289. * @param Request $request
  290. * @return bool
  291. */
  292. public function time(Request $request)
  293. {
  294. list($start, $stop, $type) = $request->getMore([
  295. ['start', strtotime(date('Y-m'))],
  296. ['stop', time()],
  297. ['type', 1]
  298. ], true);
  299. $start = strtotime(date('Y-m-d 00:00:00', (int)$start));
  300. $stop = strtotime(date('Y-m-d 23:59:59', (int)$stop));
  301. if ($start > $stop) {
  302. $middle = $stop;
  303. $stop = $start;
  304. $start = $middle;
  305. }
  306. $space = bcsub($stop, $start, 0);//间隔时间段
  307. $front = bcsub($start, $space, 0) - 1;//第一个时间段
  308. /** @var StoreOrderServices $orderService */
  309. $orderService = app()->make(StoreOrderServices::class);
  310. $order_where = [
  311. 'pid' => 0,
  312. 'paid' => 1,
  313. 'refund_status' => [0, 3],
  314. 'is_del' => 0,
  315. 'is_system_del' => 0
  316. ];
  317. if ($type == 1) {//销售额
  318. $frontPrice = $orderService->sum($order_where + ['time' => [$front, $start - 1]], 'pay_price', true);
  319. $afterPrice = $orderService->sum($order_where + ['time' => [$start, $stop]], 'pay_price', true);
  320. $chartInfo = $orderService->chartTimePrice($start, $stop);
  321. $data['chart'] = $chartInfo;//营业额图表数据
  322. $data['time'] = $afterPrice;//时间区间营业额
  323. $increase = (float)bcsub((string)$afterPrice, (string)$frontPrice, 2); //同比上个时间区间增长营业额
  324. $growthRate = abs($increase);
  325. if ($growthRate == 0) $data['growth_rate'] = 0;
  326. else if ($frontPrice == 0) $data['growth_rate'] = (int)bcmul($growthRate, 100, 0);
  327. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontPrice, 2), '100', 0);//时间区间增长率
  328. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  329. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  330. } else {//订单数
  331. $frontNumber = $orderService->count($order_where + ['time' => [$front, $start - 1]]);
  332. $afterNumber = $orderService->count($order_where + ['time' => [$start, $stop]]);
  333. $chartInfo = $orderService->chartTimeNumber($start, $stop);
  334. $data['chart'] = $chartInfo;//订单数图表数据
  335. $data['time'] = $afterNumber;//时间区间订单数
  336. $increase = $afterNumber - $frontNumber; //同比上个时间区间增长订单数
  337. $growthRate = abs($increase);
  338. if ($growthRate == 0) $data['growth_rate'] = 0;
  339. else if ($frontNumber == 0) $data['growth_rate'] = (int)bcmul($growthRate, 100, 0);
  340. else $data['growth_rate'] = (int)bcmul((string)bcdiv((string)$growthRate, (string)$frontNumber, 2), '100', 0);//时间区间增长率
  341. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  342. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  343. }
  344. return app('json')->success($data);
  345. }
  346. /**
  347. * 订单支付
  348. * @param Request $request
  349. * @param OrderOfflineServices $services
  350. * @return mixed
  351. */
  352. public function offline(Request $request, OrderOfflineServices $services)
  353. {
  354. [$orderId] = $request->postMore([['order_id', '']], true);
  355. $orderInfo = $this->service->getOne(['order_id' => $orderId], 'id');
  356. if (!$orderInfo) return app('json')->fail(100100);
  357. $id = $orderInfo->id;
  358. $services->orderOffline((int)$id);
  359. return app('json')->success(100010);
  360. }
  361. /**
  362. * 订单退款
  363. * @param Request $request
  364. * @param StoreOrderRefundServices $services
  365. * @param StoreOrderServices $orderServices
  366. * @param StoreOrderCartInfoServices $storeOrderCartInfoServices
  367. * @param StoreOrderCreateServices $storeOrderCreateServices
  368. * @return mixed
  369. * @throws \think\db\exception\DataNotFoundException
  370. * @throws \think\db\exception\DbException
  371. * @throws \think\db\exception\ModelNotFoundException
  372. */
  373. public function refund(Request $request, StoreOrderRefundServices $services, StoreOrderServices $orderServices, StoreOrderCartInfoServices $storeOrderCartInfoServices, StoreOrderCreateServices $storeOrderCreateServices)
  374. {
  375. list($orderId, $price, $type) = $request->postMore([
  376. ['order_id', ''],
  377. ['price', '0'],
  378. ['type', 1],
  379. ], true);
  380. if (!strlen(trim($orderId))) return app('json')->fail(100100);
  381. //退款订单详情
  382. $orderRefund = $services->getOne(['order_id' => $orderId]);
  383. $is_admin = 0;
  384. if (!$orderRefund) {
  385. //主动退款主订单详情
  386. $orderRefund = $orderServices->getOne(['order_id' => $orderId]);
  387. $is_admin = 1;
  388. if ($services->count(['store_order_id' => $orderRefund['id'], 'refund_type' => [0, 1, 2, 4, 5], 'is_cancel' => 0, 'is_del' => 0])) {
  389. return app('json')->fail(410178);
  390. }
  391. }
  392. if (!$is_admin) {
  393. if (!$orderRefund) {
  394. return app('json')->fail(100026);
  395. }
  396. if ($orderRefund['is_cancel'] == 1) {
  397. return app('json')->fail(410179);
  398. }
  399. $orderInfo = $this->service->get((int)$orderRefund['store_order_id']);
  400. if (!$orderInfo) {
  401. return app('json')->fail(100026);
  402. }
  403. if (!in_array($orderRefund['refund_type'], [1, 2, 5])) {
  404. return app('json')->fail(410180);
  405. }
  406. if ($type == 1) {
  407. $data['refund_type'] = 6;
  408. } else if ($type == 2) {
  409. $data['refund_type'] = 3;
  410. } else {
  411. return app('json')->fail(410181);
  412. }
  413. $data['refunded_time'] = time();
  414. //拒绝退款
  415. if ($type == 2) {
  416. $services->refuseRefund((int)$orderRefund['id'], $data, $orderRefund);
  417. return app('json')->success(410182);
  418. } else {
  419. if ($orderRefund['refund_price'] == $orderInfo['refunded_price']) return app('json')->fail(410183);
  420. if (!$price) {
  421. return app('json')->fail(410184);
  422. }
  423. $data['refunded_price'] = bcadd($price, $orderRefund['refunded_price'], 2);
  424. $bj = bccomp((float)$orderRefund['refund_price'], (float)$data['refunded_price'], 2);
  425. if ($bj < 0) {
  426. return app('json')->fail(410185);
  427. }
  428. $refundData['pay_price'] = $orderInfo['pay_price'];
  429. $refundData['refund_price'] = $price;
  430. //修改订单退款状态
  431. if ($services->agreeRefund((int)$orderRefund['id'], $refundData)) {
  432. $services->update((int)$orderRefund['id'], $data);
  433. return app('json')->success(410186);
  434. } else {
  435. $services->storeProductOrderRefundYFasle((int)$orderInfo['id'], $price);
  436. return app('json')->fail(410187);
  437. }
  438. }
  439. } else {
  440. $order = $orderRefund;
  441. $data['refund_price'] = $price;
  442. $data['type'] = $type;
  443. $id = $order['id'];
  444. //0元退款
  445. if ($order['pay_price'] == 0 && in_array($order['refund_status'], [0, 1])) {
  446. $refund_price = 0;
  447. } else {
  448. if ($order['pay_price'] == $order['refund_price']) {
  449. return app('json')->fail(410183);
  450. }
  451. if (!$data['refund_price']) {
  452. return app('json')->fail(410184);
  453. }
  454. $refund_price = $data['refund_price'];
  455. $data['refund_price'] = bcadd($data['refund_price'], $order['refund_price'], 2);
  456. $bj = bccomp((string)$order['pay_price'], (string)$data['refund_price'], 2);
  457. if ($bj < 0) {
  458. return app('json')->fail(410185);
  459. }
  460. }
  461. if ($data['type'] == 1) {
  462. $data['refund_status'] = 2;
  463. $data['refund_type'] = 6;
  464. } else if ($data['type'] == 2) {
  465. $data['refund_status'] = 0;
  466. $data['refund_type'] = 3;
  467. }
  468. $type = $data['type'];
  469. //拒绝退款
  470. if ($type == 2) {
  471. $this->service->update((int)$order['id'], ['refund_status' => 0, 'refund_type' => 3]);
  472. return app('json')->success(410182);
  473. } else {
  474. unset($data['type']);
  475. $refund_data['pay_price'] = $order['pay_price'];
  476. $refund_data['refund_price'] = $refund_price;
  477. //主动退款清楚原本退款单
  478. $services->delete(['store_order_id' => $id]);
  479. //生成退款订单
  480. $refundOrderData['uid'] = $order['uid'];
  481. $refundOrderData['store_id'] = $order['store_id'];
  482. $refundOrderData['store_order_id'] = $id;
  483. $refundOrderData['refund_num'] = $order['total_num'];
  484. $refundOrderData['refund_type'] = $data['refund_type'];
  485. $refundOrderData['refund_price'] = $order['pay_price'];
  486. $refundOrderData['refunded_price'] = $refund_price;
  487. $refundOrderData['refunded_reason'] = '管理员手动退款';
  488. $refundOrderData['order_id'] = $storeOrderCreateServices->getNewOrderId('');
  489. $refundOrderData['refunded_time'] = time();
  490. $refundOrderData['add_time'] = time();
  491. $cartInfos = $storeOrderCartInfoServices->getCartColunm(['oid' => $id], 'id,cart_id,cart_num,cart_info');
  492. foreach ($cartInfos as &$cartInfo) {
  493. $cartInfo['cart_info'] = is_string($cartInfo['cart_info']) ? json_decode($cartInfo['cart_info'], true) : $cartInfo['cart_info'];
  494. }
  495. $refundOrderData['cart_info'] = json_encode(array_column($cartInfos, 'cart_info'));
  496. $res = $services->save($refundOrderData);
  497. //修改订单退款状态
  498. if ($services->agreeRefund((int)$res->id, $refund_data)) {
  499. $this->service->update($id, $data);
  500. return app('json')->success(410186);
  501. } else {
  502. $services->storeProductOrderRefundYFasle((int)$id, $refund_price);
  503. return app('json')->fail(410187);
  504. }
  505. }
  506. }
  507. }
  508. /**
  509. * 门店核销
  510. * @param Request $request
  511. * @param StoreOrderWriteOffServices $services
  512. * @return mixed
  513. * @throws \think\db\exception\DataNotFoundException
  514. * @throws \think\db\exception\DbException
  515. * @throws \think\db\exception\ModelNotFoundException
  516. */
  517. public function order_verific(Request $request, StoreOrderWriteOffServices $services)
  518. {
  519. list($verifyCode, $isConfirm) = $request->postMore([
  520. ['verify_code', ''],
  521. ['is_confirm', 0]
  522. ], true);
  523. if (!$verifyCode) return app('json')->fail(410188);
  524. $uid = $request->uid();
  525. $orderInfo = $services->writeOffOrder($verifyCode, (int)$isConfirm, $uid);
  526. if ($isConfirm == 0) {
  527. return app('json')->success($orderInfo);
  528. }
  529. return app('json')->success(410189);
  530. }
  531. /**
  532. * 获取所有配送员列表
  533. * @param DeliveryServiceServices $services
  534. * @return mixed
  535. */
  536. public function getDeliveryAll(DeliveryServiceServices $services)
  537. {
  538. $list = $services->getDeliveryList();
  539. return app('json')->success($list['list']);
  540. }
  541. /**
  542. * 获取配置信息
  543. * @return mixed
  544. */
  545. public function getDeliveryInfo()
  546. {
  547. return app('json')->success([
  548. 'express_temp_id' => sys_config('config_export_temp_id'),
  549. 'to_name' => sys_config('config_export_to_name'),
  550. 'id' => sys_config('config_export_id'),
  551. 'to_tel' => sys_config('config_export_to_tel'),
  552. 'to_add' => sys_config('config_export_to_address')
  553. ]);
  554. }
  555. /**
  556. * 获取面单信息
  557. * @param Request $request
  558. * @param ServeServices $services
  559. * @return mixed
  560. */
  561. public function getExportTemp(Request $request, ServeServices $services)
  562. {
  563. [$com] = $request->getMore([
  564. ['com', ''],
  565. ], true);
  566. return app('json')->success($services->express()->temp($com));
  567. }
  568. /**
  569. * 物流公司
  570. * @param ExpressServices $services
  571. * @return mixed
  572. */
  573. public function getExportAll(ExpressServices $services)
  574. {
  575. return app('json')->success($services->expressList());
  576. }
  577. /**
  578. * 移动端订单管理退款列表
  579. * @param Request $request
  580. * @param StoreOrderRefundServices $services
  581. * @return mixed
  582. */
  583. public function refundOrderList(Request $request, StoreOrderRefundServices $services)
  584. {
  585. $where = $request->getMore([
  586. ['order_id', ''],
  587. ['time', ''],
  588. ['refund_type', 0],
  589. ['keywords', ''],
  590. ]);
  591. $where['is_cancel'] = 0;
  592. $data = $services->refundList($where)['list'];
  593. return app('json')->success($data);
  594. }
  595. /**
  596. * 订单详情
  597. * @param StoreOrderRefundServices $services
  598. * @param $uni
  599. * @return mixed
  600. */
  601. public function refundOrderDetail(StoreOrderRefundServices $services, $uni)
  602. {
  603. $data = $services->refundDetail($uni);
  604. return app('json')->success($data);
  605. }
  606. /**
  607. * 退款备注
  608. * @param StoreOrderRefundServices $services
  609. * @param Request $request
  610. * @return mixed
  611. */
  612. public function refundRemark(StoreOrderRefundServices $services, Request $request)
  613. {
  614. [$remark, $order_id] = $request->postMore([
  615. ['remark', ''],
  616. ['order_id', ''],
  617. ], true);
  618. if (!$remark)
  619. return app('json')->fail(410177);
  620. if (!$order_id)
  621. return app('json')->fail(100100);
  622. if (!$order = $services->get(['order_id' => $order_id])) {
  623. return app('json')->fail(410173);
  624. }
  625. $order->remark = $remark;
  626. if ($order->save()) {
  627. return app('json')->success(100024);
  628. } else
  629. return app('json')->fail(100025);
  630. }
  631. /**
  632. * 同意退货
  633. * @param StoreOrderRefundServices $services
  634. * @param Request $request
  635. * @return mixed
  636. */
  637. public function agreeExpress(StoreOrderRefundServices $services, Request $request)
  638. {
  639. [$id] = $request->postMore([
  640. ['id', ''],
  641. ], true);
  642. $services->agreeExpress($id);
  643. return app('json')->success(100010);
  644. }
  645. /**
  646. * 获取门店信息
  647. * @param Request $request
  648. * @return mixed
  649. * @throws \think\db\exception\DataNotFoundException
  650. * @throws \think\db\exception\DbException
  651. * @throws \think\db\exception\ModelNotFoundException
  652. */
  653. public function get_store_info(Request $request)
  654. {
  655. $uid = $request->uid();
  656. /** @var SystemStoreServices $storeServices */
  657. $storeServices = app()->make(SystemStoreServices::class);
  658. // 根据uid获取门店信息
  659. $storeInfo = $storeServices->getOne(['uid' => $uid]);
  660. if ($storeInfo) {
  661. $info = $storeServices->getStoreDispose($storeInfo['id']);
  662. return app('json')->success(compact('info'));
  663. }
  664. return app('json')->success(['info' => null]);
  665. }
  666. /**
  667. * 保存门店信息
  668. * @param Request $request
  669. * @return mixed
  670. */
  671. public function save_store(Request $request)
  672. {
  673. $uid = $request->uid();
  674. $data = $request->postMore([
  675. ['name', ''],
  676. ['introduction', ''],
  677. ['image', ''],
  678. ['oblong_image', ''],
  679. ['phone', ''],
  680. ['address', ''],
  681. ['detailed_address', ''],
  682. ['latlng', ''],
  683. ['day_time', []],
  684. ]);
  685. /** @var SystemStoreServices $storeServices */
  686. $storeServices = app()->make(SystemStoreServices::class);
  687. $agent_id = User::where('uid',$uid)->value('agent_level');
  688. $agent_level = \app\model\agent\AgentLevel::where('id',$agent_id)->value('grade');
  689. if ($agent_level<3){
  690. return app('json')->fail("V2会员才能创建门店");
  691. }
  692. // 检查同uid是否已有门店
  693. $existingStore = $storeServices->getOne(['uid' => $uid]);
  694. $storeId = $existingStore ? $existingStore['id'] : 0;
  695. // 处理数据
  696. $data['uid'] = $uid;
  697. $data['address'] = implode(',', $data['address']);
  698. $data['latlng'] = explode(',', $data['latlng']);
  699. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) {
  700. return app('json')->fail(400125);
  701. }
  702. $data['latitude'] = $data['latlng'][0];
  703. $data['longitude'] = $data['latlng'][1];
  704. $data['day_time'] = implode(' - ', $data['day_time']);
  705. unset($data['latlng']);
  706. if ($data['image'] && strstr($data['image'], 'http') === false) {
  707. $site_url = sys_config('site_url');
  708. $data['image'] = $site_url . $data['image'];
  709. }
  710. $storeServices->saveStore((int)$storeId, $data);
  711. // 获取刚才保存的门店信息以获得门店ID
  712. $storeInfo = $storeServices->getOne(['uid' => $uid]);
  713. $newStoreId = $storeInfo ? $storeInfo['id'] : $storeId;
  714. // 添加店员
  715. $staffServices = app()->make(SystemStoreStaffServices::class);
  716. $userServices = app()->make(UserServices::class);
  717. // 检查手机号是否已被其他店员使用
  718. if ($staffServices->count(['phone' => $data['phone']])) {
  719. return app('json')->fail('手机号码已存在');
  720. }
  721. // 检查该用户是否已经是店员
  722. if (!$staffServices->count(['uid' => $uid])) {
  723. // 从user表获取店员的image和staff_name
  724. $userInfo = $userServices->getUserInfo($uid);
  725. $staffData = [
  726. 'uid' => $uid,
  727. 'store_id' => $newStoreId,
  728. 'staff_name' => $userInfo['nickname'] ?? '',
  729. 'avatar' => $userInfo['avatar'] ?? ($userInfo['image'] ?? ''),
  730. 'phone' => $data['phone'],
  731. 'verify_status' => 1,
  732. 'status' => 1,
  733. 'add_time' => time(),
  734. ];
  735. $staffServices->save($staffData);
  736. }
  737. return app('json')->success(100014);
  738. }
  739. }