MpAdOut.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 MpAdOut 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. *
  36. * @return array
  37. */
  38. public function createDataSource($app_id) {
  39. $_mc_model = new MpConfModel();
  40. $_conf_data = $_mc_model->getDataByAppId($app_id, MpConfConst::MP_CONF_TYPE_6);
  41. if (empty($_conf_data)) {
  42. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  43. Log::write(
  44. "func=".__FUNCTION__."&class=".__CLASS__."&step=1&app_id=".$app_id."&type="
  45. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code),
  46. LOG::ERROR, true
  47. );
  48. return $this->huoError($_code, CommonStatus::getMsg($_code));
  49. }
  50. $this->conf_id = $_conf_data['id'];
  51. $this->wx_app_id = $_conf_data['mp_id'];
  52. $this->wx_app_secret = $_conf_data['app_secret'];
  53. if (!empty($_conf_data['ext_info']['user_action_set_id'])) {
  54. $_code = CommonStatus::NO_ERROR;
  55. $_data = ['user_action_set_id' => $_conf_data['ext_info']['user_action_set_id']];
  56. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_data);
  57. }
  58. if (empty($this->wx_app_id) || empty($this->wx_app_secret)) {
  59. $_code = CommonStatus::INVALID_PARAMS;
  60. Log::write(
  61. "func=".__FUNCTION__."&class=".__CLASS__."&step=2&app_id=".$app_id."&type="
  62. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&wx_app_id='
  63. .$this->wx_app_id.'&app_secret='.$this->wx_app_secret,
  64. LOG::ERROR, true
  65. );
  66. return $this->huoError($_code, CommonStatus::getMsg($_code));
  67. }
  68. $_name = empty($_conf_data['wx_name']) ? $_conf_data['mp_id'] : $_conf_data['wx_name'];
  69. $_param = ['type' => 'WEB', 'name' => $_name];
  70. $_rs = (new MpAdApi())->createDataSource($this->wx_app_id, $this->wx_app_secret, $_param);
  71. if (CommonStatus::NO_ERROR != $_rs['code']) {
  72. $_code = CommonStatus::UNKNOWN_ERROR;
  73. $_param = json_encode([$_conf_data['mp_id'], $_conf_data['app_secret'], $_param]);
  74. $_return = json_encode($_rs);
  75. Log::write(
  76. "func=".__FUNCTION__."&class=".__CLASS__."&step=3&app_id=".$app_id."&type="
  77. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&param='
  78. .$_param.'&return='.$_return,
  79. LOG::ERROR, true
  80. );
  81. return $this->huoError($_code, CommonStatus::getMsg($_code));
  82. }
  83. $_code = CommonStatus::NO_ERROR;
  84. $_rdata = $_rs['data'];
  85. if (!empty($_rdata['user_action_set_id'])) {
  86. $_conf_data['ext_info']['user_action_set_id'] = $_rdata['user_action_set_id'];
  87. $_mc_model->updateData($_conf_data, $_conf_data['id']);
  88. }
  89. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_rdata);
  90. }
  91. /**
  92. * 数据上报
  93. *
  94. * @param $app_id
  95. * @param $action_data
  96. *
  97. * @return array
  98. */
  99. public function dataReport($app_id, $action_data) {
  100. $_mc_model = new MpConfModel();
  101. $_conf_data = $_mc_model->getDataByAppId($app_id, MpConfConst::MP_CONF_TYPE_6);
  102. if (empty($_conf_data)) {
  103. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  104. Log::write(
  105. "func=".__FUNCTION__."&class=".__CLASS__."&step=1&app_id=".$app_id."&type="
  106. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code),
  107. LOG::ERROR, true
  108. );
  109. return $this->huoError($_code, CommonStatus::getMsg($_code));
  110. }
  111. $this->conf_id = $_conf_data['id'];
  112. $this->wx_app_id = $_conf_data['mp_id'];
  113. $this->wx_app_secret = $_conf_data['app_secret'];
  114. if (empty($this->wx_app_id) || empty($this->wx_app_secret)) {
  115. $_code = CommonStatus::INVALID_PARAMS;
  116. Log::write(
  117. "func=".__FUNCTION__."&class=".__CLASS__."&step=2&app_id=".$app_id."&type="
  118. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&wx_app_id='
  119. .$this->wx_app_id.'&app_secret='.$this->wx_app_secret,
  120. LOG::ERROR, true
  121. );
  122. return $this->huoError($_code, CommonStatus::getMsg($_code));
  123. }
  124. $_param = [
  125. 'actions' => [[
  126. 'user_action_set_id' => $action_data['user_action_set_id'],
  127. 'url' => $action_data['url'],
  128. 'action_time' => time(),
  129. 'action_type' => 'RESERVATION',
  130. 'trace' => ['click_id' => $action_data['click_id']]
  131. ]]
  132. ];
  133. $_rs = (new MpAdApi())->dataReport($this->wx_app_id, $this->wx_app_secret, $_param);
  134. if (CommonStatus::NO_ERROR != $_rs['code']) {
  135. $_code = CommonStatus::UNKNOWN_ERROR;
  136. $_param = json_encode([$_conf_data['mp_id'], $_conf_data['app_secret'], $_param]);
  137. $_return = json_encode($_rs);
  138. Log::write(
  139. "func=".__FUNCTION__."&class=".__CLASS__."&step=3&app_id=".$app_id."&type="
  140. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&param='
  141. .$_param.'&return='.$_return,
  142. LOG::ERROR, true
  143. );
  144. return $this->huoError($_code, CommonStatus::getMsg($_code));
  145. }
  146. $_code = CommonStatus::NO_ERROR;
  147. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  148. }
  149. /**
  150. * @return mixed|int
  151. */
  152. public function getConfId() {
  153. return $this->conf_id;
  154. }
  155. /**
  156. * @return mixed|string
  157. */
  158. public function getWxAppId() {
  159. return $this->wx_app_id;
  160. }
  161. }