FileController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace addons\Mch\client\controllers;
  3. use common\enums\RabbitMqEnum;
  4. use Yii;
  5. use common\models\common\Attachment;
  6. use common\models\common\AttachmentGroup;
  7. use Exception;
  8. use common\helpers\ArrayHelper;
  9. /**
  10. * 文件管理
  11. * @Author bing
  12. * @DateTime 2021-08-10 17:29:36
  13. * @copyright: Copyright (c) 2020 广东七件事集团
  14. */
  15. class FileController extends BaseController
  16. {
  17. public $enableCsrfValidation = false;
  18. public function actions(){
  19. $mall = Yii::$app->session->get('mall');
  20. return [
  21. 'upload' => [
  22. 'class' => 'common\components\UploadAction',
  23. 'mall_id' => !empty($mall) ? $mall['id'] : 0,
  24. 'mch_id' => $this->mch_id,
  25. 'store_id' => 0,
  26. 'upload_type' => Yii::$app->request->post('upload_type') ?? '',
  27. 'group_id' => Yii::$app->request->get('attachment_group_id') ?? 0,
  28. ]
  29. ];
  30. }
  31. /**
  32. * 资源选择器
  33. *
  34. * @return array|string
  35. */
  36. public function actionSelector()
  37. {
  38. $upload_type = Yii::$app->request->get('upload_type', Attachment::UPLOAD_TYPE_IMAGES);
  39. $keyword = Yii::$app->request->get('keyword', '');
  40. $drive = Yii::$app->request->get('drive', '');
  41. $attachment_group_id = Yii::$app->request->get('attachment_group_id', 0);
  42. $mall = Yii::$app->session->get('mall');
  43. $mall_id = !empty($mall) ? $mall['id'] : 0;
  44. $result = Yii::$app->services->attachment->getListPage($mall_id,$upload_type,$attachment_group_id, $drive, $keyword, 0, $this->mch_id);
  45. return $this->success('ok',$result);
  46. }
  47. /**
  48. * 添加、编辑目录
  49. * @return \yii\web\Response
  50. */
  51. public function actionSaveGroup()
  52. {
  53. try{
  54. $post = Yii::$app->request->post();
  55. $mall = Yii::$app->session->get('mall');
  56. $post['mall_id'] = !empty($mall) ? $mall['id'] : 0;
  57. $post['mch_id'] = $this->mch_id;
  58. $post['store_id'] = 0;
  59. AttachmentGroup::saveGroup($post, true);
  60. return $this->success('操作成功。');
  61. }catch(Exception $e){
  62. return $this->error('Error:'.$e->getMessage());
  63. }
  64. }
  65. /**
  66. * 删除目录
  67. * @return \yii\web\Response
  68. */
  69. public function actionDelGroup()
  70. {
  71. try{
  72. $post = Yii::$app->request->post();
  73. $mall = Yii::$app->session->get('mall');
  74. AttachmentGroup::deleteGroup($post['id'],$mall['id'],$this->mch_id, true, 0);
  75. return $this->success('操作成功。');
  76. }catch(Exception $e){
  77. return $this->error('Error:'.$e->getMessage());
  78. }
  79. }
  80. /**
  81. * 获取分组列表
  82. * @return mixed|void
  83. */
  84. public function actionGroupList()
  85. {
  86. $type = Yii::$app->request->get('type') ?? '';
  87. $is_recycle = Yii::$app->request->get('is_recycle') ?? 0;
  88. $mall = Yii::$app->session->get('mall');
  89. $query = AttachmentGroup::find()->where([
  90. 'mall_id' => !empty($mall) ? $mall['id'] : 0,
  91. 'is_delete' => 0,
  92. 'mch_id' => [0, $this->mch_id], // 只能看到自己多商户和平台的
  93. 'store_id' => 0
  94. ]);
  95. $type && $query->andWhere(['type' => $type === 'video' ? 1 : 0]);
  96. $is_recycle && $query->andWhere(['is_recycle' => $is_recycle]);
  97. $list = $query->asArray()->all();
  98. // 将系统图标显示出来
  99. $list = ArrayHelper::merge($list, Yii::$app->services->attachment->systemIconGroup);
  100. $iconIds = array_column(Yii::$app->services->attachment->systemIconGroup, 'id');
  101. $arr = $this->tree($list);
  102. /* 将平台的分组直接放到系统图片里面 */
  103. $systemImg = [
  104. 'id' => 2000000,
  105. 'label' => '系统图片',
  106. 'children' => []
  107. ];
  108. foreach ($arr as $key => $item) {
  109. if (empty($item['mch_id']) && !in_array($item['id'], $iconIds)) {
  110. $systemImg['children'][] = $item;
  111. unset($arr[$key]);
  112. }
  113. }
  114. $arr[] = $systemImg;
  115. $arr = array_values($arr);
  116. $return = [
  117. 'list' =>$list,
  118. 'arr' =>$arr,
  119. ];
  120. return $this->success('操作成功',$return);
  121. }
  122. /**
  123. * 文件移动目录
  124. * @return mixed|void
  125. */
  126. public function actionMove()
  127. {
  128. $validateService = Yii::$app->services->validate;
  129. $rules = [
  130. [['ids','attachment_group_id'],'required'],
  131. ['attachment_group_id', 'integer']
  132. ];
  133. $labels = [
  134. 'id' => 'ID',
  135. 'attachment_group_id' => '目录ID'
  136. ];
  137. $valid = $validateService->validate(\Yii::$app->request->post(), $rules,$labels);
  138. if (!$valid) {
  139. return $this->error( $validateService->getFirstErrorSummary());
  140. }
  141. $ids = Yii::$app->request->post('ids');
  142. $ids = is_array($ids) ? $ids : explode(',',$ids);
  143. $group_id = Yii::$app->request->post('attachment_group_id');
  144. $mall = Yii::$app->session->get('mall');
  145. $attachmentGroup = AttachmentGroup::findOne([
  146. 'id' => $group_id,
  147. 'mall_id' => $mall['id'],
  148. 'is_delete' => 0,
  149. 'store_id' => 0
  150. ]);
  151. if (!$attachmentGroup) {
  152. return $this->error('Error:分组不存在');
  153. }
  154. if ($attachmentGroup['mch_id'] != $this->mch_id) {
  155. return $this->error('Error:只允许移动到当前多商户的分组');
  156. }
  157. $findIds = Attachment::find()
  158. ->where(['id' => $ids, 'mch_id' => $this->mch_id])
  159. ->select('id')
  160. ->column();
  161. $ids = array_intersect($findIds, $ids);
  162. if (empty($ids)) {
  163. return $this->error('只允许操作当前多商户的记录');
  164. }
  165. Attachment::updateAll(['group_id' => $group_id], [
  166. 'id' => $ids,
  167. 'mall_id' => $mall['id'],
  168. ]);
  169. return $this->success('操作成功。');
  170. }
  171. /**
  172. * 删除文件
  173. * @return mixed|void
  174. */
  175. public function actionDelete()
  176. {
  177. $ids = Yii::$app->request->post('ids') ?? '';
  178. $ids = is_array($ids) ? $ids : explode(',',$ids);
  179. if(empty($ids)){
  180. return $this->error('ids 不能为空');
  181. }
  182. $mall = Yii::$app->session->get('mall');
  183. $findIds = Attachment::find()
  184. ->where(['id' => $ids, 'mch_id' => $this->mch_id])
  185. ->select('id')
  186. ->column();
  187. $ids = array_intersect($findIds, $ids);
  188. if (empty($ids)) {
  189. return $this->error('只允许操作当前多商户的记录');
  190. }
  191. Attachment::updateAll(['is_delete' => Attachment::IS_DELETE_TEMP], [
  192. 'id' => $ids,
  193. 'mall_id' => $mall['id'],
  194. ]);
  195. Yii::$app->services->rabbitMq->push(
  196. RabbitMqEnum::EXCHANGE_TASK,
  197. RabbitMqEnum::TASK_QUEUE,
  198. $ids,
  199. get_class(Yii::$app->services->attachment),
  200. 'delete'
  201. );
  202. return $this->success('操作成功。');
  203. }
  204. private function tree($list,$pid=0){
  205. $arr =[];
  206. foreach ($list as $k=>$v){
  207. if($v['parent_id']==$pid){
  208. $row['id'] = $v['id'];
  209. $row['label'] = $v['name'];
  210. $row['mch_id'] = $v['mch_id'];
  211. $row['children']= $this->tree($list,$v['id']);
  212. $arr[]=$row;
  213. }
  214. }
  215. return $arr;
  216. }
  217. }