FileController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace backend\modules\common\controllers;
  3. use Yii;
  4. use Exception;
  5. use common\enums\RabbitMqEnum;
  6. use yii\filters\AccessControl;
  7. use common\helpers\ArrayHelper;
  8. use common\helpers\UploadHelper;
  9. use common\models\common\Attachment;
  10. use services\common\RabbitMqService;
  11. use backend\controllers\BaseController;
  12. use common\models\common\AttachmentGroup;
  13. /**
  14. * 文件管理
  15. *
  16. * @Author bing
  17. * @DateTime 2021-08-10 17:29:36
  18. * @copyright: Copyright (c) 2020 广东七件事集团
  19. */
  20. class FileController extends BaseController
  21. {
  22. public function behaviors()
  23. {
  24. $behaviors = parent::behaviors();
  25. $behaviors['access']['rules'] = array_merge($behaviors['access']['rules'], [
  26. [
  27. 'actions' => ['group-list','selector'],
  28. 'allow' => true,
  29. ],
  30. ]);
  31. return $behaviors;
  32. }
  33. public function actions(){
  34. $mall = Yii::$app->session->get('mall');
  35. return [
  36. 'upload' => [
  37. 'class' => 'common\components\UploadAction',
  38. 'mall_id' => !empty($mall) ? $mall['id'] : 0,
  39. 'mch_id' => 0,
  40. 'upload_type' => Yii::$app->request->post('upload_type') ?? '',
  41. 'group_id' => Yii::$app->request->get('attachment_group_id') ?? 0,
  42. ]
  43. ];
  44. }
  45. /**
  46. * 资源选择器
  47. *
  48. * @return array|string
  49. */
  50. public function actionSelector()
  51. {
  52. $upload_type = Yii::$app->request->get('upload_type', Attachment::UPLOAD_TYPE_IMAGES);
  53. $keyword = Yii::$app->request->get('keyword', '');
  54. $drive = Yii::$app->request->get('drive', '');
  55. $attachment_group_id = Yii::$app->request->get('attachment_group_id', 0);
  56. $mall = Yii::$app->session->get('mall');
  57. $mall_id = !empty($mall) ? $mall['id'] : 0;
  58. $result = Yii::$app->services->attachment->getListPage($mall_id, $upload_type, $attachment_group_id, $drive, $keyword);
  59. return $this->success('ok',$result);
  60. }
  61. /**
  62. * 添加、编辑目录
  63. * @return \yii\web\Response
  64. */
  65. public function actionSaveGroup()
  66. {
  67. try{
  68. $post = Yii::$app->request->post();
  69. $mall = Yii::$app->session->get('mall');
  70. $post['mall_id'] = !empty($mall) ? $mall['id'] : 0;
  71. $post['mch_id'] = 0;
  72. Yii::$app->services->attachment->saveGroup($post);
  73. return $this->success('操作成功。');
  74. }catch(Exception $e){
  75. return $this->error('Error:'.$e->getMessage());
  76. }
  77. }
  78. /**
  79. * 删除目录
  80. * @return \yii\web\Response
  81. */
  82. public function actionDelGroup()
  83. {
  84. try{
  85. $post = Yii::$app->request->post();
  86. $mall = Yii::$app->session->get('mall');
  87. Yii::$app->services->attachment->deleteGroup($post['id'], $mall['id'], 0);
  88. return $this->success('操作成功。');
  89. }catch(Exception $e){
  90. return $this->error('Error:'.$e->getMessage());
  91. }
  92. }
  93. /**
  94. * 获取分组列表
  95. * @return mixed|void
  96. */
  97. public function actionGroupList()
  98. {
  99. $type = Yii::$app->request->get('type') ?? '';
  100. $is_recycle = Yii::$app->request->get('is_recycle') ?? 0;
  101. $mall = Yii::$app->session->get('mall');
  102. // $type = $type === 'video' ? 1 : 0;
  103. $type = in_array($type, ['video', 'videos']) ? 1 : 0;
  104. $list = Yii::$app->services->attachment->getGroupByTypeAndMallId($mall['id'] ?? 0, $type, $is_recycle);
  105. $arr = $this->tree($list);
  106. $return = [
  107. 'list' => $list,
  108. 'arr' => $arr,
  109. ];
  110. return $this->success('操作成功', $return);
  111. }
  112. /**
  113. * 文件移动目录
  114. * @return mixed|void
  115. */
  116. public function actionMove()
  117. {
  118. $validateService = Yii::$app->services->validate;
  119. $rules = [
  120. [['ids','attachment_group_id'],'required'],
  121. ['attachment_group_id', 'integer']
  122. ];
  123. $labels = [
  124. 'id' => 'ID',
  125. 'attachment_group_id' => '目录ID'
  126. ];
  127. $valid = $validateService->validate(\Yii::$app->request->post(), $rules,$labels);
  128. if (!$valid) {
  129. return $this->error( $validateService->getFirstErrorSummary());
  130. }
  131. $ids = Yii::$app->request->post('ids');
  132. $ids = is_array($ids) ? $ids : explode(',',$ids);
  133. $group_id = Yii::$app->request->post('attachment_group_id');
  134. $mall = Yii::$app->session->get('mall');
  135. try {
  136. Yii::$app->services->attachment->moveFileToGroupId($ids, $mall['id'], $group_id);
  137. } catch (\Exception $e) {
  138. return $this->error($e->getMessage());
  139. }
  140. return $this->success('操作成功。');
  141. }
  142. /**
  143. * 删除文件
  144. * @return mixed|void
  145. */
  146. public function actionDelete()
  147. {
  148. $ids = Yii::$app->request->post('ids') ?? '';
  149. $ids = is_array($ids) ? $ids : explode(',',$ids);
  150. if(empty($ids)){
  151. return $this->error('ids 不能为空');
  152. }
  153. $mall = Yii::$app->session->get('mall');
  154. try {
  155. Yii::$app->services->attachment->delFile($ids, $mall['id']);
  156. } catch (\Exception $e) {
  157. return $this->error($e->getMessage());
  158. }
  159. return $this->success('操作成功。');
  160. }
  161. private function tree($list, $pid = 0)
  162. {
  163. $arr =[];
  164. foreach ($list as $k=>$v){
  165. if($v['parent_id']==$pid){
  166. $row['id'] = $v['id'];
  167. $row['parent_id'] = $v['parent_id'];
  168. $row['label'] = $v['name'];
  169. $row['children']= $this->tree($list,$v['id']);
  170. $arr[]=$row;
  171. }
  172. }
  173. return $arr;
  174. }
  175. }