| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace App\Http\Controllers\V1;
- use App\Models\Product;
- use App\Models\UserCollect;
- use App\Models\UserFolder;
- use App\Models\UserFolderImage;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- /**
- * 用户文件夹
- */
- class UserFolderController extends Controller
- {
- public function __construct()
- {
- $this->user = auth('api')->user();
- $this->userId = $this->user ? $this->user->id : 0;
- //如果用户被删除,会自动退出登录
- if (!empty($this->user->deleted_at)) {
- $this->user->online = 0;
- $this->user->save();
- auth('api')->logout();
- }
- }
- /**
- * @return void
- * 新建文件夹
- */
- public function addFolder(Request $request){
- $params = $request->all();
- if(empty($params['name'])){
- return $this->error("名称不能为空!");
- }
- $params['user_id'] = $this->userId; // 用户ID
- $res = UserFolder::query()->create($params);
- if(!$res){
- return $this->error("创建失败!");
- }
- return $this->success();
- }
- /**
- * @return void
- * 文件夹列表
- */
- public function folderList(){
- $list = UserFolder::query()->withCount('images')->where('user_id','=',$this->userId)->get();
- return $this->success($list);
- }
- /**
- * @return void
- * 文件夹详情
- */
- public function folderDetail(Request $request){
- $id = $request->get('id');
- if(!$id){
- return $this->error("缺少参数ID!");
- }
- $res = UserFolder::query()->where('id',$id)->first();
- return $this->success($res);
- }
- /**
- * @return void
- * 编辑文件夹
- */
- public function editFolder(Request $request){
- $params = $request->all();
- if(empty($params['id'])){
- return $this->error("缺少参数ID");
- }
- if(empty($params['name'])){
- return $this->error("标题不能为空!");
- }
- DB::beginTransaction();
- try {
- $folder = UserFolder::query()->where('id',$params['id'])->first();
- if(!$folder){
- throw new \Exception("数据不存在!");
- }
- if(!empty($params['is_del'])){
- // 删除图片库
- UserFolderImage::query()->where('folder_id',$params['id'])->where('user_id',$this->userId)->delete();
- // 删除文件夹
- $folder->delete();
- }else{
- $folder->update($params);
- }
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return $this->error($e->getMessage());
- }
- return $this->success();
- }
- /**
- * @return void
- * 移动文件
- */
- public function moveFile(Request $request){
- $params = $request->all();
- if(empty($params['folder_id'])){
- return $this->error("缺少文件夹ID");
- }
- // $params['product_ids'] = [1];
- if(empty($params['product_ids'])){
- return $this->error("文件ID");
- }
- DB::beginTransaction();
- try {
- $product = Product::query()->whereIn('id',$params['product_ids'])->select("id","image")->get();
- if($product){
- foreach ($product as $v){
- $data = [
- 'user_id'=>$this->userId,
- 'folder_id'=>$params['folder_id'],
- 'image'=>$v['image']
- ];
- UserFolderImage::query()->create($data); // 移动文件
- UserCollect::query()->where('product_id',$v['id'])->update(['is_arrange'=>1]); // 标识已整理的文件
- }
- }
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return $this->error($e->getMessage());
- }
- return $this->success();
- }
- /**
- * @return void
- * 图片列表
- */
- public function imageList(Request $request){
- $id = $request->get('folder_id');
- $limit = $request->get('limit',10);
- if(!$id){
- return $this->error("缺少参数ID!");
- }
- $list = UserFolderImage::query()->where('folder_id','=',$id)->where('user_id',$this->userId)->paginate($limit);
- return $this->success($this->page($list));
- }
- }
|