1
0

InternalApiController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\common\controller;
  3. use app\api\logic\PerformanceLogic;
  4. use app\api\logic\ServiceOrderLogic;
  5. use app\common\enum\PayEnum;
  6. use app\common\enum\worker\WorkerAccountLogEnum;
  7. use app\common\logic\PayNotifyLogic;
  8. use app\common\logic\WorkerAccountLogLogic;
  9. use app\common\model\external\ExternalPlatform;
  10. use app\common\model\recharge\RechargeOrder;
  11. use app\common\model\works\ServiceWork;
  12. use app\common\model\works\ServiceWorkLog;
  13. use think\facade\Config;
  14. use think\facade\Log;
  15. class InternalApiController extends BaseLikeAdminController
  16. {
  17. public array $notNeedLogin = ['changeAppointment','confirmServiceFinish','paymentSuccessful','cancelOrder'];
  18. private function checkSign(){
  19. $params = $this->request->param();
  20. // 验证IP
  21. $sign = ExternalPlatform::getSign(env('internal_api.api_sign_key'),$params);
  22. Log::info('内部请求参数'.json_encode([$params,$sign]));
  23. return true;
  24. /*if ($sign && $sign === $params['sign']) {
  25. return true;
  26. }
  27. return false;*/
  28. }
  29. public function changeAppointment()
  30. {
  31. try {
  32. $params = $this->request->param();
  33. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  34. //sn appointment_time
  35. if(!isset($params['appointment_time']) || empty($params['appointment_time'])){
  36. throw new \Exception('预约时间不能为空',404);
  37. }
  38. if(!isset($params['sn']) || empty($params['sn'])){
  39. throw new \Exception('订单号不能为空',404);
  40. }
  41. $result = ServiceOrderLogic::approvalChangeAppointment($params);
  42. if (false === $result) {
  43. throw new \Exception(ServiceOrderLogic::getError(),404);
  44. }
  45. return $this->success('内部重新预约完成', [], 0, 1);
  46. }catch(\Exception $e){
  47. return $this->fail($e->getMessage(),[],$e->getCode());
  48. }
  49. }
  50. public function confirmServiceFinish()
  51. {
  52. try {
  53. $params = $this->request->param();
  54. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  55. // 工单信息
  56. $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  57. if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
  58. if($service_work->user_confirm_status < 3){
  59. //throw new \Exception('正在服务中,请稍后再试...',404);
  60. }
  61. if(in_array($service_work->service_status,[3,4,5])){
  62. return $this->success('内部确认服务完成', [], 0, 1);
  63. }
  64. $result = ServiceOrderLogic::confirmServiceFinish([
  65. 'sn' => $params['sn'],
  66. 'user_id' => $service_work->user_id,
  67. 'user_info'=>['real_name' => $service_work->real_name],
  68. ]);
  69. if (false === $result) {
  70. throw new \Exception(ServiceOrderLogic::getError(),404);
  71. }
  72. // 工程师完单的时候设置该规则关闭,以及短信通知工程师
  73. ServiceOrderLogic::orderQuantityRule($params);
  74. return $this->success('内部确认服务完成', [], 0, 1);
  75. }catch(\Exception $e){
  76. return $this->fail($e->getMessage(),[],$e->getCode());
  77. }
  78. }
  79. public function paymentSuccessful()
  80. {
  81. try {
  82. $params = $this->request->param();
  83. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  84. $params['sn'] = mb_substr($params['sn'], 0, 18);
  85. $order = RechargeOrder::where(['sn' => $params['sn']])->findOrEmpty();
  86. if($order->isEmpty()) {
  87. throw new \Exception('内部订单不存在:'.$params['sn'],404);
  88. }
  89. if($order->pay_status == PayEnum::ISPAID) {
  90. return $this->success('内部支付已完成', [], 0, 1);
  91. }
  92. if(!empty($params['pay_way']??'')) $params['extra']['pay_way'] = $params['pay_way'];
  93. $payNotifyLogic = PayNotifyLogic::handle('goods', $params['sn'], $params['extra']??[]);
  94. if($payNotifyLogic === true){
  95. // 用户下单后,给订单运营专员(配置固定ID)发送公众号提醒(订单信息)
  96. $order = RechargeOrder::where('sn', $params['sn'])
  97. ->where('payment_type','IN',[0,1])
  98. ->where('pay_status','=',1)
  99. ->findOrEmpty();
  100. if(!$order->isEmpty()){
  101. $workDetail = ServiceWork::findOrEmpty($order->work_id);
  102. if(!$workDetail->isEmpty()){
  103. event('Notice', [
  104. 'scene_id' => 100,
  105. 'params' => [
  106. 'user_id' => 0,
  107. 'order_id' => $workDetail['id'],
  108. 'thing3' => $workDetail['title'],
  109. 'time6' => $workDetail['appointment_time'],
  110. 'phone_number8' => asteriskString($workDetail['mobile']),
  111. 'thing5' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
  112. ]
  113. ]);
  114. }
  115. }
  116. return $this->success('内部支付完成', [], 0, 1);
  117. }
  118. throw new \Exception($payNotifyLogic,404);
  119. }catch(\Exception $e){
  120. return $this->fail($e->getMessage(),[],$e->getCode());
  121. }
  122. }
  123. public function cancelOrder()
  124. {
  125. try {
  126. $params = $this->request->param();
  127. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  128. // 工单信息
  129. $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  130. if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
  131. // 取消工单
  132. if($service_work->work_status < 7){
  133. //更新工单状态为已取消
  134. $service_work->service_status = 4;
  135. // 是否存在退款
  136. if($service_work->work_pay_status == 1 && (int)$params['is_refund'] === 1){ //全退不分给工程师
  137. $service_work->work_status = 7;
  138. $service_work->service_status = 5;
  139. $service_work->work_pay_status = 2;
  140. //取消订单
  141. RechargeOrder::where(['user_id'=>$service_work->user_id,'work_id'=>$service_work->id])->update([
  142. 'pay_status' => 2,
  143. ]);
  144. }else if($service_work->work_pay_status == 1 && (int)$params['is_refund'] === 0){ // 上门费分给工程师
  145. $paid_amount = RechargeOrder::where('work_id', $service_work->id)->where('payment_type', 1)->value('paid_amount');
  146. if($paid_amount > 0){
  147. // 存在上门费给工程师
  148. Log::info('内部取消并终止结束服务工单'.$service_work->id.',上门费:'.$paid_amount);
  149. WorkerAccountLogLogic::addAccountLog($service_work,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC);
  150. }
  151. ServiceWork::where('id', $params['id'])->update([
  152. 'work_status' => 7,
  153. 'user_confirm_status' => 5,
  154. 'service_status' => 3,
  155. 'work_pay_status' => 2, // 已结算则不执行 onAfterUpdate
  156. 'settlement_amount' => $paid_amount??0,
  157. 'worker_price' => $paid_amount??0,
  158. 'remark' => ($service_work->remark?:'')." | 内部取消并终止结束服务:上门费-{$paid_amount}"
  159. ]);
  160. }else{
  161. $service_work->work_status = 9;
  162. }
  163. ServiceWorkLog::create([
  164. 'work_id' => $service_work->id,
  165. 'master_worker_id' => $service_work->master_worker_id,
  166. 'opera_log' => "工单:{$service_work->work_sn} 内部取消并终止结束服务"
  167. ]);
  168. $service_work->user_confirm_status = 5;
  169. $service_work->save();
  170. }
  171. /*else{
  172. // 若存在结算则先退回
  173. }*/
  174. return $this->success('内部取消工单完成', [], 0, 1);
  175. }catch(\Exception $e){
  176. return $this->fail($e->getMessage(),[],$e->getCode());
  177. }
  178. }
  179. /**
  180. * tmp - test
  181. * @return \think\response\Json
  182. * @author liugc <466014217@qq.com>
  183. * @date 2025/4/23 11:52
  184. */
  185. public function calculatePerformanceTmp()
  186. {
  187. try {
  188. $params = $this->request->param();
  189. $work = ServiceWork::find($params['work_id']);
  190. //dd($work->toArray());
  191. PerformanceLogic::calculatePerformanceTmp($work);
  192. return $this->success('tmp工单完成', [], 0, 1);
  193. }catch(\Exception $e){
  194. return $this->fail($e->getMessage(),[],$e->getCode());
  195. }
  196. }
  197. }