123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /**
- * OauthController.php UTF-8
- * 第三方登陆
- *
- * @date : 2018/4/25 21:21
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace api\sdk\controller;
- use api\common\controller\V2ApiBaseController;
- use huo\controller\app\FloatPoint;
- use huo\controller\common\HuoCookie;
- use huo\controller\member\Oauth;
- use huolib\constant\FromConst;
- use huolib\constant\OauthConst;
- use huolib\oauth\OAuth as OAuthLib;
- use huolib\status\MemberStatus;
- use huolib\tool\SimpleSec;
- use huolib\tool\StrUtils;
- use think\Config;
- use think\Log;
- class OauthController extends V2ApiBaseController {
- function _initialize() {
- parent::_initialize();
- Log::write($this->request->getContent(), Log::LOG);
- Config::set('default_return_type', 'html');
- }
- /**
- * http://doc.1tsdk.com/138?page_id=3105
- * 第三方登陆网页入口
- * 【域名】/oauth/index
- */
- public function index() {
- $_type = get_val($this->rq_data, 'type');
- $_url = get_val($this->rq_data, 'url');
- $_from = $this->getFrom();
- $_oauth_class = new Oauth();
- $_oauth_class->setDomain(H5ISITE);
- $_rdata = $_oauth_class->getRequestCodeUrl($_type, $_from, $_url);
- if (MemberStatus::NO_ERROR != $_rdata['code']) {
- $this->error($_rdata['msg']);
- }
- $_url = $_rdata['data']['url'];
- $this->redirect($_url);
- }
- public function getReturn($mem_data, $app_id = 0) {
- $_rdata = (new FloatPoint())->getFloat($app_id);
- $_rdata['mem_id'] = $mem_data['mem_id'];
- $_rdata['agentgame'] = $mem_data['agent_game'];
- $_token = HuoCookie::getMemToken();
- $_rdata['user_token'] = $_token;
- $_rdata['is_bind'] = $mem_data['is_bind'];
- $_rdata['cp_user_token'] = SimpleSec::encode($_token, config('CPAUTHCODE'));
- $_rdata['url'] = $mem_data['url'];
- return $_rdata;
- }
- /**
- * @param $_type
- * @param $_code
- * @param $_state
- *
- * @return mixed
- */
- protected function callback($_type, $_code, $_state) {
- $_url = get_val($this->rq_data, 'back_url');
- $_url_arr = parse_url($_url);
- $_agent_id = 0;
- if (!empty($_url_arr['query'])) {
- parse_str($_url_arr['query'], $_query_arr);
- if (!empty($_query_arr['game_id'])) {
- $this->rq_data['app_id'] = $_query_arr['game_id'];
- }
- if (!empty($_query_arr['agent_id'])) {
- $this->rq_data['agent_id'] = $_query_arr['agent_id'];
- $_agent_id = $_query_arr['agent_id'];
- }
- }
- $_oauth_class = new Oauth();
- if (empty($_code) || empty($_state)) {
- $_code = MemberStatus::INVALID_PARAMS;
- $this->error(MemberStatus::getMsg($_code), '', $_code, [], $_url);
- }
- $_oauth_lib_class = OAuthLib::ins($_type);
- $_cmp_rs = $_oauth_lib_class->compareState($_state);
- if (false == $_cmp_rs) {
- $_code = MemberStatus::INVALID_PARAMS;
- $this->error(MemberStatus::getMsg($_code).'2', '', $_code, [], $_url);
- }
- $_game_rq = $this->setGameData();
- $_channel_rq = $this->setChannelData();
- if (!empty($_agent_id)) {
- $_channel_rq->setAgentId($_agent_id);
- }
- $_device_rq = $this->setDeviceData();
- $_mem_rq = $this->setMemData();
- $_from = $this->getFrom();
- $_rs = $_oauth_class->oauthLoginByCode(
- $_type, $_code, $_from, $_game_rq, $_channel_rq, $_device_rq, $_mem_rq
- );
- if (MemberStatus::NO_ERROR != $_rs['code']) {
- $this->error($_rs['msg']);
- }
- $_token = HuoCookie::getMemToken();
- $_url = StrUtils::getUrl($_url).'token='.$_token;
- $_mem_data = $_rs['data'];
- if (!empty($_url)) {
- $_mem_data['url'] = $_url;
- }
- $_rdata = $this->getReturn($_mem_data);
- $this->assign($_rdata);
- return $this->fetch('oauth/callback');
- }
- /**
- * 回调地址
- */
- function callbackWeixin() {
- $_code = get_val($this->rq_data, 'code');
- $_state = get_val($this->rq_data, 'state');
- $_type = OauthConst::OAUTH_WXQRCODE;
- if ($this->request->isWeixin()) {
- $_type = OauthConst::OAUTH_WEIXIN;
- }
- return $this->callback($_type, $_code, $_state);
- }
- /**
- * 回调地址
- */
- function callbackWeibo() {
- $_code = get_val($this->rq_data, 'code');
- $_state = get_val($this->rq_data, 'state');
- $_type = OauthConst::OAUTH_WEIBO;
- return $this->callback($_type, $_code, $_state);
- }
- /**
- * 回调地址
- */
- function callbackQq() {
- $_code = get_val($this->rq_data, 'code');
- $_state = get_val($this->rq_data, 'state');
- $_type = OauthConst::OAUTH_QQ;
- return $this->callback($_type, $_code, $_state);
- }
- /**
- * 获取来源
- *
- * @return string
- */
- private function getFrom() {
- $_from = FromConst::FROM_PC;
- if ($this->request->isMobile()) {
- $_from = FromConst::FROM_MOBILE;
- }
- if ($this->request->isWeixin()) {
- $_from = FromConst::FROM_WEIXIN;
- }
- return $_from;
- }
- /**
- * 回调地址
- */
- function callbackWeixinByToken() {
- $_access_token = get_val($this->rq_data, 'access_token');
- $_openid = get_val($this->rq_data, 'openid');
- $_type = OauthConst::OAUTH_WXQRCODE;
- if ($this->request->isWeixin()) {
- $_type = OauthConst::OAUTH_WEIXIN;
- }
- return $this->callbackByToken($_type, $_access_token, $_openid);
- }
- /**
- * 回调地址
- */
- function callbackWeiboByToken() {
- $_access_token = get_val($this->rq_data, 'access_token');
- $_openid = get_val($this->rq_data, 'openid');
- $_type = OauthConst::OAUTH_WEIBO;
- return $this->callbackByToken($_type, $_access_token, $_openid);
- }
- /**
- * 回调地址
- */
- function callbackQqByToken() {
- $_access_token = get_val($this->rq_data, 'access_token');
- $_openid = get_val($this->rq_data, 'openid');
- $_qq_app_key = get_val($this->rq_data, 'oauth_app_key', '');
- $_type = OauthConst::OAUTH_QQ;
- return $this->callbackByToken($_type, $_access_token, $_openid, $_qq_app_key);
- }
- /**
- * @param $type
- * @param $token
- * @param $openid
- * @param $ext
- *
- * @return mixed
- */
- protected function callbackByToken($type, $token, $openid, $ext = '') {
- $_url = get_val($this->rq_data, 'back_url');
- $_url_arr = parse_url($_url);
- if (!empty($_url_arr['query'])) {
- parse_str($_url_arr['query'], $_query_arr);
- if (!empty($_query_arr['game_id'])) {
- $this->rq_data['app_id'] = $_query_arr['game_id'];
- }
- }
- $_oauth_class = new Oauth();
- $_game_rq = $this->setGameData();
- $_channel_rq = $this->setChannelData();
- $_device_rq = $this->setDeviceData();
- $_mem_rq = $this->setMemData();
- $_from = $this->getFrom();
- $_rs = $_oauth_class->oauthLoginByAccessToken(
- $type, $token, $openid, $_from, $_game_rq, $_channel_rq, $_device_rq, $_mem_rq, [], $ext
- );
- if (MemberStatus::NO_ERROR != $_rs['code']) {
- $this->error($_rs['msg']);
- }
- $_token = HuoCookie::getMemToken();
- $_url = StrUtils::getUrl($_url).'token='.$_token;
- $_mem_data = $_rs['data'];
- if (!empty($_url)) {
- $_mem_data['url'] = $_url;
- }
- $_rdata = $this->getReturn($_mem_data);
- $_code = MemberStatus::NO_ERROR;
- $this->success(MemberStatus::getMsg($_code), $_rdata);
- }
- }
|