diff --git a/themes/tajian/controller/FrontapiController.php b/themes/tajian/controller/FrontapiController.php index 7f77852..243de46 100644 --- a/themes/tajian/controller/FrontapiController.php +++ b/themes/tajian/controller/FrontapiController.php @@ -753,12 +753,12 @@ eof; foreach($tags as $index => $tag) { $tag = Common::cleanSpecialChars($tag); if (!empty($tag) && !is_numeric($tag)) { - array_push($tags_ok, mb_substr($tag, 0, 5, 'utf-8')); + array_push($tags_ok, mb_substr($tag, 0, 15, 'utf-8')); } } if (empty($tags_ok)) { - $err = "请按规则填写分类:2 - 5 个汉字、数字、英文字符"; + $err = "请按规则填写分类:2 - 15 个汉字、数字、英文字符"; }else { $tags = $tags_ok; } @@ -804,7 +804,7 @@ eof; return $this->renderJson(compact('code', 'msg', 'err')); } - //删除管理 + //删除分类 public function actionDeletetag() { $ip = $this->getUserIp(); $check_time = 120; //2 分钟内 @@ -857,6 +857,94 @@ eof; return $this->renderJson(compact('code', 'msg', 'err')); } + //添加分类 + public function actionAddtag() { + $ip = $this->getUserIp(); + $check_time = 120; //2 分钟内 + $max_time_in_minutes = 15; //最多 15 次 + + $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,请求地址有误'); + } + + + //返回给视图的变量 + $code = 0; + $msg = ''; + $err = ''; + + //用户提交的数据检查 + $postParams = $this->post(); + if (!empty($postParams)) { + $tag_to_add = $this->post('tag', ''); + + if (empty($tag_to_add)) { + $err = "参数错误,缺少tag传参"; + }else { + $tagLen = mb_strlen($tag_to_add, 'utf-8'); + if ($tagLen < 2 || $tagLen > 15) { + $err = '分类名长度不符合规则,请填写 2 - 15 个汉字、数字、英文字符'; + } + } + + if (empty($err)) { //如果数据检查通过,尝试保存 + //获取已有的分类 + $scanner = new DirScanner(); + $scanner->setWebRoot(FSC::$app['config']['content_directory']); + $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3); + + $menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 + $readmeFile = $scanner->getDefaultReadme(); + if (!empty($readmeFile)) { + if (!empty($readmeFile['sort'])) { + $menus_sorted = explode("\n", $readmeFile['sort']); + } + } + + //获取tags分类 + $tags_current = $this->getTags($dirTree); + //排序 + if (!empty($menus_sorted) && !empty($tags_current)) { + $tags_current = $this->sortTags($menus_sorted, $tags_current); + } + //获取只包含分类名的数组 + $tmp_arr = array(); + foreach ($tags_current as $id => $tag) { + array_push($tmp_arr, $tag['name']); + } + + //最多添加 20 个分类 + if (count($tmp_arr) >= 20) { + $err = '最多添加 20 个分类,请合理规划视频分类哦'; + }else { + //保存 + $saved = $this->addTag(ucfirst($tag_to_add)); + if (!empty($saved)) { + $msg = "分类已添加"; + $code = 1; + }else { + $err = '添加分类失败,请稍后重试'; + } + } + } + } + + return $this->renderJson(compact('code', 'msg', 'err')); + } + //TODO: 视频管理 diff --git a/themes/tajian/controller/MyController.php b/themes/tajian/controller/MyController.php index daf54a4..7740163 100644 --- a/themes/tajian/controller/MyController.php +++ b/themes/tajian/controller/MyController.php @@ -73,4 +73,11 @@ Class MyController extends SiteController { return $this->actionIndex($viewName, $defaultTitle); } + //添加分类 + public function actionAddtag() { + $defaultTitle = "添加分类"; + $viewName = 'tag_new'; + return $this->actionIndex($viewName, $defaultTitle); + } + } \ No newline at end of file diff --git a/themes/tajian/controller/SiteController.php b/themes/tajian/controller/SiteController.php index cb2ebec..825cae6 100644 --- a/themes/tajian/controller/SiteController.php +++ b/themes/tajian/controller/SiteController.php @@ -247,6 +247,37 @@ Class SiteController extends Controller { return $done; } + //添加分类 + protected function addTag($tag) { + $done = false; + + try { + $rootDir = FSC::$app['config']['content_directory']; + $tagSaveDirName = str_replace('/', '', FSC::$app['config']['tajian']['tag_dir']); + + $tagFile = "{$rootDir}{$tagSaveDirName}/{$tag}.txt"; + if (file_exists($tagFile) == false) { + touch($tagFile); + } + + //更新排序文件 + $sortFile = "{$rootDir}README_sort.txt"; + if (file_exists($sortFile)) { + $content = file_get_contents($sortFile); + $content = "{$content}\n{$tag}"; + file_put_contents($sortFile, $content); + }else { + file_put_contents($sortFile, $tag); + } + + $done = true; + }catch(Exception $e) { + $this->logError("Add tag {$tag} failed: " . $e->getMessage()); + } + + return $done; + } + protected function getNickname($readmeFile) { $nickname = ''; diff --git a/themes/tajian/views/my/index.php b/themes/tajian/views/my/index.php index 4bdb038..250bcb6 100644 --- a/themes/tajian/views/my/index.php +++ b/themes/tajian/views/my/index.php @@ -22,7 +22,7 @@ if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['use diff --git a/themes/tajian/views/my/tag_new.php b/themes/tajian/views/my/tag_new.php new file mode 100644 index 0000000..e15a6ae --- /dev/null +++ b/themes/tajian/views/my/tag_new.php @@ -0,0 +1,30 @@ +
+
+
+ + +

