123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace app\common\model\base\user;
- /**
- * @title:
- * @Description:
- * @Author: goldenrock 112049337@qq.com
- * @Date: 2024-08-24 09:40:45
- * @LastEditTime: 2024-12-02 14:49:10
- * @LastEditors: goldenrock
- * @FilePath: \OA_hbdrwhe:\HBDRWHCODE\JindDongFang_platform\app\common\model\base\user\Token.php
- */
- /**
- * 用户TOKEN模型
- */
- use app\common\model\base\org\OrgRole;
- class Token
- {
- /**
- * @title 检查TOKEN有效性
- * @param {string} {token} {} {token字符串}
- */
- static public function checktoken($token = '')
- {
- $info = cache('TOKENUSER' . $token);
- return !empty($info);
- }
- /**
- * @title 更新TOKEN
- * @param{int} {uid} {} {用户ID}
- * @param{string} {role_code} {登录角色码}
- * @param{int} {org_id} {登录单位}
- */
- static public function updatetoken($uid, $org_id, $role_code)
- {
- $timeout = time() + 86400;
- $where = [];
- $where[] = ['uid', '=', $uid];
- $uInfo = User::where('user_id', $uid)->find()->toArray();
- $role = OrgRole::where('org_id', $org_id)->where('code', $role_code)->with(['org'])->find();
- $role = $role ? $role->toArray() : null;
- $uInfo['role'] = $role;
- $uInfo['role_id'] = $role ? $role['role_id'] : 0;
- $uInfo['role_name'] = $role ? $role['name'] : '';
- $uInfo['role_code'] = $role_code;
- $uInfo['org_id'] = $org_id;
- $uInfo['org_name'] = $role ? $role['org']['name'] : '';
- unset($uInfo['org']);
- $token = self::createtoken(json_encode($uInfo));
- $info = [
- 'uid' => $uid,
- 'token' => $token,
- 'timeout' => $timeout,
- 'user' => $uInfo,
- 'user_id' => $uid
- ];
- cache('TOKENUSER' . $token, $info, 86400);
- return $token;
- }
- /**
- * token字符串不变的情况下,更新token对应缓存的用户信息
- * @desc 用于修改用户信息后,同时更新缓存中的用户信息
- */
- static public function updateTokenUser(string $token)
- {
- $oldUser = self::tokenUser($token);
- $uid = $oldUser['user_id'];
- $role_code = $oldUser['role_code'];
- $org_id = $oldUser['org_id'];
- $timeout = time() + 86400;
- $where = [];
- $where[] = ['uid', '=', $uid];
- $uInfo = User::where('user_id', $uid)->find()->toArray();
- $role = OrgRole::where('org_id', $org_id)->where('code', $role_code)->with(['org'])->find();
- $role = $role ? $role->toArray() : null;
- $uInfo['role'] = $role;
- $uInfo['role_id'] = $role ? $role['role_id'] : 0;
- $uInfo['role_name'] = $role ? $role['name'] : '';
- $uInfo['role_code'] = $role_code;
- $uInfo['org_id'] = $org_id;
- $uInfo['org_name'] = $role ? $role['org']['name'] : '';
- unset($uInfo['org']);
- $info = [
- 'uid' => $uid,
- 'token' => $token,
- 'timeout' => $timeout,
- 'user' => $uInfo,
- 'user_id' => $uid
- ];
- cache('TOKENUSER' . $token, $info, 86400);
- }
- static public function getAdminTokenUser($uid)
- {
- $timeout = time() + 86400;
- $uInfo = User::where('user_id', $uid)->find();
- $uInfo = $uInfo ? $uInfo->toArray() : null;
- if(!$uInfo) return null;
- $token = self::createtoken(json_encode($uInfo));
- $info = [
- 'uid' => $uInfo['user_id'],
- 'token' => $token,
- 'timeout' => $timeout,
- 'user' => $uInfo,
- 'user_id' => $uid
- ];
- cache('TOKENUSER' . $token, $info, 86400);
- return $token;
- }
- /**
- * 使token失效
- * @param {string} {token} {token字符串}
- * @desc 常用于退出登录时
- */
- static public function losetoken($token)
- {
- cache('TOKENUSER' . $token, null);
- }
- /**
- * 生成TOKEN
- */
- static private function createtoken($string)
- {
- return md5($string . time() . rand(10000000, 99999999));
- }
- /**
- * 获取token用户
- * @param {string} {token} {token字符串}
- * 用于获取当前登录用户的信息
- */
- static public function tokenUser(string $token)
- {
- try {
- $tokenUser = cache('TOKENUSER' . $token);
- $user = $tokenUser['user'];
- return $user;
- } catch (\Exception $e) {
- WLog('GETTOKENUSER', $e->getFile() . $e->getLine() . $e->getMessage() . ",TOKEN:$token");
- return null;
- }
- }
- }
|