| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\common\Upload;
- use app\models\Storage;
- use app\models\UploadConfig;
- use Yii;
- use Exception;
- use yii\base\Model;
- class StorageForm extends Model
- {
- /**
- * 商城id
- * @var integer
- */
- public $store_id;
- /**
- * 父级id
- * @var integer
- */
- public $parent_id = 0;
- /**
- * 入驻商id
- * @var integer
- */
- public $mch_id;
- /**
- * 供货商id
- * @var integer
- */
- public $supplier_id;
- /**
- * 推广代理id
- * @var integer
- */
- public $bd_id;
- /**
- * 客户端获取的类型
- * @var integer
- */
- public $type;
- /**
- * 排序类型
- * id, 编号
- * name, 名称
- * ext, 类型
- * created_at, 创建时间
- * updated_at, 更新时间
- * @var string
- */
- public $sort_type;
- /**
- * 排序方式,升序ASC或降序DESC
- * @var string
- */
- public $sort_method;
- /**
- * 搜索名称
- * @var string
- */
- public $search_name;
- /**
- * @var UploadConfig $upload_config
- */
- public $upload_config;
- /**
- * 初始化
- */
- public function init()
- {
- $this->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]);
- }
- }
|