| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace addons\Mch\client\controllers;
- use common\enums\RabbitMqEnum;
- use Yii;
- use common\models\common\Attachment;
- use common\models\common\AttachmentGroup;
- use Exception;
- use common\helpers\ArrayHelper;
- /**
- * 文件管理
- * @Author bing
- * @DateTime 2021-08-10 17:29:36
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class FileController extends BaseController
- {
- public $enableCsrfValidation = false;
- public function actions(){
- $mall = Yii::$app->session->get('mall');
- return [
- 'upload' => [
- 'class' => 'common\components\UploadAction',
- 'mall_id' => !empty($mall) ? $mall['id'] : 0,
- 'mch_id' => $this->mch_id,
- 'store_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, 0, $this->mch_id);
- 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'] = $this->mch_id;
- $post['store_id'] = 0;
- AttachmentGroup::saveGroup($post, true);
- 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');
- AttachmentGroup::deleteGroup($post['id'],$mall['id'],$this->mch_id, true, 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');
- $query = AttachmentGroup::find()->where([
- 'mall_id' => !empty($mall) ? $mall['id'] : 0,
- 'is_delete' => 0,
- 'mch_id' => [0, $this->mch_id], // 只能看到自己多商户和平台的
- 'store_id' => 0
- ]);
- $type && $query->andWhere(['type' => $type === 'video' ? 1 : 0]);
- $is_recycle && $query->andWhere(['is_recycle' => $is_recycle]);
- $list = $query->asArray()->all();
- // 将系统图标显示出来
- $list = ArrayHelper::merge($list, Yii::$app->services->attachment->systemIconGroup);
- $iconIds = array_column(Yii::$app->services->attachment->systemIconGroup, 'id');
- $arr = $this->tree($list);
- /* 将平台的分组直接放到系统图片里面 */
- $systemImg = [
- 'id' => 2000000,
- 'label' => '系统图片',
- 'children' => []
- ];
- foreach ($arr as $key => $item) {
- if (empty($item['mch_id']) && !in_array($item['id'], $iconIds)) {
- $systemImg['children'][] = $item;
- unset($arr[$key]);
- }
- }
- $arr[] = $systemImg;
- $arr = array_values($arr);
- $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');
- $attachmentGroup = AttachmentGroup::findOne([
- 'id' => $group_id,
- 'mall_id' => $mall['id'],
- 'is_delete' => 0,
- 'store_id' => 0
- ]);
- if (!$attachmentGroup) {
- return $this->error('Error:分组不存在');
- }
- if ($attachmentGroup['mch_id'] != $this->mch_id) {
- return $this->error('Error:只允许移动到当前多商户的分组');
- }
- $findIds = Attachment::find()
- ->where(['id' => $ids, 'mch_id' => $this->mch_id])
- ->select('id')
- ->column();
- $ids = array_intersect($findIds, $ids);
- if (empty($ids)) {
- return $this->error('只允许操作当前多商户的记录');
- }
- Attachment::updateAll(['group_id' => $group_id], [
- 'id' => $ids,
- 'mall_id' => $mall['id'],
- ]);
- 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');
- $findIds = Attachment::find()
- ->where(['id' => $ids, 'mch_id' => $this->mch_id])
- ->select('id')
- ->column();
- $ids = array_intersect($findIds, $ids);
- if (empty($ids)) {
- return $this->error('只允许操作当前多商户的记录');
- }
- Attachment::updateAll(['is_delete' => Attachment::IS_DELETE_TEMP], [
- 'id' => $ids,
- 'mall_id' => $mall['id'],
- ]);
- Yii::$app->services->rabbitMq->push(
- RabbitMqEnum::EXCHANGE_TASK,
- RabbitMqEnum::TASK_QUEUE,
- $ids,
- get_class(Yii::$app->services->attachment),
- 'delete'
- );
- 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['label'] = $v['name'];
- $row['mch_id'] = $v['mch_id'];
- $row['children']= $this->tree($list,$v['id']);
- $arr[]=$row;
- }
- }
- return $arr;
- }
- }
|