UploadController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: weifeng
  5. * Date: 2020-02-04
  6. * Time: 10:10
  7. *
  8. * .--, .--,
  9. * ( ( \.---./ ) )
  10. * '.__/o o\__.'
  11. * {= ^ =}
  12. * > - <
  13. * / \
  14. * // \\
  15. * //| . |\\
  16. * "'\ /'"_.-~^`'-.
  17. * \ _ /--' `
  18. * ___)( )(___
  19. * (((__) (__))) 梦之所想,心之所向.
  20. */
  21. namespace app\backend\modules\upload\controllers;
  22. use app\backend\modules\upload\models\CoreAttach;
  23. use app\common\components\BaseController;
  24. use app\common\exceptions\ShopException;
  25. use app\common\services\ImageZip;
  26. use app\common\services\upload\UploadService;
  27. use app\platform\modules\system\models\SystemSetting;
  28. use getID3;
  29. class UploadController extends BaseController
  30. {
  31. protected $isPublic = true;
  32. protected $uniacid;
  33. public function __construct()
  34. {
  35. $this->uniacid = \YunShop::app()->uniacid ?: 0;
  36. }
  37. public function upload()
  38. {
  39. $file = request()->file('file');
  40. $type = request()->upload_type;
  41. if (!$file) {
  42. return $this->errorJson('文件上传失败.');
  43. }
  44. if (!$file->isValid()) {
  45. return $this->errorJson('文件上传失败.');
  46. }
  47. $auth_uid = \Auth::guard('admin')->user()->uid?:1;
  48. global $_W;
  49. // 获取文件相关信息
  50. $originalName = $file->getClientOriginalName(); // 文件原名
  51. $realPath = $file->getRealPath(); //临时文件的绝对路径
  52. $ext = $file->getClientOriginalExtension(); //文件后缀
  53. $uploadService = new UploadService();
  54. $upload_setting = $uploadService->getSetting();
  55. if ($type == 'image') {
  56. try {
  57. $upload_res = $uploadService->upload($file, $type);
  58. } catch (ShopException $exception) {
  59. return $this->errorJson($exception->getMessage());
  60. }
  61. if (config('app.framework') == 'platform') {
  62. $data = [
  63. 'uniacid' => $this->uniacid,
  64. 'uid' => $auth_uid,
  65. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  66. 'attachment' => $upload_res['relative_path'],
  67. 'type' => 1,
  68. 'module_upload_dir' => '',
  69. 'group_id' => intval($this->uniacid),
  70. 'upload_type' => $upload_setting['remote']['type'],
  71. 'tag_id' => 0
  72. ];
  73. \app\platform\modules\application\models\CoreAttach::create($data);
  74. return $this->successJson('上传成功', [
  75. 'name' => $originalName,
  76. 'ext' => $ext,
  77. 'filename' => $upload_res['file_name'],
  78. 'attachment' => $upload_res['relative_path'],
  79. 'url' => $upload_res['absolute_path'],
  80. 'is_image' => 1,
  81. 'filesize' => 'null',
  82. 'group_id' => intval($this->uniacid)
  83. ]);
  84. } else {
  85. $data = [
  86. 'uniacid' => $this->uniacid,
  87. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  88. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  89. 'attachment' => $upload_res['relative_path'],
  90. 'type' => 1,
  91. 'createtime' => TIMESTAMP,
  92. 'module_upload_dir' => '',
  93. 'group_id' => 0,
  94. ];
  95. CoreAttach::create($data);
  96. $info = array(
  97. 'name' => $originalName,
  98. 'ext' => $ext,
  99. 'filename' => $upload_res['file_name'],
  100. 'attachment' => $upload_res['relative_path'],
  101. 'url' => $upload_res['absolute_path'],
  102. 'is_image' => 1,
  103. 'filesize' => 'null',
  104. );
  105. if (request()->is_interface == 1) {
  106. return $this->successJson('上传成功',$info);
  107. }
  108. $info['state'] = 'SUCCESS';
  109. die(json_encode($info));
  110. }
  111. } elseif ($type == 'video') {
  112. try {
  113. $upload_res = $uploadService->upload($file, $type, 'videos');
  114. } catch (ShopException $exception) {
  115. return $this->errorJson($exception->getMessage());
  116. }
  117. if (config('app.framework') == 'platform') {
  118. $getID3 = new getID3();
  119. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  120. $timeline = $ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  121. $data = [
  122. 'uniacid' => $this->uniacid,
  123. 'uid' => $auth_uid,
  124. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  125. 'attachment' => $upload_res['relative_path'],
  126. 'type' => 3,
  127. 'module_upload_dir' => '',
  128. 'group_id' => intval($this->uniacid),
  129. 'upload_type' => $upload_setting['remote']['type'],
  130. 'timeline' => $timeline
  131. ];
  132. \app\platform\modules\application\models\CoreAttach::create($data);
  133. return $this->successJson('上传成功', [
  134. 'name' => $originalName,
  135. 'ext' => $ext,
  136. 'filename' => $upload_res['file_name'],
  137. 'attachment' => $upload_res['relative_path'],
  138. 'url' => $upload_res['absolute_path'],
  139. 'is_image' => 0,
  140. 'filesize' => 'null',
  141. 'group_id' => intval($this->uniacid)
  142. ]);
  143. } else {
  144. $data = [
  145. 'uniacid' => $this->uniacid,
  146. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  147. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  148. 'attachment' => $upload_res['relative_path'],
  149. 'type' => 3,
  150. 'createtime' => TIMESTAMP,
  151. 'module_upload_dir' => '',
  152. 'group_id' => 0,
  153. ];
  154. CoreAttach::create($data);
  155. $info = array(
  156. 'name' => $originalName,
  157. 'ext' => $ext,
  158. 'filename' => $upload_res['file_name'],
  159. 'attachment' => $upload_res['relative_path'],
  160. 'url' => $upload_res['absolute_path'],
  161. 'is_image' => 0,
  162. 'filesize' => 'null',
  163. );
  164. if (request()->is_interface == 1) {
  165. return $this->successJson('上传成功',$info);
  166. }
  167. $info['state'] = 'SUCCESS';
  168. die(json_encode($info));
  169. }
  170. } elseif ($type == 'audio') {
  171. try {
  172. $upload_res = $uploadService->upload($file, $type, 'audios');
  173. } catch (ShopException $exception) {
  174. return $this->errorJson($exception->getMessage());
  175. }
  176. if (config('app.framework') == 'platform') {
  177. $getID3 = new getID3();
  178. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  179. $timeline = $ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  180. $data = [
  181. 'uniacid' => $this->uniacid,
  182. 'uid' => $auth_uid,
  183. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  184. 'attachment' => $upload_res['relative_path'],
  185. 'type' => 3,
  186. 'module_upload_dir' => '',
  187. 'group_id' => intval($this->uniacid),
  188. 'upload_type' => $upload_setting['remote']['type'],
  189. 'timeline' => $timeline,
  190. 'tag_id' => 0
  191. ];
  192. \app\platform\modules\application\models\CoreAttach::create($data);
  193. return $this->successJson('上传成功', [
  194. 'name' => $originalName,
  195. 'ext' => $ext,
  196. 'filename' => $upload_res['file_name'],
  197. 'attachment' => $upload_res['relative_path'],
  198. 'url' => $upload_res['absolute_path'],
  199. 'is_image' => 0,
  200. 'filesize' => 'null',
  201. 'group_id' => intval($this->uniacid)
  202. ]);
  203. } else {
  204. $data = [
  205. 'uniacid' => $this->uniacid,
  206. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  207. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  208. 'attachment' => $upload_res['relative_path'],
  209. 'type' => 3,
  210. 'createtime' => TIMESTAMP,
  211. 'module_upload_dir' => '',
  212. 'group_id' => 0,
  213. 'tag_id' => 0
  214. ];
  215. CoreAttach::create($data);
  216. $info = array(
  217. 'name' => $originalName,
  218. 'ext' => $ext,
  219. 'filename' => $upload_res['file_name'],
  220. 'attachment' => $upload_res['relative_path'],
  221. 'url' => $upload_res['absolute_path'],
  222. 'is_image' => 0,
  223. 'filesize' => 'null',
  224. );
  225. $info['state'] = 'SUCCESS';
  226. die(json_encode($info));
  227. }
  228. }
  229. return true;
  230. }
  231. public function fetch()
  232. {
  233. $url = trim(request()->url);
  234. $resp = ihttp_get($url);
  235. if (!$resp) {
  236. return $this->errorJson('提取文件失败');
  237. }
  238. if (strexists($resp['headers']['Content-Type'], 'image')) {
  239. switch ($resp['headers']['Content-Type']) {
  240. case 'application/x-jpg':
  241. case 'image/jpeg':
  242. $ext = 'jpg';
  243. break;
  244. case 'image/png':
  245. $ext = 'png';
  246. break;
  247. case 'image/gif':
  248. $ext = 'gif';
  249. break;
  250. default:
  251. return $this->errorJson('提取资源失败, 资源文件类型错误.');
  252. break;
  253. }
  254. } else {
  255. return $this->errorJson('提取资源失败, 仅支持图片提取.');
  256. }
  257. $originName = pathinfo($url, PATHINFO_BASENAME);
  258. $newOriginalName = md5($originName . str_random(6)) . '.' . $ext;
  259. //本地上传
  260. $result = \Storage::disk('image')->put($newOriginalName, $resp['content']);
  261. if (!$result) {
  262. return $this->successJson('上传失败');
  263. }
  264. $url = \Storage::disk('image')->url($newOriginalName);
  265. if (config('app.framework') == 'platform') {
  266. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  267. $data = [
  268. 'uniacid' => $this->uniacid,
  269. 'uid' => \Auth::guard('admin')->user()->uid,
  270. 'filename' => $newOriginalName,
  271. 'attachment' => $url,
  272. 'type' => 1,
  273. 'module_upload_dir' => '',
  274. 'group_id' => intval($this->uniacid),
  275. 'upload_type' => $remote['type'],
  276. 'tag_id' => 0
  277. ];
  278. \app\platform\modules\application\models\CoreAttach::create($data);
  279. //远程上传
  280. if ($remote['type'] != 0) {
  281. file_remote_upload($url, true, $remote);
  282. }
  283. return $this->successJson('上传成功', [
  284. 'img' => $url,
  285. 'img_url' => yz_tomedia($url),
  286. ]);
  287. } else {
  288. //全局配置
  289. global $_W;
  290. //公众号独立配置信息 优先使用公众号独立配置
  291. $uni_setting = app('WqUniSetting')->get()->toArray();
  292. if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
  293. $setting['remote'] = iunserializer($uni_setting['remote']);
  294. $remote = $setting['remote'];
  295. } else {
  296. $remote = $_W['setting']['remote'];
  297. }
  298. $data = [
  299. 'uniacid' => $this->uniacid,
  300. 'uid' => $_W['uid'],
  301. 'filename' => $newOriginalName,
  302. 'attachment' => $url,
  303. 'type' => 1,
  304. 'createtime' => TIMESTAMP,
  305. 'module_upload_dir' => '',
  306. 'group_id' => 0,
  307. 'tag_id' => 0
  308. ];
  309. CoreAttach::create($data);
  310. //远程上传
  311. if ($remote['type'] != 0) {
  312. file_remote_upload_wq($url, true, $remote);
  313. }
  314. return $this->successJson('上传成功', [
  315. 'img' => $url,
  316. 'img_url' => yz_tomedia($url),
  317. ]);
  318. }
  319. }
  320. public function getImage()
  321. {
  322. if (config('app.framework') == 'platform') {
  323. $result = $this->getNewImage();
  324. } else {
  325. $result = $this->getWqImage();
  326. }
  327. return $this->successJson('ok', $result);
  328. }
  329. public function getWqImage()
  330. {
  331. $uid = \Auth::guard('admin')->user()->uid;
  332. $year = request()->year;
  333. $month = intval(request()->month);
  334. $page = max(1, intval(request()->page));
  335. $groupid = intval(request()->group_id);
  336. $page_size = 33;
  337. if ($page <= 1) {
  338. $page = 0;
  339. $offset = ($page)*$page_size;
  340. } else {
  341. $offset = ($page-1)*$page_size;
  342. }
  343. $core_attach = new CoreAttach;
  344. $core_attach = $core_attach->where(['uniacid'=>$this->uniacid,'type'=>1]);
  345. if (\YunShop::app()->isfounder !== true) {
  346. $core_attach = $core_attach->where('uid', $uid);
  347. }
  348. if ($groupid > 0) {
  349. $core_attach = $core_attach->where('group_id', $groupid);
  350. }
  351. if ($groupid == 0) {
  352. $core_attach = $core_attach->where('group_id', -1);
  353. }
  354. if ($year || $month) {
  355. $start_time = $month ? strtotime("{$year}-{$month}-01") : strtotime("{$year}-1-01");
  356. $end_time = $month ? strtotime('+1 month', $start_time) : strtotime('+12 month', $start_time);
  357. $core_attach = $core_attach->where('createtime', '>=', $start_time)->where('createtime', '<=', $end_time);
  358. }
  359. $count = $core_attach->count();
  360. $core_attach = $core_attach->orderby('createtime', 'desc')->offset($offset)->limit($page_size)->get();
  361. foreach ($core_attach as &$item) {
  362. $item['attach'] = yz_tomedia($item['attachment'], true);
  363. $item['url'] = $item['attach'];
  364. }
  365. if (request()->is_interface == 1) {
  366. return $this->successJson('上传成功',$core_attach);
  367. }
  368. $pager = pagination($count, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
  369. $result = array('items' => $core_attach, 'pager' => $pager);
  370. iajax(0, $result);
  371. }
  372. public function getNewImage()
  373. {
  374. $uid = \Auth::guard('admin')->user()->uid;
  375. $search = [];
  376. if (request()->year != '不限') {
  377. $search['year'] = request()->year;
  378. }
  379. if(request()->month != '不限') {
  380. $search['month'] = request()->month;
  381. }
  382. $query = \app\platform\modules\application\models\CoreAttach::search($search)->where('type', 1);
  383. if (\YunShop::app()->isfounder !== true) {
  384. $query->where('uid', $uid);
  385. }
  386. //type = 1 图片
  387. $core_attach = $query->orderby('created_at', 'desc')->paginate(33);
  388. foreach ($core_attach as &$item) {
  389. $item['url'] = yz_tomedia($item['attachment']);
  390. unset($item['uid']);
  391. }
  392. return $core_attach;
  393. }
  394. public function getVideo()
  395. {
  396. if (config('app.framework') == 'platform') {
  397. $uid = \Auth::guard('admin')->user()->uid;
  398. $search = [];
  399. if (request()->year != '不限') {
  400. $search['year'] = request()->year;
  401. }
  402. if (request()->month != '不限') {
  403. $search['month'] = request()->month;
  404. }
  405. $query = \app\platform\modules\application\models\CoreAttach::search($search)->where(['type'=>3]);
  406. if (\YunShop::app()->isfounder !== true) {
  407. $query->where('uid', $uid);
  408. }
  409. //type = 3 视频
  410. $core_attach = $query->orderby('created_at', 'desc')->paginate(33);
  411. foreach ($core_attach as &$item) {
  412. $item['url'] = yz_tomedia($item['attachment']);
  413. unset($item['uid']);
  414. }
  415. return $this->successJson('ok', $core_attach);
  416. }
  417. global $_W;
  418. $uid = $_W['uid'];
  419. $core_attach = new CoreAttach();
  420. $page_index = max(1, request()->page);
  421. $page_size = 5;
  422. if ($page_index <=1) {
  423. $page_index = 0;
  424. $offset = $page_index * $page_size;
  425. } else {
  426. $offset = ($page_index - 1) * $page_size;
  427. }
  428. if (\YunShop::app()->isfounder !== true) {
  429. $core_attach = $core_attach->where('uid', $uid);
  430. }
  431. $total = $core_attach->count();
  432. $core_attach = $core_attach->where(['type'=>3,'uniacid'=>$this->uniacid])->orderby('createtime', 'desc')->offset($offset)->limit(24)->get();
  433. foreach ($core_attach as &$item) {
  434. $item['url'] = yz_tomedia($item['attachment']);
  435. unset($item['uid']);
  436. }
  437. $pager = pagination($total, 1, 24, '', $context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
  438. $result = array('items' => $core_attach, 'pager' => $pager);
  439. iajax(0, $result);die;
  440. }
  441. public function delete()
  442. {
  443. $id = intval(request()->id);
  444. if (config('app.framework') == 'platform') {
  445. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  446. $core_attach = \app\platform\modules\application\models\CoreAttach::where('id', $id);
  447. } else {
  448. global $_W;
  449. $remote = $_W['setting']['remote'];
  450. $core_attach = CoreAttach::where('id', $id);
  451. }
  452. $core_attach = $core_attach->first();
  453. if ($core_attach['upload_type']) {
  454. $remote_url = '';
  455. if ($remote['type'] == 2) {
  456. $remote_url = $remote['alioss']['url'];
  457. }
  458. if ($remote['type'] == 4) {
  459. $remote_url = $remote['cos']['url'];
  460. }
  461. if ($remote_url && strexists($core_attach['attachment'], $remote_url)) {
  462. $str_len = strlen($remote_url);
  463. $core_attach['attachment'] = substr($core_attach['attachment'], $str_len+1);
  464. }
  465. $status = file_remote_delete($core_attach['attachment'], $core_attach['upload_type'], $remote);
  466. } else {
  467. $status = file_delete($core_attach['attachment']);
  468. }
  469. if ($core_attach->delete()) {
  470. return $this->successJson('删除成功');
  471. } else {
  472. return $this->errorJson('删除数据表数据失败');
  473. }
  474. }
  475. public function jsUpload()
  476. {
  477. $file = request()->file('file');
  478. $type = request()->upload_type;
  479. if (!$file) {
  480. return $this->errorJson('请传入正确参数.');
  481. }
  482. if (!$file->isValid()) {
  483. return $this->errorJson('上传失败.');
  484. }
  485. $auth_uid = \Auth::guard('admin')->user()->uid ?: 1;
  486. global $_W;
  487. // 获取文件相关信息
  488. $originalName = $file->getClientOriginalName(); // 文件原名
  489. $realPath = $file->getRealPath(); //临时文件的绝对路径
  490. $ext = $file->getClientOriginalExtension(); //文件后缀
  491. $uploadService = new UploadService();
  492. $upload_setting = $uploadService->getSetting();
  493. if ($type == 'image') {
  494. $upload_res = $uploadService->upload($file, $type);
  495. if (config('app.framework') == 'platform') {
  496. $data = [
  497. 'uniacid' => $this->uniacid,
  498. 'uid' => $auth_uid,
  499. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  500. 'attachment' => $upload_res['relative_path'],
  501. 'type' => 1,
  502. 'module_upload_dir' => '',
  503. 'group_id' => intval($this->uniacid),
  504. 'upload_type' => $upload_setting['remote']['type'],
  505. 'tag_id' => 0
  506. ];
  507. \app\platform\modules\application\models\CoreAttach::create($data);
  508. } else {
  509. $data = [
  510. 'uniacid' => $this->uniacid,
  511. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  512. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  513. 'attachment' => $upload_res['relative_path'],
  514. 'type' => 1,
  515. 'createtime' => TIMESTAMP,
  516. 'module_upload_dir' => '',
  517. 'group_id' => 0,
  518. ];
  519. CoreAttach::create($data);
  520. }
  521. $info = [
  522. 'name' => $originalName,
  523. 'ext' => $ext,
  524. 'filename' => $upload_res['file_name'],
  525. 'attachment' => $upload_res['relative_path'],
  526. 'url' => $upload_res['absolute_path'],
  527. 'is_image' => 1,
  528. 'filesize' => 'null',
  529. 'group_id' => intval($this->uniacid),
  530. 'state' => 'SUCCESS'
  531. ];
  532. die(json_encode($info));
  533. } elseif ($type == 'video') {
  534. $upload_res = $uploadService->upload($file, $type, 'videos');
  535. if (config('app.framework') == 'platform') {
  536. $getID3 = new getID3();
  537. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  538. $timeline = $ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  539. $data = [
  540. 'uniacid' => $this->uniacid,
  541. 'uid' => $auth_uid,
  542. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  543. 'attachment' => $upload_res['relative_path'],
  544. 'type' => 3,
  545. 'module_upload_dir' => '',
  546. 'group_id' => intval($this->uniacid),
  547. 'upload_type' => $upload_setting['remote']['type'],
  548. 'timeline' => $timeline
  549. ];
  550. \app\platform\modules\application\models\CoreAttach::create($data);
  551. } else {
  552. $data = [
  553. 'uniacid' => $this->uniacid,
  554. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  555. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  556. 'attachment' => $upload_res['relative_path'],
  557. 'type' => 3,
  558. 'createtime' => TIMESTAMP,
  559. 'module_upload_dir' => '',
  560. 'group_id' => 0,
  561. ];
  562. CoreAttach::create($data);
  563. }
  564. $info = [
  565. 'name' => $originalName,
  566. 'ext' => $ext,
  567. 'filename' => $upload_res['file_name'],
  568. 'attachment' => $upload_res['relative_path'],
  569. 'url' => $upload_res['absolute_path'],
  570. 'is_image' => 0,
  571. 'filesize' => 'null',
  572. 'group_id' => intval($this->uniacid),
  573. 'state' => 'SUCCESS'
  574. ];
  575. die(json_encode($info));
  576. } elseif ($type == 'audio') {
  577. $upload_res = $uploadService->upload($file, $type, 'audios');
  578. if (config('app.framework') == 'platform') {
  579. $getID3 = new getID3();
  580. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  581. $timeline = $ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  582. $data = [
  583. 'uniacid' => $this->uniacid,
  584. 'uid' => $auth_uid,
  585. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  586. 'attachment' => $upload_res['relative_path'],
  587. 'type' => 3,
  588. 'module_upload_dir' => '',
  589. 'group_id' => intval($this->uniacid),
  590. 'upload_type' => $upload_setting['remote']['type'],
  591. 'timeline' => $timeline,
  592. 'tag_id' => 0
  593. ];
  594. \app\platform\modules\application\models\CoreAttach::create($data);
  595. } else {
  596. $data = [
  597. 'uniacid' => $this->uniacid,
  598. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  599. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  600. 'attachment' => $upload_res['relative_path'],
  601. 'type' => 3,
  602. 'createtime' => TIMESTAMP,
  603. 'module_upload_dir' => '',
  604. 'group_id' => 0,
  605. 'tag_id' => 0
  606. ];
  607. CoreAttach::create($data);
  608. }
  609. $info = [
  610. 'name' => $originalName,
  611. 'ext' => $ext,
  612. 'filename' => $upload_res['file_name'],
  613. 'attachment' => $upload_res['relative_path'],
  614. 'url' => $upload_res['absolute_path'],
  615. 'is_image' => 0,
  616. 'filesize' => 'null',
  617. 'group_id' => intval($this->uniacid),
  618. 'state' => 'SUCCESS',
  619. ];
  620. die(json_encode($info));
  621. }
  622. return true;
  623. }
  624. public function jsImage()
  625. {
  626. $year = intval(request()->year);
  627. $month = intval(request()->month);
  628. $page = max(1, intval(request()->page));
  629. $page_size = 24;
  630. if ($page <= 1) {
  631. $page = 0;
  632. $offset = $page * $page_size;
  633. } else {
  634. $offset = ($page - 1) * $page_size;
  635. }
  636. if (config('app.framework') == 'platform') {
  637. $core_attach = new \app\platform\modules\application\models\CoreAttach();
  638. $group_id = $this->uniacid;
  639. $create_time = 'created_at';
  640. $uid = \Auth::guard('admin')->user()->uid ?: 1;
  641. } else {
  642. $core_attach = new CoreAttach;
  643. $group_id = 0;
  644. $create_time = 'createtime';
  645. global $_W;
  646. $uid = $_W['uid'];
  647. }
  648. $core_attach = $core_attach->where(['uniacid'=>$this->uniacid,'group_id'=>$group_id,'type'=>1]);
  649. if (\YunShop::app()->isfounder !== true) {
  650. $core_attach = $core_attach->where('uid', $uid);
  651. }
  652. if ($year || $month) {
  653. $start_time = $month ? strtotime("{$year}-{$month}-01") : strtotime("{$year}-1-01");
  654. $end_time = $month ? strtotime('+1 month', $start_time) : strtotime('+12 month', $start_time);
  655. $core_attach = $core_attach->whereBetween($create_time, [$start_time, $end_time]);
  656. }
  657. $count = $core_attach->count();
  658. $core_attach = $core_attach->orderby($create_time, 'desc')->offset($offset)->limit($page_size)->get();
  659. foreach ($core_attach as &$item) {
  660. $item['url'] = yz_tomedia($item['attachment']);
  661. $item['attach'] = yz_tomedia($item['attachment']);
  662. }
  663. $pager = pagination($count, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
  664. $result = array('items' => $core_attach, 'pager' => $pager);
  665. $array = [
  666. 'message' => [
  667. 'erron' => 0,
  668. 'message' => $result
  669. ],
  670. 'redirect' => '',
  671. 'type' => 'ajax'
  672. ];
  673. die(json_encode($array));
  674. }
  675. }