Procházet zdrojové kódy

提交外呼任务

dongxiaoqin před 1 rokem
rodič
revize
9e1c92a416

+ 109 - 45
app/adminapi/service/WeCallService.php

@@ -24,6 +24,19 @@ namespace app\adminapi\service;
  */
 class WeCallService
 {
+    protected $appKey;
+    protected $appSecret;
+    protected $taskId;
+
+    public function __construct()
+    {
+        $config = config('custom.wecall');
+        if ($config) {
+            $this->appKey = $config['appKey'];
+            $this->appSecret = $config['appSecret'];
+            $this->taskId = $config['taskId'];
+        } 
+    }
 
     /**
      * 生成sign签名
@@ -32,10 +45,7 @@ class WeCallService
      * @param $timestamp 时间戳(毫秒级)
      * @return string
      */
-    public function getSign($requestQuery,$requestBody,$timestamp) {
-        $appKey = "6143f9037f8546f6b83be0498dc19258";
-        $appSecret = "cd1a1ec8089342a7a913b726013974c7";
-        
+    public function getSign($requestQuery,$requestBody,$timestamp) {        
         // 拼接值
         $queryStr = "";
         ksort($requestQuery);
@@ -45,62 +55,116 @@ class WeCallService
         
         // 得到签名值
         $needSign = $timestamp. $queryStr. $requestBody;
-        $sign = hash_hmac('sha256', $needSign, $appSecret);
+        $sign = hash_hmac('sha256', $needSign, $this->appSecret);
         return $sign;
     }
-    /**
-     * 获取时间戳(毫秒级)
+    
+    /*
+     * 向外呼任务导入客户
      */
-    public function get_millisecond_timestamp() {
-        $microtime = microtime(true);
-        $timestamp = floor($microtime * 1000);
-        return $timestamp;
+    public function importUser($customerList){
+        if (empty($this->appKey) || empty($this->appSecret) || empty($this->taskId)) {
+            return false;
+        }
+        $requestBody = [
+            'taskId' => $this->taskId,
+            'removeRepeat' => false,
+            'encryptedPhone' => false,
+            'customerList' => $customerList
+        ];
+        $requestBody = json_encode($requestBody);
+        $timestamp = $this->get_millisecond_timestamp();
+        $sign = $this->getSign([],$requestBody,$timestamp);
+
+        //请求导入客户信息接口
+        $header = [
+            'Content-Type: application/json',
+            'X-YS-APIKEY: '.$this->appKey,
+            'X-YS-TIME: '.$timestamp,
+            'X-YS-SIGNATURE: '.$sign,
+        ];
+        $response = http_request('https://b.163.com/open/api/wecall/v1/task/importCustomer', $requestBody, $header);
+        return $response;
     }
 
-    public function importUser(){
-        $requestBody = '{
-            "taskId": 2462591,
-            "removeRepeat": false,
-            "encryptedPhone" : false,	
-            "customerList": [
-                {
-                    "phone": "13661306041",
-                    "properties": {
-                        "订单号": "40191",
-                        "详细地址":"武汉市洪山区",
-                        "服务类型":"洗衣机维修",
-                        "客户手机号":"136613060441"
-                    }
-                },
-                {
-                    "phone": "13661306044",
-                    "properties": {
-                        "订单号": "40192",
-                        "详细地址":"武汉市洪山区",
-                        "服务类型":"洗衣机清洗",
-                        "客户手机号":"136613060444"
-                    }
-                }
-            ]
-        }';
-        $requestBody = json_decode($requestBody,true);
+    /*
+     * 开启外呼任务(仅手动任务可调用)
+     */
+    public function startTask(){
+        if (empty($this->appKey) || empty($this->appSecret) || empty($this->taskId)) {
+            return false;
+        }
+        $requestBody = [
+            'taskId' => $this->taskId
+        ];
         $requestBody = json_encode($requestBody);
-        $requestQuery = [];
         $timestamp = $this->get_millisecond_timestamp();
-        $sign = $this->getSign($requestQuery,$requestBody,$timestamp);
+        $sign = $this->getSign([],$requestBody,$timestamp);
+
+        //请求导入客户信息接口
+        $header = [
+            'Content-Type: application/json',
+            'X-YS-APIKEY: '.$this->appKey,
+            'X-YS-TIME: '.$timestamp,
+            'X-YS-SIGNATURE: '.$sign,
+        ];
+        $response = http_request('https://b.163.com/open/api/wecall/v1/task/start', $requestBody, $header);
+        return $response;
+    }
 
+    /*
+     * 停止外呼任务(仅手动任务可调用)
+     */
+    public function stopTask(){
+        if (empty($this->appKey) || empty($this->appSecret) || empty($this->taskId)) {
+            return false;
+        }
+        $requestBody = [
+            'taskId' => $this->taskId
+        ];
+        $requestBody = json_encode($requestBody);
+        $timestamp = $this->get_millisecond_timestamp();
+        $sign = $this->getSign([],$requestBody,$timestamp);
 
         //请求导入客户信息接口
         $header = [
             'Content-Type: application/json',
-            'X-YS-APIKEY: 6143f9037f8546f6b83be0498dc19258',
+            'X-YS-APIKEY: '.$this->appKey,
             'X-YS-TIME: '.$timestamp,
             'X-YS-SIGNATURE: '.$sign,
         ];
-        $response = http_request('https://b.163.com/open/api/wecall/v1/task/importCustomer', $requestBody, $header);
+        $response = http_request('https://b.163.com/open/api/wecall/v1/task/stop', $requestBody, $header);
+        return $response;
+    }
 
-        echo $sign."\r\n";
-        echo $timestamp."\r\n";
-        print_r($response);
+    /**
+     * 数据回调接口
+     */
+    public function notify($body) {
+        $body = json_decode($body, true);
+        if ($body && isset($body['dataType'])) {
+            if ($body['dataType'] == 'ROBOT_TASK_STATUS_CHANGE') {
+                $this->taskStatusChange($body['data']);
+            }    
+        }
+    }
+
+    private function taskStatusChange($data)
+    {
+        $taskId = $data['taskId'];
+        $status = $data['status'];
+        $updateTime = $data['updateTime'];
+        if ($taskId == $this->taskId && $status == 'COMPLETE') {
+            // TODO 任务状态改变
+        }
+    }
+
+    /**
+     * 获取时间戳(毫秒级)
+     */
+    public function get_millisecond_timestamp() {
+        $microtime = microtime(true);
+        $timestamp = floor($microtime * 1000);
+        return $timestamp;
     }
 }

+ 56 - 9
app/common/command/AutomaticDispatch.php

@@ -6,6 +6,7 @@ use think\facade\Log;
 use think\console\Input;
 use think\console\Output;
 use think\console\Command;
+use app\adminapi\service\WeCallService;
 use app\common\model\works\ServiceWork;
 use app\common\model\goods_time\GoodsTime;
 use app\common\model\master_worker\MasterWorker;
@@ -26,6 +27,16 @@ class AutomaticDispatch extends Command
     //默认服务时长 180 分钟
     protected $defaultServiceTime = 180; 
 
+    //外呼客户列表
+    protected $customerList = [];
+
+    //服务类目
+    protected $categoryType = [
+        1 => '安装', 
+        2 => '维修', 
+        3 => '清洗', 
+    ];
+
     protected function configure()
     {
         $this->setName('automatic_dispatch')
@@ -55,15 +66,15 @@ class AutomaticDispatch extends Command
             ->where('service_status',0)
             ->where('refund_approval',0)
             ->where('work_pay_status',1)
-                    ->where(function ($query) use ($fiveMinutesAgo) {
-                        $query->where('exec_time', 0)->whereOr('exec_time', '<', $fiveMinutesAgo);
-                    })
-                    ->where('appointment_time','between', [$startTime, $endTime])
-                    ->field('id,category_type,goods_category_id,service_area_id,lon,lat,province,city,title,appointment_time,address,mobile')
-                    ->order('create_time','asc')
-                    ->limit($size)
-                    ->select()
-                    ->toArray();
+            ->where(function ($query) use ($fiveMinutesAgo) {
+                $query->where('exec_time', 0)->whereOr('exec_time', '<', $fiveMinutesAgo);
+            })
+            ->where('appointment_time','between', [$startTime, $endTime])
+            ->field('id,category_type,goods_category_id,service_area_id,lon,lat,province,city,title,appointment_time,address,mobile,work_sn')
+            ->order('create_time','asc')
+            ->limit($size)
+            ->select()
+            ->toArray();
         if (!$list) {
             return ;
         }
@@ -93,6 +104,23 @@ class AutomaticDispatch extends Command
                 ]);
             }
         }
