UploadImageController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 17/3/10
  6. * Time: 下午2:25
  7. */
  8. namespace app\backend\modules\upload\controllers;
  9. use app\common\components\BaseController;
  10. use app\backend\modules\upload\models\CoreAttach;
  11. class UploadImageController extends BaseController
  12. {
  13. protected $isPublic = true;
  14. protected $uniacid;
  15. protected $common;
  16. public function __construct()
  17. {
  18. $this->uniacid = \YunShop::app()->uniacid ? : 0 ;
  19. $this->common = $this->common();
  20. }
  21. public function getImage(){
  22. $core_attach = new CoreAttach();
  23. if (request()->year != '不限') {
  24. $search['year'] = request()->year;
  25. }
  26. if(request()->month != '不限') {
  27. $search['month'] = request()->month;
  28. }
  29. $core_attach = $core_attach->search($search);
  30. $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
  31. if (!$this->uniacid) {
  32. $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
  33. }
  34. //type = 1 图片
  35. $core_attach = $core_attach->where('type', CoreAttach::IMAGE_TYPE);
  36. $core_attach = $core_attach->orderby('createtime', 'desc')->paginate(CoreAttach::PAGE_SIZE);
  37. foreach ($core_attach as &$meterial) {
  38. $meterial['url'] = yz_tomedia($meterial['attachment']);
  39. unset($meterial['uid']);
  40. }
  41. return $this->successJson('ok',$core_attach);
  42. }
  43. public function common()
  44. {
  45. $dest_dir = request()->dest_dir;
  46. $type = in_array(request()->upload_type, array('image','audio','video')) ? request()->upload_type : 'image';
  47. $option = array_elements(array('uploadtype', 'global', 'dest_dir'), $_POST);
  48. $option['width'] = intval($option['width']);
  49. $option['global'] = request()->global;
  50. if (preg_match('/^[a-zA-Z0-9_\/]{0,50}$/', $dest_dir, $out)) {
  51. $dest_dir = trim($dest_dir, '/');
  52. $pieces = explode('/', $dest_dir);
  53. if(count($pieces) > 3){
  54. $dest_dir = '';
  55. }
  56. } else {
  57. $dest_dir = '';
  58. }
  59. $module_upload_dir = '';
  60. if($dest_dir != '') {
  61. $module_upload_dir = sha1($dest_dir);
  62. }
  63. if ($option['global']) {
  64. $folder = "{$type}s/global/";
  65. if ($dest_dir) {
  66. $folder .= '' . $dest_dir . '/';
  67. }
  68. } else {
  69. $folder = "{$type}s/{$this->uniacid}";
  70. if (!$dest_dir) {
  71. $folder .= '/' . date('Y/m/');
  72. } else {
  73. $folder .= '/' . $dest_dir . '/';
  74. }
  75. }
  76. return [
  77. 'dest_dir' => $dest_dir,
  78. 'module_upload_dir' => $module_upload_dir,
  79. 'type' => $type,
  80. 'options' => $option,
  81. 'folder' => $folder,
  82. ];
  83. }
  84. }