InternalApiController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\common\controller;
  3. use app\api\logic\ServiceOrderLogic;
  4. use app\common\enum\PayEnum;
  5. use app\common\enum\worker\WorkerAccountLogEnum;
  6. use app\common\logic\PayNotifyLogic;
  7. use app\common\logic\WorkerAccountLogLogic;
  8. use app\common\model\external\ExternalPlatform;
  9. use app\common\model\recharge\RechargeOrder;
  10. use app\common\model\works\ServiceWork;
  11. use app\common\model\works\ServiceWorkLog;
  12. use think\facade\Config;
  13. use think\facade\Log;
  14. class InternalApiController extends BaseLikeAdminController
  15. {
  16. public array $notNeedLogin = ['changeAppointment','confirmServiceFinish','paymentSuccessful','cancelOrder'];
  17. private function checkSign(){
  18. $params = $this->request->param();
  19. // 验证IP
  20. Log::info('内部请求参数'.json_encode($params));
  21. $sign = ExternalPlatform::getSign(env('internal_api.api_sign_key'),$params);
  22. if ($sign && $sign === $params['sign']) {
  23. return true;
  24. }
  25. return false;
  26. }
  27. public function changeAppointment()
  28. {
  29. try {
  30. $params = $this->request->param();
  31. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  32. //sn appointment_time
  33. if(!isset($params['appointment_time']) || empty($params['appointment_time'])){
  34. throw new \Exception('预约时间不能为空',404);
  35. }
  36. if(!isset($params['sn']) || empty($params['sn'])){
  37. throw new \Exception('订单号不能为空',404);
  38. }
  39. $result = ServiceOrderLogic::approvalChangeAppointment($params);
  40. if (false === $result) {
  41. throw new \Exception(ServiceOrderLogic::getError(),404);
  42. }
  43. return $this->success('内部重新预约完成', [], 0, 1);
  44. }catch(\Exception $e){
  45. return $this->fail($e->getMessage(),[],$e->getCode());
  46. }
  47. }
  48. public function confirmServiceFinish()
  49. {
  50. try {
  51. $params = $this->request->param();
  52. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  53. // 工单信息
  54. $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  55. if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
  56. if($service_work->user_confirm_status < 3){
  57. throw new \Exception('正在服务中,请稍后再试...',404);
  58. }
  59. if(in_array($service_work->service_status,[3,4,5])){
  60. return $this->success('内部确认服务完成', [], 0, 1);
  61. }
  62. $result = ServiceOrderLogic::confirmServiceFinish([
  63. 'sn' => $params['sn'],
  64. 'user_id' => $service_work->user_id,
  65. 'user_info'=>['real_name' => $service_work->real_name],
  66. ]);
  67. if (false === $result) {
  68. throw new \Exception(ServiceOrderLogic::getError(),404);
  69. }
  70. // 工程师完单的时候设置该规则关闭,以及短信通知工程师
  71. ServiceOrderLogic::orderQuantityRule($params);
  72. return $this->success('内部确认服务完成', [], 0, 1);
  73. }catch(\Exception $e){
  74. return $this->fail($e->getMessage(),[],$e->getCode());
  75. }
  76. }
  77. public function paymentSuccessful()
  78. {
  79. try {
  80. $params = $this->request->param();
  81. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  82. $params['sn'] = mb_substr($params['sn'], 0, 18);
  83. $order = RechargeOrder::where(['sn' => $params['sn']])->findOrEmpty();
  84. if($order->isEmpty()) {
  85. throw new \Exception('内部订单不存在:'.$params['sn'],404);
  86. }
  87. if($order->pay_status == PayEnum::ISPAID) {
  88. return $this->success('内部支付已完成', [], 0, 1);
  89. }
  90. $payNotifyLogic = PayNotifyLogic::handle('goods', $params['sn'], $params['extra']??[]);
  91. if($payNotifyLogic === true){
  92. return $this->success('内部支付完成', [], 0, 1);
  93. }
  94. throw new \Exception($payNotifyLogic,404);
  95. }catch(\Exception $e){
  96. return $this->fail($e->getMessage(),[],$e->getCode());
  97. }
  98. }
  99. public function cancelOrder()
  100. {
  101. try {
  102. $params = $this->request->param();
  103. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  104. // 工单信息
  105. $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  106. if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
  107. // 取消工单
  108. if($service_work->work_status < 7){
  109. //更新工单状态为已取消
  110. $service_work->service_status = 4;
  111. // 是否存在退款
  112. if($service_work->work_pay_status == 1 && (int)$params['is_refund'] == 1){
  113. $service_work->service_status = 5;
  114. $service_work->work_pay_status = 2;
  115. //取消订单
  116. RechargeOrder::where(['user_id'=>$service_work->user_id,'work_id'=>$service_work->id])->update([
  117. 'pay_status' => 2,
  118. ]);
  119. }else if($service_work->work_pay_status == 1 && (int)$params['is_refund'] == 0){
  120. $paid_amount = RechargeOrder::where('work_id', $service_work->id)->where('payment_type', 1)->value('paid_amount');
  121. if($paid_amount > 0){
  122. // 存在上门费给工程师
  123. Log::info('内部取消并终止结束服务工单'.$service_work->id.',上门费:'.$paid_amount);
  124. WorkerAccountLogLogic::addAccountLog($service_work,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC);
  125. }
  126. ServiceWork::where('id', $params['id'])->update([
  127. 'work_status' => 7,
  128. 'user_confirm_status' => 5,
  129. 'service_status' => 3,
  130. 'work_pay_status' => 2, // 已结算则不执行 onAfterUpdate
  131. 'settlement_amount' => $paid_amount??0,
  132. 'worker_price' => $paid_amount??0,
  133. 'remark' => ($service_work->remark?:'')." | 内部取消并终止结束服务:上门费-{$paid_amount}"
  134. ]);
  135. ServiceWorkLog::create([
  136. 'work_id' => $service_work->id,
  137. 'master_worker_id' => $service_work->master_worker_id,
  138. 'opera_log' => "工单:{$service_work->work_sn} 内部取消并终止结束服务"
  139. ]);
  140. }
  141. $service_work->work_status = 7;
  142. $service_work->user_confirm_status = 5;
  143. $service_work->save();
  144. }
  145. /*else{
  146. // 若存在结算则先退回
  147. }*/
  148. return $this->success('内部取消工单完成', [], 0, 1);
  149. }catch(\Exception $e){
  150. return $this->fail($e->getMessage(),[],$e->getCode());
  151. }
  152. }
  153. }