StorageForm.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\common\Upload;
  9. use app\models\Storage;
  10. use app\models\UploadConfig;
  11. use Yii;
  12. use Exception;
  13. use yii\base\Model;
  14. class StorageForm extends Model
  15. {
  16. /**
  17. * 商城id
  18. * @var integer
  19. */
  20. public $store_id;
  21. /**
  22. * 父级id
  23. * @var integer
  24. */
  25. public $parent_id = 0;
  26. /**
  27. * 入驻商id
  28. * @var integer
  29. */
  30. public $mch_id;
  31. /**
  32. * 供货商id
  33. * @var integer
  34. */
  35. public $supplier_id;
  36. /**
  37. * 推广代理id
  38. * @var integer
  39. */
  40. public $bd_id;
  41. /**
  42. * 客户端获取的类型
  43. * @var integer
  44. */
  45. public $type;
  46. /**
  47. * 排序类型
  48. * id, 编号
  49. * name, 名称
  50. * ext, 类型
  51. * created_at, 创建时间
  52. * updated_at, 更新时间
  53. * @var string
  54. */
  55. public $sort_type;
  56. /**
  57. * 排序方式,升序ASC或降序DESC
  58. * @var string
  59. */
  60. public $sort_method;
  61. /**
  62. * 搜索名称
  63. * @var string
  64. */
  65. public $search_name;
  66. /**
  67. * @var UploadConfig $upload_config
  68. */
  69. public $upload_config;
  70. /**
  71. * 初始化
  72. */
  73. public function init()
  74. {
  75. $this->store_id = get_store_id();
  76. $this->mch_id = get_mch_id();
  77. $this->supplier_id = get_supplier_id();
  78. $this->bd_id = get_bd_id();
  79. }
  80. /**
  81. * 上传文件
  82. * @param int $type
  83. * @return array
  84. */
  85. public function uploadFile($type = Storage::TYPE_IMAGE)
  86. {
  87. $uploadObj = new Upload();
  88. $uploadObj->upload_config = $this->upload_config;
  89. $uploadObj->mch_id = $this->mch_id;
  90. $uploadObj->supplier_id = $this->supplier_id;
  91. $uploadObj->store_id = $this->store_id;
  92. $uploadObj->bd_id = $this->bd_id;
  93. $uploadObj->parent_id = $this->parent_id;
  94. return $uploadObj->uploadFile($type);
  95. }
  96. /**
  97. * 本地文件上传到云
  98. * @return array
  99. */
  100. public function local2cloudLink($file)
  101. {
  102. $uploadObj = new Upload();
  103. $uploadObj->upload_config = UploadConfig::getConf($this->store_id);
  104. $uploadObj->mch_id = $this->mch_id;
  105. $uploadObj->supplier_id = $this->supplier_id;
  106. $uploadObj->store_id = $this->store_id;
  107. $uploadObj->bd_id = $this->bd_id;
  108. $uploadObj->parent_id = $this->parent_id;
  109. return $uploadObj->local2cloudLink($file);
  110. }
  111. /**
  112. * 获取排序类型
  113. * @return string
  114. */
  115. private function getSortType()
  116. {
  117. $sort = 'type DESC ,';
  118. $sort_type = ['id', 'name', 'ext', 'created_at', 'updated_at'];
  119. $sort_method = ['ASC', 'DESC'];
  120. if (in_array($this->sort_type, $sort_type) && in_array(strtoupper($this->sort_method), $sort_method)) {
  121. $sort .= ' ' . $this->sort_type . ' ' . strtoupper($this->sort_method);
  122. } else {
  123. $sort .= ' id DESC';
  124. }
  125. return $sort;
  126. }
  127. public function getStorageOption($type)
  128. {
  129. $storage_type = Storage::TYPE_IMAGE;
  130. if ($type == "video") {
  131. $storage_type = Storage::TYPE_VIDEO;
  132. }
  133. return [
  134. 'image_type' => Storage::IMAGE_TYPE,
  135. 'image_mime' => Storage::IMAGE_MIME,
  136. 'video_type' => Storage::VIDEO_TYPE,
  137. 'video_mime' => Storage::VIDEO_MIME,
  138. 'max_upload_size' => Upload::getMaxUploadSize($storage_type),
  139. ];
  140. }
  141. /**
  142. * 获取列表
  143. * @return array
  144. */
  145. public function getList()
  146. {
  147. $query = Storage::find()
  148. ->where([
  149. 'is_delete' => Storage::STATUS_NORMAL,
  150. 'store_id' => $this->store_id,
  151. 'mch_id' => $this->mch_id,
  152. 'supplier_id' => $this->supplier_id,
  153. 'bd_id' => $this->bd_id,
  154. ])
  155. ->select('id, name, url, type, updated_at')
  156. ->orderBy($this->getSortType());
  157. if ($this->search_name) {
  158. $query->andWhere(['like', 'name', $this->search_name]);
  159. $query->andWhere(['type' => $this->type]);
  160. } else {
  161. $query->andWhere(['parent_id' => $this->parent_id]);
  162. $query->andWhere(['in', 'type', [$this->type, Storage::TYPE_DIR]]);
  163. }
  164. return pagination_make($query);
  165. }
  166. /**
  167. * @param $dirName
  168. * @return bool
  169. * @throws Exception
  170. */
  171. public function addDir($dirName)
  172. {
  173. $storage = Storage::find()->where([
  174. 'parent_id' => $this->parent_id,
  175. 'name' => $dirName,
  176. 'type' => Storage::TYPE_DIR,
  177. 'is_delete' => Storage::STATUS_NORMAL,
  178. 'store_id' => $this->store_id,
  179. ])->one();
  180. if ($storage) {
  181. throw new \Exception('目录名称已经存在');
  182. }
  183. $storage = new Storage();
  184. $storage->parent_id = $this->parent_id;
  185. $storage->name = $dirName;
  186. $storage->platform = Storage::PLATFORM_LOCAL;
  187. $storage->type = Storage::TYPE_DIR;
  188. $storage->store_id = $this->store_id;
  189. $storage->mch_id = $this->mch_id;
  190. $storage->supplier_id = $this->supplier_id;
  191. $storage->bd_id = $this->bd_id;
  192. return $storage->save();
  193. }
  194. /**
  195. * 重命名
  196. * @param $id
  197. * @param $name
  198. * @return bool
  199. * @throws Exception
  200. */
  201. public function rename($id, $name)
  202. {
  203. $storage = Storage::findOne($id);
  204. if ($storage->type == Storage::TYPE_DIR) {
  205. $is = Storage::find()->where([
  206. 'parent_id' => $storage->parent_id,
  207. 'name' => $name,
  208. 'type' => Storage::TYPE_DIR,
  209. 'is_delete' => Storage::STATUS_NORMAL,
  210. 'store_id' => $this->store_id,
  211. ])->andWhere(['<>', 'id', $id])->one();
  212. if ($is) {
  213. throw new \Exception('目录名称已经存在');
  214. }
  215. }
  216. $storage->name = $name;
  217. return $storage->save();
  218. }
  219. /**
  220. * 批量删除
  221. * @param $ids
  222. * @return mixed
  223. */
  224. public function deleteById($ids)
  225. {
  226. if (! is_array($ids)) {
  227. $ids = [$ids];
  228. }
  229. return Storage::updateAll(['is_delete' => 1], ['in', 'id', $ids]);
  230. }
  231. /**
  232. * 删除目录
  233. * @param $id
  234. * @return bool
  235. * @throws Exception
  236. */
  237. public function deleteDirById($id)
  238. {
  239. $this->getAllParentId($id, $parentIds);
  240. $count = Storage::find()->where([
  241. 'is_delete' => 0,
  242. ])->andWhere(['in', 'parent_id', $parentIds])
  243. ->andWhere(['<>', 'type', Storage::TYPE_DIR])->count();
  244. if ($count > 0) {
  245. throw new \Exception('该目录下还有资源,不能删除');
  246. }
  247. return Storage::updateAll(['is_delete' => 1], ['in', 'id', $parentIds]);
  248. }
  249. /**
  250. * 根据父id获取所有下级id
  251. * @param $parent_id
  252. * @param $ids
  253. * @return mixed
  254. */
  255. private function getAllParentId($parent_id, &$ids)
  256. {
  257. $ids[] = $parent_id;
  258. $query = Storage::find()->where([
  259. 'parent_id' => $parent_id,
  260. 'is_delete' => 0,
  261. 'store_id' => $this->store_id,
  262. 'mch_id' => $this->mch_id,
  263. 'supplier_id' => $this->supplier_id,
  264. 'bd_id' => $this->bd_id,
  265. 'type' => Storage::TYPE_DIR,
  266. ]);
  267. $res = $query->asArray()->all();
  268. if (empty($res)) {
  269. return $ids;
  270. }
  271. foreach ($res as &$val) {
  272. $this->getAllParentId($val['id'], $ids);
  273. }
  274. return $ids;
  275. }
  276. /**
  277. * 获取目录层级数据
  278. * @param $parent_id
  279. * @param null|integer $exclude 排除数据
  280. * @return array
  281. */
  282. public function getDirTree($parent_id, $exclude = null)
  283. {
  284. $res = [];
  285. $query = Storage::find()->select('id as key, name as title')->where([
  286. 'parent_id' => $parent_id,
  287. 'is_delete' => 0,
  288. 'store_id' => $this->store_id,
  289. 'mch_id' => $this->mch_id,
  290. 'supplier_id' => $this->supplier_id,
  291. 'bd_id' => $this->bd_id,
  292. 'type' => Storage::TYPE_DIR,
  293. ]);
  294. $list = $query->asArray()->all();
  295. if (empty($list)) {
  296. return $res;
  297. }
  298. foreach ($list as &$val) {
  299. if ($exclude && $val['key'] == $exclude) {
  300. continue;
  301. }
  302. $val['children'] = $this->getDirTree($val['key'], $exclude);
  303. $res[] = $val;
  304. }
  305. return $res;
  306. }
  307. /**
  308. * 移动目录
  309. * @param $parent_id
  310. * @param $ids
  311. * @return int
  312. */
  313. public function dragDir($parent_id, $ids)
  314. {
  315. if (! is_array($ids)) {
  316. $ids = [$ids];
  317. }
  318. return Storage::updateAll(['parent_id' => $parent_id], ['in', 'id', $ids]);
  319. }
  320. }