MpOpenMkt.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * MpOpenMkt.php UTF-8
  4. *
  5. *
  6. * @date : 2021-02-24 14:31
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK 9.0
  11. */
  12. namespace huoMpAd;
  13. use huolib\constant\MpConfConst;
  14. use huolib\status\CommonStatus;
  15. use huomp\model\weixin\MpConfModel;
  16. use think\Log;
  17. class MpOpenMkt extends MpAdOut {
  18. /**
  19. * 数据上报
  20. *
  21. * @param $app_id
  22. * @param $action_data
  23. *
  24. * @return array
  25. */
  26. public function dataReport($app_id, $action_data) {
  27. $_mc_model = new MpConfModel();
  28. $_conf_data = $_mc_model->getDataByAppId($app_id, MpConfConst::MP_CONF_TYPE_6);
  29. if (empty($_conf_data)) {
  30. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  31. Log::write(
  32. "func=".__FUNCTION__."&class=".__CLASS__."&step=1&app_id=".$app_id."&type="
  33. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code),
  34. LOG::ERROR, true
  35. );
  36. return $this->huoError($_code, CommonStatus::getMsg($_code));
  37. }
  38. $this->conf_id = $_conf_data['id'];
  39. $this->wx_app_id = $_conf_data['mp_id'];
  40. $this->wx_app_secret = $_conf_data['app_secret'];
  41. if (empty($this->wx_app_id) || empty($this->wx_app_secret)) {
  42. $_code = CommonStatus::INVALID_PARAMS;
  43. Log::write(
  44. "func=".__FUNCTION__."&class=".__CLASS__."&step=2&app_id=".$app_id."&type="
  45. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&wx_app_id='
  46. .$this->wx_app_id.'&app_secret='.$this->wx_app_secret,
  47. LOG::ERROR, true
  48. );
  49. return $this->huoError($_code, CommonStatus::getMsg($_code));
  50. }
  51. $_param = [
  52. 'actions' => [[
  53. 'user_action_set_id' => $action_data['user_action_set_id'],
  54. 'action_time' => time(),
  55. 'action_type' => $action_data['action_type'],
  56. 'user_id' => [
  57. 'wechat_app_id' => $this->wx_app_id,
  58. 'wechat_openid' => $action_data['openid'],
  59. ],
  60. 'action_param' => [
  61. 'claim_type' => 0,
  62. 'source' => 0,
  63. 'value' => $action_data['action_param_value'],
  64. ],
  65. ]]
  66. ];
  67. $_rs = (new MpAdApi())->dataReport($this->wx_app_id, $this->wx_app_secret, $_param);
  68. if (CommonStatus::NO_ERROR != $_rs['code']) {
  69. $_code = CommonStatus::UNKNOWN_ERROR;
  70. $_param = json_encode([$_conf_data['mp_id'], $_conf_data['app_secret'], $_param]);
  71. $_return = json_encode($_rs);
  72. Log::write(
  73. "func=".__FUNCTION__."&class=".__CLASS__."&step=3&app_id=".$app_id."&type="
  74. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&param='
  75. .$_param.'&return='.$_return,
  76. LOG::ERROR, true
  77. );
  78. return $this->huoError($_code, CommonStatus::getMsg($_code));
  79. }
  80. $_code = CommonStatus::NO_ERROR;
  81. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  82. }
  83. public function getDataSourceReport($app_id, $start_date, $end_date) {
  84. $_mc_model = new MpConfModel();
  85. $_conf_data = $_mc_model->getDataByAppId($app_id, MpConfConst::MP_CONF_TYPE_6);
  86. if (empty($_conf_data)) {
  87. $_code = CommonStatus::DATA_NOT_FOUND_EXCEPTION;
  88. Log::write(
  89. "func=".__FUNCTION__."&class=".__CLASS__."&step=1&app_id=".$app_id."&type="
  90. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code),
  91. LOG::ERROR, true
  92. );
  93. return $this->huoError($_code, CommonStatus::getMsg($_code));
  94. }
  95. $this->wx_app_id = $_conf_data['mp_id'];
  96. $this->wx_app_secret = $_conf_data['app_secret'];
  97. if (empty($this->wx_app_id) || empty($this->wx_app_secret)) {
  98. $_code = CommonStatus::INVALID_PARAMS;
  99. Log::write(
  100. "func=".__FUNCTION__."&class=".__CLASS__."&step=2&app_id=".$app_id."&type="
  101. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&wx_app_id='
  102. .$this->wx_app_id.'&app_secret='.$this->wx_app_secret,
  103. LOG::ERROR, true
  104. );
  105. return $this->huoError($_code, CommonStatus::getMsg($_code));
  106. }
  107. $_rs = $this->createDataSource($app_id);
  108. if (CommonStatus::NO_ERROR != $_rs['code']) {
  109. $_msg = '创建数据源错误:'.$_rs['msg'];
  110. $_code = CommonStatus::INNER_ERROR;
  111. return $this->huoError($_code, $_msg);
  112. }
  113. $_user_action_set_id = $_rs['data']['user_action_set_id'];
  114. $_param = [
  115. 'user_action_set_id' => $_user_action_set_id,
  116. // 'time_granularity' => 'DAILY',
  117. // 'aggregation' => 'ACTION_TYPE',
  118. 'date_range' => json_encode([
  119. 'start_date' => $start_date,
  120. 'end_date' => $end_date,
  121. ], JSON_UNESCAPED_UNICODE),
  122. ];
  123. $_rs = (new MpAdApi())->getDataSourceReport($this->wx_app_id, $this->wx_app_secret, $_param);
  124. if (CommonStatus::NO_ERROR != $_rs['code']) {
  125. $_code = CommonStatus::UNKNOWN_ERROR;
  126. $_param = json_encode([$_conf_data['mp_id'], $_conf_data['app_secret'], $_param]);
  127. $_return = json_encode($_rs);
  128. Log::write(
  129. "func=".__FUNCTION__."&class=".__CLASS__."&step=3&app_id=".$app_id."&type="
  130. .MpConfConst::MP_CONF_TYPE_6."&code=".$_code.'&msg=conf'.CommonStatus::getMsg($_code).'&param='
  131. .$_param.'&return='.$_return,
  132. LOG::ERROR, true
  133. );
  134. return $this->huoError($_code, CommonStatus::getMsg($_code));
  135. }
  136. $_code = CommonStatus::NO_ERROR;
  137. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_rs['data']);
  138. }
  139. }