123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace App\Admin\Http\Logics;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Cache;
- use function App\Http\_header;
- use function App\Http\_microtime;
- use function App\Http\success;
- use Illuminate\Support\Facades\Log;
- class AppUserLogic
- {
- const CACHE_PREFIX = "TM_USER_CACHE_";
- const TOKEN_CACHE_PREFIX = "TM_USER_TOKEN_";
- public static function token($id, $user)
- {
- $token = uniqid(_microtime(13), true);
- $user = $user->toArray();
- $user = Arr::except($user, 'created_at');
- $user = Arr::except($user, 'updated_at');
- $user = Arr::except($user, 'deleted_at');
- $user = Arr::except($user, 'status');
- $prefix = "USER_CACHE_" . $id;
- Cache::put($token, $user, 8640000 * 1); //1天
- if (Cache::has($prefix)) {
- $oldToken = Cache::get($prefix);
- Cache::forget($oldToken);
- }
- Cache::put($prefix, $token, 86400 * 8);
- return success(['token' => $token, 'user' => $user], "登录成功");
- }
- public static function updateToekn($user)
- {
- $user = $user->toArray();
- $user = Arr::except($user, 'created_at');
- $user = Arr::except($user, 'updated_at');
- $user = Arr::except($user, 'deleted_at');
- $user = Arr::except($user, 'status');
- $token = _header('authorization');
- Cache::put($token, $user, 8640000 * 1); //1天
- }
- /**
- * 校验token
- *
- * @param $token
- *
- * @return mixed
- */
- public static function validToken($token = null,$platform = null): mixed
- {
- $token = $token ?: _header('authorization');
- if (empty($token)) {
- return false;
- }
-
- $user = Cache::get($token);
- if (empty($user)) {
- return false;
- }
-
- $userId = $user['id'];
- $platform = $platform ?: _header('platform');
- if (empty($platform)) {
- return false;
- }
- if($platform == 1){
- $cacheKeyPrefix = "ADMIN_CACHE_";
- }
- if($platform == 2 || $platform == 3){
- $cacheKeyPrefix = "FONT_CACHE_";
- }
- $cacheKey = $cacheKeyPrefix . $userId;
-
- if (Cache::get($cacheKey) != $token) {
- return false;
- }
-
- return $user;
- }
- public static function delToken($id)
- {
- $prefix = "USER_CACHE_" . $id;
- if (Cache::has($prefix)) {
- $oldToken = Cache::get($prefix);
- Cache::forget($oldToken);
- }
- return success([], "退出成功");
- }
- }
|