From 9f1f60ff9afaabb27f8751c80d6d3de0b06b0b00 Mon Sep 17 00:00:00 2001 From: filesite Date: Tue, 7 May 2024 09:25:54 +0000 Subject: [PATCH] nickname save done --- .../tajian/controller/FrontapiController.php | 63 ++++++++++++++++--- themes/tajian/controller/MyController.php | 27 +++++++- themes/tajian/controller/SiteController.php | 16 +++++ themes/tajian/views/layout/index.php | 7 +++ themes/tajian/views/layout/main.php | 7 +++ themes/tajian/views/my/index.php | 2 +- themes/tajian/views/my/setnickname.php | 30 +++++++++ themes/tajian/views/site/login.php | 2 +- themes/tajian/views/site/new.php | 11 +--- www/js/tajian.js | 48 ++++++++++++-- 10 files changed, 188 insertions(+), 25 deletions(-) create mode 100644 themes/tajian/views/my/setnickname.php diff --git a/themes/tajian/controller/FrontapiController.php b/themes/tajian/controller/FrontapiController.php index 82102d7..5081973 100644 --- a/themes/tajian/controller/FrontapiController.php +++ b/themes/tajian/controller/FrontapiController.php @@ -638,13 +638,6 @@ eof; $err = "{$sms_code} 验证码已过期或错误,请检查是否输入正确"; } - if (empty($err)) { - $userDataDir = Common::getUserDataDir($cellphone); - if (empty($userDataDir)) { - $err = '你还没注册,请先注册'; - } - } - if (empty($err)) { //如果数据检查通过,尝试登录 $newUser = Common::saveUserIntoSession($cellphone); if (!empty($newUser)) { @@ -661,9 +654,63 @@ eof; return $this->renderJson(compact('code', 'msg', 'err', 'shareUrl')); } + //昵称设置 + public function actionSetnickname() { + $ip = $this->getUserIp(); + $check_time = 120; //2 分钟内 + $max_time_in_minutes = 5; //最多 5 次 + + $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)) { + $nickname = $this->post('nickname', ''); + + if (empty($nickname)) { + $err = "请填写注册你的昵称"; + }else if (mb_strlen($nickname, 'utf-8') < 2 || mb_strlen($nickname, 'utf-8') > 5) { + $err = "昵称至少 2 个汉字,最多 5 个汉字,请按规则填写"; + } + + if (empty($err)) { //如果数据检查通过,尝试保存 + $saved = $this->saveNickname($nickname); + if (!empty($saved)) { + $msg = "昵称设置完成"; + $code = 1; + }else { + $err = '昵称设置失败,请稍后重试'; + } + } + } + + return $this->renderJson(compact('code', 'msg', 'err')); + } + + //TODO: 分类管理 - //TODO: 昵称设置 //TODO: 视频管理 diff --git a/themes/tajian/controller/MyController.php b/themes/tajian/controller/MyController.php index b7037a2..e1d6a0d 100644 --- a/themes/tajian/controller/MyController.php +++ b/themes/tajian/controller/MyController.php @@ -61,6 +61,31 @@ Class MyController extends SiteController { return $this->render($viewName, $params, $pageTitle); } - + //修改昵称 + public function actionSetnickname() { + //判断是否已经登录 + $loginedUser = Common::getUserFromSession(); + if (empty($loginedUser['username'])) { + return $this->redirect('/site/login'); + }else if (!empty(FSC::$app['config']['multipleUserUriParse']) && FSC::$app['user_id'] != $loginedUser['username']) { + $shareUrl = "/{$loginedUser['username']}/my/setnickname/"; + return $this->redirect($shareUrl); + } + + $scanner = new DirScanner(); + $scanner->setWebRoot(FSC::$app['config']['content_directory']); + $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); + $readmeFile = $scanner->getDefaultReadme(); + $nickname = $this->getNickname($readmeFile); + + $pageTitle = "修改昵称 | " . FSC::$app['config']['site_name']; + + $this->layout = 'index'; + $viewName = 'setnickname'; + $params = compact( + 'pageTitle', 'nickname' + ); + return $this->render($viewName, $params, $pageTitle); + } } \ No newline at end of file diff --git a/themes/tajian/controller/SiteController.php b/themes/tajian/controller/SiteController.php index 8382858..61c84b0 100644 --- a/themes/tajian/controller/SiteController.php +++ b/themes/tajian/controller/SiteController.php @@ -180,6 +180,22 @@ Class SiteController extends Controller { return $nickname; } + protected function saveNickname($nickname) { + $done = false; + + try { + $filename = FSC::$app['config']['content_directory'] . 'README_nickname.txt'; + $savedBytes = file_put_contents($filename, $nickname); + if ($savedBytes !== false) { + $done = true; + } + }catch(Exception $e) { + $this->logError('Save nickname failed: ' . $e->getMessage()); + } + + return $done; + } + //添加新视频 //增加必须登录才能使用限制 public function actionNew() { diff --git a/themes/tajian/views/layout/index.php b/themes/tajian/views/layout/index.php index 487ef28..bcaa174 100644 --- a/themes/tajian/views/layout/index.php +++ b/themes/tajian/views/layout/index.php @@ -56,6 +56,13 @@ if (!empty($viewFile) && file_exists($viewFile)) { 水面上的冰山一角 + + diff --git a/themes/tajian/views/layout/main.php b/themes/tajian/views/layout/main.php index f7ba54c..453f10a 100644 --- a/themes/tajian/views/layout/main.php +++ b/themes/tajian/views/layout/main.php @@ -154,6 +154,13 @@ eof; + + diff --git a/themes/tajian/views/my/index.php b/themes/tajian/views/my/index.php index 333f6d9..ab507a6 100644 --- a/themes/tajian/views/my/index.php +++ b/themes/tajian/views/my/index.php @@ -13,7 +13,7 @@ if (!empty(FSC::$app['config']['multipleUserUriParse']) && !empty(FSC::$app['use
- (修改) + (修改)

退出登录

diff --git a/themes/tajian/views/my/setnickname.php b/themes/tajian/views/my/setnickname.php new file mode 100644 index 0000000..ec00a16 --- /dev/null +++ b/themes/tajian/views/my/setnickname.php @@ -0,0 +1,30 @@ +
+
+
+ + +

请填写 2 - 5 个汉字

+
+
+ + + + 返回 + +
+
+
\ No newline at end of file diff --git a/themes/tajian/views/site/login.php b/themes/tajian/views/site/login.php index ecd1d91..e455665 100644 --- a/themes/tajian/views/site/login.php +++ b/themes/tajian/views/site/login.php @@ -10,7 +10,7 @@
-
- - - \ No newline at end of file + \ No newline at end of file diff --git a/www/js/tajian.js b/www/js/tajian.js index f77881f..ad29347 100644 --- a/www/js/tajian.js +++ b/www/js/tajian.js @@ -6,10 +6,11 @@ var taJian = { debug: false, domain: 'https://tajian.tv', apis: { - addVideos: '/frontapi/addfav', //添加视频 - sendSmsCode: '/frontapi/sendsmscode', //发送短信验证码 - register: '/frontapi/createuser', //注册 - login: '/frontapi/loginuser' //登入 + addVideos: '/frontapi/addfav', //添加视频 + setNickname: '/frontapi/setnickname', //设置昵称 + sendSmsCode: '/frontapi/sendsmscode', //发送短信验证码 + register: '/frontapi/createuser', //注册 + login: '/frontapi/loginuser' //登入 } }; @@ -356,4 +357,43 @@ if ($('#login_form').get(0)) { }); } + +// 设置昵称 +if ($('#nickname_form').get(0)) { + $('#nickname_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.setNickname, 'POST', datas, function (data) { + btLoading.addClass('elementNone'); + bt.prop('disabled', false); + btText.text('保存'); + if (data.code == 1) { + alert(data.msg); + } else { + alert(data.err); + } + }, function (jqXHR, textStatus, errorThrown) { + bt.prop('disabled', false); + btText.text('保存'); + btLoading.addClass('elementNone'); + alert('网络请求失败,请重试。'); + }); + }); +} + })();