CpaReversalController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * CpaReversalController.php UTF-8
  4. * 渠道二维码倒量
  5. *
  6. * @date : 2019/3/21 19:51
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\controller\cpa_reversal;
  13. use huo\controller\agent\AgentCache;
  14. use huo\controller\common\Base;
  15. use huo\controller\member\Oauth;
  16. use huo\controller\queue\CpaReversalQueue;
  17. use huo\controller\request\Game;
  18. use huo\model\member\MemoauthModel;
  19. use huolib\constant\FromConst;
  20. use huolib\constant\MemConst;
  21. use huolib\constant\OauthConst;
  22. use huolib\status\CommonStatus;
  23. use huomp\controller\agent\AgentState;
  24. use huomp\model\agent\AgentAdsCfgModel;
  25. class CpaReversalController extends Base {
  26. /**
  27. * 倒量处理
  28. *
  29. * @param $code
  30. * @param $_ext
  31. * @param Game $game_rq
  32. *
  33. * @return array
  34. */
  35. public function reversal($code, $_ext, Game $game_rq) {
  36. $_wx_code = $code;
  37. $_url_param = parse_url(urldecode($_ext));
  38. parse_str($_url_param['query'], $_query_arr);
  39. if (empty($_query_arr['huo_state'])) {
  40. $_code = CommonStatus::INVALID_PARAMS;
  41. return $this->huoError(CommonStatus::getMsg($_code), [], $_code);
  42. }
  43. $_ag_id = (new AgentState())->getIdByCode($_query_arr['huo_state']);
  44. $_agent_id = AgentCache::ins()->getAgentIdByAgId($_ag_id);
  45. //参数校验,成功返回配置
  46. $_cfg = $this->checkParam($_agent_id, $_query_arr);
  47. if (false == $_cfg) {
  48. $_code = CommonStatus::INVALID_PARAMS;
  49. return $this->huoError(CommonStatus::getMsg($_code), [], $_code);
  50. }
  51. $_type = OauthConst::OAUTH_MP;
  52. $_from = FromConst::FROM_WEIXIN;
  53. $_oauth_data = (new Oauth())->getOauthDataByCode($_type, $_wx_code, $_from, $game_rq->getHAppId());
  54. $_queue_data = $_query_arr; //队列数据
  55. $_queue_data['event'] = $_cfg['agent_code'];
  56. $_queue_data['mp_id'] = $game_rq->getMpId();
  57. $_queue_data['agent_id'] = $_agent_id;
  58. if (is_numeric($_oauth_data)) {
  59. /* 有注册玩家 */
  60. $_mem_id = $_oauth_data;
  61. $_queue_data['mem_id'] = $_mem_id;
  62. $_mo_data = (new MemoauthModel())->getInfoByMpMemId($game_rq->getMpId(), $_mem_id);
  63. $_queue_data['openid'] = empty($_mo_data['openid']) ? '' : $_mo_data['openid'];
  64. $_queue_data['status'] = MemConst::MEMBER_OLD;
  65. } else {
  66. $_queue_data['openid'] = $_oauth_data['openid'];
  67. $_queue_data['status'] = MemConst::MEMBER_NEW;
  68. }
  69. $_rs = (new CpaReversalQueue($_queue_data))->pushQueue();
  70. return $_rs;
  71. }
  72. /**
  73. * 参数校验
  74. *
  75. * @param $agent_id
  76. * @param array $param
  77. *
  78. * @return bool|array
  79. */
  80. public function checkParam($agent_id, $param = []) {
  81. $_cfg = $this->getAgentCfg($agent_id);
  82. if (empty($_cfg['url_param'])) {
  83. return false;
  84. }
  85. foreach ($_cfg['url_param'] as $_key) {
  86. if (empty($param[$_key])) {
  87. return false;
  88. }
  89. }
  90. return $_cfg;
  91. }
  92. /**
  93. * 获取渠道参数
  94. *
  95. * @param $agent_id
  96. *
  97. * @return array|bool
  98. */
  99. public function getAgentCfg($agent_id) {
  100. if (empty($agent_id)) {
  101. return false;
  102. }
  103. $_cfg = (new AgentAdsCfgModel())->getInfoByAgentId($agent_id);
  104. if (empty($_cfg)) {
  105. return false;
  106. }
  107. if (!empty($_cfg['url_param'])) {
  108. $_cfg['url_param'] = explode(',', $_cfg['url_param']);
  109. }
  110. return $_cfg;
  111. }
  112. }