From 6c1901946a201bb6fe385de69c17ab0b144d39ed Mon Sep 17 00:00:00 2001 From: filesite Date: Mon, 20 May 2024 18:26:04 +0800 Subject: [PATCH] create fav dir done --- conf/app.php | 11 +++ plugins/Common.php | 98 ++++++++++++++++++- .../tajian/controller/FrontapiController.php | 69 +++++++++++++ themes/tajian/views/my/createdir.php | 6 +- themes/tajian/views/my/index.php | 10 +- www/js/tajian.js | 40 ++++++++ 6 files changed, 224 insertions(+), 10 deletions(-) diff --git a/conf/app.php b/conf/app.php index c043a64..7206d91 100644 --- a/conf/app.php +++ b/conf/app.php @@ -64,6 +64,7 @@ $configs = array( 'tag_dir' => 'tags/', //tag分类目录 'task_dir' => 'task/', //分享视频下载任务文件保存目录 'task_log' => 'tasks.log', //分享视频下载任务文件日志文件 + 'max_dir_num' => 20, //一个手机可创建的最大收藏夹数量 'supportedPlatforms' => array( 'B站', '抖音', @@ -155,6 +156,16 @@ if (file_exists($customConfigFile)) { } +//用户管理多账号自定义配置 +$customConfigFile = __DIR__ . "/../runtime/custom_config_usermap.json"; +if (file_exists($customConfigFile)) { + try { + $json = file_get_contents($customConfigFile); + $customConfigs = json_decode($json, true); + $configs = array_merge($configs, $customConfigs); + }catch(Exception $e) {} +} + //VIP用户自定义配置 $customConfigFile = __DIR__ . "/../runtime/custom_config_vip.json"; if (file_exists($customConfigFile)) { diff --git a/plugins/Common.php b/plugins/Common.php index f5ff015..8836ef7 100644 --- a/plugins/Common.php +++ b/plugins/Common.php @@ -10,7 +10,8 @@ Class Common { '&', '<', '>', - '\/', + '/', + '\\', ' ', ';', ';', @@ -47,7 +48,97 @@ Class Common { $logOk = @error_log("{$logTime} invite {$cellphone}\n", 3, "{$logDir}{$friendsLogfile}"); } - //初始化用户数据目录 + //保存用户多收藏夹目录映射配置 + public static function saveUserDirMap($cellphone, $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($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); + } + + $tajian_user_map[$cellphone] = $map; + } + + $cache_filename = __DIR__ . '/../runtime/custom_config_usermap.json'; + file_put_contents($cache_filename, json_encode(compact('tajian_user_map'), JSON_PRETTY_PRINT)); + } + + //获取新收藏夹目录名 + public static function getNewFavDir($cellphone) + { + $new_dir = 2000; //默认从编号2000开始 + + $cache_filename = __DIR__ . '/../runtime/userCustomFavDirs.json'; + if (file_exists($cache_filename)) { + $json = file_get_contents($cache_filename); + $data = json_decode($json, true); + if (!empty($data['dir'])) { + $new_dir = $data['dir'] + 1; + } + } + + return $new_dir; + } + + //老用户创建新的收藏夹 + public static function createNewFavDir($cellphone, $username, $new_dir, $nickname) { + try { + $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; + $rootDir = str_replace("/{$username}", '', $rootDir); //获取当前收藏夹的上一级目录 + + $userDir = "{$rootDir}/{$new_dir}"; //新收藏夹目录 + if (is_dir($userDir)) { //如果已经存在 + return false; + } + + mkdir("{$userDir}/data/", 0755, true); //分享视频目录 + if (!is_dir("{$userDir}/data/")) { + throw new Exception("创建用户数据目录失败,请检查目录 www/" . FSC::$app['config']['content_directory'] . " 权限配置,允许PHP写入"); + } + + mkdir("{$userDir}/tags/", 0700, true); //分类目录 + copy("{$rootDir}README.md", "{$userDir}/README.md"); + copy("{$rootDir}README_title.txt", "{$userDir}/README_title.txt"); + + if (!empty($nickname)) { + file_put_contents("{$userDir}/README_nickname.txt", $nickname); + } + + if (!empty($_COOKIE['friends_code'])) { + $friends_code = $_COOKIE['friends_code']; + file_put_contents("{$userDir}/README_friendscode.txt", $friends_code); + } + + file_put_contents("{$userDir}/README_cellphone.txt", $cellphone); + + //用户新收藏夹创建成功后,保存最新用户创建的收藏夹记录 + $data = array( + 'dir' => $new_dir, + 'update' => time(), + 'lastUser' => $cellphone, + ); + $cache_filename = __DIR__ . '/../runtime/userCustomFavDirs.json'; + file_put_contents($cache_filename, json_encode($data, JSON_PRETTY_PRINT)); + + //保存用户手机和收藏夹映射关系 + self::saveUserDirMap($cellphone, $new_dir); + }catch(Exception $e) { + return false; + } + + return true; + } + + //新用户注册时初始化用户数据目录 public static function initUserData($cellphone, $friends_code = '') { $userDir = self::getUserDataDir($cellphone); if (!empty($userDir)) { @@ -58,6 +149,7 @@ Class Common { $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $username = self::getMappedUsername($cellphone); $userDir = "{$rootDir}{$username}"; + mkdir("{$userDir}/data/", 0755, true); //分享视频目录 if (!is_dir("{$userDir}/data/")) { throw new Exception("创建用户数据目录失败,请检查目录 www/" . FSC::$app['config']['content_directory'] . " 权限配置,允许PHP写入"); @@ -128,7 +220,7 @@ Class Common { public static function getNicknameByDir($dir, $username){ $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; - $dirPath = str_replace($username, $dir, $rootDir); + $dirPath = str_replace("/{$username}", "/{$dir}", $rootDir); $filepath = "{$dirPath}/README_nickname.txt"; $nickname = ''; diff --git a/themes/tajian/controller/FrontapiController.php b/themes/tajian/controller/FrontapiController.php index 9b766c3..8cc8cfe 100644 --- a/themes/tajian/controller/FrontapiController.php +++ b/themes/tajian/controller/FrontapiController.php @@ -1183,5 +1183,74 @@ eof; return $this->renderJson(compact('code', 'msg', 'err')); } + //创建收藏夹 + public function actionCreatedir() { + $ip = $this->getUserIp(); + $check_time = 120; //2 分钟内 + $max_time_in_minutes = 30; //最多 30 次 + + $isUserGotRequestLimit = $this->requestLimit($ip, $max_time_in_minutes, $check_time); + if ($isUserGotRequestLimit) { + $this->logError("Request limit got, ip: {$ip}"); + throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...'); + } + + //只允许修改自己的数据 + $loginedUser = Common::getUserFromSession(); + if (empty($loginedUser['username'])) { + throw new Exception('Oops,你还没登录哦'); + }else if ( + !empty(FSC::$app['config']['multipleUserUriParse']) + && (empty(FSC::$app['user_id']) || FSC::$app['user_id'] != $loginedUser['username']) + ) { + throw new Exception('Oops,请求地址有误'); + } + + //VIP身份判断 + if (empty($loginedUser['cellphone']) || !in_array($loginedUser['cellphone'], FSC::$app['config']['tajian_vip_user'])) { + throw new Exception('Oops,你还不是VIP,请联系首页底部客服邮箱开通。'); + } + + + //返回给视图的变量 + $code = 0; + $msg = ''; + $err = ''; + + //用户提交的数据检查 + $postParams = $this->post(); + if (!empty($postParams)) { + $new_nickname = $this->post('nickname', ''); + + if (empty($new_nickname)) { + $err = "请填写新账号的昵称"; + }else { + $new_nickname = Common::cleanSpecialChars($new_nickname); + } + + if (empty($err)) { //如果数据检查通过,尝试保存 + //已经创建的收藏夹数量检查 + //每个手机号最多创建 20 个收藏夹 + $max_num = !empty(FSC::$app['config']['tajian']['max_dir_num']) ? FSC::$app['config']['tajian']['max_dir_num'] : 10; + $myDirs = Common::getMyDirs($loginedUser['cellphone']); + if (count($myDirs) >= $max_num) { + $err = "你已经创建了 {$max_num} 个账号,已达到最大数量"; + }else { + $new_dir = Common::getNewFavDir($loginedUser['cellphone']); + $saved = Common::createNewFavDir($loginedUser['cellphone'], $loginedUser['username'], $new_dir, $new_nickname); + + if ($saved !== false) { + $msg = "新账号创建完成"; + $code = 1; + }else { + $err = "{$new_dir} 创建失败,请稍后重试"; + } + } + } + } + + return $this->renderJson(compact('code', 'msg', 'err')); + } + } diff --git a/themes/tajian/views/my/createdir.php b/themes/tajian/views/my/createdir.php index f0778ff..08e0713 100644 --- a/themes/tajian/views/my/createdir.php +++ b/themes/tajian/views/my/createdir.php @@ -4,6 +4,8 @@ $linkPrefix = ''; if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['user_id'])) { $linkPrefix = '/' . FSC::$app['user_id']; } + +$max_num = !empty(FSC::$app['config']['tajian']['max_dir_num']) ? FSC::$app['config']['tajian']['max_dir_num'] : 10; ?>
diff --git a/www/js/tajian.js b/www/js/tajian.js index 4a71e75..ebff657 100644 --- a/www/js/tajian.js +++ b/www/js/tajian.js @@ -13,6 +13,7 @@ var taJian = { addTag: '/frontapi/addtag', //添加分类 updateFavsTag: '/frontapi/updatefavstag', //修改视频的分类 deleteFav: '/frontapi/deletefav', //删除收藏的视频 + createNewFav: '/frontapi/createdir', //创建新的收藏夹 sendSmsCode: '/frontapi/sendsmscode', //发送短信验证码 register: '/frontapi/createuser', //注册 login: '/frontapi/loginuser' //登入 @@ -620,4 +621,43 @@ if ($('#share_form').get(0)) { }); } +// 创建新收藏夹账号 +if ($('#dir_new_form').get(0)) { + $('#dir_new_form .jsbtn').click(function(e) { + e.preventDefault(); + + var nickname = $('input[name=nickname]').val(); + + if (!nickname) { + alert('请填写 2 - 5 个汉字的昵称!'); + return false; + } + + var bt = $(this), btLoading = bt.children('.bt_class_JS'), btText = bt.children('.bt_text_JS'); + btLoading.removeClass('elementNone'); + bt.prop('disabled', true); + btText.text('提交中...'); + + var datas = { + 'nickname': nickname + }; + publicAjax(taJian.apis.createNewFav, 'POST', datas, function (data) { + btLoading.addClass('elementNone'); + bt.prop('disabled', false); + btText.text('保存'); + if (data.code == 1) { + //alert(data.msg); + location.href = '/' + current_user_id + '/my/dirs'; + } else { + alert(data.err); + } + }, function (jqXHR, textStatus, errorThrown) { + bt.prop('disabled', false); + btText.text('保存'); + btLoading.addClass('elementNone'); + alert('网络请求失败,请重试。'); + }); + }); +} + })();