MpAdOutNew.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * MpAdOut.php UTF-8
  4. * 小程序广告转化行为数据
  5. *
  6. * @date : 2018/11/16 10:46
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huoMpAd;
  13. use huo\controller\common\Base;
  14. use huolib\constant\MpConfConst;
  15. use huolib\status\CommonStatus;
  16. use huomp\model\weixin\MpConfModel;
  17. use think\Log;
  18. class MpAdOutNew extends Base {
  19. protected $conf_id = 0;
  20. protected $wx_app_id = '';
  21. protected $wx_app_secret = '';
  22. public function __construct() {
  23. if (file_exists(GLOBAL_CONF_PATH."extra/mpad/weixin.php")) {
  24. $_config = include GLOBAL_CONF_PATH."extra/mpad/weixin.php";
  25. } else {
  26. $_config = array();
  27. }
  28. $this->wx_app_id = get_val($_config, 'APP_KEY', '');
  29. $this->wx_app_secret = get_val($_config, 'APP_SECRET', '');
  30. }
  31. /**
  32. * 创建数据源
  33. *
  34. * @param $app_id
  35. * @param string $type
  36. * @param string $_promoted_mp_id
  37. *
  38. * @return array
  39. */
  40. public function createDataSource($app_id, $type = 'WECHAT_MINI_PROGRAM', $_promoted_mp_id = '') {
  41. $_mc_model = new MpConfModel();
  42. $_conf_data = $_mc_model->getDataByAppId($app_id, MpConfConst::MP_CONF_TYPE_6);
  43. if (empty($_conf_data)) {
  44. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  45. Log::write(
  46. "func=".__FUNCTION__."&class=".__CLASS__."&step=1&app_id=".$app_id."&type="
  47. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code),
  48. LOG::ERROR, true
  49. );
  50. return $this->huoError($_code, CommonStatus::getMsg($_code));
  51. }
  52. $this->conf_id = $_conf_data['id'];
  53. $this->wx_app_id = $_conf_data['mp_id'];
  54. $this->wx_app_secret = $_conf_data['app_secret'];
  55. if (!empty($_conf_data['ext_info']['user_action_set_id'])) {
  56. $_code = CommonStatus::NO_ERROR;
  57. $_data = ['user_action_set_id' => $_conf_data['ext_info']['user_action_set_id']];
  58. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_data);
  59. }
  60. if (empty($this->wx_app_id) || empty($this->wx_app_secret)) {
  61. $_code = CommonStatus::INVALID_PARAMS;
  62. Log::write(
  63. "func=".__FUNCTION__."&class=".__CLASS__."&step=2&app_id=".$app_id."&type="
  64. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&wx_app_id='
  65. .$this->wx_app_id.'&app_secret='.$this->wx_app_secret,
  66. LOG::ERROR, true
  67. );
  68. return $this->huoError($_code, CommonStatus::getMsg($_code));
  69. }
  70. $_name = empty($_conf_data['wx_name']) ? $_conf_data['mp_id'] : $_conf_data['wx_name'];
  71. $_param = ['type' => $type, 'name' => $_name];
  72. if (!empty($_promoted_mp_id)) {
  73. $_param['wechat_app_id'] = $_promoted_mp_id;
  74. }
  75. return $this->createDataSourceByAppidSecretParam($this->wx_app_id, $this->wx_app_secret, $_param);
  76. }
  77. public function createDataSourceByAppidSecretParam($wx_app_id, $wx_app_secret,$_param) {
  78. $_rs = (new MpAdApi())->createDataSource($wx_app_id, $wx_app_secret, $_param);
  79. if (CommonStatus::NO_ERROR != $_rs['code']) {
  80. $_code = CommonStatus::UNKNOWN_ERROR;
  81. $_param = json_encode([$wx_app_id, $wx_app_id, $_param]);
  82. $_return = json_encode($_rs);
  83. Log::write(
  84. "func=".__FUNCTION__."&class=".__CLASS__."&step=3"."&type="
  85. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&param='
  86. .$_param.'&return='.$_return,
  87. LOG::ERROR, true
  88. );
  89. return $this->huoError($_code, CommonStatus::getMsg($_code));
  90. }
  91. $_code = CommonStatus::NO_ERROR;
  92. $_rdata = $_rs['data'];
  93. if (!empty($_rdata['user_action_set_id'])) {
  94. $_mc_model = new MpConfModel();
  95. $_conf_data = $_mc_model->getDataByMpId($wx_app_id);
  96. if (!empty($_conf_data)) {
  97. $_conf_data['ext_info']['user_action_set_id'] = $_rdata['user_action_set_id'];
  98. $_mc_model->updateData($_conf_data, $_conf_data['id']);
  99. }
  100. }
  101. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_rdata);
  102. }
  103. /**
  104. * 数据上报
  105. *
  106. * @param $app_id
  107. * @param $action_data
  108. *
  109. * @return array
  110. */
  111. public function dataReport($app_id, $action_data) {
  112. $_mc_model = new MpConfModel();
  113. $_conf_data = $_mc_model->getDataByAppId($app_id, MpConfConst::MP_CONF_TYPE_6);
  114. if (empty($_conf_data)) {
  115. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  116. Log::write(
  117. "func=".__FUNCTION__."&class=".__CLASS__."&step=1&app_id=".$app_id."&type="
  118. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code),
  119. LOG::ERROR, true
  120. );
  121. return $this->huoError($_code, CommonStatus::getMsg($_code));
  122. }
  123. $this->conf_id = $_conf_data['id'];
  124. $this->wx_app_id = $_conf_data['mp_id'];
  125. $this->wx_app_secret = $_conf_data['app_secret'];
  126. if (empty($this->wx_app_id) || empty($this->wx_app_secret)) {
  127. $_code = CommonStatus::INVALID_PARAMS;
  128. Log::write(
  129. "func=".__FUNCTION__."&class=".__CLASS__."&step=2&app_id=".$app_id."&type="
  130. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&wx_app_id='
  131. .$this->wx_app_id.'&app_secret='.$this->wx_app_secret,
  132. LOG::ERROR, true
  133. );
  134. return $this->huoError($_code, CommonStatus::getMsg($_code));
  135. }
  136. $_param = [
  137. 'actions' => [[
  138. 'user_action_set_id' => $action_data['user_action_set_id'],
  139. 'url' => $action_data['url'],
  140. 'action_time' => time(),
  141. 'action_type' => 'RESERVATION',
  142. 'trace' => ['click_id' => $action_data['click_id']]
  143. ]]
  144. ];
  145. $_rs = (new MpAdApi())->dataReport($this->wx_app_id, $this->wx_app_secret, $_param);
  146. if (CommonStatus::NO_ERROR != $_rs['code']) {
  147. $_code = CommonStatus::UNKNOWN_ERROR;
  148. $_param = json_encode([$_conf_data['mp_id'], $_conf_data['app_secret'], $_param]);
  149. $_return = json_encode($_rs);
  150. Log::write(
  151. "func=".__FUNCTION__."&class=".__CLASS__."&step=3&app_id=".$app_id."&type="
  152. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&param='
  153. .$_param.'&return='.$_return,
  154. LOG::ERROR, true
  155. );
  156. return $this->huoError($_code, CommonStatus::getMsg($_code));
  157. }
  158. $_code = CommonStatus::NO_ERROR;
  159. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  160. }
  161. /**
  162. * @return mixed|int
  163. */
  164. public function getConfId() {
  165. return $this->conf_id;
  166. }
  167. /**
  168. * @return mixed|string
  169. */
  170. public function getWxAppId() {
  171. return $this->wx_app_id;
  172. }
  173. }