OauthController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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\sdk\controller;
  13. use api\common\controller\V2ApiBaseController;
  14. use huo\controller\app\FloatPoint;
  15. use huo\controller\common\HuoCookie;
  16. use huo\controller\member\Oauth;
  17. use huolib\constant\FromConst;
  18. use huolib\constant\OauthConst;
  19. use huolib\oauth\OAuth as OAuthLib;
  20. use huolib\status\MemberStatus;
  21. use huolib\tool\SimpleSec;
  22. use huolib\tool\StrUtils;
  23. use think\Config;
  24. use think\Log;
  25. class OauthController extends V2ApiBaseController {
  26. function _initialize() {
  27. parent::_initialize();
  28. Log::write($this->request->getContent(), Log::LOG);
  29. Config::set('default_return_type', 'html');
  30. }
  31. /**
  32. * http://doc.1tsdk.com/138?page_id=3105
  33. * 第三方登陆网页入口
  34. * 【域名】/oauth/index
  35. */
  36. public function index() {
  37. $_type = get_val($this->rq_data, 'type');
  38. $_url = get_val($this->rq_data, 'url');
  39. $_from = $this->getFrom();
  40. $_oauth_class = new Oauth();
  41. $_oauth_class->setDomain(H5ISITE);
  42. $_rdata = $_oauth_class->getRequestCodeUrl($_type, $_from, $_url);
  43. if (MemberStatus::NO_ERROR != $_rdata['code']) {
  44. $this->error($_rdata['msg']);
  45. }
  46. $_url = $_rdata['data']['url'];
  47. $this->redirect($_url);
  48. }
  49. public function getReturn($mem_data, $app_id = 0) {
  50. $_rdata = (new FloatPoint())->getFloat($app_id);
  51. $_rdata['mem_id'] = $mem_data['mem_id'];
  52. $_rdata['agentgame'] = $mem_data['agent_game'];
  53. $_token = HuoCookie::getMemToken();
  54. $_rdata['user_token'] = $_token;
  55. $_rdata['is_bind'] = $mem_data['is_bind'];
  56. $_rdata['cp_user_token'] = SimpleSec::encode($_token, config('CPAUTHCODE'));
  57. $_rdata['url'] = $mem_data['url'];
  58. return $_rdata;
  59. }
  60. /**
  61. * @param $_type
  62. * @param $_code
  63. * @param $_state
  64. *
  65. * @return mixed
  66. */
  67. protected function callback($_type, $_code, $_state) {
  68. $_url = get_val($this->rq_data, 'back_url');
  69. $_url_arr = parse_url($_url);
  70. $_agent_id = 0;
  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. if (!empty($_query_arr['agent_id'])) {
  77. $this->rq_data['agent_id'] = $_query_arr['agent_id'];
  78. $_agent_id = $_query_arr['agent_id'];
  79. }
  80. }
  81. $_oauth_class = new Oauth();
  82. if (empty($_code) || empty($_state)) {
  83. $_code = MemberStatus::INVALID_PARAMS;
  84. $this->error(MemberStatus::getMsg($_code), '', $_code, [], $_url);
  85. }
  86. $_oauth_lib_class = OAuthLib::ins($_type);
  87. $_cmp_rs = $_oauth_lib_class->compareState($_state);
  88. if (false == $_cmp_rs) {
  89. $_code = MemberStatus::INVALID_PARAMS;
  90. $this->error(MemberStatus::getMsg($_code).'2', '', $_code, [], $_url);
  91. }
  92. $_game_rq = $this->setGameData();
  93. $_channel_rq = $this->setChannelData();
  94. if (!empty($_agent_id)) {
  95. $_channel_rq->setAgentId($_agent_id);
  96. }
  97. $_device_rq = $this->setDeviceData();
  98. $_mem_rq = $this->setMemData();
  99. $_from = $this->getFrom();
  100. $_rs = $_oauth_class->oauthLoginByCode(
  101. $_type, $_code, $_from, $_game_rq, $_channel_rq, $_device_rq, $_mem_rq
  102. );
  103. if (MemberStatus::NO_ERROR != $_rs['code']) {
  104. $this->error($_rs['msg']);
  105. }
  106. $_token = HuoCookie::getMemToken();
  107. $_url = StrUtils::getUrl($_url).'token='.$_token;
  108. $_mem_data = $_rs['data'];
  109. if (!empty($_url)) {
  110. $_mem_data['url'] = $_url;
  111. }
  112. $_rdata = $this->getReturn($_mem_data);
  113. $this->assign($_rdata);
  114. return $this->fetch('oauth/callback');
  115. }
  116. /**
  117. * 回调地址
  118. */
  119. function callbackWeixin() {
  120. $_code = get_val($this->rq_data, 'code');
  121. $_state = get_val($this->rq_data, 'state');
  122. $_type = OauthConst::OAUTH_WXQRCODE;
  123. if ($this->request->isWeixin()) {
  124. $_type = OauthConst::OAUTH_WEIXIN;
  125. }
  126. return $this->callback($_type, $_code, $_state);
  127. }
  128. /**
  129. * 回调地址
  130. */
  131. function callbackWeibo() {
  132. $_code = get_val($this->rq_data, 'code');
  133. $_state = get_val($this->rq_data, 'state');
  134. $_type = OauthConst::OAUTH_WEIBO;
  135. return $this->callback($_type, $_code, $_state);
  136. }
  137. /**
  138. * 回调地址
  139. */
  140. function callbackQq() {
  141. $_code = get_val($this->rq_data, 'code');
  142. $_state = get_val($this->rq_data, 'state');
  143. $_type = OauthConst::OAUTH_QQ;
  144. return $this->callback($_type, $_code, $_state);
  145. }
  146. /**
  147. * 获取来源
  148. *
  149. * @return string
  150. */
  151. private function getFrom() {
  152. $_from = FromConst::FROM_PC;
  153. if ($this->request->isMobile()) {
  154. $_from = FromConst::FROM_MOBILE;
  155. }
  156. if ($this->request->isWeixin()) {
  157. $_from = FromConst::FROM_WEIXIN;
  158. }
  159. return $_from;
  160. }
  161. /**
  162. * 回调地址
  163. */
  164. function callbackWeixinByToken() {
  165. $_access_token = get_val($this->rq_data, 'access_token');
  166. $_openid = get_val($this->rq_data, 'openid');
  167. $_type = OauthConst::OAUTH_WXQRCODE;
  168. if ($this->request->isWeixin()) {
  169. $_type = OauthConst::OAUTH_WEIXIN;
  170. }
  171. return $this->callbackByToken($_type, $_access_token, $_openid);
  172. }
  173. /**
  174. * 回调地址
  175. */
  176. function callbackWeiboByToken() {
  177. $_access_token = get_val($this->rq_data, 'access_token');
  178. $_openid = get_val($this->rq_data, 'openid');
  179. $_type = OauthConst::OAUTH_WEIBO;
  180. return $this->callbackByToken($_type, $_access_token, $_openid);
  181. }
  182. /**
  183. * 回调地址
  184. */
  185. function callbackQqByToken() {
  186. $_access_token = get_val($this->rq_data, 'access_token');
  187. $_openid = get_val($this->rq_data, 'openid');
  188. $_qq_app_key = get_val($this->rq_data, 'oauth_app_key', '');
  189. $_type = OauthConst::OAUTH_QQ;
  190. return $this->callbackByToken($_type, $_access_token, $_openid, $_qq_app_key);
  191. }
  192. /**
  193. * @param $type
  194. * @param $token
  195. * @param $openid
  196. * @param $ext
  197. *
  198. * @return mixed
  199. */
  200. protected function callbackByToken($type, $token, $openid, $ext = '') {
  201. $_url = get_val($this->rq_data, 'back_url');
  202. $_url_arr = parse_url($_url);
  203. if (!empty($_url_arr['query'])) {
  204. parse_str($_url_arr['query'], $_query_arr);
  205. if (!empty($_query_arr['game_id'])) {
  206. $this->rq_data['app_id'] = $_query_arr['game_id'];
  207. }
  208. }
  209. $_oauth_class = new Oauth();
  210. $_game_rq = $this->setGameData();
  211. $_channel_rq = $this->setChannelData();
  212. $_device_rq = $this->setDeviceData();
  213. $_mem_rq = $this->setMemData();
  214. $_from = $this->getFrom();
  215. $_rs = $_oauth_class->oauthLoginByAccessToken(
  216. $type, $token, $openid, $_from, $_game_rq, $_channel_rq, $_device_rq, $_mem_rq, [], $ext
  217. );
  218. if (MemberStatus::NO_ERROR != $_rs['code']) {
  219. $this->error($_rs['msg']);
  220. }
  221. $_token = HuoCookie::getMemToken();
  222. $_url = StrUtils::getUrl($_url).'token='.$_token;
  223. $_mem_data = $_rs['data'];
  224. if (!empty($_url)) {
  225. $_mem_data['url'] = $_url;
  226. }
  227. $_rdata = $this->getReturn($_mem_data);
  228. $_code = MemberStatus::NO_ERROR;
  229. $this->success(MemberStatus::getMsg($_code), $_rdata);
  230. }
  231. }