| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- /**
- * This file is part of webman.
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the MIT-LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @author walkor<walkor@workerman.net>
- * @copyright walkor<walkor@workerman.net>
- * @link http://www.workerman.net/
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace app\middleware;
- use app\business\UserBusiness;
- use plugin\admin\app\model\User;
- use support\Db;
- use support\Redis;
- use Webman\MiddlewareInterface;
- use Webman\Http\Response;
- use Webman\Http\Request;
- /**
- * Class StaticFile
- * @package app\middleware
- */
- class UserToken implements MiddlewareInterface
- {
- public function process(Request $request, callable $handler): Response
- {
- Db::beginTransaction();
- try {
- $token=$request->header('token');
- if(empty($token)){
- throw new \Exception('请登录!');
- }
- $jwtDecode=jwtDecode($token);
- if(empty($jwtDecode)){
- throw new \Exception('请登录!');
- }
- if(Redis::get(getenv('PROJECTWEB').'_'.$jwtDecode->id) != $token){
- throw new \Exception('当前账号已被其他人强登!');
- }
- $adminUser=UserBusiness::userData(['id'=>$jwtDecode->id]);
- if(empty($adminUser)){
- throw new \Exception('非法操作!');
- }
- if(empty(Redis::get('token_'.$jwtDecode->id))){
- Db::table('wa_users')->where('id',$jwtDecode->id)->update([
- 'last_login' =>bcadd(time(),60)
- ]);
- Redis::setEx('token_'.$jwtDecode->id,50,$jwtDecode->id);
- }
- $request->user_data=$adminUser;
- Db::commit();
- }catch (\Throwable $exception){
- Db::rollBack();
- return error($exception->getMessage(),[],4008);
- }
- return $handler($request);
- }
- }
|