123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- /**
- * MpAdOut.php UTF-8
- * 小程序广告转化行为数据
- *
- * @date : 2018/11/16 10:46
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : chenbingling <cbl@huosdk.com>
- * @version : HuoMp 1.0
- */
- namespace huoMpAd;
- use huo\controller\common\Base;
- use huolib\constant\MpConfConst;
- use huolib\status\CommonStatus;
- use huomp\model\weixin\MpConfModel;
- use think\Log;
- class MpAdOut extends Base {
- protected $conf_id = 0;
- protected $wx_app_id = '';
- protected $wx_app_secret = '';
- public function __construct() {
- if (file_exists(GLOBAL_CONF_PATH."extra/mpad/weixin.php")) {
- $_config = include GLOBAL_CONF_PATH."extra/mpad/weixin.php";
- } else {
- $_config = array();
- }
- $this->wx_app_id = get_val($_config, 'APP_KEY', '');
- $this->wx_app_secret = get_val($_config, 'APP_SECRET', '');
- }
- /**
- * 创建数据源
- *
- * @param $app_id
- *
- * @return array
- */
- public function createDataSource($app_id) {
- $_mc_model = new MpConfModel();
- $_conf_data = $_mc_model->getDataByAppId($app_id, MpConfConst::MP_CONF_TYPE_6);
- if (empty($_conf_data)) {
- $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&step=1&app_id=".$app_id."&type="
- .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code),
- LOG::ERROR, true
- );
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- $this->conf_id = $_conf_data['id'];
- $this->wx_app_id = $_conf_data['mp_id'];
- $this->wx_app_secret = $_conf_data['app_secret'];
- if (!empty($_conf_data['ext_info']['user_action_set_id'])) {
- $_code = CommonStatus::NO_ERROR;
- $_data = ['user_action_set_id' => $_conf_data['ext_info']['user_action_set_id']];
- return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_data);
- }
- if (empty($this->wx_app_id) || empty($this->wx_app_secret)) {
- $_code = CommonStatus::INVALID_PARAMS;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&step=2&app_id=".$app_id."&type="
- .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&wx_app_id='
- .$this->wx_app_id.'&app_secret='.$this->wx_app_secret,
- LOG::ERROR, true
- );
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- $_name = empty($_conf_data['wx_name']) ? $_conf_data['mp_id'] : $_conf_data['wx_name'];
- $_param = ['type' => 'WEB', 'name' => $_name];
- $_rs = (new MpAdApi())->createDataSource($this->wx_app_id, $this->wx_app_secret, $_param);
- if (CommonStatus::NO_ERROR != $_rs['code']) {
- $_code = CommonStatus::UNKNOWN_ERROR;
- $_param = json_encode([$_conf_data['mp_id'], $_conf_data['app_secret'], $_param]);
- $_return = json_encode($_rs);
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&step=3&app_id=".$app_id."&type="
- .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'¶m='
- .$_param.'&return='.$_return,
- LOG::ERROR, true
- );
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- $_code = CommonStatus::NO_ERROR;
- $_rdata = $_rs['data'];
- if (!empty($_rdata['user_action_set_id'])) {
- $_conf_data['ext_info']['user_action_set_id'] = $_rdata['user_action_set_id'];
- $_mc_model->updateData($_conf_data, $_conf_data['id']);
- }
- return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_rdata);
- }
- /**
- * 数据上报
- *
- * @param $app_id
- * @param $action_data
- *
- * @return array
- */
- public function dataReport($app_id, $action_data) {
- $_mc_model = new MpConfModel();
- $_conf_data = $_mc_model->getDataByAppId($app_id, MpConfConst::MP_CONF_TYPE_6);
- if (empty($_conf_data)) {
- $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&step=1&app_id=".$app_id."&type="
- .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code),
- LOG::ERROR, true
- );
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- $this->conf_id = $_conf_data['id'];
- $this->wx_app_id = $_conf_data['mp_id'];
- $this->wx_app_secret = $_conf_data['app_secret'];
- if (empty($this->wx_app_id) || empty($this->wx_app_secret)) {
- $_code = CommonStatus::INVALID_PARAMS;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&step=2&app_id=".$app_id."&type="
- .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&wx_app_id='
- .$this->wx_app_id.'&app_secret='.$this->wx_app_secret,
- LOG::ERROR, true
- );
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- $_param = [
- 'actions' => [[
- 'user_action_set_id' => $action_data['user_action_set_id'],
- 'url' => $action_data['url'],
- 'action_time' => time(),
- 'action_type' => 'RESERVATION',
- 'trace' => ['click_id' => $action_data['click_id']]
- ]]
- ];
- $_rs = (new MpAdApi())->dataReport($this->wx_app_id, $this->wx_app_secret, $_param);
- if (CommonStatus::NO_ERROR != $_rs['code']) {
- $_code = CommonStatus::UNKNOWN_ERROR;
- $_param = json_encode([$_conf_data['mp_id'], $_conf_data['app_secret'], $_param]);
- $_return = json_encode($_rs);
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&step=3&app_id=".$app_id."&type="
- .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'¶m='
- .$_param.'&return='.$_return,
- LOG::ERROR, true
- );
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- $_code = CommonStatus::NO_ERROR;
- return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
- }
- /**
- * @return mixed|int
- */
- public function getConfId() {
- return $this->conf_id;
- }
- /**
- * @return mixed|string
- */
- public function getWxAppId() {
- return $this->wx_app_id;
- }
- }
|