Browse Source

tag manage done

master
filesite 7 months ago
parent
commit
17ff18da78
  1. 24
      lib/DirScanner.php
  2. 143
      themes/tajian/controller/FrontapiController.php
  3. 86
      themes/tajian/controller/SiteController.php
  4. 9
      themes/tajian/views/my/tags.php
  5. 32
      www/js/tajian.js

24
lib/DirScanner.php

@ -285,10 +285,10 @@ Class DirScanner { @@ -285,10 +285,10 @@ Class DirScanner {
//try to find the md file
$targetFile = '';
$targetFile_md = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.md', $realpath);
$targetFile_mp4 = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.mp4', $realpath);
$targetFile_m3u8 = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.m3u8', $realpath);
$targetFile_url = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.url', $realpath);
$targetFile_md = preg_replace('/_?[a-z0-9]+\.txt$/U', '.md', $realpath);
$targetFile_mp4 = preg_replace('/_?[a-z0-9]+\.txt$/U', '.mp4', $realpath);
$targetFile_m3u8 = preg_replace('/_?[a-z0-9]+\.txt$/U', '.m3u8', $realpath);
$targetFile_url = preg_replace('/_?[a-z0-9]+\.txt$/U', '.url', $realpath);
if (file_exists($targetFile_md)) {
$targetFile = $targetFile_md;
}else if (file_exists($targetFile_mp4)) {
@ -299,7 +299,7 @@ Class DirScanner { @@ -299,7 +299,7 @@ Class DirScanner {
$targetFile = $targetFile_url;
}
if (!empty($targetFile)) {
if (!empty($targetFile) && $targetFile != $realpath) {
$fileId = $this->getId($targetFile);
if (empty($this->scanResults[$fileId])) {
$ext['id'] = $fileId;
@ -312,7 +312,7 @@ Class DirScanner { @@ -312,7 +312,7 @@ Class DirScanner {
}
}else {
//try to merge to the parent directory
$targetDir = preg_replace('/\/[a-z0-9]+\.txt$/i', '', $realpath);
$targetDir = preg_replace('/\/[a-z0-9]+\.txt$/U', '', $realpath);
if (is_dir($targetDir)) {
$dirId = $this->getId($targetDir);
if (empty($this->scanResults[$dirId])) {
@ -324,6 +324,18 @@ Class DirScanner { @@ -324,6 +324,18 @@ Class DirScanner {
$data = array_merge($data, $ext);
$this->scanResults[$dirId] = $data;
}
}else {
//keep it in files
$fileId = $this->getId($realpath);
if (empty($this->scanResults[$fileId])) {
$ext['id'] = $fileId;
$this->scanResults[$fileId] = $ext;
$data = $ext;
}else {
$data = $this->scanResults[$fileId];
$data = array_merge($data, $ext);
$this->scanResults[$fileId] = $data;
}
}
}

143
themes/tajian/controller/FrontapiController.php

@ -712,9 +712,150 @@ eof; @@ -712,9 +712,150 @@ eof;
return $this->renderJson(compact('code', 'msg', 'err'));
}
//分类管理
public function actionSavetags() {
$ip = $this->getUserIp();
$check_time = 120; //2 分钟内
$max_time_in_minutes = 10; //最多 10 次
$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)) {
$tags = $this->post('tags', '');
if (empty($tags)) {
$err = "请至少保留一个分类";
}else {
$tags_ok = array();
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'));
}
}
if (empty($tags_ok)) {
$err = "请按规则填写分类:2 - 5 个汉字、数字、英文字符";
}else {
$tags = $tags_ok;
}
}
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']);
}
}
//TODO: 分类管理
//获取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']);
}
//保存
$saved = $this->saveTags($tags, $tmp_arr);
if (!empty($saved)) {
$msg = "分类已保存";
$code = 1;
}else {
$err = '分类保存失败,请稍后重试';
}
}
}
return $this->renderJson(compact('code', 'msg', 'err'));
}
//删除管理
public function actionDeletetag() {
$ip = $this->getUserIp();
$check_time = 120; //2 分钟内
$max_time_in_minutes = 10; //最多 10 次
$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_delete = $this->post('tag', '');
if (empty($tag_to_delete)) {
$err = "参数错误,缺少tag传参";
}
if (empty($err)) { //如果数据检查通过,尝试保存
//保存
$saved = $this->deleteTag($tag_to_delete);
if (!empty($saved)) {
$msg = "分类已删除";
$code = 1;
}else {
$err = '分类删除失败,请稍后重试';
}
}
}
return $this->renderJson(compact('code', 'msg', 'err'));
}
//TODO: 视频管理

86
themes/tajian/controller/SiteController.php

