UserFolderController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Models\Product;
  4. use App\Models\UserCollect;
  5. use App\Models\UserFolder;
  6. use App\Models\UserFolderImage;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 用户文件夹
  11. */
  12. class UserFolderController extends Controller
  13. {
  14. public function __construct()
  15. {
  16. $this->user = auth('api')->user();
  17. $this->userId = $this->user ? $this->user->id : 0;
  18. //如果用户被删除,会自动退出登录
  19. if (!empty($this->user->deleted_at)) {
  20. $this->user->online = 0;
  21. $this->user->save();
  22. auth('api')->logout();
  23. }
  24. }
  25. /**
  26. * @return void
  27. * 新建文件夹
  28. */
  29. public function addFolder(Request $request){
  30. $params = $request->all();
  31. if(empty($params['name'])){
  32. return $this->error("名称不能为空!");
  33. }
  34. $params['user_id'] = $this->userId; // 用户ID
  35. $res = UserFolder::query()->create($params);
  36. if(!$res){
  37. return $this->error("创建失败!");
  38. }
  39. return $this->success();
  40. }
  41. /**
  42. * @return void
  43. * 文件夹列表
  44. */
  45. public function folderList(){
  46. $list = UserFolder::query()->withCount('images')->where('user_id','=',$this->userId)->get();
  47. return $this->success($list);
  48. }
  49. /**
  50. * @return void
  51. * 文件夹详情
  52. */
  53. public function folderDetail(Request $request){
  54. $id = $request->get('id');
  55. if(!$id){
  56. return $this->error("缺少参数ID!");
  57. }
  58. $res = UserFolder::query()->where('id',$id)->first();
  59. return $this->success($res);
  60. }
  61. /**
  62. * @return void
  63. * 编辑文件夹
  64. */
  65. public function editFolder(Request $request){
  66. $params = $request->all();
  67. if(empty($params['id'])){
  68. return $this->error("缺少参数ID");
  69. }
  70. if(empty($params['name'])){
  71. return $this->error("标题不能为空!");
  72. }
  73. DB::beginTransaction();
  74. try {
  75. $folder = UserFolder::query()->where('id',$params['id'])->first();
  76. if(!$folder){
  77. throw new \Exception("数据不存在!");
  78. }
  79. if(!empty($params['is_del'])){
  80. // 删除图片库
  81. UserFolderImage::query()->where('folder_id',$params['id'])->where('user_id',$this->userId)->delete();
  82. // 删除文件夹
  83. $folder->delete();
  84. }else{
  85. $folder->update($params);
  86. }
  87. DB::commit();
  88. }catch (\Exception $e){
  89. DB::rollBack();
  90. return $this->error($e->getMessage());
  91. }
  92. return $this->success();
  93. }
  94. /**
  95. * @return void
  96. * 移动文件
  97. */
  98. public function moveFile(Request $request){
  99. $params = $request->all();
  100. if(empty($params['folder_id'])){
  101. return $this->error("缺少文件夹ID");
  102. }
  103. // $params['product_ids'] = [1];
  104. if(empty($params['product_ids'])){
  105. return $this->error("文件ID");
  106. }
  107. DB::beginTransaction();
  108. try {
  109. $product = Product::query()->whereIn('id',$params['product_ids'])->select("id","image")->get();
  110. if($product){
  111. foreach ($product as $v){
  112. $data = [
  113. 'user_id'=>$this->userId,
  114. 'folder_id'=>$params['folder_id'],
  115. 'image'=>$v['image']
  116. ];
  117. UserFolderImage::query()->create($data); // 移动文件
  118. UserCollect::query()->where('product_id',$v['id'])->update(['is_arrange'=>1]); // 标识已整理的文件
  119. }
  120. }
  121. DB::commit();
  122. }catch (\Exception $e){
  123. DB::rollBack();
  124. return $this->error($e->getMessage());
  125. }
  126. return $this->success();
  127. }
  128. /**
  129. * @return void
  130. * 图片列表
  131. */
  132. public function imageList(Request $request){
  133. $id = $request->get('folder_id');
  134. $limit = $request->get('limit',10);
  135. if(!$id){
  136. return $this->error("缺少参数ID!");
  137. }
  138. $list = UserFolderImage::query()->where('folder_id','=',$id)->where('user_id',$this->userId)->paginate($limit);
  139. return $this->success($this->page($list));
  140. }
  141. }