| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace backend\modules\common\controllers;
- use Yii;
- use Exception;
- use common\enums\RabbitMqEnum;
- use yii\filters\AccessControl;
- use common\helpers\ArrayHelper;
- use common\helpers\UploadHelper;
- use common\models\common\Attachment;
- use services\common\RabbitMqService;
- use backend\controllers\BaseController;
- use common\models\common\AttachmentGroup;
- /**
- * 文件管理
- *
- * @Author bing
- * @DateTime 2021-08-10 17:29:36
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class FileController extends BaseController
- {
- public function behaviors()
- {
- $behaviors = parent::behaviors();
- $behaviors['access']['rules'] = array_merge($behaviors['access']['rules'], [
- [
- 'actions' => ['group-list','selector'],
- 'allow' => true,
- ],
- ]);
- return $behaviors;
- }
- public function actions(){
- $mall = Yii::$app->session->get('mall');
- return [
- 'upload' => [
- 'class' => 'common\components\UploadAction',
- 'mall_id' => !empty($mall) ? $mall['id'] : 0,
- 'mch_id' => 0,
- 'upload_type' => Yii::$app->request->post('upload_type') ?? '',
- 'group_id' => Yii::$app->request->get('attachment_group_id') ?? 0,
- ]
- ];
- }
- /**
- * 资源选择器
- *
- * @return array|string
- */
- public function actionSelector()
- {
- $upload_type = Yii::$app->request->get('upload_type', Attachment::UPLOAD_TYPE_IMAGES);
- $keyword = Yii::$app->request->get('keyword', '');
- $drive = Yii::$app->request->get('drive', '');
- $attachment_group_id = Yii::$app->request->get('attachment_group_id', 0);
- $mall = Yii::$app->session->get('mall');
- $mall_id = !empty($mall) ? $mall['id'] : 0;
- $result = Yii::$app->services->attachment->getListPage($mall_id, $upload_type, $attachment_group_id, $drive, $keyword);
- return $this->success('ok',$result);
- }
- /**
- * 添加、编辑目录
- * @return \yii\web\Response
- */
- public function actionSaveGroup()
- {
- try{
- $post = Yii::$app->request->post();
- $mall = Yii::$app->session->get('mall');
- $post['mall_id'] = !empty($mall) ? $mall['id'] : 0;
- $post['mch_id'] = 0;
- Yii::$app->services->attachment->saveGroup($post);
- return $this->success('操作成功。');
- }catch(Exception $e){
- return $this->error('Error:'.$e->getMessage());
- }
- }
- /**
- * 删除目录
- * @return \yii\web\Response
- */
- public function actionDelGroup()
- {
- try{
- $post = Yii::$app->request->post();
- $mall = Yii::$app->session->get('mall');
- Yii::$app->services->attachment->deleteGroup($post['id'], $mall['id'], 0);
- return $this->success('操作成功。');
- }catch(Exception $e){
- return $this->error('Error:'.$e->getMessage());
- }
- }
- /**
- * 获取分组列表
- * @return mixed|void
- */
- public function actionGroupList()
- {
- $type = Yii::$app->request->get('type') ?? '';
- $is_recycle = Yii::$app->request->get('is_recycle') ?? 0;
- $mall = Yii::$app->session->get('mall');
- // $type = $type === 'video' ? 1 : 0;
- $type = in_array($type, ['video', 'videos']) ? 1 : 0;
- $list = Yii::$app->services->attachment->getGroupByTypeAndMallId($mall['id'] ?? 0, $type, $is_recycle);
- $arr = $this->tree($list);
- $return = [
- 'list' => $list,
- 'arr' => $arr,
- ];
- return $this->success('操作成功', $return);
- }
- /**
- * 文件移动目录
- * @return mixed|void
- */
- public function actionMove()
- {
- $validateService = Yii::$app->services->validate;
- $rules = [
- [['ids','attachment_group_id'],'required'],
- ['attachment_group_id', 'integer']
- ];
- $labels = [
- 'id' => 'ID',
- 'attachment_group_id' => '目录ID'
- ];
- $valid = $validateService->validate(\Yii::$app->request->post(), $rules,$labels);
- if (!$valid) {
- return $this->error( $validateService->getFirstErrorSummary());
- }
- $ids = Yii::$app->request->post('ids');
- $ids = is_array($ids) ? $ids : explode(',',$ids);
- $group_id = Yii::$app->request->post('attachment_group_id');
- $mall = Yii::$app->session->get('mall');
- try {
- Yii::$app->services->attachment->moveFileToGroupId($ids, $mall['id'], $group_id);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功。');
- }
- /**
- * 删除文件
- * @return mixed|void
- */
- public function actionDelete()
- {
- $ids = Yii::$app->request->post('ids') ?? '';
- $ids = is_array($ids) ? $ids : explode(',',$ids);
- if(empty($ids)){
- return $this->error('ids 不能为空');
- }
- $mall = Yii::$app->session->get('mall');
- try {
- Yii::$app->services->attachment->delFile($ids, $mall['id']);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('操作成功。');
- }
- private function tree($list, $pid = 0)
- {
- $arr =[];
- foreach ($list as $k=>$v){
- if($v['parent_id']==$pid){
- $row['id'] = $v['id'];
- $row['parent_id'] = $v['parent_id'];
- $row['label'] = $v['name'];
- $row['children']= $this->tree($list,$v['id']);
- $arr[]=$row;
- }
- }
- return $arr;
- }
- }
|