FileController.php 7.6 KB

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