Browse Source

improve user dir map get and set

master
filesite 6 months ago
parent
commit
f2c0efbe3b
  1. 71
      plugins/Common.php
  2. 2
      themes/tajian/controller/FrontapiController.php
  3. 6
      themes/tajian/controller/MyController.php

71
plugins/Common.php

@ -50,36 +50,23 @@ Class Common {
//保存用户多收藏夹目录映射配置 //保存用户多收藏夹目录映射配置
public static function saveUserDirMap($cellphone, $username, $new_dir) { public static function saveUserDirMap($cellphone, $username, $new_dir) {
$tajian_user_map = FSC::$app['config']['tajian_user_map']; $my_user_map = self::getMyDirs($cellphone, $username);
if (empty($tajian_user_map)) { array_push($my_user_map, $new_dir);
$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) { $my_id = self::getUserId($cellphone);
array_push($map, $new_dir); $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
} $rootDir = str_replace("/{$username}", "/{$my_id}", $rootDir); //获取自己的目录
}else if (is_string($map)) { //如果有配置过字符串格式的单个目录 if (!is_dir($rootDir)) {
$old = $map; $my_first_id = self::getMappedUsername($cellphone);
$map = array($old, $new_dir); $rootDir = str_replace("/{$my_id}", "/{$my_first_id}", $rootDir); //获取自己的目录
}else if (is_array($map) && !in_array($new_dir, $map)) {
array_push($map, $new_dir);
} }
$tajian_user_map[$cellphone] = $map; $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));
} }
$cache_filename = __DIR__ . '/../runtime/custom_config_usermap.json';
$saved = file_put_contents($cache_filename, json_encode(compact('tajian_user_map'), JSON_PRETTY_PRINT));
return $saved === false ? false : true; return $saved === false ? false : true;
} }
@ -236,19 +223,35 @@ Class Common {
return $username; 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']; $my_id = self::getUserId($cellphone);
if (!empty($user_map[$cellphone])) { $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
if (is_string($user_map[$cellphone])) { $rootDir = str_replace("/{$username}", "/{$my_id}", $rootDir); //获取自己的目录
array_push($userDirs, $user_map[$cellphone]); if (!is_dir($rootDir)) {
}else if (is_array($user_map[$cellphone])) { $my_first_id = self::getMappedUsername($cellphone);
$userDirs = $user_map[$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);
}
}
//跟公用配置合并
$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 $userDirs; return array_unique($map);
} }
public static function getNicknameByDir($dir, $username){ public static function getNicknameByDir($dir, $username){

2
themes/tajian/controller/FrontapiController.php

@ -1233,7 +1233,7 @@ eof;
//已经创建的收藏夹数量检查 //已经创建的收藏夹数量检查
//每个手机号最多创建 20 个收藏夹 //每个手机号最多创建 20 个收藏夹
$max_num = !empty(FSC::$app['config']['tajian']['max_dir_num']) ? FSC::$app['config']['tajian']['max_dir_num'] : 10; $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) { if (count($myDirs) >= $max_num) {
$err = "你已经创建了 {$max_num} 个账号,已达到最大数量"; $err = "你已经创建了 {$max_num} 个账号,已达到最大数量";
}else { }else {

6
themes/tajian/controller/MyController.php

@ -21,7 +21,7 @@ Class MyController extends SiteController {
//账号切换支持 //账号切换支持
$goDir = $this->get('dir', ''); $goDir = $this->get('dir', '');
if (!empty($goDir) && !empty($loginedUser['cellphone'])) { if (!empty($goDir) && !empty($loginedUser['cellphone'])) {
$myDirs = Common::getMyDirs($loginedUser['cellphone']); $myDirs = Common::getMyDirs($loginedUser['cellphone'], $loginedUser['username']);
if (in_array($goDir, $myDirs)) { if (in_array($goDir, $myDirs)) {
Common::switchUserDir($goDir); Common::switchUserDir($goDir);
return $this->redirect("/{$goDir}/my/"); return $this->redirect("/{$goDir}/my/");
@ -130,7 +130,7 @@ Class MyController extends SiteController {
$loginedUser = Common::getUserFromSession(); $loginedUser = Common::getUserFromSession();
if (!empty($loginedUser['cellphone'])) { if (!empty($loginedUser['cellphone'])) {
$myDirs = Common::getMyDirs($loginedUser['cellphone']); $myDirs = Common::getMyDirs($loginedUser['cellphone'], $loginedUser['username']);
if (!empty($myDirs)) { if (!empty($myDirs)) {
foreach($myDirs as $dir) { foreach($myDirs as $dir) {
$myNicks[$dir] = Common::getNicknameByDir($dir, $loginedUser['username']); $myNicks[$dir] = Common::getNicknameByDir($dir, $loginedUser['username']);
@ -164,7 +164,7 @@ Class MyController extends SiteController {
$loginedUser = Common::getUserFromSession(); $loginedUser = Common::getUserFromSession();
if (!empty($loginedUser['cellphone'])) { if (!empty($loginedUser['cellphone'])) {
$myDirs = Common::getMyDirs($loginedUser['cellphone']); $myDirs = Common::getMyDirs($loginedUser['cellphone'], $loginedUser['username']);
if (!empty($myDirs)) { if (!empty($myDirs)) {
foreach($myDirs as $dir) { foreach($myDirs as $dir) {
$myNicks[$dir] = Common::getNicknameByDir($dir, $loginedUser['username']); $myNicks[$dir] = Common::getNicknameByDir($dir, $loginedUser['username']);

Loading…
Cancel
Save