Browse Source

add tag done

master
filesite 7 months ago
parent
commit
70b87c1cb2
  1. 94
      themes/tajian/controller/FrontapiController.php
  2. 7
      themes/tajian/controller/MyController.php
  3. 31
      themes/tajian/controller/SiteController.php
  4. 2
      themes/tajian/views/my/index.php
  5. 30
      themes/tajian/views/my/tag_new.php
  6. 2
      themes/tajian/views/my/tags.php
  7. 43
      www/js/tajian.js

94
themes/tajian/controller/FrontapiController.php

@ -753,12 +753,12 @@ eof; @@ -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; @@ -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; @@ -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: 视频管理

7
themes/tajian/controller/MyController.php

@ -73,4 +73,11 @@ Class MyController extends SiteController { @@ -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);
}
}

31
themes/tajian/controller/SiteController.php

@ -247,6 +247,37 @@ Class SiteController extends Controller { @@ -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 = '';

2
themes/tajian/views/my/index.php

@ -22,7 +22,7 @@ if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['use @@ -22,7 +22,7 @@ if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['use
<ul class="mg_menus">
<li><a href="<?=$linkPrefix?>/my/tags"><img src="/img/collection.svg" alt="collection" width="18"> 管理分类</a></li>
<li><a href="###"><img src="/img/favorite.png" alt="favorite" width="20"> 管理收藏</a></li>
<li><a href="###"><img src="/img/edit.svg" alt="edit" width="18"> 添加分类</a></li>
<li><a href="<?=$linkPrefix?>/my/addtag"><img src="/img/edit.svg" alt="edit" width="18"> 添加分类</a></li>
<li><a href="###"><img src="/img/addvideos.svg" alt="add favorite" width="20"> 添加收藏</a></li>
</ul>
</div>

30
themes/tajian/views/my/tag_new.php

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
<?php
$linkPrefix = '';
//多用户路径支持
if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['user_id'])) {
$linkPrefix = '/' . FSC::$app['user_id'];
}
?><main class="g_main_lay">
<form class="g_form_style mt65" id="tag_new_form" action="" method="POST">
<div class="mb-3 pt20">
<label for="text_input_tag" class="form-label">分类名</label>
<input id="text_input_tag" name="tag" placeholder="请填写 2 - 15 个汉字、数字、英文" value="">
<p class="mt10">说明:<br>最多添加 <strong>20</strong> 个分类,如果以英文开头,首字母请大写。</p>
</div>
<div class="avform_bt vercenter">
<button class="jsbtn" aria-label="保存" type="button">
<div class="loading_bt bt_class_JS elementNone verMiddle">
<svg viewBox="25 25 50 50">
<circle cx="50" cy="50" r="20"></circle>
</svg>
</div>
<span class="bt_text_JS">保存</span>
<div class="bt_loading_cover bt_class_JS elementNone"></div>
</button>
<span class="ml20">
<a href="<?=$linkPrefix?>/my/">返回</a>
</span>
</div>
</form>
</main>

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

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

43
www/js/tajian.js

@ -10,6 +10,7 @@ var taJian = { @@ -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)) { @@ -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)) { @@ -457,7 +459,7 @@ if ($('#tags_form').get(0)) {
var allTagsOk = true, tagName = '';
for (var index=0;index<tag_els.length;index++) {
tagName = tag_els[index].value;
if (!tagName || tagName.length < 2 || tagName.length > 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)) { @@ -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('网络请求失败,请重试。');
});
});
}
})();

Loading…
Cancel
Save