From f2c0efbe3bbc1fd30180ca7b0fadd5be3295fb34 Mon Sep 17 00:00:00 2001 From: filesite Date: Wed, 5 Jun 2024 17:34:38 +0800 Subject: [PATCH] improve user dir map get and set --- plugins/Common.php | 73 ++++++++++--------- .../tajian/controller/FrontapiController.php | 2 +- themes/tajian/controller/MyController.php | 6 +- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/plugins/Common.php b/plugins/Common.php index b74a25f..9402c39 100644 --- a/plugins/Common.php +++ b/plugins/Common.php @@ -50,35 +50,22 @@ Class Common { //保存用户多收藏夹目录映射配置 public static function saveUserDirMap($cellphone, $username, $new_dir) { - $tajian_user_map = FSC::$app['config']['tajian_user_map']; - if (empty($tajian_user_map)) { - $tajian_user_map = array(); - $tajian_user_map[$cellphone] = array($new_dir); - }else { - $map = $tajian_user_map[$cellphone]; - if (empty($map)) { //如果之前没有配置过 - $map = array(); - - $defaultDir = self::getUserId($cellphone); //先获取用户自己的目录 - if (self::existUserDataDir($defaultDir, $username)) { - array_push($map, $defaultDir); - } - - if ($new_dir != $defaultDir) { - array_push($map, $new_dir); - } - }else if (is_string($map)) { //如果有配置过字符串格式的单个目录 - $old = $map; - $map = array($old, $new_dir); - }else if (is_array($map) && !in_array($new_dir, $map)) { - array_push($map, $new_dir); - } + $my_user_map = self::getMyDirs($cellphone, $username); + array_push($my_user_map, $new_dir); - $tajian_user_map[$cellphone] = $map; + $my_id = self::getUserId($cellphone); + $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; + $rootDir = str_replace("/{$username}", "/{$my_id}", $rootDir); //获取自己的目录 + if (!is_dir($rootDir)) { + $my_first_id = self::getMappedUsername($cellphone); + $rootDir = str_replace("/{$my_id}", "/{$my_first_id}", $rootDir); //获取自己的目录 } - $cache_filename = __DIR__ . '/../runtime/custom_config_usermap.json'; - $saved = file_put_contents($cache_filename, json_encode(compact('tajian_user_map'), JSON_PRETTY_PRINT)); + $saved = false; + if (is_dir($rootDir)) { + $cache_filename = "{$rootDir}/custom_config_usermap.json"; + $saved = file_put_contents($cache_filename, json_encode($my_user_map, JSON_PRETTY_PRINT)); + } return $saved === false ? false : true; } @@ -236,19 +223,35 @@ Class Common { return $username; } - public static function getMyDirs($cellphone){ - $userDirs = array(); + //从自己的目录里获取收藏夹映射关系 + //返回:数组 + public static function getMyDirs($cellphone, $username){ + $map = array(); - $user_map = FSC::$app['config']['tajian_user_map']; - if (!empty($user_map[$cellphone])) { - if (is_string($user_map[$cellphone])) { - array_push($userDirs, $user_map[$cellphone]); - }else if (is_array($user_map[$cellphone])) { - $userDirs = $user_map[$cellphone]; + $my_id = self::getUserId($cellphone); + $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; + $rootDir = str_replace("/{$username}", "/{$my_id}", $rootDir); //获取自己的目录 + if (!is_dir($rootDir)) { + $my_first_id = self::getMappedUsername($cellphone); + $rootDir = str_replace("/{$my_id}", "/{$my_first_id}", $rootDir); //获取自己的目录 + } + + if (is_dir($rootDir)) { + $cache_filename = "{$rootDir}/custom_config_usermap.json"; + if (file_exists($cache_filename)) { + $mapContent = file_get_contents($cache_filename); + $map = json_decode($mapContent, true); } } - return $userDirs; + //跟公用配置合并 + $tajian_user_map = FSC::$app['config']['tajian_user_map']; + if (!empty($tajian_user_map[$cellphone])) { + $map = is_array($tajian_user_map[$cellphone]) ? + array_merge($map, $tajian_user_map[$cellphone]) : array_push($map, $tajian_user_map[$cellphone]); + } + + return array_unique($map); } public static function getNicknameByDir($dir, $username){ diff --git a/themes/tajian/controller/FrontapiController.php b/themes/tajian/controller/FrontapiController.php index c63c49b..ee77d69 100644 --- a/themes/tajian/controller/FrontapiController.php +++ b/themes/tajian/controller/FrontapiController.php @@ -1233,7 +1233,7 @@ eof; //已经创建的收藏夹数量检查 //每个手机号最多创建 20 个收藏夹 $max_num = !empty(FSC::$app['config']['tajian']['max_dir_num']) ? FSC::$app['config']['tajian']['max_dir_num'] : 10; - $myDirs = Common::getMyDirs($loginedUser['cellphone']); + $myDirs = Common::getMyDirs($loginedUser['cellphone'], $loginedUser['username']); if (count($myDirs) >= $max_num) { $err = "你已经创建了 {$max_num} 个账号,已达到最大数量"; }else { diff --git a/themes/tajian/controller/MyController.php b/themes/tajian/controller/MyController.php index 0d7b721..29d3f25 100644 --- a/themes/tajian/controller/MyController.php +++ b/themes/tajian/controller/MyController.php @@ -21,7 +21,7 @@ Class MyController extends SiteController { //账号切换支持 $goDir = $this->get('dir', ''); if (!empty($goDir) && !empty($loginedUser['cellphone'])) { - $myDirs = Common::getMyDirs($loginedUser['cellphone']); + $myDirs = Common::getMyDirs($loginedUser['cellphone'], $loginedUser['username']); if (in_array($goDir, $myDirs)) { Common::switchUserDir($goDir); return $this->redirect("/{$goDir}/my/"); @@ -130,7 +130,7 @@ Class MyController extends SiteController { $loginedUser = Common::getUserFromSession(); if (!empty($loginedUser['cellphone'])) { - $myDirs = Common::getMyDirs($loginedUser['cellphone']); + $myDirs = Common::getMyDirs($loginedUser['cellphone'], $loginedUser['username']); if (!empty($myDirs)) { foreach($myDirs as $dir) { $myNicks[$dir] = Common::getNicknameByDir($dir, $loginedUser['username']); @@ -164,7 +164,7 @@ Class MyController extends SiteController { $loginedUser = Common::getUserFromSession(); if (!empty($loginedUser['cellphone'])) { - $myDirs = Common::getMyDirs($loginedUser['cellphone']); + $myDirs = Common::getMyDirs($loginedUser['cellphone'], $loginedUser['username']); if (!empty($myDirs)) { foreach($myDirs as $dir) { $myNicks[$dir] = Common::getNicknameByDir($dir, $loginedUser['username']);