MediaController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace app\backend\modules\setting\controllers;
  3. use app\common\components\BaseController;
  4. use app\common\facades\Setting;
  5. use app\common\helpers\Url;
  6. use app\platform\modules\application\models\CoreAttach;
  7. use app\backend\modules\upload\models\CoreAttach as WQCoreAttach;
  8. use app\platform\modules\application\models\CoreAttachTags;
  9. use app\platform\modules\system\models\SystemSetting;
  10. class MediaController extends BaseController
  11. {
  12. private $coreAttchment;
  13. public function preAction()
  14. {
  15. parent::preAction(); // TODO: Change the autogenerated stub
  16. if (config('app.framework') == 'platform') {
  17. $this->coreAttchment = new CoreAttach();
  18. }else{
  19. $this->coreAttchment = new WQCoreAttach();
  20. }
  21. }
  22. public function index()
  23. {
  24. return view('setting.media.index')->render();
  25. }
  26. public function tags(){
  27. $sourceType = request()->source_type;
  28. if (!$sourceType) {
  29. return $this->successJson('获取失败');
  30. }
  31. $list = CoreAttachTags::uniacid()->with(['hasManySource' => function($query){
  32. $query->uniacid();
  33. if (\YunShop::app()->isfounder !== true)
  34. {
  35. $query->where('uid', \YunShop::app()->uid);
  36. }
  37. }])->where('source_type',$sourceType)->orderBy('sort', 'desc')->get()->toArray();
  38. $allCount = $this->coreAttchment->uniacid()->where('type',$sourceType);
  39. $unCount = $this->coreAttchment->uniacid()->where('type',$sourceType)->where(function($query){
  40. $query->where('tag_id', 0)->orWhere('tag_id', null);
  41. });
  42. if (\YunShop::app()->isfounder !== true)
  43. {
  44. $allCount = $allCount->where('uid', \YunShop::app()->uid);
  45. $unCount = $unCount->where('uid', \YunShop::app()->uid);
  46. }
  47. $allCount = $allCount->count();
  48. $unCount = $unCount->count();
  49. foreach ($list as &$item)
  50. {
  51. $item['source_count'] = count($item['has_many_source']);
  52. unset($item['has_many_source']);
  53. }
  54. array_unshift($list,
  55. ['id' => '', 'title' => '全部', 'source_type' => $sourceType, 'tag_type' => 1, 'source_count' => $allCount],
  56. ['id' => 0, 'title' => '未分组', 'source_type' => $sourceType, 'tag_type' => 1, 'source_count' => $unCount]
  57. );
  58. return $this->successJson('成功',$list);
  59. }
  60. public function source(){
  61. $sourceType = request()->source_type;
  62. $tagId = request()->tag_id;
  63. $pageSize = request()->pageSize;
  64. $date = request()->date;
  65. if (!$sourceType) {
  66. return $this->successJson('获取失败');
  67. }
  68. $list = $this->coreAttchment->uniacid()
  69. ->where('type', $sourceType)
  70. ->search($date);
  71. if (\YunShop::app()->isfounder !== true)
  72. {
  73. $list->where('uid', \YunShop::app()->uid);
  74. }
  75. if (is_numeric($tagId))
  76. {
  77. if ($tagId == 0)
  78. {
  79. $list = $list->where(function($query)use($tagId){
  80. $query->where('tag_id', $tagId)->orWhere('tag_id', null);
  81. });
  82. }
  83. else
  84. {
  85. $list->where('tag_id', $tagId);
  86. }
  87. }
  88. if (config('app.framework') == 'platform') {
  89. $list = $list->orderBy('created_at', 'desc');
  90. }else{
  91. $list = $list->orderBy('createtime', 'desc');
  92. }
  93. $list = $list->paginate($pageSize);
  94. foreach($list as &$item){
  95. $item->attachment = yz_tomedia($item->attachment);
  96. }
  97. return $this->successJson('成功', $list);
  98. }
  99. public function addTag(){
  100. $title = request()->title;
  101. $sourceType = request()->source_type;
  102. $tagType = 2;
  103. $result = CoreAttachTags::create([
  104. 'source_type' => $sourceType,
  105. 'tag_type' => $tagType,
  106. 'title' => $title,
  107. 'uniacid' => \YunShop::app()->uniacid,
  108. ]);
  109. if ($result)
  110. {
  111. return $this->successJson('成功');
  112. }
  113. else
  114. {
  115. return $this->errorJson('失败');
  116. }
  117. }
  118. //批量删除 所有资源类型通用
  119. public function batchDelete(){
  120. $ids = request()->ids;
  121. $tags = request()->tags;
  122. $date = request()->date;
  123. $sourceType = request()->source_type;
  124. if(!$sourceType || (!$ids && !$tags && !$date)){
  125. return $this->errorJson('缺少参数');
  126. }
  127. $model = $this->coreAttchment->uniacid()->where('type', $sourceType);
  128. if (!empty($ids))
  129. {
  130. $model = $model->whereIn('id', $ids);
  131. }
  132. if (!empty($tags))
  133. {
  134. $model = $model->whereIn('tag_id', $tags);
  135. }
  136. if (!empty($date))
  137. {
  138. switch($date){
  139. case 'all':
  140. break;
  141. case 'year':
  142. $model = $model->whereBetween(
  143. 'created_at',
  144. [
  145. time(),
  146. time()-86400*365
  147. ]
  148. );
  149. break;
  150. case 'half_year':
  151. $model = $model->whereBetween(
  152. 'created_at',
  153. [
  154. time(),
  155. time()-86400*365/2
  156. ]
  157. );
  158. break;
  159. case 'three_month':
  160. $model = $model->whereBetween(
  161. 'created_at',
  162. [
  163. time(),
  164. time()-86400*90
  165. ]
  166. );
  167. break;
  168. case 'month':
  169. $model = $model->whereBetween(
  170. 'created_at',
  171. [
  172. time(),
  173. time()-86400*30
  174. ]
  175. );
  176. break;
  177. }
  178. }
  179. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  180. $core_attach = $model->get();
  181. foreach ($core_attach as $attach)
  182. {
  183. if ($attach['upload_type']) {
  184. $remote_url = '';
  185. if ($remote['type'] == 2) {
  186. $remote_url = $remote['alioss']['url'];
  187. }
  188. if ($remote['type'] == 4) {
  189. $remote_url = $remote['cos']['url'];
  190. }
  191. if ($remote_url && strexists($attach['attachment'], $remote_url)) {
  192. $str_len = strlen($remote_url);
  193. $attach['attachment'] = substr($attach['attachment'], $str_len+1);
  194. }
  195. if (config('app.framework') == 'platform')
  196. {
  197. $status = file_remote_delete($attach['attachment'], $attach['upload_type'], $remote);
  198. }
  199. else
  200. {
  201. $status = file_remote_delete($attach['attachment']);
  202. }
  203. } else {
  204. $status = file_delete($attach['attachment']);
  205. }
  206. $attach->delete();
  207. }
  208. return $this->successJson('删除成功');
  209. }
  210. public function batchMove(){
  211. $ids = request()->ids;
  212. $tagId = request()->tag_id;
  213. if(!$ids || !is_numeric($tagId)){
  214. return $this->errorJson('缺少参数');
  215. }
  216. $result = $this->coreAttchment->uniacid()->whereIn('id', $ids)->update(['tag_id' => $tagId]);
  217. if ($result)
  218. {
  219. return $this->successJson('成功');
  220. }
  221. else
  222. {
  223. return $this->errorJson('失败');
  224. }
  225. }
  226. public function rename(){
  227. $id = request()->id;
  228. $filename = request()->filename;
  229. if(!$id || !$filename){
  230. return $this->errorJson('缺少参数');
  231. }
  232. $result = $this->coreAttchment->uniacid()->where('id', $id)->update(['filename' => $filename]);
  233. if ($result)
  234. {
  235. return $this->successJson('成功');
  236. }
  237. else
  238. {
  239. return $this->errorJson('失败');
  240. }
  241. }
  242. public function renameTag(){
  243. $id = request()->id;
  244. $title = request()->title;
  245. if(!$id || !$title){
  246. return $this->errorJson('缺少参数');
  247. }
  248. $result = CoreAttachTags::uniacid()->where('id', $id)->update(['title' => $title]);
  249. if ($result)
  250. {
  251. return $this->successJson('成功');
  252. }
  253. else
  254. {
  255. return $this->errorJson('失败');
  256. }
  257. }
  258. public function deleteTag(){
  259. $id = request()->id;
  260. $status = request()->moveOrDelete;
  261. if(!$id){
  262. return $this->errorJson('缺少参数');
  263. }
  264. $result = CoreAttachTags::uniacid()->where('id', $id)->delete();
  265. if ($status == 1)
  266. {
  267. $this->coreAttchment->uniacid()->where('tag_id', $id)->update(['tag_id' => 0]);
  268. }
  269. else if ($status == 2)
  270. {
  271. $this->coreAttchment->uniacid()->where('tag_id', $id)->delete();
  272. }
  273. if ($result)
  274. {
  275. return $this->successJson('成功');
  276. }
  277. else
  278. {
  279. return $this->errorJson('失败');
  280. }
  281. }
  282. public function sortTag(){
  283. $id = request()->id;
  284. $sort = request()->sort;
  285. if(!$id){
  286. return $this->errorJson('缺少参数');
  287. }
  288. $result = CoreAttachTags::uniacid()->where('id', $id)->update(['sort' => $sort]);
  289. if ($result)
  290. {
  291. return $this->successJson('成功');
  292. }
  293. else
  294. {
  295. return $this->errorJson('失败');
  296. }
  297. }
  298. }