-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
208 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: JHC | ||
* Date: 2021-01-09 | ||
* Time: 18:36 | ||
*/ | ||
|
||
namespace XinXiHua\SDK\Auth; | ||
|
||
use Illuminate\Contracts\Session\Session; | ||
use XinXiHua\SDK\Models\OauthUser; | ||
|
||
|
||
class OauthManager | ||
{ | ||
/** | ||
* @var OauthUser|null | ||
*/ | ||
protected $oauth = null; | ||
protected $session; | ||
|
||
|
||
/** | ||
* OauthManager constructor. | ||
* @param \Illuminate\Contracts\Foundation\Application $app | ||
*/ | ||
public function __construct($app) | ||
{ | ||
$this->session = $app->make(Session::class); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function check() | ||
{ | ||
return !is_null($this->oauth()); | ||
} | ||
|
||
/** | ||
* @return OauthUser|null | ||
*/ | ||
public function oauth() | ||
{ | ||
|
||
if (!is_null($this->oauth)) { | ||
return $this->oauth; | ||
} | ||
|
||
if ($this->session->isStarted()) { | ||
$oauth = json_decode($this->session->get($this->getName()), true); | ||
$this->setOauth($oauth); | ||
} | ||
|
||
return $this->oauth; | ||
} | ||
|
||
public function id() | ||
{ | ||
return $this->oauth() ? $this->oauth->oauthId : null; | ||
} | ||
|
||
protected function getName() | ||
{ | ||
return 'login_oauth_' . sha1(static::class); | ||
} | ||
|
||
public function setOauth($oauth) | ||
{ | ||
$oauthUser = new OauthUser(); | ||
$oauthUser->oauthId = $oauth['open_id']; | ||
$oauthUser->oauthType = $oauth['oauth_type'] ?? 'wechat'; | ||
$this->oauth = $oauthUser; | ||
} | ||
|
||
public function login($oauth) | ||
{ | ||
$this->session->put($this->getName(), json_encode($oauth, JSON_UNESCAPED_UNICODE)); | ||
$this->setOauth($oauth); | ||
return $this->oauth; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: JHC | ||
* Date: 2021-01-11 | ||
* Time: 11:36 | ||
*/ | ||
|
||
namespace XinXiHua\SDK\Facades; | ||
|
||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @method static bool check() | ||
* @method static \XinXiHua\SDK\Models\OauthUser|null oauth() | ||
* @method static string|null id() | ||
* @method static void login(array $oauth) | ||
* | ||
* @see \XinXiHua\SDK\Auth\OauthManager | ||
*/ | ||
class OAuth extends Facade | ||
{ | ||
|
||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'oauth'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: JHC | ||
* Date: 2021-01-09 | ||
* Time: 18:58 | ||
*/ | ||
|
||
namespace XinXiHua\SDK\Models; | ||
|
||
|
||
class OauthUser | ||
{ | ||
|
||
public $oauthId; | ||
public $oauthType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters