BaseController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Menu;
  4. use App\Models\MenusAd;
  5. use App\Http\Controllers\Controller;
  6. class BaseController extends Controller
  7. {
  8. public function __construct($menu_name = null, $menu_href = null)
  9. {
  10. $this->menu = new MenusAd();
  11. $this->menu_home = new Menu();
  12. if ($menu_name) {
  13. $this_menu = MenusAd::where('ename', $menu_name)
  14. ->first();
  15. }
  16. else if ($menu_href) {
  17. $this_menu = MenusAd::where('href', $menu_href)
  18. ->first();
  19. }
  20. $first_menus = $this->menu->getFirstMenus();
  21. $this->view_para = [
  22. 'this_menu' => $this_menu,
  23. 'model_menu' => $this->menu,
  24. 'first_menus' => $first_menus,
  25. ];
  26. $this->image_name = time().rand(100000, 999999);
  27. $this->image_path = 'uploads/image/'.date('Ymd');
  28. }
  29. public function getSomeSecondMenus($menus_sort1)
  30. {
  31. return Menu::whereIn('sort1', $menus_sort1)
  32. ->where('sort2', '<>', 0)
  33. ->orderBy('sort1')
  34. ->get();
  35. }
  36. public function imageValidator($image)
  37. {
  38. $ext = $image->getClientOriginalExtension();
  39. $ext_rule = ['jpg', 'jpeg', 'png', 'gif'];
  40. if (!in_array($ext, $ext_rule)) {
  41. return '图片格式只能为jpg/jpeg/png/gif';
  42. }
  43. $size = $image->getClientSize();
  44. $max_size = 2097152; // 字节数
  45. if ($size > $max_size) {
  46. return '图片大小不可超过2048kb';
  47. }
  48. }
  49. public function imageUpload($name, $image, $path)
  50. {
  51. $ext = $image->getClientOriginalExtension();
  52. $newName = $name.'.'.$ext;
  53. $image->move($path, $newName);
  54. $urlPath = $path.'/'.$newName;
  55. return $urlPath;
  56. }
  57. public function imageDelete($path)
  58. {
  59. $result = unlink($path);
  60. return $result;
  61. }
  62. public function imageRename($oldUrlPath, $newUrlPath)
  63. {
  64. $result = rename($oldUrlPath, $newUrlPath);
  65. return $result;
  66. }
  67. }