Files
spacestation/api/controllers/plan/Yxcall.php
T
2024-09-04 11:48:34 +08:00

138 lines
6.2 KiB
PHP

<?php
require_once COMMPATH . 'third_party/Ycall/Ycall.php';
class Yxcall extends HD_Controller
{
private $log_file;
public function __construct()
{
parent::__construct();
$this->log_file = 'call.log';
$this->load->model('receiver/receiver_yx_model');
$this->load->model('receiver/receiver_customers_model', 'customers_model');
$this->load->library('receiver/customers_entity');
}
public function index()
{
debug_log("start", $this->log_file);
$str = file_get_contents('php://input');
debug_log('string:' . $str, $this->log_file);
$data = json_decode($str, true);
if ($data && $data['bindId']) {
$row = $this->receiver_yx_model->get(array('bind_id' => $data['bindId']));
if ($row) {
$jsondata = json_decode($row['json_data'], true);
$data['uname'] = $jsondata['uname'];
$up_data['caller_state'] = $data['callerState'];
$up_data['status'] = 1;
$data['displayNumber'] && $up_data['mid'] = $data['displayNumber'];
$data['calleeNumber'] && $up_data['called_num'] = $data['calleeNumber'];
$data['callerNumber'] && $up_data['caller_num'] = $data['callerNumber'];
if ($data['calleeDuration'] > 0) {
$up_data['duration'] = $data['calleeDuration'];
$up_data['answer_time'] = $data['callerAnsweredTime'];
}
$data['startCallTime'] && $up_data['start_time'] = $data['startCallTime'];
$data['recUrl'] && $up_data['rec_url'] = $data['recUrl'];
$data['endTime'] && $up_data['end_time'] = $data['endTime'];
$up_data['json_data'] = json_encode($data, JSON_UNESCAPED_UNICODE);
$this->receiver_yx_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'],
'cf_platform' => 'admin',
'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);
$ycall = new Ycall();
//解绑
$ycall->AXBUnbind($data['virtualMobile'], $data['calleeNumber']);
// $result = $ycall->AXBUnbind($data['displayNumber'], $data['calleeNumber'], $data['callerNumber']);
} elseif ($row['cf_id'] && $row['cf_platform'] == 'api') { //理车宝
$ycall = new Ycall();
//解绑
$result = $ycall->AXBUnbind($data['displayNumber'], $data['calleeNumber'], $data['callerNumber']);
//删除redis
$cache_key = "XZ_LICHEB_MOBILEA_{$data['calleeNumber']}_MOBILEB_{$data['callerNumber']}_{$row['cf_uid']}";
$redis = &load_cache();
$redis->delete($cache_key);
$cust = $this->customers_model->get(['id' => $row['cf_id']]);
$visit = $row['cf_title'] == 'customer' ? 1 : 0;
if ($data['callerNumber'] == $row['user_mobile']) $this->customers_entity->add_log_visit($cust['id'], $row['cf_uid'], $data['uname'], $row['id'], 2, $visit);
}
} else {
debug_log("error: 未找到拨打记录", $this->log_file);
}
}
debug_log("end", $this->log_file);
die(json_encode(array('resultCode' => 200)));
}
//下载链接到七牛云
public function down_video()
{
$redis = &load_cache('redis');
$key = "RECEIVER_LAST_ID";
$last_id = $redis->get($key);
$where = array(
"rec_url <> ''" => null,
'c_time<' => time() - 50 * 60
);
if ($last_id) {
$where['id>'] = $last_id;
} else {
$where["rec_url like '%winnerlook-voice.oss-cn-hangzhou.aliyuncs.com%'"] = null;
}
$count = $this->receiver_yx_model->count($where);
$failed_num = $success_num = $do_num = 0;
if ($count) {
$rows = $this->receiver_yx_model->select($where, 'id asc', 1, 20, 'id,rec_url');
foreach ($rows as $key => $val) {
$file = $this->upload($val['rec_url']);
if ($file) {
$this->receiver_yx_model->update(array('rec_url' => $file), array('id' => $val['id']));
$success_num++;
} else {
$failed_num++;
}
$do_num++;
$last_id = $val['id'];
}
$redis->save($key, $last_id);
}
echo "do_num:{$do_num},success_num:{$success_num},failed_num:{$failed_num}";
}
private function upload($url, $i = '')
{
$this->load->helper('image');
$arr = explode('?', $url);
$this->load->library('qiniu', array('type' => 'video'));
$filename = $this->qiniu->getFileName($arr[0] . $i);
$filename = 'yx/' . $filename;
$info = $this->qiniu->getInfo($filename);
if ($info) {
return $filename;
} else {
$file = $this->qiniu->fetch($url, $filename);
return $file ? $file['file'] : '';
}
}
}