OauthController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * OauthController.php UTF-8
  4. * 第三方登陆
  5. *
  6. * @date : 2018/4/25 21:21
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace api\apple\controller;
  13. use api\common\controller\AppleApiBaseController;
  14. use huo\controller\app\FloatPoint;
  15. use huo\controller\member\OauthOut;
  16. use huo\controller\request\Channel;
  17. use huo\controller\request\Device;
  18. use huo\controller\request\Game;
  19. use huo\controller\request\Mem;
  20. use huolib\constant\FromConst;
  21. use huolib\constant\OauthConst;
  22. use huolib\oauth\OAuth as OAuthLib;
  23. use huolib\status\MemberStatus;
  24. use huolib\tool\SimpleSec;
  25. use huolib\tool\StrUtils;
  26. use think\Config;
  27. use think\Log;
  28. class OauthController extends AppleApiBaseController {
  29. function _initialize() {
  30. parent::_initialize();
  31. Log::write($this->request->getContent(), Log::LOG);
  32. Config::set('default_return_type', 'html');
  33. }
  34. /**
  35. * http://doc.1tsdk.com/138?page_id=3105
  36. * 第三方登陆网页入口
  37. * 【域名】/oauth/index
  38. */
  39. public function index() {
  40. $_type = get_val($this->rq_data, 'type');
  41. $_url = get_val($this->rq_data, 'url');
  42. $_from = $this->getFrom();
  43. $_oauth_class = new OauthOut();
  44. $_rdata = $_oauth_class->getRequestCodeUrl($_type, $_from, $_url);
  45. if (MemberStatus::NO_ERROR != $_rdata['code']) {
  46. $this->error($_rdata['msg']);
  47. }
  48. $_url = $_rdata['data']['url'];
  49. $this->redirect($_url);
  50. }
  51. public function getReturn($mem_data, $app_id = 0) {
  52. $_rdata = (new FloatPoint())->getFloat($app_id);
  53. $_rdata['mem_id'] = $mem_data['mem_id'];
  54. $_rdata['agentgame'] = $mem_data['agent_game'];
  55. $_rdata['user_token'] = session_id();
  56. $_rdata['is_bind'] = $mem_data['is_bind'];
  57. $_rdata['cp_user_token'] = SimpleSec::encode(session_id(), config('CPAUTHCODE'));
  58. $_rdata['url'] = $mem_data['url'];
  59. return $_rdata;
  60. }
  61. /**
  62. * @param $_type
  63. * @param $_code
  64. * @param $_state
  65. *
  66. * @return mixed
  67. */
  68. protected function callback($_type, $_code, $_state) {
  69. $_url = get_val($this->rq_data, 'back_url');
  70. $_url_arr = parse_url($_url);
  71. if (!empty($_url_arr['query'])) {
  72. parse_str($_url_arr['query'], $_query_arr);
  73. if (!empty($_query_arr['game_id'])) {
  74. $this->rq_data['app_id'] = $_query_arr['game_id'];
  75. }
  76. }
  77. $_url = StrUtils::getUrl($_url).'token='.session_id();
  78. $_oauth_class = new OauthOut();
  79. if (empty($_code) || empty($_state)) {
  80. $_code = MemberStatus::INVALID_PARAMS;
  81. $this->error(MemberStatus::getMsg($_code), '', $_code, [], $_url);
  82. }
  83. $_oauth_lib_class = OAuthLib::ins($_type);
  84. $_cmp_rs = $_oauth_lib_class->compareState($_state);
  85. if (false == $_cmp_rs) {
  86. $_code = MemberStatus::INVALID_PARAMS;
  87. $this->error(MemberStatus::getMsg($_code).'2', '', $_code, [], $_url);
  88. }
  89. $_game_rq = $this->setGameData();
  90. $_channel_rq = $this->setChannelData();
  91. $_device_rq = $this->setDeviceData();
  92. $_mem_rq = $this->setMemData();
  93. $_from = $this->getFrom();
  94. $_rs = $_oauth_class->oauthLoginByCode(
  95. $_type, $_code, $_from, $_game_rq, $_channel_rq, $_device_rq, $_mem_rq
  96. );
  97. if (MemberStatus::NO_ERROR != $_rs['code']) {
  98. $this->error($_rs['msg']);
  99. }
  100. $_mem_data = $_rs['data'];
  101. if (!empty($_url)) {
  102. $_mem_data['url'] = $_url;
  103. }
  104. $_rdata = $this->getReturn($_mem_data);
  105. $this->assign($_rdata);
  106. return $this->fetch('oauth/callback');
  107. }
  108. /**
  109. * 回调地址
  110. */
  111. function callbackWeixin() {
  112. $_code = get_val($this->rq_data, 'code');
  113. $_state = get_val($this->rq_data, 'state');
  114. $_type = OauthConst::OAUTH_WXQRCODE;
  115. if ($this->request->isWeixin()) {
  116. $_type = OauthConst::OAUTH_WEIXIN;
  117. }
  118. return $this->callback($_type, $_code, $_state);
  119. }
  120. /**
  121. * 回调地址
  122. */
  123. function callbackWeibo() {
  124. $_code = get_val($this->rq_data, 'code');
  125. $_state = get_val($this->rq_data, 'state');
  126. $_type = OauthConst::OAUTH_WEIBO;
  127. return $this->callback($_type, $_code, $_state);
  128. }
  129. /**
  130. * 回调地址
  131. */
  132. function callbackQq() {
  133. $_code = get_val($this->rq_data, 'code');
  134. $_state = get_val($this->rq_data, 'state');
  135. $_type = OauthConst::OAUTH_QQ;
  136. return $this->callback($_type, $_code, $_state);
  137. }
  138. /**
  139. * 获取来源
  140. *
  141. * @return string
  142. */
  143. private function getFrom() {
  144. $_from = FromConst::FROM_PC;
  145. if ($this->request->isMobile()) {
  146. $_from = FromConst::FROM_MOBILE;
  147. }
  148. if ($this->request->isWeixin()) {
  149. $_from = FromConst::FROM_WEIXIN;
  150. }
  151. return $_from;
  152. }
  153. }