* @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; } }