From 5f63daf075da8145f1f16974d358eeaf5abd7b83 Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 24 Jul 2016 15:56:23 +0800 Subject: [PATCH] add function of renaming texture --- app/Controllers/SkinlibController.php | 68 ++++++++++++++++++--------- app/Services/Validate.php | 2 + assets/src/js/skinlib.js | 24 +++++++++- config/routes.php | 5 +- resources/views/skinlib/show.tpl | 12 ++++- 5 files changed, 84 insertions(+), 27 deletions(-) diff --git a/app/Controllers/SkinlibController.php b/app/Controllers/SkinlibController.php index 17739dd5..6a8178b5 100644 --- a/app/Controllers/SkinlibController.php +++ b/app/Controllers/SkinlibController.php @@ -169,29 +169,37 @@ class SkinlibController extends BaseController public function delete() { - if (is_numeric(Utils::getValue('tid', $_POST))) { - $result = Texture::find($_POST['tid']); + \Utils::checkPost(['tid']); - if (!$result) - throw new E('Unexistent texture.', 1); + $result = Texture::find($_POST['tid']); - // check if file occupied - if (Texture::where('hash', $result['hash'])->count() == 1) - \Storage::remove("./textures/".$result['hash']); + if (!$result) + View::json('Unexistent texture.', 1); - $this->user->setScore($result->size, 'plus'); + if ($result->uploader != $this->user->uid && !$this->user->is_admin) + View::json('你不是这个材质的上传者哦', 1); - if ($result->delete()) - View::json('材质已被成功删除', 0); + // check if file occupied + if (Texture::where('hash', $result['hash'])->count() == 1) + \Storage::remove("./textures/".$result['hash']); - } else { - throw new E('Invalid parameters.', 1); - } + $this->user->setScore($result->size, 'plus'); + + if ($result->delete()) + View::json('材质已被成功删除', 0); } public function privacy($tid) { + \Utils::checkPost(['tid']); + $t = Texture::find($tid); + + if (!$t) View::json('Unexistent texture.', 1); + + if ($t->uploader != $this->user->uid && !$this->user->is_admin) + View::json('你不是这个材质的上传者哦', 1); + if ($t->setPrivacy(!$t->public)) { View::json([ 'errno' => 0, @@ -201,40 +209,58 @@ class SkinlibController extends BaseController } } + public function rename() { + \Utils::checkPost(['tid', 'new_name']); + \Validate::checkValidTextureName($_POST['new_name']); + + $t = Texture::find($_POST['tid']); + + if (!$t) View::json('材质不存在', 1); + + if ($t->uploader != $this->user->uid && !$this->user->is_admin) + View::json('你不是这个材质的上传者哦', 1); + + $t->name = $_POST['new_name']; + + if ($t->save()) { + View::json('材质名称已被成功设置为'.$_POST['new_name'], 0); + } + } + private function checkUpload($type) { \Validate::checkValidTextureName(Utils::getValue('name', $_POST)); if (!Utils::getValue('file', $_FILES)) - throw new E('你还没有选择任何文件哟', 1); + View::json('你还没有选择任何文件哟', 1); if (!isset($_POST['public']) || ($_POST['public'] != 0 && $_POST['public'] != 1)) - throw new E('Invalid parameters.', 1); + View::json('Invalid parameters.', 1); if ($_FILES['file']['type'] == "image/png" || $_FILES['file']['type'] == "image/x-png") { // if error occured while uploading file if ($_FILES['file']["error"] > 0) - throw new E($_FILES['file']["error"], 1); + View::json($_FILES['file']["error"], 1); $size = getimagesize($_FILES['file']["tmp_name"]); $ratio = $size[0] / $size[1]; if ($type == "steve" || $type == "alex") { if ($ratio != 2 && $ratio != 1) - throw new E("不是有效的皮肤文件(宽 {$size[0]},高 {$size[1]})", 1); + View::json("不是有效的皮肤文件(宽 {$size[0]},高 {$size[1]})", 1); } elseif ($type == "cape") { if ($ratio != 2) - throw new E("不是有效的披风文件(宽 {$size[0]},高 {$size[1]})", 1); + View::json("不是有效的披风文件(宽 {$size[0]},高 {$size[1]})", 1); } else { - throw new E('Invalid parameters.', 1); + View::json('Invalid parameters.', 1); } } else { if (Utils::getValue('file', $_FILES)) { - throw new E('文件格式不对哦', 1); + View::json('文件格式不对哦', 1); } else { - throw new E('No file selected.', 1); + View::json('No file selected.', 1); } } diff --git a/app/Services/Validate.php b/app/Services/Validate.php index 6f99b88b..a1210dff 100644 --- a/app/Services/Validate.php +++ b/app/Services/Validate.php @@ -2,6 +2,8 @@ namespace App\Services; +use App\Exceptions\E; + class Validate { public static function checkValidPlayerName($player_name) { diff --git a/assets/src/js/skinlib.js b/assets/src/js/skinlib.js index cbef251f..75dde752 100644 --- a/assets/src/js/skinlib.js +++ b/assets/src/js/skinlib.js @@ -2,7 +2,7 @@ * @Author: prpr * @Date: 2016-07-19 10:46:38 * @Last Modified by: printempw -* @Last Modified time: 2016-07-23 15:23:19 +* @Last Modified time: 2016-07-24 15:38:22 */ 'use strict'; @@ -193,6 +193,28 @@ function upload() { return false; } +function changeTextureName(tid) { + var new_name = prompt("请输入新的材质名称:"); + + if (!new_name) return; + + $.ajax({ + type: "POST", + url: "./rename", + dataType: "json", + data: { 'tid': tid, 'new_name': new_name }, + success: function(json) { + if (json.errno == 0) { + $('#name').text(new_name); + toastr.success(json.msg); + } else { + toastr.warning(json.msg); + } + }, + error: showAjaxError + }); +} + function changePrivacy(tid) { $.ajax({ type: "POST", diff --git a/config/routes.php b/config/routes.php index 0854b176..72a56056 100644 --- a/config/routes.php +++ b/config/routes.php @@ -77,16 +77,15 @@ Route::group(['prefix' => 'skinlib'], function() Route::get ('', 'SkinlibController@index'); Route::all ('/info/{tid}', 'SkinlibController@info'); Route::all ('/show', 'SkinlibController@show'); - Route::post('/save', 'SkinlibController@save'); Route::all ('/search', 'SkinlibController@search'); - Route::post('/privacy/{tid}', 'SkinlibController@privacy'); - Route::group(['middleware' => 'App\Middlewares\CheckLoggedInMiddleware'], function() { Route::get ('/upload', 'SkinlibController@upload'); Route::post('/upload', 'SkinlibController@handleUpload'); + Route::post('/rename', 'SkinlibController@rename'); + Route::post('/privacy/{tid}', 'SkinlibController@privacy'); Route::post('/delete', 'SkinlibController@delete'); }); }); diff --git a/resources/views/skinlib/show.tpl b/resources/views/skinlib/show.tpl index 37a5194e..e524d826 100644 --- a/resources/views/skinlib/show.tpl +++ b/resources/views/skinlib/show.tpl @@ -59,7 +59,13 @@ 名称 - {{ $texture->name }} + {{ $texture->name }} + @if (!is_null($user) && ($texture->uploader == $user->uid || $user->is_admin)) + + 修改名称 + + @endif + 适用模型 @@ -89,7 +95,8 @@ - @if (!is_null($user) && $texture->uploader == $user->uid) + @if (!is_null($user)) + @if ($texture->uploader == $user->uid)

删除材质 / 设置隐私

@@ -124,6 +131,7 @@
@endif + @endif