InternalApiController.php 5.0 KB

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