* @version : HUOSDK 7.2 */ namespace huolib\promotion; use think\Config; use think\Db; use think\Log; class Uc { protected $usertable = 'promotion_uc_user'; protected $platform_id = 7; /*7为uc*/ public $uc_conf = array( 'uc_key' => '', /* key */ ); /** * 自定义错误处理 * * @param string $msg * @param string $level * */ private function _error($msg, $level = 'error') { $_info = 'promotion\Uc Error:'.$msg; Log::record($_info, $level); } public function __construct() { } /**验证是否配置 * * @param int $app_id * @param int $agent_id * * @return bool * @internal param array $params * */ public function check($app_id = 0, $agent_id = 0) { if (empty($app_id) || empty($agent_id)) { return false; } $_map['app_id'] = $app_id; $_map['agent_id'] = $agent_id; /*投放平台验证*/ $_check_platform = $this->checkPlatform($app_id, $agent_id); if (!$_check_platform) { return false; } $_key = Db::name('promotion_detail')->where($_map)->value('toutiao_key'); if (empty($_key)) { return false; } /*数据库表检测*/ $_rs = $this->checkUserTable(); if (!$_rs) { return false; } $this->uc_conf['uc_key'] = $_key; return true; } /**投放平台验证 * * @param $app_id * @param $agent_id * * @return bool */ public function checkPlatform($app_id, $agent_id) { $_map['app_id'] = $app_id; $_map['agent_id'] = $agent_id; $_plan_id = Db::name('promotion_detail')->where($_map)->value('promotion_plan_id'); $_pp_map['id'] = $_plan_id; $_platform_id = Db::name('promotion_plan')->where($_pp_map)->value('platform_id'); if ($this->platform_id == $_platform_id) { return true; } else { return false; } } /** * 验证 * * @param array $params * @param string $type * * @return bool|int|string */ public function varify($params = array(), $type = '') { if (empty($params)) { \think\Log::write(array('uc记录:验证参数空', $params), 'error'); return false; } if (empty($type)) { \think\Log::write(array('uc记录:type为空', $params), 'error'); return false; } //$_check_map["agent_id"] = $params['agent_id']; $_check_map["app_id"] = $params['app_id']; if (!empty($params['from']) && \huolib\promotion\Base::FROM_ANDROID == $params['from']) { $_check_map["imei_md5"] = strtoupper(md5($params['device_id'])); } else { $_check_map["idfa"] = strtoupper($params['idfa']); } /*是否推广白名单*/ $_is_while = $this->isWhile($params['from'], $params['device_id'], $params['idfa']); if ($_is_while) { $_check_map["is_test"] = 1; } else { $_check_map["is_test"] = 2; } $_check_data = DB::name($this->usertable)->where($_check_map)->order('id desc')->find(); if (empty($_check_data)) { \think\Log::write(array('uc记录:不是通过uc过来数据', $_check_map), 'error'); return false; } if (2 == $_check_data['status'] && !$_is_while) { \think\Log::write(array('uc记录:设备已存在', $_check_map), 'error'); return false; } $_base_url = $_check_data['callback_url']; $_result = $this->httpSend($_base_url); if (200 == $_result) { $_set_data['status'] = 2; $_set_data['update_time'] = time(); $_rs = DB::name($this->usertable)->where($_check_map)->update($_set_data); /*下载统计记录表c_promotion_day*/ if ($_rs) { $_base_class = new \huolib\promotion\Base(); return $_base_class->entDownLog($params['agent_id'], $params['app_id']); } else { return $_rs; } } else { \think\Log::write(array('uc记录:回传信息error_code不为0', $_result), 'error'); return false; } } function httpSend($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, ''); curl_exec($ch); $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); return $return_code; } /**检查是否为调试设备 * * @param $from 来源 3为安卓,4为ios * @param $device_id * @param $idfa * @param int $is_md5 是否已经md5 * * @return bool */ public function isWhile($from, $device_id, $idfa, $is_md5 = false) { if (!empty($from) && \huolib\promotion\Base::FROM_ANDROID == $from) { if ($is_md5) { $_device_id = strtoupper($device_id); } else { $_device_id = strtoupper(md5($device_id)); } } else { $_device_id = strtoupper($idfa); } if (empty($_device_id)) { return false; } $_map['platform_id'] = $this->platform_id; $_map['is_delete'] = 2; $_map['status'] = 2; $_lists = DB::name("promotion_whitelist")->where($_map)->column('device_id'); if (!empty($_lists) && in_array($_device_id, $_lists)) { return true; } else { return false; } } /** * 激活 * * @param array $params 请求参数 * * @return bool */ public function install($params = array()) { /*1 查看参数是否配置*/ $_rs = $this->check($params['app_id'], $params['agent_id']); if (false === $_rs) { \think\Log::write(array('uc记录:未配置参数', $params), 'error'); return false; } /*2 是否本地有记录及回调验证*/ $_type = 'activate'; /*wake_up:唤醒;download:下载;activate:激活;register:注册;new_customers:新客;orders:成单*/ $_check = $this->varify($params, $_type); return $_check; } /**注册 * * @param array $params * * @return bool */ public function register($params = array()) { /*1 查看参数是否配置*/ $_rs = $this->check($params); if (false === $_rs) { return false; } /*2 是否本地有记录及回调验证*/ $_type = 1; /*0为激活,1为注册,2为充值*/ $_check = $this->varify($params, $_type); return $_check; } /**充值 * * @param array $params * * @return bool */ public function payment($params = array()) { /*1 查看参数是否配置*/ $_rs = $this->check($params); if (false === $_rs) { return false; } /*2 是否本地有记录及回调验证*/ $_type = 2; /*0为激活,1为注册,2为充值*/ $_check = $this->varify($params, $_type); return $_check; } /** * @return bool */ private function checkUserTable() { $_table_name = Config::get('database.prefix').$this->usertable; $_sql = ''; $_sql .= "CREATE TABLE IF NOT EXISTS `".$_table_name."` ("; $_sql .= " `id` int(11) NOT NULL AUTO_INCREMENT,"; $_sql .= " `app_id` INT(11) NOT NULL DEFAULT 0 COMMENT '游戏id',"; $_sql .= " `agent_id` INT(11) NOT NULL DEFAULT 0 COMMENT '子渠道id',"; $_sql .= " `imei_md5` VARCHAR(64) NOT NULL COMMENT '用户终端的IMEI码md5后的值',"; $_sql .= " `idfa` VARCHAR(64) NOT NULL COMMENT 'iOS 手机广告唯一标识',"; $_sql .= " `os` VARCHAR(10) NOT NULL DEFAULT '' COMMENT '客户端操作系统的类型,0表示Ios ,1表示Android,2其他',"; $_sql .= " `callback_url` VARCHAR(1000) NOT NULL DEFAULT '' COMMENT '回调地址',"; $_sql .= " `create_time` INT(11) NOT NULL DEFAULT 0 COMMENT '创建时间戳',"; $_sql .= " `update_time` INT(11) NOT NULL DEFAULT 0 COMMENT '更新时间戳',"; $_sql .= " `status` TINYINT(2) NOT NULL DEFAULT 1 COMMENT '状态,1为待激活,2为已激活',"; $_sql .= " `is_test` TINYINT(2) NOT NULL DEFAULT 2 COMMENT '是否测试数据,1是,2不是',"; $_sql .= " PRIMARY KEY (`id`)"; $_sql .= " ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='uc点击用户';"; try { $_rs = db()->execute($_sql); if (false === $_rs) { Log::write("uc_log create table ".$_table_name." failed1", 'error'); return false; } return true; } catch (Exception $_e) { Log::write("uc_log create table ".$_table_name." failed2", 'error'); return false; } } }