作者 竞泽

add:session live

<?php
namespace Lackoxygen\TiktokShop\Command;
use Illuminate\Console\Command;
use Lackoxygen\TiktokShop\Supervisor\Session\SessionHeart;
class RefreshToken extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tiktok-shop:refresh.token';
/**
* The console command description.
*
* @var string
*/
protected $description = 'The refresh tiktok shop token';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
SessionHeart::new()->keepalive();
return 0;
}
}
... ...
<?php
namespace Lackoxygen\TiktokShop\Contracts;
interface SessionInterface
{
public function id(): string;
public function unEffective(): bool;
public function effective(): bool;
public function discard();
public function watch(): bool;
public function appKey(): string;
}
... ...
<?php
namespace Lackoxygen\TiktokShop\Supervisor\Session;
use Lackoxygen\TiktokShop\Util\Json;
class Factory
{
protected \Closure $callback;
public function __construct(\Closure $callback)
{
$this->callback = $callback;
}
public function load(string $raw)
{
if (!$raw) {
return;
}
$array = Json::unmarshal($raw);
$callback = $this->callback;
foreach ($array as $row) {
$callback($this->create($row));
}
}
protected function create($row)
{
return unserialize($row);
}
}
... ...
<?php
namespace Lackoxygen\TiktokShop\Supervisor\Session;
use Carbon\Carbon;
use Lackoxygen\TiktokShop\Contracts\SessionInterface;
class Session implements SessionInterface
{
use Swap;
protected Store $store;
protected string $index = '';
protected string $appKey = '';
public function __construct(string $appKey, Store $store)
{
$this->appKey = $appKey;
$this->store = $store;
$this->index = $this->id();
}
public function id(): string
{
return md5(serialize($this->store));
}
public function unEffective(): bool
{
return !$this->effective();
}
public function effective(): bool
{
return $this->store->getExpiredAt()->gt(Carbon::now());
}
public function discard()
{
$this->store->setExpiredAt(Carbon::now()->subMinutes(30));
}
public function watch(): bool
{
return $this->index !== $this->id();
}
public function appKey(): string
{
return $this->appKey;
}
}
... ...
<?php
namespace Lackoxygen\TiktokShop\Supervisor\Session;
use Illuminate\Cache\CacheManager;
use Lackoxygen\TiktokShop\Contracts\SessionInterface;
use Lackoxygen\TiktokShop\Util\Json;
class SessionHeart
{
protected static ?SessionHeart $sessionHeart = null;
protected CacheManager $cacheManager;
public function __construct()
{
SessionMemory::init();
}
public static function new(): SessionHeart
{
if (is_null(static::$sessionHeart)) {
static::$sessionHeart = new static;
}
return static::$sessionHeart;
}
public function keepalive()
{
SessionMemory::each(function (SessionInterface $session) {
if ($session->effective()) {
return;
}
$session->refresh();
});
}
public function __destruct()
{
$watch = false;
$serializes = [];
SessionMemory::each(function (SessionInterface $session) use (&$watch, &$serializes) {
$serializes[] = serialize($session);
if (!$watch && $session->watch()) {
$watch = true;
}
});
if ($watch) {
SessionMemory::persistence(Json::marshal($serializes));
}
}
}
... ...
<?php
namespace Lackoxygen\TiktokShop\Supervisor\Session;
use Lackoxygen\TiktokShop\Contracts\SessionInterface;
class SessionMemory
{
/**
* @var Session [] $sessions
*/
protected static array $sessions = [];
/**
* @var string
*/
protected static string $cachePrefix = 'tiktok.shop:session:';
protected static function cache()
{
static $cacheManager;
if (is_null($cacheManager)) {
$cacheManager = app('cache');
}
return $cacheManager;
}
public static function init()
{
$factory = new Factory(function (Session $session) {
static::$sessions[$session->appKey()] = $session;
});
$factory->load(static::persistence());
}
public static function persistence()
{
$key = static::$cachePrefix.'keepalive_table';
try {
if (func_get_args()) {
static::cache()->set($key, func_get_arg(0));
} else {
$raw = static::cache()->get($key);
if (is_null($raw)) {
return null;
}
return $raw;
}
} catch (\Exception $e) {
}
}
public static function each(\Closure $callback)
{
foreach (static::$sessions as $session) {
$callback($session);
}
}
public static function get(string $appKey = null)
{
if (is_null($appKey)) {
return static::$sessions;
}
return static::exist($appKey) ? static::$sessions[$appKey] : null;
}
public static function exist(string $appKey): bool
{
return array_key_exists($appKey, static::$sessions);
}
public static function put(SessionInterface $session, $force = true)
{
if (static::exist($session->appKey()) && !$force) {
return;
}
static::$sessions[$session->appKey()] = $session;
}
}
... ...
<?php
namespace Lackoxygen\TiktokShop\Supervisor\Session;
use Carbon\Carbon;
use Illuminate\Queue\SerializesModels;
class Store
{
use SerializesModels;
/**
* The ID.
*
* @var string
*/
protected string $id;
/**
* The access token.
*
* @var string
*/
protected string $accessToken;
/**
* The refresh token.
*
* @var string
*/
protected string $refreshToken;
/**
* The created at.
*
* @var Carbon
*/
protected Carbon $createdAt;
/**
* The expired at.
*
* @var Carbon
*/
protected Carbon $expiredAt;
/**
* The expires in.
*
* @var int
*/
protected int $expiresIn;
/**
* the more data.
*
* @var array
*/
protected array $extras = [];
/**
* the refresh num.
*
* @var int
*/
protected int $refreshNum = 0;
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @param string $id
*/
public function setId(string $id): void
{
$this->id = $id;
}
/**
* @return string
*/
public function getAccessToken(): string
{
return $this->accessToken;
}
/**
* @param string $accessToken
*/
public function setAccessToken(string $accessToken): void
{
$this->accessToken = $accessToken;
}
/**
* @return string
*/
public function getRefreshToken(): string
{
return $this->refreshToken;
}
/**
* @param string $refreshToken
*/
public function setRefreshToken(string $refreshToken): void
{
$this->refreshToken = $refreshToken;
}
/**
* @return Carbon
*/
public function getCreatedAt(): Carbon
{
return $this->createdAt;
}
/**
* @param Carbon $createdAt
*/
public function setCreatedAt(Carbon $createdAt): void
{
$this->createdAt = $createdAt;
}
/**
* @return Carbon
*/
public function getExpiredAt(): Carbon
{
return $this->expiredAt;
}
/**
* @param Carbon $expiredAt
*/
public function setExpiredAt(Carbon $expiredAt): void
{
$this->expiredAt = $expiredAt;
}
/**
* @return int
*/
public function getExpiresIn(): int
{
return $this->expiresIn;
}
/**
* @param int $expiresIn
*/
public function setExpiresIn(int $expiresIn): void
{
$this->expiresIn = $expiresIn;
}
/**
* @return array
*/
public function getExtras(): array
{
return $this->extras;
}
/**
* @param array $extras
*/
public function setExtras(array $extras): void
{
$this->extras = $extras;
}
/**
* @return int
*/
public function getRefreshNum(): int
{
return $this->refreshNum;
}
/**
* @param int $refreshNum
*/
public function setRefreshNum(int $refreshNum): void
{
$this->refreshNum = $refreshNum;
}
}
... ...
<?php
namespace Lackoxygen\TiktokShop\Supervisor\Session;
use Lackoxygen\TiktokShop\Exception\ClientException;
use Lackoxygen\TiktokShop\Passage\ResultSet;
trait Swap
{
public function refresh()
{
$app = app('tiktok.shop');
try {
/**
* @var ResultSet $result
*/
$result = $app->authorize->refresh($this->store->getRefreshToken());
if ($result->isSuccess()) {
$this->store->setAccessToken($result->get('access_token'));
$this->store->setRefreshToken($result->get('refresh_token'));
$this->store->setRefreshNum($this->store->getRefreshNum() + 1);
$this->store->setCreatedAt(now());
$this->store->setExpiredAt(now()->addMicroseconds($result->get('expired_in')));
}
} catch (ClientException $exception) {
$this->discard();
}
}
}
... ...