说明:
最多添加 20 个分类,如果以英文开头,首字母请大写。

+
+
+ + + + 返回 + +
+
+
\ No newline at end of file diff --git a/themes/tajian/views/my/tags.php b/themes/tajian/views/my/tags.php index fb94e59..7b344e6 100644 --- a/themes/tajian/views/my/tags.php +++ b/themes/tajian/views/my/tags.php @@ -34,7 +34,7 @@ eof;

说明:
- 分类名请填 2 - 5 个汉字、数字、英文字符; + 分类名请填 2 - 15 个汉字、数字、英文字符;
点击上下箭头图标改变分类顺序,删除某个分类并不会删除这个分类里的视频。

diff --git a/www/js/tajian.js b/www/js/tajian.js index a2f53c3..55cf4ef 100644 --- a/www/js/tajian.js +++ b/www/js/tajian.js @@ -10,6 +10,7 @@ var taJian = { setNickname: '/frontapi/setnickname', //设置昵称 saveTags: '/frontapi/savetags', //保存分类 deleteTag: '/frontapi/deletetag', //删除分类 + addTag: '/frontapi/addtag', //添加分类 sendSmsCode: '/frontapi/sendsmscode', //发送短信验证码 register: '/frontapi/createuser', //注册 login: '/frontapi/loginuser' //登入 @@ -434,6 +435,7 @@ if ($('#tags_form').get(0)) { publicAjax(taJian.apis.deleteTag, 'POST', datas, function (data) { if (data.code == 1) { current_el.remove(); + location.reload(); } else { alert(data.err); } @@ -457,7 +459,7 @@ if ($('#tags_form').get(0)) { var allTagsOk = true, tagName = ''; for (var index=0;index 5 || isNaN(tagName) == false) { + if (!tagName || tagName.length < 2 || tagName.length > 15 || isNaN(tagName) == false) { allTagsOk = false; break; } @@ -497,4 +499,43 @@ if ($('#tags_form').get(0)) { }); } +// tag分类管理 +if ($('#tag_new_form').get(0)) { + $('#tag_new_form .jsbtn').click(function(e) { + e.preventDefault(); + + var tag = $('#tag_new_form input[name=tag]').val(); + + if (!tag || tag.length < 2 || tag.length > 15 || isNaN(tag) == false) { + alert('请按规则填写分类名称!'); + 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 = { + 'tag': tag + }; + publicAjax(taJian.apis.addTag, 'POST', datas, function (data) { + btLoading.addClass('elementNone'); + bt.prop('disabled', false); + btText.text('保存'); + if (data.code == 1) { + alert(data.msg); + location.reload(); + } else { + alert(data.err); + } + }, function (jqXHR, textStatus, errorThrown) { + bt.prop('disabled', false); + btText.text('保存'); + btLoading.addClass('elementNone'); + alert('网络请求失败,请重试。'); + }); + }); +} + })();