@ -30,7 +30,7 @@ Class SiteController extends Controller { @@ -30,7 +30,7 @@ Class SiteController extends Controller {
$scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']);
$dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4);
$dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3);
$scanResults = $scanner->getScanResults();
//获取目录
@ -129,7 +129,7 @@ Class SiteController extends Controller { @@ -129,7 +129,7 @@ Class SiteController extends Controller {
}else {
$tag['name'] = $name;
if ($noFiles == false) {
$tag['files'] = explode("\n", $item);
$tag['files'] = !empty($item) ? explode("\n", trim($item)) : array();
}
}
}
@ -167,6 +167,86 @@ Class SiteController extends Controller { @@ -167,6 +167,86 @@ Class SiteController extends Controller {
return $files;
}
//保存分类,如果tag是纯英文,则自动把首字母改为大写
//@tags 新分类数组
//@tags_current 当前分类数组
protected function saveTags($tags, $tags_current) {
$done = false;
try {
$rootDir = FSC::$app['config']['content_directory'];
$tagSaveDirName = str_replace('/', '', FSC::$app['config']['tajian']['tag_dir']);
//添加新分类
foreach ($tags as $index => $tag) {
//首字母转大写
$tag = ucfirst($tag);
$tags[$index] = $tag;
if (in_array($tag, $tags_current)) {continue;} //忽略已存在的分类
$tagFile = "{$rootDir}{$tagSaveDirName}/{$tag}.txt";
if (file_exists($tagFile) == false) { //添加新分类
touch($tagFile);
}
}
//删除或者改名新分类中被移除的老分类
foreach ($tags_current as $index => $tag) {
if (in_array($tag, $tags)) {continue;} //跳过新分类中保留的
$tagFile = "{$rootDir}{$tagSaveDirName}/{$tag}.txt";
if (file_exists($tagFile)) {
if (!empty($tags[$index])) {
$newTagFile = "{$rootDir}{$tagSaveDirName}/{$tags[$index]}.txt";
rename($tagFile, $newTagFile);
}else {
unlink($tagFile);
}
}
}
//更新排序文件
$sortFile = "{$rootDir}README_sort.txt";
file_put_contents($sortFile, implode("\n", $tags));
$done = true;
}catch(Exception $e) {
$this->logError('Save tags failed: ' . $e->getMessage());
}
return $done;
}
//删除分类
protected function deleteTag($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)) {
unlink($tagFile);
}
//更新排序文件
$sortFile = "{$rootDir}README_sort.txt";
if (file_exists($sortFile)) {
$content = file_get_contents($sortFile);
$content = preg_replace("/{$tag}\n?/", '', $content);
file_put_contents($sortFile, $content);
}
$done = true;
}catch(Exception $e) {
$this->logError("Delete tag {$tag} failed: " . $e->getMessage());
}
return $done;
}
protected function getNickname($readmeFile) {
$nickname = '';
@ -216,7 +296,7 @@ Class SiteController extends Controller { @@ -216,7 +296,7 @@ Class SiteController extends Controller {
$scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']);
$dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4);
$dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 3);
$scanResults = $scanner->getScanResults();
$titles = array();

9
themes/tajian/views/my/tags.php

@ -17,7 +17,7 @@ if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['use @@ -17,7 +17,7 @@ if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['use
foreach($viewData['tags'] as $id => $tag) {
$upIconCls = $index == 0 ? 'hide' : '';
$downIconCls = $index < $tagsTotal - 1 ? '' : 'hide';
$index ++;
echo <<<eof
<div class="mb-3 tag-item">
@ -31,6 +31,13 @@ eof; @@ -31,6 +31,13 @@ eof;
}
?>
<p>
说明:
<br>
分类名请填 2 - 5 个汉字、数字、英文字符;
<br>
点击上下箭头图标改变分类顺序,删除某个分类并不会删除这个分类里的视频。
</p>
<div class="avform_bt vercenter">
<button class="jsbtn" aria-label="保存" type="button">
<div class="loading_bt bt_class_JS elementNone verMiddle">

32
www/js/tajian.js

@ -9,6 +9,7 @@ var taJian = { @@ -9,6 +9,7 @@ var taJian = {
addVideos: '/frontapi/addfav', //添加视频
setNickname: '/frontapi/setnickname', //设置昵称
saveTags: '/frontapi/savetags', //保存分类
deleteTag: '/frontapi/deletetag', //删除分类
sendSmsCode: '/frontapi/sendsmscode', //发送短信验证码
register: '/frontapi/createuser', //注册
login: '/frontapi/loginuser' //登入
@ -427,7 +428,18 @@ if ($('#tags_form').get(0)) { @@ -427,7 +428,18 @@ if ($('#tags_form').get(0)) {
}
if (confirm('确定删除分类“' + current_tag + '”吗?')) {
current_el.remove();
var datas = {
'tag': current_tag
};
publicAjax(taJian.apis.deleteTag, 'POST', datas, function (data) {
if (data.code == 1) {
current_el.remove();
} else {
alert(data.err);
}
}, function (jqXHR, textStatus, errorThrown) {
alert('网络请求失败,请重试。');
});
}
});
@ -442,8 +454,20 @@ if ($('#tags_form').get(0)) { @@ -442,8 +454,20 @@ if ($('#tags_form').get(0)) {
return false;
}
var allTagsOk = true, tagName = '';
for (var index=0;index<tag_els.length;index++) {
tags.push(tag_els[index].value);
tagName = tag_els[index].value;
if (!tagName || tagName.length < 2 || tagName.length > 5 || isNaN(tagName) == false) {
allTagsOk = false;
break;
}
tags.push(tagName);
}
if (!allTagsOk) {
alert('请按规则填写分类名称');
return;
}
var bt = $(this), btLoading = bt.children('.bt_class_JS'), btText = bt.children('.bt_text_JS');
@ -454,14 +478,13 @@ if ($('#tags_form').get(0)) { @@ -454,14 +478,13 @@ if ($('#tags_form').get(0)) {
var datas = {
'tags': tags
};
console.log(datas);
/*
publicAjax(taJian.apis.saveTags, '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);
}
@ -471,7 +494,6 @@ if ($('#tags_form').get(0)) { @@ -471,7 +494,6 @@ if ($('#tags_form').get(0)) {
btLoading.addClass('elementNone');
alert('网络请求失败,请重试。');
});
*/
});
}

Loading…
Cancel
Save