diff --git a/application/common/validate/Users.php b/application/common/validate/Users.php index c0e3f361..ec9a02a8 100644 --- a/application/common/validate/Users.php +++ b/application/common/validate/Users.php @@ -15,29 +15,32 @@ class Users extends Validate protected $rule = [ 'username' => 'require|max:15|unique:users', 'nickname' => 'max:15', + 'default_folder' => 'max:30|chsAlphaNum', 'email' => 'require|email|max:50|unique:users', 'password' => 'require|confirm', 'captcha' => 'require|captcha|token', ]; protected $message = [ - 'username.require' => '用户名不能为空', - 'username.max' => '用户名字符长度超出', - 'username.unique' => '用户名已存在,请更换', - 'nickname.max' => '昵称字符长度超出', - 'email.require' => '邮箱不能为空', - 'email.email' => '邮箱格式不正确', - 'email.max' => '邮箱字符长度超出', - 'email.unique' => '邮箱已存在', - 'password.require' => '密码不能为空', - 'password.confirm' => '两次输入的密码不一致', - 'captcha.require' => '请输入验证码', - 'captcha.captcha' => '验证码错误', + 'username.require' => '用户名不能为空', + 'username.max' => '用户名字符长度超出', + 'username.unique' => '用户名已存在,请更换', + 'nickname.max' => '昵称字符长度超出', + 'default_folder.max' => '默认上传文件夹名称长度超出', + 'default_folder.chsAlphaNum'=> '默认上传文件夹名称只能是汉字、字母和数字', + 'email.require' => '邮箱不能为空', + 'email.email' => '邮箱格式不正确', + 'email.max' => '邮箱字符长度超出', + 'email.unique' => '邮箱已存在', + 'password.require' => '密码不能为空', + 'password.confirm' => '两次输入的密码不一致', + 'captcha.require' => '请输入验证码', + 'captcha.captcha' => '验证码错误', ]; public function sceneEdit() { - return $this->only(['nickname', 'password'])->remove('password', 'require'); + return $this->only(['nickname', 'default_folder', 'password'])->remove('password', 'require'); } public function sceneAdminEdit() diff --git a/application/index/controller/Install.php b/application/index/controller/Install.php index 47b4ab74..67602355 100644 --- a/application/index/controller/Install.php +++ b/application/index/controller/Install.php @@ -217,8 +217,11 @@ EOT; // 新建表字段 $tableFields = [ 'lsky_images' => [ - 'folder_id' => 'ALTER TABLE `lsky_images` ADD `folder_id` INT NOT NULL DEFAULT \'0\' COMMENT \'文件夹ID\' AFTER `user_id`;' - ] + 'folder_id' => "ALTER TABLE `lsky_images` ADD `folder_id` INT NOT NULL DEFAULT '0' COMMENT '文件夹ID' AFTER `user_id`;" + ], + 'lsky_users' => [ + 'default_folder' => "ALTER TABLE `lsky_users` ADD `default_folder` VARCHAR(32) DEFAULT NULL COMMENT '默认上传文件夹' AFTER `quota`;" + ], ]; foreach ($tableFields as $table => $fields) { diff --git a/application/index/controller/Upload.php b/application/index/controller/Upload.php index 85604690..2a1b78c8 100644 --- a/application/index/controller/Upload.php +++ b/application/index/controller/Upload.php @@ -8,6 +8,7 @@ namespace app\index\controller; +use app\common\model\Folders; use app\common\model\Images; use app\common\model\Users; use GuzzleHttp\Client; @@ -77,7 +78,7 @@ class Upload extends Base if (Config::get('app.app_debug')) { throw new Exception($strategy->getError()); } - throw new Exception('上传失败'); + throw new Exception('上传失败,请检查策略配置是否正确!'); } $cdnDomain = $currentStrategy . '_cdn_domain'; @@ -107,7 +108,7 @@ class Upload extends Base } } - if (!Images::create([ + $imageData = [ 'user_id' => $user ? $user->id : 0, 'strategy' => $currentStrategy, 'path' => dirname($pathname), @@ -117,7 +118,25 @@ class Upload extends Base 'mime' => $mime, 'sha1' => $sha1, 'md5' => $md5 - ])) { + ]; + + // 默认上传文件夹,暂只支持一级 + if ($this->user->default_folder) { + $folderId = $this->user->folders()->where('name', $this->user->default_folder)->value('id'); + if (!$folderId) { + if (!$folderId = $this->user->folders()->insertGetId([ + 'user_id' => $this->user->id, + 'parent_id' => 0, + 'name' => $this->user->default_folder + ])) { + throw new Exception('文件夹创建失败!'); + } + } + + $imageData['folder_id'] = $folderId; + } + + if (!Images::create($imageData)) { $strategy->delete($pathname); throw new Exception('图片数据保存失败'); } diff --git a/application/index/view/user/settings.html b/application/index/view/user/settings.html index 44d501ca..c08295f6 100644 --- a/application/index/view/user/settings.html +++ b/application/index/view/user/settings.html @@ -19,6 +19,10 @@ +