ExternalPlatformLogic.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\common\logic;
  3. use app\common\model\external\ExternalPlatform;
  4. use app\common\model\recharge\RechargeOrder;
  5. use app\common\model\works\ServiceWork;
  6. use think\facade\Log;
  7. class ExternalPlatformLogic extends BaseLogic
  8. {
  9. /**
  10. * @notes 根据场景执行任务
  11. */
  12. public static function handleByScene($params)
  13. {
  14. try {
  15. switch ($params['scene']){
  16. case 'cancel_order':
  17. //self::cancelOrder($params['params']);
  18. break;
  19. case 'confirm_price':
  20. self::confirmPrice($params['params']);
  21. break;
  22. case 'service_finish':
  23. self::serviceFinish($params['params']);
  24. break;
  25. default:
  26. throw new \Exception('场景不存在');
  27. }
  28. return true;
  29. } catch (\Exception $e) {
  30. Log::info('ExternalPlatform-error:'.$e->getMessage());
  31. self::setError($e->getMessage());
  32. return false;
  33. }
  34. }
  35. // 工程师报价通知
  36. private static function confirmPrice($params)
  37. {
  38. try {
  39. $work = ServiceWork::where(['id'=>$params['work_id']])->findOrEmpty();
  40. if($work->isEmpty()){
  41. throw new \Exception('工单不存在');
  42. }
  43. if($work->external_platform_id > 0){
  44. $externalPlatform = ExternalPlatform::find($work->external_platform_id);
  45. $order_amount = RechargeOrder::where([
  46. 'user_id'=>$work->user_id,
  47. 'work_id'=>$params['work_id'],
  48. 'pay_status'=>0,
  49. 'payment_type'=>2
  50. ])->value('order_amount');
  51. $data = [
  52. 'timestamp'=>time(),
  53. 'scene'=>'confirm_price',
  54. 'version'=>'1',
  55. 'notice_data'=>json_encode([
  56. 'work_sn'=>$work->work_sn,
  57. 'order_amount'=>$order_amount * 100, // 单位分
  58. ],JSON_UNESCAPED_UNICODE)
  59. ];
  60. $data['sign'] = ExternalPlatform::getSign($externalPlatform['appkey'],$data);
  61. if($externalPlatform['send_url']){
  62. $res = http_request($externalPlatform['send_url'],http_build_query($data));
  63. }else{
  64. $res = '未配置接口地址';
  65. }
  66. Log::info('ExternalPlatform-confirmPrice:'
  67. .'url:'.$externalPlatform['send_url']
  68. .'|data:'.json_encode($data,JSON_UNESCAPED_UNICODE)
  69. .'|res:'.json_encode([$res],JSON_UNESCAPED_UNICODE)
  70. );
  71. }
  72. return true;
  73. } catch (\Exception $e) {
  74. throw new \Exception($e->getMessage());
  75. }
  76. }
  77. // 工程师服务完成通知
  78. private static function serviceFinish($params)
  79. {
  80. try {
  81. $work = ServiceWork::where(['id'=>$params['work_id']])->findOrEmpty();
  82. if($work->isEmpty()){
  83. throw new \Exception('工单不存在');
  84. }
  85. if($work->external_platform_id > 0){
  86. $externalPlatform = ExternalPlatform::find($work->external_platform_id);
  87. $data = [
  88. 'timestamp'=>time(),
  89. 'scene'=>'service_finish',
  90. 'version'=>'1',
  91. 'notice_data'=>json_encode([
  92. 'work_sn'=>$work->work_sn
  93. ],JSON_UNESCAPED_UNICODE)
  94. ];
  95. $data['sign'] = ExternalPlatform::getSign($externalPlatform['appkey'],$data);
  96. if($externalPlatform['send_url']){
  97. $res = http_request($externalPlatform['send_url'],http_build_query($data));
  98. }else{
  99. $res = '未配置接口地址';
  100. }
  101. Log::info('ExternalPlatform-serviceFinish:'
  102. .'url:'.$externalPlatform['send_url']
  103. .'|data:'.json_encode($data,JSON_UNESCAPED_UNICODE)
  104. .'|res:'.json_encode([$res],JSON_UNESCAPED_UNICODE)
  105. );
  106. }
  107. return true;
  108. } catch (\Exception $e) {
  109. throw new \Exception($e->getMessage());
  110. }
  111. }
  112. }