Ver Fonte

add - 外部SDK

liugc há 1 ano atrás
pai
commit
37850b449e

+ 10 - 0
app/adminapi/logic/works/ServiceWorkLogic.php

@@ -315,6 +315,16 @@ class ServiceWorkLogic extends BaseLogic
             ];
             ServiceWorkLogLogic::add($work_log);
             Db::commit();
+
+            // 外部平台工单 - 通知外边平台
+            if($work->external_platform_id > 0){
+                event('ExternalPlatform',  [
+                    'scene' => 'confirm_price',
+                    'params' => [
+                        'work_id' => $work->id,
+                    ]
+                ]);
+            }
         }
         catch (\Exception $e) {
             Db::rollback();

+ 107 - 0
app/api/controller/notify/InternalApi.php

@@ -0,0 +1,107 @@
+<?php
+
+namespace app\api\controller\notify;
+
+use app\api\controller\BaseApiController;
+use app\api\logic\ServiceOrderLogic;
+use app\common\enum\PayEnum;
+use app\common\logic\PayNotifyLogic;
+use app\common\model\external\ExternalPlatform;
+use app\common\model\recharge\RechargeOrder;
+use app\common\model\works\ServiceWork;
+use think\facade\Config;
+
+class InternalApi extends BaseApiController
+{
+    public array $notNeedLogin = ['confirmServiceFinish','paymentSuccessful','cancelOrder'];
+
+    private function checkSign(){
+        $params = $this->request->param();
+        // 验证IP
+
+        $sign = ExternalPlatform::getSign(Config::get('internal_api.api_sign_key'),$params);
+        if ($sign && $sign === $params['sign']) {
+            return true;
+        }
+        return false;
+    }
+
+
+    public function confirmServiceFinish()
+    {
+        try {
+            $params = $this->request->param();
+            if(!$this->checkSign()) throw new \Exception('签名错误',404);
+
+            // 工单信息
+            $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
+            if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
+
+            $result = ServiceOrderLogic::confirmServiceFinish([
+                'sn' => $params['sn'],
+                'user_id' => $service_work->user_id,
+                'user_info'=>['real_name' => $service_work->real_name],
+            ]);
+            if (false === $result) {
+                throw new \Exception(ServiceOrderLogic::getError(),404);
+            }
+            // 工程师完单的时候设置该规则关闭,以及短信通知工程师
+            ServiceOrderLogic::orderQuantityRule($params);
+            return $this->success('确认服务完成', [], 0, 1);
+        }catch(\Exception $e){
+            return $this->fail($e->getMessage(),[],$e->getCode());
+        }
+    }
+
+    public function paymentSuccessful()
+    {
+        try {
+            $params = $this->request->param();
+            if(!$this->checkSign()) throw new \Exception('签名错误',404);
+
+            $params['sn'] = mb_substr($params['sn'], 0, 18);
+            $order = RechargeOrder::where(['sn' => $params['sn']])->findOrEmpty();
+            if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
+                return $this->success('内部支付完成', [], 0, 1);
+            }
+            $payNotifyLogic = PayNotifyLogic::handle('goods', $params['sn'], $params['extra']??[]);
+            if($payNotifyLogic === true){
+                return $this->success('内部支付完成', [], 0, 1);
+            }
+            throw new \Exception($payNotifyLogic,404);
+        }catch(\Exception $e){
+            return $this->fail($e->getMessage(),[],$e->getCode());
+        }
+    }
+
+    public function cancelOrder()
+    {
+        try {
+            $params = $this->request->param();
+            if(!$this->checkSign()) throw new \Exception('签名错误',404);
+
+            // 工单信息
+            $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
+            if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
+
+            //取消订单
+            RechargeOrder::update([
+                'user_id'=>$service_work->user_id,
+                'work_id'=>$service_work->id,
+                'pay_status' => 2,
+            ]);
+            //更新工单状态为已取消
+            $service_work->service_status = 4;
+            $service_work->save();
+
+            return $this->success('内部取消工单完成', [], 0, 1);
+        }catch(\Exception $e){
+            return $this->fail($e->getMessage(),[],$e->getCode());
+        }
+    }
+
+
+
+
+
+}

+ 31 - 0
app/common/listener/ExternalPlatformListener.php

