67 lines
1.9 KiB
PHP
67 lines
1.9 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: home
|
|
* Date: 2019/6/20
|
|
* Time: 11:29
|
|
*/
|
|
|
|
namespace app\service;
|
|
|
|
|
|
use app\model\AdminInfo;
|
|
use app\model\AdminToken;
|
|
use think\facade\Config;
|
|
|
|
class AdminService
|
|
{
|
|
/**
|
|
* @param AdminInfo $adminInfo
|
|
* @return AdminToken
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public static function createAdminToken(AdminInfo $adminInfo)
|
|
{
|
|
$allowMultiLogin = Config::get("app.admin.multi_login", true);
|
|
$loginExpired = Config::get("app.admin.expired", 1800);
|
|
$data = [
|
|
'token' => md5($adminInfo->id . time()),
|
|
'admin_id' => $adminInfo->id,
|
|
'device_type' => 'web',
|
|
//过期时间
|
|
'expired_at' => date('Y-m-d H:i:s', time() + $loginExpired)
|
|
];
|
|
if ($allowMultiLogin) {
|
|
return AdminToken::create($data);
|
|
} else {
|
|
$token = AdminToken::where('admin_id', $adminInfo->id)->find();
|
|
if (empty($token)) {
|
|
return AdminToken::create($data);
|
|
}
|
|
// $data['updated_at'] = date('Y-m-d H:i:s', time());
|
|
|
|
$token->db()
|
|
->where('admin_id',$token->admin_id)
|
|
->where('device_type',$token->device_type)
|
|
->where('token',$token->token)
|
|
->update($data);
|
|
return AdminToken::find($data['token']);
|
|
}
|
|
}
|
|
|
|
public static function queryToken(string $token, int $expired = 1800)
|
|
{
|
|
return AdminToken::where('token', $token)
|
|
->where('expired_at', '>', date('Y-m-d H:i:s', time()))->find();
|
|
}
|
|
|
|
public static function updateToken(string $token, int $expired = 1800)
|
|
{
|
|
return AdminToken::update(
|
|
['expired_at' => date('Y-m-d H:i:s', time() + $expired)],
|
|
['token' => $token]
|
|
);
|
|
}
|
|
} |