123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- /**
- * Baidu.php UTF-8
- * 百度api
- *
- * @date : 2018/7/6 17:23
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : ouzhongfu <ozf@huosdk.com>
- * @version : HUOSDK 7.2
- */
- namespace huolib\promotion;
- use huolib\promotion\Base;
- use think\Config;
- use think\Db;
- use think\Log;
- class Baidu {
- protected $usertable = 'promotion_baidu_user';
- protected $platform_id = 1; /*1为百度*/
- public $baidu_conf
- = array(
- 'baidu_key' => '', /* key */
- );
- /**
- * 自定义错误处理
- *
- * @param string $msg
- * @param string $level
- *
- */
- private function _error($msg, $level = 'error') {
- $_info = 'promotion\Baidu 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->baidu_conf['baidu_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('百度信息流记录:验证参数空', $params), 'error');
- return false;
- }
- if (empty($type)) {
- \think\Log::write(array('百度信息流记录:type为空', $params), 'error');
- return false;
- }
- //$_check_map["agent_id"] = $params['agent_id'];
- $_check_map["app_id"] = $params['app_id'];
- if (!empty($params['from']) && Base::FROM_ANDROID == $params['from']) {
- $_check_map["imei_md5"] = md5($params['device_id']);
- } else {
- $_check_map["idfa"] = $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('百度信息流记录:不是通过百度信息流过来数据', $_check_map), 'error');
- return false;
- }
- if (2 == $_check_data['status'] && !$_is_while) {
- \think\Log::write(array('百度信息流记录:设备已存在', $_check_map), 'error');
- return false;
- }
- $_base_url = $_check_data['callback_url'];
- $_a_value = !empty($params['amount']) ? $params['amount'] : 0;
- $_base_url = str_replace("{{ATYPE}}", $type, $_base_url);
- $_check_url = str_replace("{{AVALUE}}", $_a_value, $_base_url);
- $_md5_string = $_check_url.$this->baidu_conf['baidu_key'];
- $_signature = md5($_md5_string);
- $_vare_url = $_check_url."&sign=".$_signature;
- $_result = file_get_contents($_vare_url);
- $_result = json_decode($_result, true);
- if (0 == $_result["error_code"]) {
- $_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 Base();
- $_re = $_base_class->entDownLog($params['agent_id'], $params['app_id']);
- if ($_re) {
- return $_re;
- }
- } else {
- return $_rs;
- }
- } else {
- \think\Log::write(array('百度信息流记录:回传信息error_code不为0', $_result), 'error');
- return false;
- }
- return true;
- }
- /**检查是否为调试设备
- *
- * @param $from 来源 3为安卓,4为ios
- * @param $device_id
- * @param $idfa
- * @param bool|int $is_md5 是否已经md5
- *
- * @return bool
- */
- public function isWhile($from, $device_id, $idfa, $is_md5 = false) {
- if (!empty($from) && Base::FROM_ANDROID == $from) {
- if ($is_md5) {
- $_device_id = $device_id;
- } else {
- $_device_id = md5($device_id);
- }
- } else {
- $_device_id = $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()) {
- \think\Log::write(array('百度信息流记录:kaishi', $params), 'error');
- /*1 查看参数是否配置*/
- $_rs = $this->check($params['app_id'], $params['agent_id']);
- if (false === $_rs) {
- \think\Log::write(array('百度信息流记录:未配置参数', $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 '客户端操作系统的类型,1–iOS;2–Android',";
- $_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 .= " PRIMARY KEY (`id`)";
- $_sql .= " ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='百度点击用户';";
- try {
- $_rs = db()->execute($_sql);
- if (false === $_rs) {
- Log::write("baidu_log create table ".$_table_name." failed1", 'error');
- return false;
- }
- return true;
- } catch (Exception $_e) {
- Log::write("baidu_log create table ".$_table_name." failed2", 'error');
- return false;
- }
- }
- }
|