InternalApiController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 = ['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 confirmServiceFinish()
  25. {
  26. try {
  27. $params = $this->request->param();
  28. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  29. // 工单信息
  30. $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  31. if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
  32. if($service_work->user_confirm_status < 3){
  33. throw new \Exception('正在服务中,请稍后再试...',404);
  34. }
  35. if(in_array($service_work->service_status,[3,4,5])){
  36. return $this->success('内部确认服务完成', [], 0, 1);
  37. }
  38. $result = ServiceOrderLogic::confirmServiceFinish([
  39. 'sn' => $params['sn'],
  40. 'user_id' => $service_work->user_id,
  41. 'user_info'=>['real_name' => $service_work->real_name],
  42. ]);
  43. if (false === $result) {
  44. throw new \Exception(ServiceOrderLogic::getError(),404);
  45. }
  46. // 工程师完单的时候设置该规则关闭,以及短信通知工程师
  47. ServiceOrderLogic::orderQuantityRule($params);
  48. return $this->success('内部确认服务完成', [], 0, 1);
  49. }catch(\Exception $e){
  50. return $this->fail($e->getMessage(),[],$e->getCode());
  51. }
  52. }
  53. public function paymentSuccessful()
  54. {
  55. try {
  56. $params = $this->request->param();
  57. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  58. $params['sn'] = mb_substr($params['sn'], 0, 18);
  59. $order = RechargeOrder::where(['sn' => $params['sn']])->findOrEmpty();
  60. if($order->isEmpty()) {
  61. throw new \Exception('内部订单不存在:'.$params['sn'],404);
  62. }
  63. if($order->pay_status == PayEnum::ISPAID) {
  64. return $this->success('内部支付已完成', [], 0, 1);
  65. }
  66. $payNotifyLogic = PayNotifyLogic::handle('goods', $params['sn'], $params['extra']??[]);
  67. if($payNotifyLogic === true){
  68. return $this->success('内部支付完成', [], 0, 1);
  69. }
  70. throw new \Exception($payNotifyLogic,404);
  71. }catch(\Exception $e){
  72. return $this->fail($e->getMessage(),[],$e->getCode());
  73. }
  74. }
  75. public function cancelOrder()
  76. {
  77. try {
  78. $params = $this->request->param();
  79. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  80. // 工单信息
  81. $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  82. if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
  83. //取消订单
  84. RechargeOrder::where(['user_id'=>$service_work->user_id,'work_id'=>$service_work->id])->update([
  85. 'pay_status' => 2,
  86. ]);
  87. //更新工单状态为已取消
  88. $service_work->service_status = 4;
  89. $service_work->save();
  90. return $this->success('内部取消工单完成', [], 0, 1);
  91. }catch(\Exception $e){
  92. return $this->fail($e->getMessage(),[],$e->getCode());
  93. }
  94. }
  95. }