getMessage()); } return true; } //根据手机号码获取用户名ID //规则:前6位对 97 求余数,再拼接后5位 public static function getUserId($cellphone){ $user_id = $cellphone; $prefix = substr($cellphone, 0, 6); $prefix = str_pad( (int)$prefix % 97, 2, '0', STR_PAD_LEFT); $suffix = substr($cellphone, -5); return "{$prefix}{$suffix}"; } //根据手机号码获取映射的用户名 public static function getMappedUsername($cellphone){ $username = $cellphone; if (!empty(FSC::$app['config']['tajia_user_map']) && !empty(FSC::$app['config']['tajia_user_map'][$username])) { $username = FSC::$app['config']['tajia_user_map'][$username]; }else { $username = self::getUserId($cellphone); } return $username; } //判断用户数据目录是否存在 public static function getUserDataDir($cellphone) { $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $username = self::getMappedUsername($cellphone); $userDir = "{$rootDir}{$username}"; return is_dir($userDir) ? $userDir : false; } //检查朋友的邀请码是否存在 public static function existFriendsCode($code) { if (self::isFriendsCode($code) == false) {return false;} if (!empty(FSC::$app['config']['default_friends_code']) && $code == FSC::$app['config']['default_friends_code']) { return true; } $logDir = __DIR__ . '/../runtime/friendscode/'; $logFilename = "{$logDir}{$code}.log"; return file_exists($logFilename); } //用户注册或登录成功时保存用户信息到session //login_time, username, friends_code //增加账号映射支持,配置项:tajia_user_map public static function saveUserIntoSession($cellphone, $friends_code = '') { if(session_status() !== PHP_SESSION_ACTIVE) { session_start(); } $login_time = time(); $username = self::getMappedUsername($cellphone); if (empty($friends_code) && !empty($_COOKIE['friends_code'])) { $friends_code = $_COOKIE['friends_code']; } $_SESSION['login_time'] = $login_time; $_SESSION['username'] = $username; $_SESSION['friends_code'] = $friends_code; //cookie保存 1 年 if (!empty($friends_code)) { setcookie('friends_code', $friends_code, $login_time + 86400*365, '/'); } return compact('login_time', 'username', 'friends_code'); } //从session里获取用户数据 public static function getUserFromSession() { if(session_status() !== PHP_SESSION_ACTIVE) { session_start(); } $login_time = !empty($_SESSION['login_time']) ? $_SESSION['login_time'] : 0; $username = !empty($_SESSION['username']) ? $_SESSION['username'] : ''; $friends_code = !empty($_SESSION['friends_code']) ? $_SESSION['friends_code'] : ''; //尝试从cookie中获取 if (empty($friends_code) && !empty($_COOKIE['friends_code'])) { $friends_code = $_COOKIE['friends_code']; } return compact('login_time', 'username', 'friends_code'); } public static function logoutUserFromSession() { if(session_status() !== PHP_SESSION_ACTIVE) { session_start(); } return session_destroy(); } }