You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
991 B
PHTML
37 lines
991 B
PHTML
2 years ago
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Service;
|
||
|
|
||
|
use App\Helper\Redis;
|
||
|
use App\Helper\RedisKey;
|
||
|
use App\Helper\StringHelper;
|
||
|
use App\Model\App;
|
||
|
use App\Model\Merchant;
|
||
|
|
||
|
class AppService extends AbstractService
|
||
|
{
|
||
|
public function createApp(Merchant $merchant) {
|
||
|
$app = new App();
|
||
|
$app->merchant_id = $merchant->id;
|
||
|
$app->app_id = $this->generateAppId();
|
||
|
$app->app_key = StringHelper::getRandomString(32);
|
||
|
$app->status = App::STATUS_ACTIVE;
|
||
|
$app->save();
|
||
|
return $app;
|
||
|
}
|
||
|
|
||
|
private function generateAppId() {
|
||
|
$now = time();
|
||
|
$key = RedisKey::getGenerateAppIdKey($now);
|
||
|
$expireAt = strtotime(date('Y-m-d 23:59:59', $now)) + 1;
|
||
|
$incrId = Redis::incr($key);
|
||
|
$incrId = '' . $incrId;
|
||
|
Redis::expireAt($key, $expireAt);
|
||
|
$padLength = 8 - strlen($incrId);
|
||
|
$incrId = str_pad($incrId, $padLength, '0', STR_PAD_LEFT);
|
||
|
return date('Ymd', $now) . $incrId;
|
||
|
}
|
||
|
}
|