| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Exceptions\Api\VerifySignException;
- use App\Exceptions\Api\LoginException;
- use App\Exceptions\Api\ApiException;
- use App\Facades\Servers\Encrypts\AccessToken;
- use App\Http\Controllers\Controller;
- use App\Models\Api\Personnel\Employee as EmployeeModel;
- use App\Http\Requests\Manager\Login as Request;
- /**
- * 接口控制器
- * @author 唐远望
- * @version 1.0
- * @date 2025-12-09
- *
- */
- class Api extends Controller
- {
- protected $token;
- /**
- * $prefix表前缀
- */
- public function __construct(Request $request)
- {
- $this->token = $request->input('access_token_api', '');
- }
- /**
- * 登录验证
- * @author 唐远望
- * @version 1.0
- * @date 2025-12-09
- *
- */
- public function checkLogin()
- {
- // 获取登录结果
- // 解码
- $userInfo = AccessToken::decode($this->token);
- // 判断登录时效
- if (isset($userInfo['error'])) throw new LoginException("login_error");
- if ($userInfo['expire'] < time()) throw new LoginException("login_exprie");
- // 获取就得令牌
- $oldToken = (new EmployeeModel())->getLogin($userInfo['uid'], 'api');
- if ($oldToken != md5($this->token)) throw new LoginException("login_error");
- // 追加入
- return $userInfo;
- }
- /**
- * 登录验证
- * @author 唐远望
- * @version 1.0
- * @date 2025-12-09
- *
- */
- public function getUid()
- {
- // 尝试执行
- try {
- // 解码
- $decode = AccessToken::decode($this->token);
- // 错误返回
- if (isset($decode['error'])) return 0;
- // 如果没有过期时间,过期处理
- if (empty($decode['expire'])) return 0;
- // 如果过期,过期处理
- if ($decode['expire'] <= time()) return 0;
- // 返回用户ID
- return $decode['uid'];
- } catch (\Throwable $th) {
- // 返回0
- return 0;
- }
- }
- }
|