@@ -0,0 +1,31 @@
+<?php
+namespace app\common\listener;
+
+use app\common\logic\ExternalPlatformLogic;
+use think\facade\Log;
+
+/**
+ * 外部平台通知事件监听
+ * Class ExternalPlatformListener
+ * @package app\listener
+ */
+class ExternalPlatformListener
+{
+    public function handle($params)
+    {
+        try {
+            // 工单服务取消通知 cancel_order 尾款报价通知 confirm_price
+            if (empty($params['scene'])) {
+                throw new \Exception('场景不能为空');
+            }
+            $result = ExternalPlatformLogic::handleByScene($params);
+            if (false === $result) {
+                throw new \Exception(ExternalPlatformLogic::getError());
+            }
+            return true;
+        } catch (\Exception $e) {
+            Log::write($params['scene'].':执行失败:'.$e->getMessage());
+            return $e->getMessage();
+        }
+    }
+}

+ 80 - 0
app/common/logic/ExternalPlatformLogic.php

@@ -0,0 +1,80 @@
+<?php
+
+namespace app\common\logic;
+
+use app\common\model\external\ExternalPlatform;
+use app\common\model\recharge\RechargeOrder;
+use app\common\model\works\ServiceWork;
+use think\facade\Log;
+
+class ExternalPlatformLogic  extends BaseLogic
+{
+    /**
+     * @notes 根据场景执行任务
+     */
+    public static function handleByScene($params)
+    {
+        try {
+            switch ($params['scene']){
+                case 'cancel_order':
+                    //self::cancelOrder($params['params']);
+                    break;
+                case 'confirm_price':
+                    self::confirmPrice($params['params']);
+                    break;
+                default:
+                    throw new \Exception('场景不存在');
+            }
+            return true;
+        } catch (\Exception $e) {
+            Log::info('ExternalPlatform-error:'.$e->getMessage());
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+
+    private static function confirmPrice($params)
+    {
+        try {
+
+            $work = ServiceWork::where(['id'=>$params['work_id']])->findOrEmpty();
+            if($work->isEmpty()){
+                throw new \Exception('工单不存在');
+            }
+            if($work->external_platform_id > 0){
+                $externalPlatform = ExternalPlatform::find($work->external_platform_id);
+
+                $order_amount = RechargeOrder::where([
+                    'user_id'=>$work->user_id,
+                    'work_id'=>$params['work_id'],
+                    'pay_status'=>0,
+                    'payment_type'=>2
+                ])->value('order_amount');
+
+                $data = [
+                    'timestamp'=>time(),
+                    'scene'=>'confirm_price',
+                    'version'=>'1',
+                    'notice_data'=>json_encode([
+                        'work_sn'=>$work->work_sn,
+                        'order_amount'=>$order_amount * 100, // 单位分
+                    ],JSON_UNESCAPED_UNICODE)
+                ];
+                $data['sign'] = ExternalPlatform::getSign($externalPlatform['appkey'],$data);
+                $res = http_request($externalPlatform['send_url'],http_build_query($data));
+                Log::info('ExternalPlatform-confirmPrice:'
+                    .'url:'.$externalPlatform['send_url']
+                    .'|data:'.json_encode($data,JSON_UNESCAPED_UNICODE)
+                    .'|res:'.json_encode([$res],JSON_UNESCAPED_UNICODE)
+                );
+            }
+            return true;
+        } catch (\Exception $e) {
+            throw new \Exception($e->getMessage());
+        }
+    }
+
+
+}

+ 30 - 0
app/common/model/external/ExternalPlatform.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace app\common\model\external;
+
+use app\common\model\BaseModel;
+use think\facade\Config;
+
+class ExternalPlatform extends BaseModel
+{
+
+    protected $name = 'external_platform';
+
+    public static function getSign($sign_key,$data){
+        if (empty($data) || empty($sign_key)) {
+            return false;
+        }
+        ksort($data);
+        $result_str = "";
+        foreach ($data as $key => $val) {
+            if ($key != "debug" && $key != "sign" && $val != null && $val != "") {
+                $result_str = $result_str . $key . $val;
+            }
+        }
+        $result_str = $sign_key . $result_str;
+        return bin2hex(sha1($result_str, true));
+    }
+
+
+
+}

+ 2 - 0
app/event.php

@@ -1,5 +1,6 @@
 <?php
 // 事件定义文件
+use app\common\listener\ExternalPlatformListener;
 use app\common\listener\NoticeListener;
 use app\common\listener\PropertyCommissionListener;
 use app\common\listener\ReverseCouponToActivityListener;
@@ -21,6 +22,7 @@ return [
         'PropertyCommission' => [PropertyCommissionListener::class],
         // 优惠券逆向为活动工单+代理订单
         'ReverseCouponToActivity' => [ReverseCouponToActivityListener::class],
+        'ExternalPlatform' => [ExternalPlatformListener::class],
     ],
 
     'subscribe' => [