InternalApiController.php 7.2 KB

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