diff --git a/app/Http/Controllers/Api/FileController.php b/app/Http/Controllers/Api/FileController.php index 15695999..8130e670 100755 --- a/app/Http/Controllers/Api/FileController.php +++ b/app/Http/Controllers/Api/FileController.php @@ -26,16 +26,25 @@ class FileController extends AbstractController // $pid = intval(Request::input('pid')); // - $list = File::whereUserid($user->userid)->wherePid($pid)->orderBy('name')->take(500)->get(); + $list = File::whereUserid($user->userid)->wherePid($pid)->take(500)->get(); + $array = $list->toArray(); // - return Base::retSuccess('success', $list); + while ($pid > 0) { + $file = File::whereUserid($user->userid)->whereId($pid)->first(); + if ($file) { + $array[] = $file->toArray(); + $pid = $file->pid; + } + } + return Base::retSuccess('success', $array); } /** - * 添加项目 + * 添加、修改文件(夹) * * @apiParam {String} name 项目名称 * @apiParam {String} type 文件类型 + * @apiParam {Number} [id] 文件ID(赋值修改文件名称) * @apiParam {Number} [pid] 父级ID */ public function add() @@ -44,6 +53,7 @@ class FileController extends AbstractController // 文件名称 $name = trim(Request::input('name')); $type = trim(Request::input('type')); + $id = intval(Request::input('id')); $pid = intval(Request::input('pid')); if (mb_strlen($name) < 2) { return Base::retError('文件名称不可以少于2个字'); @@ -51,34 +61,137 @@ class FileController extends AbstractController return Base::retError('文件名称最多只能设置32个字'); } // - if (!in_array($type, [ - 'folder', - 'document', - 'mind', - 'sheet', - 'flow', - ])) { - return Base::retError('类型错误'); + if ($id > 0) { + // 修改 + $file = File::whereUserid($user->userid)->whereId($id)->first(); + if (empty($file)) { + return Base::retError('文件不存在或已被删除'); + } + $file->name = $name; + $file->save(); + return Base::retSuccess('修改成功', $file); + } else { + // 添加 + if (!in_array($type, [ + 'folder', + 'document', + 'mind', + 'sheet', + 'flow', + ])) { + return Base::retError('类型错误'); + } + // + if ($pid > 0) { + if (!File::whereUserid($user->userid)->whereId($pid)->exists()) { + return Base::retError('参数错误'); + } + } + if (File::whereUserid($user->userid)->wherePid($pid)->count() >= 300) { + return Base::retError('每个文件夹里最多只能创建300个文件或文件夹'); + } + // 开始创建 + $file = File::createInstance([ + 'pid' => $pid, + 'name' => $name, + 'type' => $type, + 'userid' => $user->userid, + ]); + $file->save(); + // + $data = File::find($file->id); + return Base::retSuccess('添加成功', $data); + } + } + + /** + * 复制文件(夹) + * + * @apiParam {Number} id 文件ID + */ + public function copy() + { + $user = User::auth(); + // + $id = intval(Request::input('id')); + // + $row = File::whereUserid($user->userid)->whereId($id)->first(); + if (empty($row)) { + return Base::retError('文件不存在或已被删除'); + } + if ($row->type == 'folder') { + return Base::retError('不支持复制文件夹'); + } + $num = File::whereCid($row->id)->count() + 1; + $name = $row->name . " ({$num})"; + // 开始复制 + $file = File::createInstance([ + 'cid' => $row->id, + 'pid' => $row->pid, + 'name' => $name, + 'type' => $row->type, + 'userid' => $user->userid, + ]); + $file->save(); + // + $data = File::find($file->id); + return Base::retSuccess('复制成功', $data); + } + + /** + * 移动文件(夹) + * + * @apiParam {Number} id 文件ID + * @apiParam {Number} pid 移动到的文件夹ID + */ + public function move() + { + $user = User::auth(); + // + $id = intval(Request::input('id')); + $pid = intval(Request::input('pid')); + // + $file = File::whereUserid($user->userid)->whereId($id)->first(); + if (empty($file)) { + return Base::retError('文件不存在或已被删除'); } // if ($pid > 0) { if (!File::whereUserid($user->userid)->whereId($pid)->exists()) { return Base::retError('参数错误'); } + $arr = []; + $tid = $pid; + while ($tid > 0) { + $arr[] = $tid; + $tid = intval(File::whereId($tid)->value('pid')); + } + if (in_array($id, $arr)) { + return Base::retError('位置错误'); + } } - if (File::whereUserid($user->userid)->wherePid($pid)->count() >= 300) { - return Base::retError('每个文件夹里最多只能创建300个文件或文件夹'); - } - // 开始创建 - $file = File::createInstance([ - 'pid' => $pid, - 'name' => $name, - 'type' => $type, - 'userid' => $user->userid, - ]); - $file->save(); // - $data = File::find($file->id); - return Base::retSuccess('添加成功', $data); + $file->pid = $pid; + $file->save(); + return Base::retSuccess('操作成功', $file); + } + + /** + * 删除文件(夹) + * + * @apiParam {Number} id 文件ID + */ + public function remove() + { + $user = User::auth(); + // + $id = intval(Request::input('id')); + // + $file = File::whereUserid($user->userid)->whereId($id)->first(); + if (empty($file)) { + return Base::retError('文件不存在或已被删除'); + } + $file->deleteFile(); + return Base::retSuccess('删除成功', $file); } } diff --git a/app/Models/File.php b/app/Models/File.php index 9e78d8eb..5ef53784 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -2,30 +2,57 @@ namespace App\Models; +use Illuminate\Database\Eloquent\SoftDeletes; + /** * Class File * * @package App\Models * @property int $id * @property int|null $pid 上级ID + * @property int|null $cid 复制ID * @property string|null $name 名称 * @property string|null $type 类型 * @property int|null $userid 拥有者ID * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at + * @property \Illuminate\Support\Carbon|null $deleted_at * @method static \Illuminate\Database\Eloquent\Builder|File newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|File newQuery() + * @method static \Illuminate\Database\Query\Builder|File onlyTrashed() * @method static \Illuminate\Database\Eloquent\Builder|File query() + * @method static \Illuminate\Database\Eloquent\Builder|File whereCid($value) * @method static \Illuminate\Database\Eloquent\Builder|File whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|File whereDeletedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|File whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|File whereName($value) * @method static \Illuminate\Database\Eloquent\Builder|File wherePid($value) * @method static \Illuminate\Database\Eloquent\Builder|File whereType($value) * @method static \Illuminate\Database\Eloquent\Builder|File whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|File whereUserid($value) + * @method static \Illuminate\Database\Query\Builder|File withTrashed() + * @method static \Illuminate\Database\Query\Builder|File withoutTrashed() * @mixin \Eloquent */ class File extends AbstractModel { + use SoftDeletes; + /** + * 遍历删除文件(夹) + * @return bool + */ + public function deleteFile() + { + AbstractModel::transaction(function () { + $this->delete(); + $list = self::wherePid($this->id)->get(); + if ($list->isNotEmpty()) { + foreach ($list as $item) { + $item->deleteFile(); + } + } + }); + return true; + } } diff --git a/app/Models/FileContent.php b/app/Models/FileContent.php new file mode 100644 index 00000000..905209c4 --- /dev/null +++ b/app/Models/FileContent.php @@ -0,0 +1,31 @@ +
{{$L('没有任何文件')}}