['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; } }