OauthOut.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * OauthOut.php UTF-8
  4. * 小程序登陆
  5. *
  6. * @date : 2018/8/17 15:02
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\controller\member;
  13. use huo\controller\agent\AgentCache;
  14. use huo\controller\common\Base;
  15. use huo\controller\game\GameCache;
  16. use huo\controller\member\MemAgent;
  17. use huo\controller\member\MemCache;
  18. use huo\controller\member\Oauth;
  19. use huo\controller\request\Channel;
  20. use huo\controller\request\Device;
  21. use huo\controller\request\Game;
  22. use huo\controller\request\Mem;
  23. use huolib\constant\AgentConst;
  24. use huolib\constant\CommonConst;
  25. use huolib\constant\FromConst;
  26. use huolib\constant\OauthConst;
  27. use huolib\constant\OrderConst;
  28. use huolib\status\MemberStatus;
  29. use huolib\tool\Rand;
  30. use huomp\controller\agent\AgentState;
  31. class OauthOut extends Base {
  32. /**
  33. * 小程序用户登陆注册
  34. *
  35. * @param string $code 第三方用户登录获取的code码
  36. * @param string $state 上级state码
  37. * @param Game $game_rq
  38. * @param Channel $channel
  39. * @param Device $device
  40. * @param Mem $member
  41. *
  42. * @return array
  43. */
  44. public function oauthLogin($code, $state, Game $game_rq, Channel $channel, Device $device, Mem $member) {
  45. $_code = $code;
  46. if (empty($_code)) {
  47. $_code = MemberStatus::INVALID_PARAMS;
  48. return $this->huoError($_code, MemberStatus::getMsg($_code));
  49. }
  50. $_parent_ag_id = (new AgentState())->getIdByCode($state);
  51. $_parent_agent_id = 0; /* 上级渠道ID */
  52. $_parent_mem_id = 0; /* 上级玩家ID */
  53. if (!empty($_parent_ag_id)) {
  54. $_ac_class = AgentCache::ins();
  55. $_parent_agent_id = $_ac_class->getAgentIdByAgId($_parent_ag_id);
  56. $_parent_mem_id = $_ac_class->getMemIdByAgentId($_parent_agent_id);
  57. }
  58. $_type = OauthConst::OAUTH_MP;
  59. $_oauth_class = new Oauth();
  60. $_game_info = (GameCache::ins())->getInfoByAppId($game_rq->getHAppId());
  61. $_ext_info = !empty($_game_info['ext_info']) ? $_game_info['ext_info'] : [];
  62. if (!empty($_parent_mem_id)) {
  63. $_parent_mem_data = (MemCache::ins())->getInfoById($_parent_mem_id);
  64. if (!empty($_parent_mem_data)) {
  65. //设置父级玩家ID
  66. $member->setParentMemId($_parent_mem_id);
  67. // $member->setAgentId($_parent_agent_id);
  68. // $channel->setAgentId($_parent_agent_id);
  69. }
  70. } elseif (!empty($_parent_agent_id) && empty($_parent_mem_id)) {
  71. $_agent_data = AgentCache::ins()->getInfoByAgentId($_parent_agent_id);
  72. $member->setAgentId($_parent_agent_id);
  73. $channel->setAgentId($_parent_agent_id);
  74. if (AgentConst::AGENT_ROLE_MP_AGENT == $_agent_data['role_id']) { //小程序渠道
  75. if (empty($_ext_info['agent_id']) && OrderConst::PAY_SWITCH_YES == $_agent_data['is_switch']) {
  76. //概率性切换玩家
  77. $_is_switch = $_agent_data['switch_rate'];
  78. $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch];
  79. $_key = Rand::getRand($_arr);
  80. if ('is_switch' == $_key) {
  81. $member->setSwitch(OrderConst::PAY_SWITCH_YES);
  82. }
  83. }
  84. }
  85. }
  86. /* 判断小程序马甲是否有游戏管理员 */
  87. if (!empty($_ext_info['agent_id']) && empty($_parent_agent_id)) {
  88. $member->setAgentId($_ext_info['agent_id']);
  89. $channel->setAgentId($_ext_info['agent_id']);
  90. }
  91. $from = FromConst::FROM_WEIXIN;
  92. $_rs = $_oauth_class->oauthLoginByCode($_type, $_code, $from, $game_rq, $channel, $device, $member);
  93. if (MemberStatus::NO_ERROR != $_rs['code']) {
  94. return $this->huoReturn($_rs);
  95. }
  96. $_mem_id = $_rs['data']['mem_id'];
  97. if (empty($_mem_id)) {
  98. $_code = MemberStatus::INNER_ERROR;
  99. return $this->huoError($_code, MemberStatus::getMsg($_code));
  100. }
  101. //生成玩家渠道
  102. // $_agent_id = (new MemAgent())->getMemAgentId($_mem_id);
  103. // if (empty($_agent_id)) {
  104. // $_code = MemberStatus::INNER_ERROR;
  105. //// return $this->huoError($_code, MemberStatus::getMsg($_code));
  106. // }
  107. // //新注册添加三款有奖励的游戏作为推广游戏
  108. // $_isReg = isset($_rs['data']['is_reg']) ? $_rs['data']['is_reg'] : 0;
  109. // if (!empty($_agent_id) && !empty($_isReg)) {
  110. // $_ids = (new AgentGameListLogic())->getRewardGame();
  111. // if (!empty($_ids)) {
  112. // foreach ($_ids as $_v) {
  113. // (new AgentGame())->addGame($_agent_id, $_v);
  114. // }
  115. // }
  116. // }
  117. return $this->huoReturn($_rs);
  118. }
  119. }