store_id = get_store_id(); $this->mch_id = get_mch_id(); $this->supplier_id = get_supplier_id(); $this->bd_id = get_bd_id(); } /** * 上传文件 * @param int $type * @return array */ public function uploadFile($type = Storage::TYPE_IMAGE) { $uploadObj = new Upload(); $uploadObj->upload_config = $this->upload_config; $uploadObj->mch_id = $this->mch_id; $uploadObj->supplier_id = $this->supplier_id; $uploadObj->store_id = $this->store_id; $uploadObj->bd_id = $this->bd_id; $uploadObj->parent_id = $this->parent_id; return $uploadObj->uploadFile($type); } /** * 本地文件上传到云 * @return array */ public function local2cloudLink($file) { $uploadObj = new Upload(); $uploadObj->upload_config = UploadConfig::getConf($this->store_id); $uploadObj->mch_id = $this->mch_id; $uploadObj->supplier_id = $this->supplier_id; $uploadObj->store_id = $this->store_id; $uploadObj->bd_id = $this->bd_id; $uploadObj->parent_id = $this->parent_id; return $uploadObj->local2cloudLink($file); } /** * 获取排序类型 * @return string */ private function getSortType() { $sort = 'type DESC ,'; $sort_type = ['id', 'name', 'ext', 'created_at', 'updated_at']; $sort_method = ['ASC', 'DESC']; if (in_array($this->sort_type, $sort_type) && in_array(strtoupper($this->sort_method), $sort_method)) { $sort .= ' ' . $this->sort_type . ' ' . strtoupper($this->sort_method); } else { $sort .= ' id DESC'; } return $sort; } public function getStorageOption($type) { $storage_type = Storage::TYPE_IMAGE; if ($type == "video") { $storage_type = Storage::TYPE_VIDEO; } return [ 'image_type' => Storage::IMAGE_TYPE, 'image_mime' => Storage::IMAGE_MIME, 'video_type' => Storage::VIDEO_TYPE, 'video_mime' => Storage::VIDEO_MIME, 'max_upload_size' => Upload::getMaxUploadSize($storage_type), ]; } /** * 获取列表 * @return array */ public function getList() { $query = Storage::find() ->where([ 'is_delete' => Storage::STATUS_NORMAL, 'store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'supplier_id' => $this->supplier_id, 'bd_id' => $this->bd_id, ]) ->select('id, name, url, type, updated_at') ->orderBy($this->getSortType()); if ($this->search_name) { $query->andWhere(['like', 'name', $this->search_name]); $query->andWhere(['type' => $this->type]); } else { $query->andWhere(['parent_id' => $this->parent_id]); $query->andWhere(['in', 'type', [$this->type, Storage::TYPE_DIR]]); } return pagination_make($query); } /** * @param $dirName * @return bool * @throws Exception */ public function addDir($dirName) { $storage = Storage::find()->where([ 'parent_id' => $this->parent_id, 'name' => $dirName, 'type' => Storage::TYPE_DIR, 'is_delete' => Storage::STATUS_NORMAL, 'store_id' => $this->store_id, ])->one(); if ($storage) { throw new \Exception('目录名称已经存在'); } $storage = new Storage(); $storage->parent_id = $this->parent_id; $storage->name = $dirName; $storage->platform = Storage::PLATFORM_LOCAL; $storage->type = Storage::TYPE_DIR; $storage->store_id = $this->store_id; $storage->mch_id = $this->mch_id; $storage->supplier_id = $this->supplier_id; $storage->bd_id = $this->bd_id; return $storage->save(); } /** * 重命名 * @param $id * @param $name * @return bool * @throws Exception */ public function rename($id, $name) { $storage = Storage::findOne($id); if ($storage->type == Storage::TYPE_DIR) { $is = Storage::find()->where([ 'parent_id' => $storage->parent_id, 'name' => $name, 'type' => Storage::TYPE_DIR, 'is_delete' => Storage::STATUS_NORMAL, 'store_id' => $this->store_id, ])->andWhere(['<>', 'id', $id])->one(); if ($is) { throw new \Exception('目录名称已经存在'); } } $storage->name = $name; return $storage->save(); } /** * 批量删除 * @param $ids * @return mixed */ public function deleteById($ids) { if (! is_array($ids)) { $ids = [$ids]; } return Storage::updateAll(['is_delete' => 1], ['in', 'id', $ids]); } /** * 删除目录 * @param $id * @return bool * @throws Exception */ public function deleteDirById($id) { $this->getAllParentId($id, $parentIds); $count = Storage::find()->where([ 'is_delete' => 0, ])->andWhere(['in', 'parent_id', $parentIds]) ->andWhere(['<>', 'type', Storage::TYPE_DIR])->count(); if ($count > 0) { throw new \Exception('该目录下还有资源,不能删除'); } return Storage::updateAll(['is_delete' => 1], ['in', 'id', $parentIds]); } /** * 根据父id获取所有下级id * @param $parent_id * @param $ids * @return mixed */ private function getAllParentId($parent_id, &$ids) { $ids[] = $parent_id; $query = Storage::find()->where([ 'parent_id' => $parent_id, 'is_delete' => 0, 'store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'supplier_id' => $this->supplier_id, 'bd_id' => $this->bd_id, 'type' => Storage::TYPE_DIR, ]); $res = $query->asArray()->all(); if (empty($res)) { return $ids; } foreach ($res as &$val) { $this->getAllParentId($val['id'], $ids); } return $ids; } /** * 获取目录层级数据 * @param $parent_id * @param null|integer $exclude 排除数据 * @return array */ public function getDirTree($parent_id, $exclude = null) { $res = []; $query = Storage::find()->select('id as key, name as title')->where([ 'parent_id' => $parent_id, 'is_delete' => 0, 'store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'supplier_id' => $this->supplier_id, 'bd_id' => $this->bd_id, 'type' => Storage::TYPE_DIR, ]); $list = $query->asArray()->all(); if (empty($list)) { return $res; } foreach ($list as &$val) { if ($exclude && $val['key'] == $exclude) { continue; } $val['children'] = $this->getDirTree($val['key'], $exclude); $res[] = $val; } return $res; } /** * 移动目录 * @param $parent_id * @param $ids * @return int */ public function dragDir($parent_id, $ids) { if (! is_array($ids)) { $ids = [$ids]; } return Storage::updateAll(['parent_id' => $parent_id], ['in', 'id', $ids]); } }