1
0

InternalApiController.php 5.0 KB

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