Parcourir la source

Merge branch 'wecall_0321'

dongxiaoqin il y a 1 an
Parent
commit
75314a9e37

+ 2 - 0
app/adminapi/logic/master_worker/MasterWorkerLogic.php

@@ -83,6 +83,7 @@ class MasterWorkerLogic extends BaseLogic
                 'settlement_type' => $params['settlement_type']??2,
                 'labels' => (isset($params['labels']) && $params['labels'])?implode(',',$params['labels']):'',
                 'remark' => $params['remark']??'',
+                'is_wecall' => $params['is_wecall']??0,
             ]);
             
             //添加工程师汇总评分数据
@@ -166,6 +167,7 @@ class MasterWorkerLogic extends BaseLogic
                 'settlement_type' => $params['settlement_type']??2,
                 'labels' => (isset($params['labels']) && $params['labels'])?implode(',',$params['labels']):'',
                 'remark' => $params['remark']??'',
+                'is_wecall' => $params['is_wecall']??0,
             ];
             //'tenant_id' => $params['tenant_id']??0,
             MasterWorker::where('id', $params['id'])->update($update);

+ 173 - 0
app/adminapi/service/WeCallService.php

@@ -0,0 +1,173 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+
+namespace app\adminapi\service;
+
+use app\common\model\dict\DictData;
+
+
+
+/**
+ * 网易云商智能外呼service
+ * Class WeCallService
+ * @package app\adminapi\service
+ */
+class WeCallService
+{
+    protected $appKey;
+    protected $appSecret;
+    protected $taskId;
+    protected $status = 0;
+
+    public function __construct()
+    {
+        $config = DictData::where('type_value', 'wecall')->column('value','name');
+        if ($config) {
+            $this->appKey = isset($config['appKey']) ? $config['appKey'] : '';
+            $this->appSecret = isset($config['appSecret']) ? $config['appSecret'] : '';
+            $this->taskId = isset($config['taskId']) ? $config['taskId'] : '';
+            $this->status = isset($config['status']) ? $config['status'] : 0;
+        } 
+    }
+
+    /**
+     * 生成sign签名
+     * @param $requestQuery GET请求时的url参数数组
+     * @param $requestBody POST请求时的body参数Json字符串
+     * @param $timestamp 时间戳(毫秒级)
+     * @return string
+     */
+    public function getSign($requestQuery,$requestBody,$timestamp) {        
+        // 拼接值
+        $queryStr = "";
+        ksort($requestQuery);
+        foreach($requestQuery as $value) {
+            $queryStr .= $value;
+        }
+        
+        // 得到签名值
+        $needSign = $timestamp. $queryStr. $requestBody;
+        $sign = hash_hmac('sha256', $needSign, $this->appSecret);
+        return $sign;
+    }
+    
+    /*
+     * 向外呼任务导入客户
+     */
+    public function importUser($customerList){
+        if ($this->status == 0) {
+            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 startTask(){
+        if ($this->status == 0) {
+            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: '.$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 ($this->status == 0) {
+            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: '.$this->appKey,
+            'X-YS-TIME: '.$timestamp,
+            'X-YS-SIGNATURE: '.$sign,
+        ];
+        $response = http_request('https://b.163.com/open/api/wecall/v1/task/stop', $requestBody, $header);
+        return $response;
+    }
+
+    /**
+     * 数据回调接口
+     */
+    public function notify($params) {
+        if ($params && isset($params['dataType'])) {
+            if ($params['dataType'] == 'ROBOT_TASK_STATUS_CHANGE') {
+                $this->taskStatusChange($params['data']);
+            }    
+        }
+    }
+
+    private function taskStatusChange($data)
+    {
+        $taskId = $data['taskId'];
+        $status = $data['status'];
+        if ($taskId == $this->taskId && $status == 'COMPLETE') {
+            // TODO 任务状态改变
+            $this->stopTask();
+        }
+    }
+
+    /**
+     * 获取时间戳(毫秒级)
+     */
+    public function get_millisecond_timestamp() {
+        $microtime = microtime(true);
+        $timestamp = floor($microtime * 1000);
+        return $timestamp;
+    }
+}

+ 31 - 0
app/api/controller/notify/WeCallController.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace app\api\controller\notify;
+
+use app\adminapi\service\WeCallService;
+use app\api\controller\BaseApiController;
+
+/**
+ * 网易云商外呼任务回调接口
+ * Class WeCallController
+ * @package app\api\controller\notify
+ */
+class WeCallController extends BaseApiController
+{
+
+    public array $notNeedLogin = ['notify'];
+
+    public function notify()
+    {
+        $params = $this->request->param();
+        if (!empty($params['dataType']) && $params['dataType'] == 'ROBOT_TASK_STATUS_CHANGE') {
+            // 任务状态变更回调
+            $weCallService = new WeCallService();
+            $weCallService->notify($params);
+        }
+        
+        return $this->success('success');
+       
+    }
+
+}

+ 63 - 11
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')
@@ -34,7 +45,14 @@ class AutomaticDispatch extends Command
 
     protected function execute(Input $input, Output $output)
     {
-        $this->autoDispatch();
+        //自动派单
+        //$this->autoDispatch();
+
+        //执行外呼任务
+        $h = date('H');
+        if ($h >= 8 && $h <= 22) {
+            $this->startTask();
+        }
     }
 
     /*
@@ -55,15 +73,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 ;
         }
@@ -95,6 +113,20 @@ class AutomaticDispatch extends Command
         }
     }
 
+    /**
+     * 执行外呼任务
+     */
+    protected function startTask() {
+        if ($this->customerList) {
+            $weCallService = new WeCallService();
+            $res = $weCallService->importUser($this->customerList);
+            if (isset($res['code']) && $res['code'] == 200) {
+                $res = $weCallService->startTask();
+            }
+        }
+        $this->customerList = [];
+    }
+
     /**
      * 派单给平台工程师
      */
@@ -134,6 +166,8 @@ class AutomaticDispatch extends Command
                 'a.lat',
                 'a.worker_number',
                 'a.real_name',
+                'a.mobile',
+                'a.is_wecall',
                 'b.comprehensive_score',
                 'b.weight_score',
                 $real_distance
@@ -195,7 +229,16 @@ class AutomaticDispatch extends Command
             if ($count == 0) {
                 $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) {
+                if ($res === true && $worker['is_wecall'] == 1) {
+                    $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 +324,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;
             }
         }

+ 1 - 1
config/custom.php

@@ -4,5 +4,5 @@
 // +----------------------------------------------------------------------
 
 return [
-    'cdn_url' => 'https://cdnweixiu.kyjlkj.com/',
+    'cdn_url' => 'https://cdnweixiu.kyjlkj.com/',   //静态资源域名
 ];