session->get('mall'); return [ 'upload' => [ 'class' => 'common\components\UploadAction', 'mall_id' => !empty($mall) ? $mall['id'] : 0, 'store_id' => $this->store_id, '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, $this->store_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'] = 0; $post['store_id'] = $this->store_id; 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'],0, true, $this->store_id); 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, 'store_id' => [0, $this->store_id], // 只能看到自己门店和平台的 ]); $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['store_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, 'mch_id' => 0 ]); if (!$attachmentGroup) { return $this->error('Error:分组不存在'); } if ($attachmentGroup['store_id'] != $this->store_id) { return $this->error('Error:只允许移动到当前门店的分组'); } $findIds = Attachment::find() ->where(['id' => $ids, 'store_id' => $this->store_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, 'store_id' => $this->store_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['store_id'] = $v['store_id']; $row['children']= $this->tree($list,$v['id']); $arr[]=$row; } } return $arr; } }