123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- /**
- * Toutiao.php UTF-8
- * 头条api
- *
- * @date : 2018/6/21 14:29
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : ouzhongfu <ozf@huosdk.com>
- * @version : HUOSDK 7.2
- */
- namespace huolib\promotion;
- use think\Db;
- use think\Log;
- class Toutiao {
- protected $platform_id = 4; /*4为今日头条*/
- public $toutiao_conf
- = array(
- 'toutiao_key' => '', /* 今日头条key */
- );
- /**
- * 自定义错误处理
- *
- * @param string $msg
- * @param string $level
- *
- */
- private function _error($msg, $level = 'error') {
- $_info = 'toutiao\Api Error:'.$msg;
- Log::record($_info, $level);
- }
- public function __construct() {
- }
- /**验证是否配置
- *
- * @param array $params
- *
- * @return bool
- */
- public function check($params = array()) {
- $_app_id = isset($params['app_id']) ? $params['app_id'] : 0;
- $_agent_id = isset($params['agent_id']) ? $params['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;
- }
- $_toutiao_key = Db::name('promotion_detail')->where($_map)->value('toutiao_key');
- if (empty($_toutiao_key)) {
- return false;
- }
- $this->toutiao_conf['toutiao_key'] = $_toutiao_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 int $type_id
- *
- * @return bool|int|string
- */
- public function varify($params = array(), $type_id = 0) {
- if (empty($params)) {
- \think\Log::write(array('头条记录:验证参数空', $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($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('promotion_toutiao_user')->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'];
- $_check_url = $_base_url."&event_type=".$type_id;
- $_str = hash_hmac("sha1", $_check_url, $this->toutiao_conf['toutiao_key']);
- $_signature = $this->base64urlEncode($_str);
- $_vare_url = $_check_url."&signature=".$_signature;
- $_result = file_get_contents($_vare_url);
- $_result = json_decode($_result, true);
- if (0 == $_result["ret"]) {
- $_set_data['status'] = 2;
- $_set_data['update_time'] = time();
- $_rs = DB::name('promotion_toutiao_user')->where($_check_map)->update($_set_data);
- if ($_rs) {
- $_base_class = new \huolib\promotion\Base();
- return $_base_class->entDownLog($params['agent_id'], $params['app_id']);
- }
- } else {
- \think\Log::write(array('头条记录:回传信息ret不为0', $_result), 'error');
- return false;
- }
- }
- /**检查是否为调试设备
- *
- * @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 = $device_id;
- } else {
- $_device_id = md5($device_id);
- }
- } else {
- $_device_id = $idfa;
- }
- $_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;
- }
- }
- public function base64urlEncode($data) {
- return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
- }
- /**
- * 激活
- *
- * @param array $params 请求参数
- *
- * @return bool
- */
- public function install($params = array()) {
- /*1 查看参数是否配置*/
- $_rs = $this->check($params);
- if (false === $_rs) {
- return false;
- }
- /*2 是否本地有记录及回调验证*/
- $_type = 0; /*0为激活,1为注册,2为充值*/
- $_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;
- }
- }
|