Files
liche/api/controllers/wxapp/licheb/Xz.php
T
2021-07-27 10:37:59 +08:00

113 lines
4.6 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';
class Xz extends Wxapp{
private $log_file = 'call.log';
private $log_dir = 'xiaozhi';
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/order/receiver_orders_model','orders_model');
$this->load->model('receiver/receiver_xz_model');
}
public function get(){
$id = $this->input_param('id');
$type = intval($this->input_param('type'));
!$type && $type = 0;
$session = $this->session;
$admin_id = $session['uid'];
if($type==1){ //订单
$row = $this->orders_model->get(['id'=>$id]);
}else{//客户
$row = $this->customers_model->get(['id'=>$id]);
}
if(!$row || !$row['mobile']){
throw new Hd_Exception('用户不存在', API_CODE_INVILD_PARAM);
}
$customer_mobile = $row['mobile'];
$redis = &load_cache('redis');
$cache_key = "XZ_LICHEB_MOBILEB_{$customer_mobile}_{$admin_id}";
$call_mobile = $redis->get($cache_key);
if (!$call_mobile) {
$this->config->load('xcall', TRUE);
$mobile_list = $this->config->item('mobile_list', 'xcall');//晓智号码库
require_once COMMPATH.'third_party/Xcall/Xcall.php';
$seq_id = 'liche_'.create_order_no();
$result = '';
//循环绑定
$xcall = new Xcall();
$maxBindingTime = 30 * 60;//绑定时间
foreach($mobile_list as $key => $val){
$mid_mobile = $val;
$result = $xcall -> SWbind($customer_mobile,$seq_id,$mid_mobile,$maxBindingTime);
if($result['code']){
break;
}
}
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);
//解绑并删除最早绑定手机号
$redis_obj = $redis->redis();
$lists = $redis_obj->keys("XZ_LICHEB_MOBILEB_*");
$del_cache = '';
$temp_time = $maxBindingTime;
foreach ($lists as $key => $val) {
$l_c_time = $redis_obj->TTL($val);
if ($l_c_time < $temp_time) {
$temp_time = $l_c_time;
$del_cache = $val;
}
}
if ($del_cache) {
$mid_mobile = $redis->get($del_cache);
//解绑
$s_res = $xcall -> Searchbind($mid_mobile);
$bined_mobile = $s_res['data']['value'];//已绑定手机号
if($bined_mobile){
$xcall -> SWunbind($bined_mobile,$mid_mobile);//解绑
$redis->delete($del_cache);
}
$result = $xcall -> SWbind($customer_mobile,$seq_id,$mid_mobile);
}
}
if ($result['code']) {
$this->data['middlenumber'] = $result['data']['virtualMobile'];
$add_data = [
'call_id' => $seq_id,
'display_number' => $result['data']['virtualMobile'],
'cf_id' => $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);
} else {
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);
}
}
$data['mobile'] = $call_mobile;
return $data;
}
}