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; } } }