+
+        //执行外呼任务
+        $this->startTask();
+    }
+
+    /**
+     * 执行外呼任务
+     */
+    protected function startTask() {
+        if ($this->customerList) {
+            $weCallService = new WeCallService();
+            $res = $weCallService->importUser($this->customerList);
+            if (isset($res['code']) && $res['code'] == 200) {
+                $res = $weCallService->startTask();
+                print_r($res);
+            }
+        }
     }
 
     /**
@@ -134,6 +162,7 @@ class AutomaticDispatch extends Command
                 'a.lat',
                 'a.worker_number',
                 'a.real_name',
+                'a.mobile',
                 'b.comprehensive_score',
                 'b.weight_score',
                 $real_distance
@@ -196,6 +225,15 @@ class AutomaticDispatch extends Command
                 $operaLog = '系统自动派单于'.date('Y-m-d H:i:s',time()).'分配了工程师'.'编号['.$worker['worker_number'].']'.$worker['real_name'];
                 $res = $this->allocateWorker($item,$worker['id'],$worker['tenant_id'],$operaLog,$estimated_finish_time);
                 if ($res === true) {
+                    $this->customerList[] = [
+                        'phone' => $worker['mobile'],
+                        'properties' => [
+                            '订单号' => substr($item['work_sn'], -4),
+                            '详细地址'=>$item['address'],
+                            '服务类型'=> isset($this->categoryType[$item['category_type']]) ?? '',
+                            '客户手机号'=>$item['mobile']
+                        ]
+                    ];
                     return true;
                 }
             }
@@ -281,6 +319,15 @@ class AutomaticDispatch extends Command
             if ($res === true) {
                 $updateData = $isAm == 1 ? ['am_order' => Db::raw('am_order + 1')] : ['pm_order' => Db::raw('pm_order + 1')];
                 MasterWorkerTeam::where('id',$worker['id'])->update($updateData);
+                $this->customerList[] = [
+                    'phone' => MasterWorker::where('id',$worker['master_worker_id'])->value('mobile'),
+                    'properties' => [
+                        '订单号' => substr($item['work_sn'], -4),
+                        '详细地址'=>$item['address'],
+                        '服务类型'=> isset($this->categoryType[$item['category_type']]) ?? '',
+                        '客户手机号'=>$item['mobile']
+                    ]
+                ];
                 return true;
             }
         }

+ 11 - 1
config/custom.php

@@ -4,5 +4,15 @@
 // +----------------------------------------------------------------------
 
 return [
-    'cdn_url' => 'https://cdnweixiu.kyjlkj.com/',
+    'cdn_url' => 'https://cdnweixiu.kyjlkj.com/',   //静态资源域名
+    //网易云商智能外呼配置
+    'wecall' => [
+        // 'appKey' => '6143f9037f8546f6b83be0498dc19258',
+        // 'appSecret' => 'cd1a1ec8089342a7a913b726013974c7',
+        // 'taskId' => 2462591,
+        'appKey' => '9db89ac3e679404c87b82697fcb321cc',
+        'appSecret' => 'f1511c3a49064070b4de1afd8abcd1d4',
+        'taskId' => 2472602,
+        
+    ],
 ];