123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace app\parent;
- use app\BaseController;
- use app\common\model\base\user\JwtToken;
- use app\common\model\guardian\JdfGuardian;
- class ParentBase extends BaseController
- {
- /** 用户信息 */
- protected $userinfo;
- protected $org;
- /** 无需登录的方法 */
- protected $noNeedLogin = [];
- // 初始化方法
- public function initialize()
- {
- parent::initialize();
- $this->token = $_SERVER["HTTP_AUTHORIZATION"] ?? $_SERVER['HTTP_TOKEN'] ?? $this->request->param('token');
- $this->action = strtolower($this->app->request->action(true));
- $this->checkLogin();
- }
- /**
- * @title:[验证登录]
- * @return json|{*}
- * @Author: byl
- * @Date:2023/8/9 16:31
- */
- private function checkLogin()
- {
- if ($this->token) {
- $checkTokenRes = config('develop.jwt_open') ? JwtToken::Decode($this->token) : $this->checkToken($this->token);
- } else {
- $checkTokenRes = [
- 'code' => 401,
- 'msg' => '用户暂未登录(token为空)',
- ];
- }
- //无需登录--需登录或者需要权限的请求都要验证登录
- if ($checkTokenRes['code'] != 1 && !in_array($this->action, $this->noNeedLogin)) {
- return Result($checkTokenRes['code'] ?? -1, $checkTokenRes['msg'] ?? '',);
- // throw new \Exception($checkTokenRes['msg'] ?? '', $checkTokenRes['code'] ?? -1);
- } else {
- $this->userinfo = $checkTokenRes['data'] ?? [];
- $this->org = $this->userinfo['org'] ?? null;
- }
- // //无需登录--需登录或者需要权限的请求都要验证登录
- // if (!in_array($this->action, $this->noNeedLogin)) {
- // $checkTokenRes = config('develop.jwt_open') ? JwtToken::Decode($this->token) : $this->checkToken($this->token);
- // if ($checkTokenRes['code'] != 1) {
- // throw new \Exception($checkTokenRes['msg']??"", $checkTokenRes['code']??-1);
- // } else {
- // $this->userinfo = $checkTokenRes['data'];
- // $this->org = $this->userinfo['org'] ?? null;
- // }
- // }
- }
- /** 通用验证TOKEN是否有效 */
- protected function checkToken($token = "")
- {
- //检查token是否有效
- if (!self::checkUserToken($token)) {
- return Result(-1, "登录失效,请重新登录");
- }
- $user = self::tokenUser($token);
- if (!$user) {
- return Result(0, "未找到用户");
- } elseif ($user->status <> 1) {
- return Result(0, "您的帐号已被禁用,请联系管理员");
- } else {
- return Result(1, "验证成功", $user);
- }
- }
- //更新TOKEN
- static public function updatetoken($uid)
- {
- $timeout = time() + 86400;
- $uInfo = JdfGuardian::withoutField('delete_at,password,salt')->find($uid);
- $token = self::createtoken(json_encode($uInfo));
- $info = [
- 'uid' => $uid,
- 'token' => $token,
- 'timeout' => $timeout,
- 'user' => $uInfo,
- 'user_id' => $uid
- ];
- cache('PARENT' . $token, $info, 86400);
- return $token;
- }
- //生成TOKEN
- static private function createtoken($string)
- {
- return md5($string . time() . rand(10000000, 99999999));
- }
- //检查TOKEN有效性
- static public function checkUserToken($token = '')
- {
- $info = cache('PARENT' . $token);
- return !empty($info);
- }
- // 获取token用户
- static public function tokenUser($token)
- {
- try {
- $tokenUser = cache('PARENT' . $token);
- $user = $tokenUser['user'];
- return $user;
- } catch (\Exception $e) {
- WLog('GETTOKENUSER', $e->getFile() . $e->getLine() . $e->getMessage() . ",TOKEN:$token");
- return null;
- }
- }
- }
|