Wallpaper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\WallpaperModel;
  5. class Wallpaper extends BaseController
  6. {
  7. function editFolder()
  8. {
  9. $this->getAdmin();
  10. is_demo_mode(true);
  11. $data = $this->request->post();
  12. if (isset($data['id']) && strlen($data['id']) > 0) {
  13. $mode = WallpaperModel::find($data['id']);
  14. } else {
  15. $mode = new WallpaperModel();
  16. }
  17. $mode->name = $data['name'];
  18. $mode->type = 1;
  19. $mode->save();
  20. $list = WallpaperModel::where("type", 1)->field("id,name,type,sort,create_time")->order("sort")->select();
  21. return $this->success("处理完毕", $list);
  22. }
  23. function DelFolder()
  24. {
  25. $this->getAdmin();
  26. $id = $this->request->post("id");
  27. if ($id) {
  28. $find = WallpaperModel::where("id", $id)->find();
  29. if (!$find) {
  30. return $this->error("分类不存在!");
  31. }
  32. }
  33. $find->delete();
  34. $list = WallpaperModel::where("type", 0)->where("folder", $id)->select()->toArray();
  35. foreach ($list as $key => $value) {
  36. //删除资源
  37. $url = joinPath(public_path(), $value['url']);
  38. if (file_exists($url)) {
  39. try {
  40. unlink($url);
  41. } catch (\Throwable $th) {
  42. }
  43. }
  44. $cover = joinPath(public_path(), $value['cover']);
  45. if (file_exists($cover)) {
  46. try {
  47. unlink($cover);
  48. } catch (\Throwable $th) {
  49. }
  50. }
  51. }
  52. $list = WallpaperModel::where("type", 0)->where("folder", $id)->delete(); //删除数据库数据;
  53. $list = WallpaperModel::where("type", 1)->field("id,name,type,sort,create_time")->order("sort")->select();
  54. return $this->success("ok", $list);
  55. }
  56. function getFolder()
  57. {
  58. $this->getAdmin();
  59. $list = WallpaperModel::where("type", 1)->field("id,name,type,sort,create_time")->order("sort")->select();
  60. return $this->success("ok", $list);
  61. }
  62. function getFolderClient()
  63. {
  64. $list = WallpaperModel::where("type", 1)->field("id,name,type,sort,create_time")->order("sort")->select();
  65. return $this->success("ok", $list);
  66. }
  67. function getFolderWallpaper()
  68. {
  69. $this->getAdmin();
  70. $folder_id = $this->request->post("id");
  71. if ($folder_id) {
  72. $list = WallpaperModel::where('type', 0)->where("folder", $folder_id)->order("create_time", 'desc')->paginate($this->request->post("limit", 19));
  73. return $this->success("ok", $list);
  74. }
  75. }
  76. function getFolderWallpaperClient()
  77. {
  78. $folder_id = $this->request->post("id");
  79. $offset = $this->request->post("offset", 0);
  80. if ($folder_id) {
  81. $list = WallpaperModel::where("type", 0)->where("folder", $folder_id)->field("create_time,id,folder,cover,type,mime,url")->order("id", 'desc')->limit($offset * 20, 20)->select();
  82. return $this->success("ok", $list);
  83. }
  84. }
  85. function deleteWallpaper()
  86. {
  87. $this->getAdmin();
  88. $id = $this->request->post("id");
  89. if ($id) {
  90. $find = WallpaperModel::where("id", $id)->find();
  91. if ($find) {
  92. $find->delete();
  93. //删除资源
  94. $url = joinPath(public_path(), $find['url']);
  95. if (file_exists($url)) {
  96. try {
  97. unlink($url);
  98. } catch (\Throwable $th) {
  99. }
  100. }
  101. $cover = joinPath(public_path(), $find['cover']);
  102. if (file_exists($cover)) {
  103. try {
  104. unlink($cover);
  105. } catch (\Throwable $th) {
  106. }
  107. }
  108. }
  109. return $this->success("ok");
  110. }
  111. }
  112. function addWallpaper()
  113. {
  114. $this->getAdmin();
  115. $data = [];
  116. $data['cover'] = $this->request->post("cover");
  117. $data['url'] = $this->request->post("url");
  118. $data['type'] = $this->request->post("type");
  119. $data['mime'] = $this->request->post("mime");
  120. $data['folder'] = $this->request->post("folder");
  121. $id = $this->request->post("id");
  122. if($id){
  123. $res = WallpaperModel::where("id", $id)->find();
  124. if($res){
  125. $res->save($data);
  126. }
  127. }else{
  128. $res = WallpaperModel::create($data);
  129. }
  130. return $this->success("ok", $res);
  131. }
  132. function sortFolder()
  133. {
  134. $this->getAdmin();
  135. $sort = (array)$this->request->post();
  136. foreach ($sort as $key => $value) {
  137. WallpaperModel::where("id", $value['id'])->update(['sort' => $value['sort']]);
  138. }
  139. return $this->success("ok");
  140. }
  141. }