Files
liche/api/controllers/plan/Xzcall.php
T
2022-05-14 12:43:01 +08:00

242 lines
10 KiB
PHP

<?php
/**
* Created by Vim
* User: lcc
* Desc: 晓致拨号回调
* Date: 2021/4/14
* Time: 19:15
*/
require_once COMMPATH . 'third_party/Xcall/Xcall.php';
class Xzcall extends HD_Controller
{
private $log_file;
private $admin_config = [
'app_id' => 1206,
'app_key' => 'WX6HDVZX3AYSZDR1739332ZM'
];
public function __construct()
{
parent::__construct();
$this->log_file = 'call_xz.log';
$this->load->model('receiver/receiver_xz_model');
$this->load->model('receiver/receiver_customers_model', 'customers_model');
$this->load->model('receiver/order/receiver_orders_model', 'orders_model');
}
public function index()
{
debug_log("start function:" . __FUNCTION__, $this->log_file);
$data = $this->input->post();
debug_log('data:' . json_encode($data, JSON_UNESCAPED_UNICODE), $this->log_file);
if ($data && $data['seqId']) {
$row = $this->receiver_xz_model->get(array('call_id' => $data['seqId']));
if ($row) {
$jsondata = json_decode($row['json_data'], true);
$data['uname'] = $jsondata['uname'];
$data['brand_id'] = $jsondata['brand_id'];
$up_data['caller_state'] = $data['status'];
$up_data['status'] = 1;
$data['mid'] && $up_data['mid'] = $data['mid'];
$data['to'] && $up_data['called_num'] = $data['to'];
$data['from'] && $up_data['caller_num'] = $data['from'];
if ($data['callAnswer']) {
$up_data['duration'] = $data['callEnd'] - $data['callAnswer'];
$up_data['answer_time'] = $this->getMsecToMescdate($data['callAnswer']);
}
$data['callStart'] && $up_data['start_time'] = $this->getMsecToMescdate($data['callStart']);
$data['callEnd'] && $up_data['end_time'] = $this->getMsecToMescdate($data['callEnd']);
$up_data['json_data'] = json_encode($data, JSON_UNESCAPED_UNICODE);
$this->receiver_xz_model->update($up_data, ['id' => $row['id']]);
if ($row['cf_id'] && $row['cf_platform'] == 'admin') { //后台
$admin_id = $row['cf_uid'];
$this->load->model('sys/sys_admin_model');
$admin = $this->sys_admin_model->get(array('id' => $admin_id));
$addData = array(
'uid' => $admin_id,
'uname' => $admin['username'] ? $admin['username'] : '',
'type' => 2,//类型 0 普通 1短信 2电话
'log' => $row['id'],
'c_time' => time()
);
if ($row['cf_title'] == 'clues') {//线索拨打电话回调
$this->load->model('receiver/receiver_clue_oplogs_model', 'mdOplogs');
$addData['clue_id'] = $row['cf_id'];
} else if ($row['cf_title'] == 'customer') {//客户拨打电话回调
$this->load->model('receiver/receiver_customer_oplogs_model', 'mdOplogs');
$addData['customer_id'] = $row['cf_id'];
}
$this->mdOplogs->add($addData);
$xcall = new Xcall($this->admin_config);
//解绑
$xcall->SWunbind($data['to'], $data['virtualMobile']);
} elseif ($row['cf_id'] && $row['cf_platform'] == 'api') { //狸车宝
$this->config->load('xcall');
$params = [
'app_id' => $this->config->item('app_id'),
'app_key' => $this->config->item('app_key'),
];
$xcall = new Xcall($params);
//解绑
$result = $xcall->AXBUnbind($data['to'], $data['from'], $data['virtualMobile']);
//删除redis
$cache_key = "XZ_LICHEB_MOBILEA_{$data['from']}_MOBILEB_{$data['to']}_{$row['cf_uid']}";
$redis = &load_cache('redis');
$redis->delete($cache_key);
if ($row['cf_title'] == 'order') { //订单
$order = $this->orders_model->get(['id' => $row['cf_id']]);
$cust = $this->customers_model->get(['id' => $order['rid']]);
} else { //客户
$cust = $this->customers_model->get(['id' => $row['cf_id']]);
}
$this->load->library('receiver/customers_entity');
$visit = $row['cf_title'] == 'customer' ? 1 : 0;
$this->customers_entity->add_log_visit($cust['id'], $row['cf_uid'], $data['uname'], $row['id'], 2,$visit);
}
} else {
if($data['virtualMobile']=='13391247942'){ //推送test环境
$url = "https://api.lc.haodian.cn/plan/xzcall";
$this->load->library('mycurl');
$res = $this->mycurl->httpPost($url,$data);
debug_log('结束,推送test环境:'.json_encode($res,JSON_UNESCAPED_UNICODE), $this->log_file);
}
debug_log("error: 未找到拨打记录", $this->log_file);
}
}
debug_log("end " . __FUNCTION__, $this->log_file);
echo '{"result":"0","error":""}';
}
//录音推送地址
public function video()
{
$data = $this->input->post();
debug_log("start function:" . __FUNCTION__, $this->log_file);
debug_log('data:' . json_encode($data, JSON_UNESCAPED_UNICODE), $this->log_file);
$row = $this->receiver_xz_model->get(array('mid' => $data['mid']));
if ($row) {
$jsondata = json_decode($row['json_data'], true);
if ($data['getFileUrl']) {
if ($data['id'] == 1206) {
$params = $this->admin_config;
} else {
$this->config->load('xcall');
$params = [
'app_id' => $this->config->item('app_id'),
'app_key' => $this->config->item('app_key'),
];
}
$xcall = new Xcall($params);
$result = $xcall->getFile($data['getFileUrl'], $data['mid'], $data['tag']);
if ($result['code']) { //上传七牛
$this->load->library('qiniu');
$qiniu = new Qiniu(['type' => 'video']);
$filename = $qiniu->getFileName($data['mid'], 'mp3', 'xz_video_');
$q_res = $qiniu->save($filename, $result['data']);
if (!$q_res) {
debug_log('error:文件上传七牛失败', $this->log_file);
} else {
$up_data['rec_url'] = $q_res['url'];
}
} else {
debug_log('error:' . json_encode($result, JSON_UNESCAPED_UNICODE), $this->log_file);
}
}
$jsondata['file_info'] = $data;
$up_data['json_data'] = json_encode($jsondata, JSON_UNESCAPED_UNICODE);
$this->receiver_xz_model->update($up_data, ['id' => $row['id']]);
}else{
//推送test环境
$url = "https://api.lc.haodian.cn/plan/xzcall/video";
$this->load->library('mycurl');
$res = $this->mycurl->httpPost($url,$data);
debug_log('结束,推送test环境:'.json_encode($res,JSON_UNESCAPED_UNICODE), $this->log_file);
}
debug_log("end " . __FUNCTION__, $this->log_file);
echo '{"result":"0","error":""}';
}
/**
* 毫秒转日期
*/
private function getMsecToMescdate($msectime)
{
$msectime = $msectime * 0.001;
if (strstr($msectime, '.')) {
sprintf("%01.3f", $msectime);
list($usec, $sec) = explode(".", $msectime);
$sec = str_pad($sec, 3, "0", STR_PAD_RIGHT);
} else {
$usec = $msectime;
$sec = "000";
}
$date = date("Y-m-d H:i:s.x", $usec);
return $mescdate = str_replace('x', $sec, $date);
}
//执行企业微信机器人
public function dorobot()
{
$o_id = $this->input->get('id');
$test = $this->input->get('test');
$type = $this->input->get('type');
$pay_time = $this->input->get('pay_time');
if ($o_id) {
$params = [];
$test && $params['test'] = 1;
$this->load->library('qyrobot',$params);
if($type){
if(!$pay_time){
die('pay_time:不能为空');
}
$robot_res = $this->qyrobot->deposit_notify_v2($o_id,1,$pay_time);
}else{
$robot_res = $this->qyrobot->deposit_notify_v2($o_id);
}
var_dump($robot_res);
}
}
//更新订单服务id
public function up_srv_ids(){
$page = $this->input->get('page');
$size = $this->input->get('size');
!$page && $page = 1;
!$size && $size = 20;
$where = [
"srv_ids like '%3%'" => null
];
$rows = $this->orders_model->select($where,'id asc',$page,$size,'id,srv_ids,jsondata');
if($rows){
foreach($rows as $item){
$res = false;
$srv_ids_arr = explode(',',$item['srv_ids']);
if(in_array(3,$srv_ids_arr)){
$jsondata = json_decode($item['jsondata'],true);
$jsondata['old_srv_ids'] = $item['srv_ids'];
$jsondata['if_fine'] = 1;
foreach($srv_ids_arr as $key=>$val){
if($val==3){
unset($srv_ids_arr[$key]);
break;
}
}
$updata = [
'jsondata' => json_encode($jsondata,JSON_UNESCAPED_UNICODE),
'srv_ids' => implode(',',$srv_ids_arr)
];
$res = $this->orders_model->update($updata,['id'=>$item['id']]);
}
$log = "{$item['id']}更新结果:{$res}</br>";
echo $log;
}
}else{
echo "finish";
}
}
}