| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 | <?php/** * MpOpenMktNew.php UTF-8 * * * @date    : 2021-03-06 17:43 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : luowei <lw@huosdk.com> * @version : HUOSDK 9.0 */namespace huoMpAd;use huo\controller\common\Base;use huolib\status\CommonStatus;use huomp\model\weixin\MpAdReportLogModel;use huomp\model\weixin\MpAdSourceModel;use huomp\model\weixin\MpConfAgentModel;use huomp\model\weixin\MpConfModel;use think\Log;class MpOpenMktNew extends Base {    /**     * 创建数据源     * https://ad.weixin.qq.com/guide/1680     *     * @param        $wx_app_id     * @param        $wx_app_secret     * @param string $name           用户行为源名称     * @param string $type           用户行为源类型    默认:WECHAT_MINI_GAME     * @param string $promoted_mp_id 小程序/小游戏对应的appid     *     * @return array     */    public function createDataSourceByAppidSecret(        $wx_app_id, $wx_app_secret, $name, $promoted_mp_id, $type = 'WECHAT_MINI_GAME'    ) {        $_param = [            'type' => $type,            'name' => $name        ];        if (!empty($promoted_mp_id)) {            $_param['wechat_app_id'] = $promoted_mp_id;        }        $_rs = (new MpAdApi())->createDataSource($wx_app_id, $wx_app_secret, $_param);        if (CommonStatus::NO_ERROR != $_rs['code']) {            if ($_rs['code'] == '900351000') {                // 已经创建过                // msg = "WECHAT_MINI_PROGRAM App wxf4603b9c18a0b5ee already registered with existed one : 1111504663"                $_msg_arr = explode(" ", $_rs['msg']);                $_user_action_set_id = array_pop($_msg_arr);            } else {                $_code = $_rs['code'];                Log::write(                    ['func' => __FUNCTION__, 'args' => func_get_args(), 'param' => $_param, 'result' => $_rs],                    LOG::ERROR, true                );                return $this->huoError($_code, $_rs['msg']);            }        } else {            $_user_action_set_id = $_rs['data']['user_action_set_id'];        }        $_data = [            'user_action_set_id' => $_user_action_set_id,        ];        $_code = CommonStatus::NO_ERROR;        return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_data);    }    /**     * 数据上报     *     * @param $mp_conf_info     * @param $agent_id     * @param $action_data     *     * @return array     */    public function dataReport($mp_conf_info, $agent_id, $action_data) {        // 根据渠道游戏,查找关联的广告账号        $_promoted_conf_id = $mp_conf_info['id'];        $_promoted_appid = $mp_conf_info['mp_id'];        $_promoted_game_id = $mp_conf_info['app_id'];        $_mp_conf_agent_model = new MpConfAgentModel();        $_mp_conf_agent_info = $_mp_conf_agent_model->getInfoByConfAgent($_promoted_conf_id, $agent_id);        if (empty($_mp_conf_agent_info)) {            $_code = CommonStatus::INNER_ERROR;            return $this->huoSuccess($_code, CommonStatus::getMsg($_code).'1');        }        // 查找关联的广告账号        $_advertiser_conf_id = $_mp_conf_agent_info['advertiser_conf_id'];        if (empty($_advertiser_conf_id)) {            $_code = CommonStatus::NO_ERROR;            return $this->huoSuccess($_code, CommonStatus::getMsg($_code));        }        $_mp_conf_model = new MpConfModel();        $_advertiser_conf_info = $_mp_conf_model->getDataById($_advertiser_conf_id);        $_advertiser_appid = $_advertiser_conf_info['mp_id'];        $_advertiser_appsecret = $_advertiser_conf_info['app_secret'];        $_mp_source_info = (new MpAdSourceModel())->getInfoByConfType(            $_promoted_conf_id, $agent_id, $_advertiser_conf_id        );        if (empty($_mp_source_info['source_id'])) {            $_code = CommonStatus::INNER_ERROR;            return $this->huoSuccess($_code, CommonStatus::getMsg($_code).'2');        }        $_user_action_set_id = $_mp_source_info['source_id'];        $_action_param_value = $action_data['action_param_value'];        if ($action_data['action_type'] == 'PURCHASE') {            $_action_param_value = (int)$_action_param_value * 100;        }        $_action_data_url = $action_data['url'] ?? 'http://www.qq.com';        $_action_time = $action_data['action_time'] ?? time();        $_action_param_claim_type = $action_data['claim_type'] ?? 0;        $_action_param_source = $action_data['source'] ?? "Web";        $_param = [            'user_action_set_id' => "{$_user_action_set_id}",            'actions'            => [[                                         'url'          => $_action_data_url,                                         'action_time'  => $_action_time,                                         'action_type'  => $action_data['action_type'],                                         'user_id'      => [                                             'wechat_app_id' => $_promoted_appid,                                             'wechat_openid' => $action_data['openid'],                                         ],                                         'action_param' => [                                             'claim_type' => $_action_param_claim_type,                                             'source'     => $_action_param_source,                                             'value'      => $_action_param_value,                                         ],                                     ]]        ];        Log::write(['广告回传start:', date('Y-m-d H:i:s'), "玩家ID:{$action_data['mem_id']}", $_advertiser_appid, $_advertiser_appsecret, $_param], 'info', true);        $_rs = (new MpAdApi())->dataReport($_advertiser_appid, $_advertiser_appsecret, $_param);        Log::write(['广告回传end:', date('Y-m-d H:i:s'),  $_rs], 'info', true);        if (CommonStatus::NO_ERROR != $_rs['code']) {            Log::write(                ['func'  => __FUNCTION__, 'args' => func_get_args(),                 'param' => [$_advertiser_appid, $_advertiser_appsecret, $_param], 'result' => $_rs],                LOG::ERROR, true            );            $_code = CommonStatus::UNKNOWN_ERROR;            return $this->huoError($_code, CommonStatus::getMsg($_code));        }        $_add_data = [            'agent_id'                => $action_data['agent_id'] ?? 0,            'mem_id'                  => $action_data['mem_id'] ?? 0,            'app_id'                  => $action_data['app_id'] ?? 0,            'mg_mem_id'               => $action_data['mg_mem_id'] ?? 0,            'server_id'               => $action_data['server_id'] ?? '',            'role_id'                 => $action_data['role_id'] ?? '',            'advertiser_app_id'       => $_advertiser_appid,            'source_id'               => $_user_action_set_id,            'url'                     => $_action_data_url,            'action_type'             => $action_data['action_type'],            'action_time'             => $_action_time,            'wechat_app_id'           => $_promoted_appid,            'wechat_openid'           => $action_data['openid'],            'action_param_value'      => $_action_param_value,            'action_param_source'     => $_action_param_source,            'action_param_claim_type' => $_action_param_claim_type,        ];        Log::write(['广告本地start:', date('Y-m-d H:i:s'),  $_add_data], 'info', true);        (new MpAdReportLogModel())->addData($_add_data);        $_code = CommonStatus::NO_ERROR;        return $this->huoSuccess($_code, CommonStatus::getMsg($_code));    }    /**     * 获取数据源报表     *     * @param $mp_conf_info     * @param $agent_id     * @param $start_date     * @param $end_date     *     * @return array     */    public function getDataSourceReport($mp_conf_info, $agent_id, $start_date, $end_date) {        $_promoted_conf_id = $mp_conf_info['id'];        $_promoted_appid = $mp_conf_info['mp_id'];        $_mp_conf_agent_model = new MpConfAgentModel();        $_mp_conf_agent_info = $_mp_conf_agent_model->getInfoByConfAgent($_promoted_conf_id, $agent_id);        if (empty($_mp_conf_agent_info)) {            $_code = CommonStatus::INNER_ERROR;            return $this->huoSuccess($_code, CommonStatus::getMsg($_code).'1');        }        // 查找关联的广告账号        $_advertiser_conf_id = $_mp_conf_agent_info['advertiser_conf_id'];        if (empty($_advertiser_conf_id)) {            $_code = CommonStatus::NO_ERROR;            return $this->huoSuccess($_code, CommonStatus::getMsg($_code));        }        $_mp_conf_model = new MpConfModel();        $_advertiser_conf_info = $_mp_conf_model->getDataById($_advertiser_conf_id);        $_advertiser_appid = $_advertiser_conf_info['mp_id'];        $_advertiser_appsecret = $_advertiser_conf_info['app_secret'];        $_mp_source_info = (new MpAdSourceModel())->getInfoByConfType(            $_promoted_conf_id, $agent_id, $_advertiser_conf_id        );        if (empty($_mp_source_info['source_id'])) {            $_code = CommonStatus::INNER_ERROR;            return $this->huoSuccess($_code, CommonStatus::getMsg($_code));        }        $_user_action_set_id = $_mp_source_info['source_id'];        $_param = [            'user_action_set_id' => $_user_action_set_id,            'time_granularity'   => 'HOURLY',            'aggregation'        => 'ACTION_TYPE',            'date_range'         => json_encode(                [                    'start_date' => $start_date,                    'end_date'   => $end_date,                ], JSON_UNESCAPED_UNICODE            ),        ];        $_rs = (new MpAdApi())->getDataSourceReport($_advertiser_appid, $_advertiser_appsecret, $_param);        \think\Log::write(['rrrrr', $_advertiser_appid, $_advertiser_appsecret, $_param], 'error', true);        if (CommonStatus::NO_ERROR != $_rs['code']) {            $_code = CommonStatus::UNKNOWN_ERROR;            Log::write(                ['func' => __FUNCTION__, 'args' => func_get_args(), 'param' => $_param, 'result' => $_rs],                LOG::ERROR, true            );            return $this->huoError($_code, CommonStatus::getMsg($_code));        }        $_code = CommonStatus::NO_ERROR;        return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_rs['data']);    }    /**     * 获取数据源消息     *     * @param $mp_conf_info     * @param $agent_id     *     * @return array     */    public function getDataSourceInfo($mp_conf_info, $agent_id) {        $_promoted_conf_id = $mp_conf_info['id'];        $_promoted_appid = $mp_conf_info['mp_id'];        $_mp_conf_agent_model = new MpConfAgentModel();        $_mp_conf_agent_info = $_mp_conf_agent_model->getInfoByConfAgent($_promoted_conf_id, $agent_id);        if (empty($_mp_conf_agent_info)) {            $_code = CommonStatus::INNER_ERROR;            return $this->huoSuccess($_code, CommonStatus::getMsg($_code).'1');        }        // 查找关联的广告账号        $_advertiser_conf_id = $_mp_conf_agent_info['advertiser_conf_id'];        if (empty($_advertiser_conf_id)) {            $_code = CommonStatus::NO_ERROR;            return $this->huoSuccess($_code, CommonStatus::getMsg($_code));        }        $_mp_conf_model = new MpConfModel();        $_advertiser_conf_info = $_mp_conf_model->getDataById($_advertiser_conf_id);        $_advertiser_appid = $_advertiser_conf_info['mp_id'];        $_advertiser_appsecret = $_advertiser_conf_info['app_secret'];        $_mp_source_info = (new MpAdSourceModel())->getInfoByConfType(            $_promoted_conf_id, $agent_id, $_advertiser_conf_id        );        if (empty($_mp_source_info['source_id'])) {            $_code = CommonStatus::INNER_ERROR;            return $this->huoSuccess($_code, CommonStatus::getMsg($_code));        }        $_user_action_set_id = $_mp_source_info['source_id'];        $_param = [            'user_action_set_id' => $_user_action_set_id,        ];        $_rs = (new MpAdApi())->getDataSourceInfo($_advertiser_appid, $_advertiser_appsecret, $_param);        \think\Log::write(['rrrrr', $_advertiser_appid, $_advertiser_appsecret, $_param], 'error', true);        if (CommonStatus::NO_ERROR != $_rs['code']) {            $_code = CommonStatus::UNKNOWN_ERROR;            Log::write(                ['func' => __FUNCTION__, 'args' => func_get_args(), 'param' => $_param, 'result' => $_rs],                LOG::ERROR, true            );            return $this->huoError($_code, CommonStatus::getMsg($_code));        }        $_code = CommonStatus::NO_ERROR;        return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_rs['data']);    }}
 |