Files
2025-09-30 16:57:56 +08:00

175 lines
7.0 KiB
PHP

<?php
defined('WXAPP_APP') or exit('No direct script access allowed');
/**
* Created by Vim.
* User: lcc
* Desc: 晓致虚拟号
* Date: 2021/07/26
* Time: 10:17
*/
require_once APPPATH . 'controllers/wxapp/Wxapp.php';
require_once COMMPATH . 'third_party/Xcall/Xcall.php';
class Xz extends Wxapp
{
function __construct($inputs, $app_key)
{
parent::__construct($inputs, $app_key);
$this->login_white = array();//登录白名单
$this->load->model('receiver/receiver_customers_model', 'customers_model');
$this->load->model('receiver/receiver_xz_model');
$this->load->library('carHome/callOutWechat');
$this->load->model('sys/sys_config_model', 'confModel');
$this->load->model('receiver/receiver_call_wechat_model', 'callWechat');
$this->load->model("biz/biz_model");
$this->log_file = 'call.log';
}
protected function get()
{
// $biz = $this->get_biz_info();
// $biz_type = $biz['type'];
// if ($biz_type != Biz_model::BIZ_TYPE_4s) {
// throw new Hd_Exception('系统调整,暂停服务', API_CODE_FAIL);
// }
//平安外呼
$id = $this->input_param('id');
$type = intval($this->input_param('type'));
!$type && $type = 0;
$session = $this->session;
$admin_id = $session['uid'];
$cf_title = 'customer';
$cus_id = $id;
$row = $this->customers_model->get(['id' => $cus_id]);
if (!$row || !$row['mobile']) {
throw new Hd_Exception('用户不存在', API_CODE_INVILD_PARAM);
}
$customer_mobile = $row['mobile'];
$user_mobile = $session['mobile'];
$json = $session['jsondata'] ? json_decode($session['jsondata'], true) : array();
if ($json && $json['hdy'] && $json['hdy']['tel']) {
$user_mobile = $json['hdy']['tel'];
}
//平安外呼
$cityId = $row['city_id'] ?: 350200;
$requestId = create_order_no($cityId, 'licheb');
//获取绑定配置信息
$configRow = $this->confModel->get(array('k' => Sys_config_model::CALL_CONFIG_KEY));
$jsonData = $configRow['v'] ? json_decode($configRow['v'], true) : [];
$callBackPhoneNo = $jsonData['callBackPhoneNo'] ?: '';
$callTime = $jsonData['callTime'] ?: 2;
$callbackTime = $jsonData['callbackTime'] ?: 60;
$callOutWechat = new CallOutWechat();
$req = $callOutWechat->bind($requestId, $row['cid'], $user_mobile, $customer_mobile, $callBackPhoneNo, $callTime, $callbackTime);
if (!$req->isSuccess()) {
throw new Hd_Exception($req->getMessage(), API_CODE_FAIL);
}
$data['mobile'] = $req->getData()['x'];
if (!$data['mobile']) {
throw new Hd_Exception('获取虚拟号码失败', API_CODE_FAIL);
}
//添加记录
$add_data = [
'reqId' => $requestId,
'cfId' => $cus_id,
'cfUid' => $admin_id,
'cfType' => Receiver_call_wechat_model::CF_TYPE_CUSTOMERS,
'cfPlatform' => Receiver_call_wechat_model::CF_PLATFORM_WX_APP,
'createTime' => time()
];
$req = $this->callWechat->add($add_data);
if (!is_numeric($req)) {
throw new Hd_Exception('保存记录失败', API_CODE_FAIL);
}
return $data;
// $redis = &load_cache('redis');
// $cache_key = "XZ_LICHEB_MOBILEA_{$user_mobile}_MOBILEB_{$customer_mobile}_{$admin_id}";
// $call_mobile = $redis->get($cache_key);
// if (!$call_mobile) {
// $this->config->load('xcall');
// $params = [
// 'app_id' => $this->config->item('app_id'),
// 'app_key' => $this->config->item('app_key'),
// ];
// $seq_id = create_order_no();
// $xcall = new Xcall($params);
// $maxBindingTime = 10;//绑定时间
// $result = $xcall->ABXbind($user_mobile, $customer_mobile, $seq_id, $maxBindingTime);
// if (!$result['code']) { //绑定失败
// debug_log("xz_failed_bind:customer_mobile【{$customer_mobile}】", $this->log_file);
// debug_log("xz_result:" . json_encode($result, JSON_UNESCAPED_UNICODE), $this->log_file);
// throw new Hd_Exception($result['msg'], API_CODE_FAIL);
// } else {
// $this->data['middlenumber'] = $result['data']['virtualMobile'];
// $add_data = [
// 'call_id' => $seq_id,
// 'display_number' => $result['data']['virtualMobile'],
// 'biz_id' => $row['biz_id'],
// 'cf_id' => $cus_id,
// 'cf_uid' => $admin_id,
// 'cf_title' => $cf_title,
// 'cf_platform' => 'api',
// 'json_data' => json_encode(['uname' => $session['uname']], JSON_UNESCAPED_UNICODE),
// 'c_time' => time()
// ];
// $this->receiver_xz_model->add($add_data);
// $call_mobile = $result['data']['virtualMobile'];
// $redis->save($cache_key, $call_mobile, $maxBindingTime * 60);
// }
// }
// $data['mobile'] = $call_mobile;
return $data;
}
//电话统计
protected function get_stat()
{
$biz_id = $this->input_param('biz_id');
$day = $this->input_param('day');
$admin_id = $this->input_param('admin_id');
if (!$biz_id) {
throw new Hd_Exception('参数错误', API_CODE_INVILD_PARAM);
}
//接通次数
$where1 = [
'cf_title' => 'customer',
'cf_platform' => 'api',
'biz_id' => $biz_id,
'status' => 1,
];
if ($day) {
$s_time = date('Y-m-d 00:00:00', strtotime($day));
$e_time = date('Y-m-t 23:59:59', strtotime($day));
$where1["c_time>="] = strtotime($s_time);
$where1["c_time<="] = strtotime($e_time);
}
$admin_id && $where1['cf_uid'] = $admin_id;
$where2 = [
'duration' => 0
];
$where = array_merge($where1, $where2);
$num = $this->receiver_xz_model->count($where);
$where2 = [
'duration>' => 0
];
$where = array_merge($where1, $where2);
$count_call = $this->receiver_xz_model->count($where); //接通次数
$sum_call = $this->receiver_xz_model->sum('duration', $where); //接通总时长
$call_time = $sum_call['duration'] / 60000;
if ($call_time) {
$avg_time = $call_time / $count_call;
}
$data = [
'call_time' => round($call_time, 2),
'avg_time' => round($avg_time, 2),
'unget_num' => $num
];
return $data;
}
}