<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 小米平台的广告
 * https://api.e.mi.com/doc.html#/1.0.0-mdtag9b26f-omd/document-f0283649125f62138db43c6f5fc25686
 */
class Advert extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index(){
        $list = Db::name('yimi_advert')->order('id desc')->page($this->page,100)->select();
        foreach($list as $key => &$val){
            $val['createdate'] = date('Y-m-d H:i:s',$val['createtime']);
            //dump($val);
        }
        dump($list);
    }

    //异或加密，解密
    public function xor_enc($str,$key)
    {
        $crytxt = '';
        $keylen = strlen($key);
        for($i=0;$i<strlen($str);$i++)
        {
            $k = $i%$keylen;
            $crytxt .= $str[$i] ^ $key[$k];
        }
        return $crytxt;
    }

    //文档参数测试加密
    /*public function test(){
        $str = 'imei=91b9185dba1772851dd02b276a6c969e&oaid=5fb96f268628810c&conv_time=1504687208890&client_ip=127.0.0.1&ua=Dalvik%2F2.1.0+%28Linux%3B+U%3B+Android+11%3B+M2012K11AC+Build%2FRKQ1.200826.002%29&sign=c5f2f40d2355c3a8cc65a3b2d0dc9b72';
        $key = 'AchcmaxVksLhPGjA';
        $crytxt = $this->xor_enc($str,$key);
        $a = urlencode(base64_encode($crytxt));

        $b = 'KA4NClBYSTRSQnRdNCULcHZUWltYUBwyW0EuWmdxC3ciWl5aCEcXNwIXcV02JVN3J1FeW1tTQG5aQy9OMygENx4XAQ4IXEljW0d6UGd1Wnl5WlhFDg0RMwUHEwEgeltzdk1YTV1PSXAeEnEsMSscKCpGWiVfT0l4W1hpWmgLAy80G01QL0otc1gxZyk%2BIxguKAdDUlxESxRAPn5YYXUhcHAiK0gvFBE6D1Z%2BLgIMO3BvUVhTVVNOeFtDfk1ifkwyKAQGXg5UHmQNR3wMYnRfdCJQCVsOAk5jCkAuWjR3DiJ4AV9R';

        dump($a === $b);
    }*/

    //APP 转化数据统计方案（即：APP 上报对接方案）： 广告主上报激活数据，亿米平台搭建服务系统关联点击&下载数据和广告主提供的所有激活数据，将激活数据归因到对应广告。
    public function indextest(){
        //http://trail.e.mi.com/global/log?appId={appid}&info={data}&conv_type={convType}&customer_id={customerId}
        $api_url      = 'http://trail.e.mi.com/global/log?';
        $api_url_test = 'http://trail.e.mi.com/global/test?';

        //应用id 1453045
        //秘钥A(encrypt_key):ZxdIaVHvFqSQYzWD
        //秘钥B(sign_key):uaeWeunykLRnkyLw
        $sign_key = 'uaeWeunykLRnkyLw'; //真的
        $encrypt_key = 'ZxdIaVHvFqSQYzWD';//真的

        $appid = '1453045';
        $conv_type = 'APP_REGISTER';
        $customer_id = '292232';

        //推荐模式
        /*$imei = md5('imei');
        $data = [
            'imei' => '91b9185dba1772851dd02b276a6c969e',
            'oaid' => '5fb96f268628810c',
            'conv_time' => '1504687208890',
            'client_ip' => '127.0.0.1',
            'ua' => 'Dalvik/2.1.0 (Linux; U; Android 11; M2012K11AC Build/RKQ1.200826.002)',
        ];*/

        //采用模式
        $ua = input('ua','','trim');
        if(empty($ua)){
            $this->error();
        }
        $data = [
            'conv_time' => time().substr(microtime(),2,3),
            'client_ip' => request()->ip(),
            'ua' => $ua,
        ];

        $data_query = http_build_query($data);
        //dump($data_query);

        $property = $sign_key.'&'.urlencode($data_query);
        //dump($property);

        $signature = md5($property);
        //dump($signature);

        $base_data = $data_query .'&sign='.urlencode($signature);
        //echo $base_data;

        $info = urlencode(base64_encode($this->xor_enc($base_data, $encrypt_key)));
        //dump($info);

        $request_url = $api_url.'appId='.$appid.'&info='.$info.'&customer_id='.$customer_id.'&conv_type='.$conv_type;
        //echo $request_url;

        $result = curl_get($request_url);
        //dump($result);
        return $result;
    }


}
