chackAllow($userid)) { throw new ApiException('没有访问权限'); } } /** * 是否有访问权限 * ① 自己的文件夹 * ② 共享所有人的文件夹 * ③ 在指定共享人员内 * @param $userid * @return bool */ public function chackAllow($userid) { if ($userid == $this->userid) { // ① 自己的文件夹 return true; } $row = $this->getShareInfo(); if ($row) { if ($row->share == 1) { // ② 共享所有人的文件夹 return true; } elseif ($row->share == 2) { // ③ 在指定共享人员内 if (FileUser::whereFileId($row->id)->whereUserid($userid)->exists()) { return true; } } } return false; } /** * 获取共享数据(含自身) * @return $this|null */ public function getShareInfo() { if ($this->share > 0) { return $this; } $pid = $this->pid; while ($pid > 0) { $row = self::whereId($pid)->first(); if (empty($row)) { break; } if ($row->share > 0) { return $row; } $pid = $row->pid; } return null; } /** * 是否处于共享文件夹内(不含自身) * @return bool */ public function isNnShare() { $pid = $this->pid; while ($pid > 0) { $row = self::whereId($pid)->first(); if (empty($row)) { break; } if ($row->share > 0) { return true; } $pid = $row->pid; } return false; } /** * 设置/关闭 共享(同时遍历取消里面的共享) * @param $share * @return bool */ public function setShare($share) { AbstractModel::transaction(function () use ($share) { $this->share = $share; $this->save(); $list = self::wherePid($this->id)->get(); if ($list->isNotEmpty()) { foreach ($list as $item) { $item->setShare(0); } } }); return true; } /** * 遍历删除文件(夹) * @return bool */ public function deleteFile() { AbstractModel::transaction(function () { $this->delete(); $this->pushMsg('delete'); FileContent::whereFid($this->id)->delete(); $list = self::wherePid($this->id)->get(); if ($list->isNotEmpty()) { foreach ($list as $file) { $file->deleteFile(); } } }); return true; } /** * 推送消息 * @param $action * @param File|null $data 发送内容,默认为[id] * @param array $userid 指定会员,默认为可查看文件的人 */ public function pushMsg($action, $data = null, $userid = null) { if ($data === null) { $data = [ 'id' => $this->id ]; } // if ($userid === null) { $userid = [$this->userid]; if ($this->share == 1) { $builder = WebSocket::select(['userid']); if ($action == 'content') { $builder->wherePath('file/content/' . $this->id); } $userid = array_merge($userid, $builder->pluck('userid')->toArray()); } elseif ($this->share == 2) { $userid = array_merge($userid, FileUser::whereFileId($this->id)->pluck('userid')->toArray()); } $userid = array_values(array_filter(array_unique($userid))); } if (empty($userid)) { return; } // $msg = [ 'type' => 'file', 'action' => $action, 'data' => $data, ]; if ($action == 'content') { $msg['nickname'] = User::nickname(); $msg['time'] = time(); } $params = [ 'ignoreFd' => Request::header('fd'), 'userid' => $userid, 'msg' => $msg ]; $task = new PushTask($params, false); Task::deliver($task); } /** * 获取文件并检测权限 * @param $id * @param null $noExistTis * @return File */ public static function allowFind($id, $noExistTis = null) { $file = File::find($id); if (empty($file)) { throw new ApiException($noExistTis ?: '文件不存在或已被删除'); } $file->exceAllow(User::userid()); return $file; } }