StoreOrder.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\outapi\controller;
  12. use app\services\order\OutStoreOrderServices;
  13. use app\services\shipping\ExpressServices;
  14. use crmeb\services\CacheService;
  15. use think\facade\App;
  16. /**
  17. * 订单管理
  18. * Class StoreOrder
  19. * @package app\outapi\controller
  20. */
  21. class StoreOrder extends AuthController
  22. {
  23. /**
  24. * StoreOrder constructor.
  25. * @param App $app
  26. * @param OutStoreOrderServices $service
  27. * @method temp
  28. */
  29. public function __construct(App $app, OutStoreOrderServices $service)
  30. {
  31. parent::__construct($app);
  32. $this->services = $service;
  33. }
  34. /**
  35. * 获取订单列表
  36. * @return mixed
  37. */
  38. public function lst()
  39. {
  40. $where = $this->request->getMore([
  41. ['status', ''],
  42. ['real_name', ''],
  43. ['is_del', ''],
  44. ['data', '', '', 'time'],
  45. ['type', ''],
  46. ['pay_type', ''],
  47. ['order', ''],
  48. ['field_key', ''],
  49. ['paid', '']
  50. ]);
  51. $where['is_system_del'] = 0;
  52. $where['pid'] = 0;
  53. return app('json')->success($this->services->getOrderList($where));
  54. }
  55. /**
  56. * 快递公司列表
  57. * @return mixed
  58. */
  59. public function express(ExpressServices $services)
  60. {
  61. [$status] = $this->request->getMore([
  62. ['status', ''],
  63. ], true);
  64. if ($status != '') $data['status'] = $status;
  65. $data['is_show'] = 1;
  66. $list = CacheService::remember('EXPRESS_LIST', function () use ($services, $data) {
  67. return $services->express($data);
  68. }, 86400);
  69. return app('json')->success($list);
  70. }
  71. /**
  72. * 订单发货
  73. * @param string $order_id 订单号
  74. * @return mixed
  75. */
  76. public function delivery(string $order_id)
  77. {
  78. if (!$order_id) return app('json')->fail('参数错误');
  79. $data = $this->request->postMore([
  80. ['delivery_name', ''],//快递公司名称
  81. ['delivery_id', ''],//快递单号
  82. ['delivery_code', ''],//快递公司编码
  83. ]);
  84. $data['express_record_type'] = 1;
  85. $data['type'] = 1;
  86. return app('json')->success('操作成功', $this->services->delivery($order_id, $data));
  87. }
  88. /**
  89. * 获取订单可拆分发货商品列表
  90. * @param string $order_id 订单号
  91. * @return mixed
  92. */
  93. public function splitCartInfo(string $order_id)
  94. {
  95. if (!$order_id) return app('json')->fail('参数错误');
  96. return app('json')->success($this->services->getCartList($order_id));
  97. }
  98. /**
  99. * 订单拆单发送货
  100. * @param string $order_id 订单号
  101. * @return mixed
  102. */
  103. public function splitDelivery(string $order_id)
  104. {
  105. if (!$order_id) return app('json')->fail('参数错误');
  106. $data = $this->request->postMore([
  107. ['delivery_name', ''],//快递公司名称
  108. ['delivery_id', ''],//快递单号
  109. ['delivery_code', ''],//快递公司编码
  110. ['fictitious_content', ''],//虚拟发货内容
  111. ['cart_ids', []]
  112. ]);
  113. if (!$data['cart_ids']) {
  114. return app('json')->fail('请选择发货商品');
  115. }
  116. foreach ($data['cart_ids'] as &$cart) {
  117. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num']) {
  118. return app('json')->fail('请重新选择发货商品或发货件数');
  119. }
  120. $cart['cart_id'] = (int)$cart['cart_id'];
  121. $cart['cart_num'] = (int)$cart['cart_num'];
  122. }
  123. $data['express_record_type'] = 1;
  124. $data['type'] = 1;
  125. $this->services->splitDelivery($order_id, $data);
  126. return app('json')->success('操作成功');
  127. }
  128. /**
  129. * 确认收货
  130. * @param string $order_id 订单号
  131. * @return mixed
  132. * @throws \Exception
  133. */
  134. public function receive(string $order_id)
  135. {
  136. if (!$order_id) return app('json')->fail('参数错误');
  137. $this->services->receive($order_id);
  138. return app('json')->success('收货成功');
  139. }
  140. /**
  141. * 设置发票信息
  142. * @param string $order_id 订单号
  143. * @return mixed
  144. */
  145. public function setInvoice(string $order_id)
  146. {
  147. if (!$order_id) return app('json')->fail('参数错误');
  148. $data = $this->request->postMore([
  149. [['header_type', 'd'], 1],
  150. [['type', 'd'], 1],
  151. ['drawer_phone', ''],
  152. ['email', ''],
  153. ['name', ''],
  154. ['duty_number', ''],
  155. ['tell', ''],
  156. ['address', ''],
  157. ['bank', ''],
  158. ['card_number', ''],
  159. ]);
  160. if (!$data['drawer_phone']) return app('json')->fail('请填写开票手机号');
  161. if (!check_phone($data['drawer_phone'])) return app('json')->fail('手机号格式不正确');
  162. if (!$data['name']) return app('json')->fail('请填写发票抬头(开具发票企业名称)');
  163. if (!in_array($data['header_type'], [1, 2])) {
  164. $data['header_type'] = empty($data['duty_number']) ? 1 : 2;
  165. }
  166. if ($data['header_type'] == 1 && !preg_match('/^[\x80-\xff]{2,60}$/', $data['name'])) {
  167. return app('json')->fail('请填写正确的发票抬头(开具发票企业名称)');
  168. }
  169. if ($data['header_type'] == 2 && !preg_match('/^[0-9a-zA-Z&\(\)\(\)\x80-\xff]{2,150}$/', $data['name'])) {
  170. return app('json')->fail('请填写正确的发票抬头(开具发票企业名称)');
  171. }
  172. if ($data['header_type'] == 2 && !$data['duty_number']) {
  173. return app('json')->fail('请填写发票税号');
  174. }
  175. if ($data['header_type'] == 2 && !preg_match('/^[A-Z0-9]{15}$|^[A-Z0-9]{17}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/', $data['duty_number'])) {
  176. return app('json')->fail('请填写正确的发票税号');
  177. }
  178. if ($data['card_number'] && !preg_match('/^[1-9]\d{11,19}$/', $data['card_number'])) {
  179. return app('json')->fail('请填写正确的银行卡号');
  180. }
  181. if ($this->services->setInvoice($order_id, $data)) {
  182. return app('json')->success('修改成功');
  183. } else {
  184. return app('json')->fail('操作失败');
  185. }
  186. }
  187. /**
  188. * 设置发票状态
  189. * @param string $order_id 订单号
  190. * @return mixed
  191. */
  192. public function setInvoiceStatus(string $order_id)
  193. {
  194. if (!$order_id) return app('json')->fail('参数错误');
  195. $data = $this->request->postMore([
  196. ['is_invoice', 0],
  197. ['invoice_number', 0],
  198. ['remark', '']
  199. ]);
  200. if ($data['is_invoice'] == 1 && !$data['invoice_number']) {
  201. return app('json')->fail('请填写开票号');
  202. }
  203. $this->services->setInvoice($order_id, $data);
  204. return app('json')->success('设置成功');
  205. }
  206. /**
  207. * 订单详情
  208. * @param string $order_id 订单号
  209. * @return mixed
  210. */
  211. public function read(string $order_id)
  212. {
  213. if (!$order_id) return app('json')->fail('参数错误');
  214. return app('json')->success($this->services->getInfo($order_id));
  215. }
  216. /**
  217. * 修改备注
  218. * @param string $order_id 订单号
  219. * @return mixed
  220. */
  221. public function remark(string $order_id)
  222. {
  223. if (!$order_id) return app('json')->fail('参数错误');
  224. $data = $this->request->postMore([['remark', '']]);
  225. if (!$data['remark']) return app('json')->fail('备注不能为空');
  226. if (!$order = $this->services->get(['order_id' => $order_id])) {
  227. return app('json')->fail('订单不存在');
  228. }
  229. $order->remark = $data['remark'];
  230. if ($order->save()) {
  231. return app('json')->success('备注成功');
  232. } else
  233. return app('json')->fail('备注失败');
  234. }
  235. /**
  236. * 修改配送信息
  237. * @param string $order_id 订单号
  238. * @return mixed
  239. */
  240. public function updateDistribution(string $order_id)
  241. {
  242. if (!$order_id) return app('json')->fail('参数错误');
  243. $data = $this->request->postMore([['delivery_name', ''], ['delivery_code', ''], ['delivery_id', '']]);
  244. $this->services->updateDistribution($order_id, $data);
  245. return app('json')->success('操作成功');
  246. }
  247. }