StoreOrderVerify.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\store\order;
  12. use think\exception\ValidateException;
  13. use app\common\repositories\store\order\StoreOrderRepository;
  14. use crmeb\basic\BaseController;
  15. use think\App;
  16. class StoreOrderVerify extends BaseController
  17. {
  18. protected $user;
  19. protected $service;
  20. public function __construct(App $app)
  21. {
  22. parent::__construct($app);
  23. }
  24. /**
  25. * 根据订单ID查询订单详情
  26. *
  27. * 本函数用于通过提供的订单ID和商家ID来查询特定订单的详细信息。
  28. * 它首先尝试根据ID获取订单,如果订单不存在,则返回错误信息。
  29. * 如果订单存在但不属于请求的商家,则同样返回错误信息。
  30. * 如果订单存在且属于请求的商家,则成功返回订单详情。
  31. *
  32. * @param string $merId 商家ID,用于权限检查,确定请求的商家是否有权查看该订单。
  33. * @param int $id 订单ID,用于查询特定的订单。
  34. * @param StoreOrderRepository $repository 订单仓库对象,用于执行订单查询操作。
  35. * @return json 返回订单详情的JSON响应,如果订单不存在或无权查看,则返回错误的JSON响应。
  36. */
  37. public function detail($merId, $id, StoreOrderRepository $repository)
  38. {
  39. // 根据订单ID查询订单详情
  40. $order = $repository->codeByDetail($id);
  41. // 检查订单是否存在,如果不存在则返回错误信息
  42. if (!$order) return app('json')->fail('订单不存在');
  43. if ($order['is_virtual'] == 4) {
  44. $auth = $this->checkStaffAuth($order);
  45. if (!$auth) $this->checkServerAuth($order);
  46. } else {
  47. $auth = $this->checkServerAuth($order);
  48. }
  49. // 检查当前商家是否有权查看该订单,如果订单商家ID与请求的商家ID不匹配,则返回错误信息
  50. if (!$auth)
  51. return app('json')->fail('没有权限查询该订单');
  52. // 返回订单详情的成功响应
  53. return app('json')->success($order);
  54. }
  55. public function checkStaffAuth($order)
  56. {
  57. if ($this->request->isStaffs()){
  58. if (!in_array($order->mer_id,$this->request->staffsMerIds())) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. return false;
  64. }
  65. public function checkServerAuth($order)
  66. {
  67. if ($this->request->isServer()){
  68. if ($order->mer_id !== $this->merId) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. return false;
  74. }
  75. /**
  76. * 核验订单
  77. *
  78. * 本函数用于验证商家订单的合法性并进行核销操作。通过接收商家ID和订单ID,结合请求中的验证数据,
  79. * 调用存储层的相应方法来执行订单核销流程。此过程对于确保订单的准确性和维护业务正常运行至关重要。
  80. *
  81. * @param string $merId 商家ID,用于识别订单所属的商家
  82. * @param string $id 订单ID,用于唯一标识待核销的订单
  83. * @param StoreOrderRepository $repository 订单存储层的接口,用于执行实际的订单核销操作
  84. * @return \think\Response 返回一个表示核销成功的结果对象
  85. */
  86. public function verify($merId, $id, StoreOrderRepository $repository)
  87. {
  88. // 从请求中获取核销数据和验证码
  89. $data = $this->request->params(['data','verify_code']);
  90. // 根据订单ID、商家ID、验证码和订单类型查询订单,并连带查询订单产品信息
  91. $order = $repository->getWhere(['order_id' => $id, 'mer_id' => $merId, 'verify_code' => $data['verify_code']], '*', ['orderProduct']);
  92. // 如果订单不存在,则抛出验证异常
  93. if (!$order) return app('json')->fail('[1]订单不存在');
  94. // 如果订单未支付,则抛出验证异常
  95. if (!$order->paid) return app('json')->fail('订单未支付');
  96. // 如果订单已全部核销,则抛出验证异常
  97. if ($order['status']) return app('json')->fail('[1]订单已全部核销,请勿重复操作');
  98. if ($order['is_virtual'] == 4) {
  99. $auth = $this->checkStaffAuth($order);
  100. $staffs_id = $this->request->staffsList()[$order->mer_id]['staffs_id'];
  101. if (!$auth){
  102. $this->checkServerAuth($order);
  103. $service_id = $this->request->serviceInfo()->service_id;
  104. }
  105. } else {
  106. $auth = $this->checkServerAuth($order);
  107. }
  108. if (!$auth) app('json')->fail('没有权限操作');
  109. if ($staffs_id ?? false)
  110. $repository->reservationVerify($id, $order->mer_id, 0,$staffs_id);
  111. if ($service_id ?? false)
  112. $repository->verifyOrder($order, $data,$service_id);
  113. // 返回表示核销成功的结果
  114. return app('json')->success('订单核销成功');
  115. }
  116. }